1 package jalview.ext.android;
4 * Copyright (C) 2006 The Android Open Source Project
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * SparseDoubleArray map integers to doubles. Unlike a normal array of integers,
20 * there can be gaps in the indices. It is intended to be more memory efficient
21 * than using a HashMap to map Integer to Double, both because it avoids
22 * auto-boxing keys and values and its data structure doesn't rely on an extra
23 * entry object for each mapping.
26 * Note that this container keeps its mappings in an array data structure, using
27 * a binary search to find keys. The implementation is not intended to be
28 * appropriate for data structures that may contain large numbers of items. It
29 * is generally slower than a traditional HashMap, since lookups require a
30 * binary search and adds and removes require inserting and deleting entries in
31 * the array. For containers holding up to hundreds of items, the performance
32 * difference is not significant, less than 50%.
36 * It is possible to iterate over the items in this container using
37 * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using
38 * <code>keyAt(int)</code> with ascending values of the index will return the
39 * keys in ascending order, or the values corresponding to the keys in ascending
40 * order in the case of <code>valueAt(int)<code>.
46 * Jan 2017 cloned from SparseIntArray for Jalview to support SparseMatrix
47 * - SparseDoubleArray(double[]) constructor added
48 * - multiply() added for more efficient multiply (or divide) of a value
50 public class SparseDoubleArray implements Cloneable
54 private double[] mValues;
59 * Creates a new SparseDoubleArray containing no mappings.
61 public SparseDoubleArray()
67 * Creates a new SparseDoubleArray containing no mappings that will not
68 * require any additional memory allocation to store the specified number of
69 * mappings. If you supply an initial capacity of 0, the sparse array will be
70 * initialized with a light-weight representation not requiring any additional
73 public SparseDoubleArray(int initialCapacity)
75 if (initialCapacity == 0)
77 mKeys = ContainerHelpers.EMPTY_INTS;
78 mValues = ContainerHelpers.EMPTY_DOUBLES;
82 initialCapacity = idealDoubleArraySize(initialCapacity);
83 mKeys = new int[initialCapacity];
84 mValues = new double[initialCapacity];
90 * Constructor given an array of double values; stores the non-zero values
94 public SparseDoubleArray(double[] row)
97 for (int i = 0; i < row.length; i++)
107 public SparseDoubleArray clone()
109 SparseDoubleArray clone = null;
112 clone = (SparseDoubleArray) super.clone();
113 clone.mKeys = mKeys.clone();
114 clone.mValues = mValues.clone();
115 } catch (CloneNotSupportedException cnse)
123 * Gets the value mapped from the specified key, or <code>0</code> if no such
124 * mapping has been made.
126 public double get(int key)
132 * Gets the int mapped from the specified key, or the specified value if no
133 * such mapping has been made.
135 public double get(int key, double valueIfKeyNotFound)
137 int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
140 return valueIfKeyNotFound;
149 * Removes the mapping from the specified key, if there was any.
151 public void delete(int key)
153 int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
161 * Removes the mapping at the given index.
163 public void removeAt(int index)
165 System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1));
166 System.arraycopy(mValues, index + 1, mValues, index, mSize
172 * Adds a mapping from the specified key to the specified value, replacing the
173 * previous mapping from the specified key if there was one.
175 public void put(int key, double value)
177 int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
185 if (mSize >= mKeys.length)
187 int n = idealDoubleArraySize(mSize + 1);
188 int[] nkeys = new int[n];
189 double[] nvalues = new double[n];
190 // Log.e("SparseDoubleArray", "grow " + mKeys.length + " to " + n);
191 System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
192 System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
198 // Log.e("SparseDoubleArray", "move " + (mSize - i));
199 System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
200 System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
209 * Returns the number of key-value mappings that this SparseDoubleArray
218 * Given an index in the range <code>0...size()-1</code>, returns the key from
219 * the <code>index</code>th key-value mapping that this SparseDoubleArray
223 * The keys corresponding to indices in ascending order are guaranteed to be
224 * in ascending order, e.g., <code>keyAt(0)</code> will return the smallest
225 * key and <code>keyAt(size()-1)</code> will return the largest key.
228 public int keyAt(int index)
234 * Given an index in the range <code>0...size()-1</code>, returns the value
235 * from the <code>index</code>th key-value mapping that this SparseDoubleArray
239 * The values corresponding to indices in ascending order are guaranteed to be
240 * associated with keys in ascending order, e.g., <code>valueAt(0)</code> will
241 * return the value associated with the smallest key and
242 * <code>valueAt(size()-1)</code> will return the value associated with the
246 public double valueAt(int index)
248 return mValues[index];
252 * Returns the index for which {@link #keyAt} would return the specified key,
253 * or a negative number if the specified key is not mapped.
255 public int indexOfKey(int key)
257 return ContainerHelpers.binarySearch(mKeys, mSize, key);
261 * Returns an index for which {@link #valueAt} would return the specified key,
262 * or a negative number if no keys map to the specified value. Beware that
263 * this is a linear search, unlike lookups by key, and that multiple keys can
264 * map to the same value and this will find only one of them.
266 public int indexOfValue(double value)
268 for (int i = 0; i < mSize; i++)
270 if (mValues[i] == value)
279 * Removes all key-value mappings from this SparseDoubleArray.
287 * Puts a key/value pair into the array, optimizing for the case where the key
288 * is greater than all existing keys in the array.
290 public void append(int key, double value)
292 if (mSize != 0 && key <= mKeys[mSize - 1])
298 if (pos >= mKeys.length)
300 int n = idealDoubleArraySize(pos + 1);
301 int[] nkeys = new int[n];
302 double[] nvalues = new double[n];
303 // Log.e("SparseDoubleArray", "grow " + mKeys.length + " to " + n);
304 System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
305 System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
310 mValues[pos] = value;
315 * Created by analogy with
316 * com.android.internal.util.ArrayUtils#idealLongArraySize
321 public static int idealDoubleArraySize(int need)
323 return idealByteArraySize(need * 8) / 8;
327 * Inlined here by copying from com.android.internal.util.ArrayUtils
332 public static int idealByteArraySize(int need)
334 for (int i = 4; i < 32; i++)
336 if (need <= (1 << i) - 12)
338 return (1 << i) - 12;
349 * This implementation composes a string by iterating over its mappings.
352 public String toString()
358 StringBuilder buffer = new StringBuilder(mSize * 28);
360 for (int i = 0; i < mSize; i++)
369 double value = valueAt(i);
370 buffer.append(value);
373 return buffer.toString();
377 * Method (copied from put) added for Jalview to efficiently increment a key's
378 * value if present, else add it with the given value. This avoids a double
379 * binary search (once to get the value, again to put the updated value).
383 * @return the new value for the key
385 public double add(int key, double toAdd)
387 double newValue = toAdd;
388 int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
392 newValue = mValues[i];
397 if (mSize >= mKeys.length)
399 int n = idealDoubleArraySize(mSize + 1);
400 int[] nkeys = new int[n];
401 double[] nvalues = new double[n];
402 System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
403 System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
409 System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
410 System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
420 * Method added for Jalview to efficiently multiply a key's value if present,
421 * else do nothing. This avoids a double binary search (once to get the value,
422 * again to put the updated value).
426 * @return the new value for the key
428 public double divide(int key, double divisor)
430 double newValue = 0d;
435 int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
438 mValues[i] /= divisor;
439 newValue = mValues[i];