3 import jalview.ext.android.SparseIntArray;
4 import jalview.ext.android.SparseShortArray;
7 * A class to count occurrences of characters with minimal memory footprint.
8 * Sparse arrays of short values are used to hold the counts, with automatic
9 * promotion to arrays of int if any count exceeds the maximum value for a
15 public class SparseCount
17 private static final int DEFAULT_PROFILE_SIZE = 2;
20 * array of keys (chars) and values (counts)
21 * held either as shorts or (if shorts overflow) as ints
23 private SparseShortArray shortProfile;
25 private SparseIntArray intProfile;
28 * flag is set true after short overflow occurs
30 private boolean useInts;
33 * Constructor which initially creates a new sparse array of short values to
38 public SparseCount(int profileSize)
40 this.shortProfile = new SparseShortArray(profileSize);
44 * Constructor which allocates an initial count array for only two distinct
45 * values (the array will grow if needed)
49 this(DEFAULT_PROFILE_SIZE);
53 * Adds the given value for the given key (or sets the initial value), and
54 * returns the new value
59 public int add(int key, int value)
64 newValue = intProfile.add(key, value);
69 newValue = shortProfile.add(key, value);
70 } catch (ArithmeticException e) {
72 newValue = intProfile.add(key, value);
79 * Switch from counting shorts to counting ints
81 synchronized void handleOverflow()
83 int size = shortProfile.size();
84 intProfile = new SparseIntArray(size);
85 for (int i = 0; i < size; i++)
87 short key = shortProfile.keyAt(i);
88 short value = shortProfile.valueAt(i);
89 intProfile.put(key, value);
96 * Returns the size of the profile (number of distinct items counted)
102 return useInts ? intProfile.size() : shortProfile.size();
106 * Returns the value for the key (zero if no such key)
111 public int get(int key)
113 return useInts ? intProfile.get(key) : shortProfile.get(key);
117 * Sets the value for the given key
122 public void put(int key, int value)
126 intProfile.put(key, value);
130 shortProfile.put(key, value);
134 public int keyAt(int k)
136 return useInts ? intProfile.keyAt(k) : shortProfile.keyAt(k);
139 public int valueAt(int k)
141 return useInts ? intProfile.valueAt(k) : shortProfile.valueAt(k);
145 * Answers true if this object wraps arrays of int values, false if using