/**************************************************************************** Copyright (c) 2003, Landmark Graphics and others. All rights reserved. This program and accompanying materials are made available under the terms of the Common Public License - v1.0, which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html ****************************************************************************/ package edu.mines.jtk.opt; import java.util.LinkedHashMap; import java.util.Set; import java.util.logging.Logger; import edu.mines.jtk.util.Almost; /** A VectContainer implemented as an unsynchronized Map. Keys will be returned in the order of insertion. @author W.S. Harlan */ public class VectMap implements VectContainer { private static final Logger LOG = Logger.getLogger("edu.mines.jtk.opt"); private LinkedHashMap _map = new LinkedHashMap(); private boolean _cloneContents = false; private static final long serialVersionUID = 1L; /** Specify whether contents are copied or not. @param cloneContents If true, all put and get methods will clone the passed Vect. */ public VectMap(boolean cloneContents) { if (cloneContents) { LOG.warning("Cloning hurts performance. " + "Use only for testing a VectContainer that requires puts."); } _cloneContents = cloneContents; } // VectContainer public void put(int index, Vect vect) { _map.put(index, (_cloneContents) ? vect.clone() : vect); } // VectContainer public Vect get(int index) { Vect result = getPrivate(index); if (result != null && _cloneContents) { result = result.clone(); } return result; } // Get private instance private Vect getPrivate(int index) { return _map.get(index); } // VectContainer public int size() {return _map.size();} // VectContainer public boolean containsKey(int index) { return _map.containsKey(index); } // VectContainer public int[] getKeys() { Set keys = _map.keySet(); int[] result = new int[keys.size()]; int i=0; for (Integer j: keys) { result[i++] = j; } return result; } // VectConst public double dot(VectConst other) { VectMap otherMap = (VectMap) other; int[] keys = getKeys(); double result = 0.; for (int i=0; i(); int[] keys = getKeys(); for (int i=0; i