1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 import java.io.Serializable;
9 public interface Basket extends Serializable {
10 public boolean containsValue(Object object);
13 public void remove(Object object);
15 public interface List extends Basket {
16 public void add(Object object);
17 public void add(int index, Object object);
18 public Object set(int index, Object object);
19 public Object get(int index);
20 public Object remove(int index);
21 public int indexOf(Object object);
22 public void reverse();
23 public void sort(CompareFunc c);
26 public interface RandomAccess extends List { }
28 public interface Queue extends Basket {
30 //public void enqueue(Object o);
31 //public Object dequeue();
34 public interface Stack extends Basket {
37 public void push(Object object);
40 public interface Map extends Basket {
41 public boolean containsKey(Object key);
42 public Object get(Object key);
43 public Object put(Object key, Object value);
46 public interface CompareFunc {
47 public int compare(Object a, Object b);
51 // Implementations ////////////////////////////////////////////////////////
53 public class Array implements RandomAccess, Stack, Queue {
54 private static final long serialVersionUID = 1233428092L;
59 public Array() { this(10); }
60 public Array(int initialCapacity) { o = new Object[initialCapacity]; }
61 public Array(Object entry) { this(1); add(entry); }
63 public void add(Object obj) { add(size, obj); }
64 public void add(int i, Object obj) {
66 if (size - 1 > i) System.arraycopy(o, i, o, size, size - i - 1);
69 public Object set(int i, Object obj) {
70 if (i >= size) throw new IndexOutOfBoundsException(
71 "index "+i+" is beyond list boundary "+size);
72 Object old = o[i]; o[i] = obj; return old;
74 public Object get(int i) {
75 if (i >= size) throw new IndexOutOfBoundsException(
76 "index "+i+" is beyond list boundary "+size);
79 public Object remove(int i) {
80 if (i >= size || i < 0) throw new IndexOutOfBoundsException(
81 "index "+i+" is beyond list boundary "+size);
82 Object old = o[i]; o[i] = null;
83 if (size - 1 > i) System.arraycopy(o, i + 1, o, i, size - i - 1);
86 public void remove(Object obj) { remove(indexOf(obj)); }
88 public int indexOf(Object obj) {
89 for (int i=0; i < size; i++)
90 if ((obj == null && o[i] == null) || obj.equals(o[i])) return i;
94 public boolean containsValue(Object obj) {
95 for (int i=0; i < size; i++)
96 if ((obj == null && o[i] == null) || obj.equals(o[i])) return true;
99 public void clear() { for (int i=0; i < size; i++) o[i] = null; size = 0; }
100 public int size() { return size; }
101 public void size(int s) {
102 if (o.length >= s) return;
103 Object[] newo = new Object[s];
104 System.arraycopy(o, 0, newo, 0, size);
108 public void reverse() {
109 Object tmp; int max = (int)Math.floor((double)size / 2);
110 for (int i=0; i < size; i++) { tmp = o[i]; o[i] = o[size - i]; o[size - i] = tmp; }
113 public void sort(CompareFunc c) { sort(this, null, c, 0, size); }
115 public static void sort(Array a, Array b, CompareFunc c, int start, int end) {
116 Object tmpa, tmpb = null;
117 if(start >= end) return;
119 for(int i=start+1;i<=end;i++) {
121 if (b != null) tmpb = b.o[i];
123 for(j=i-1;j>=start;j--) {
124 if(c.compare(a.o[j],tmpa) <= 0) break;
126 if (b != null) b.o[j+1] = b.o[j];
129 if (b != null) b.o[j+1] = tmpb;
133 Object pivot = a.o[end];
137 while(c.compare(a.o[++lo],pivot) < 0) { }
138 while((hi > lo) && c.compare(a.o[--hi],pivot) > 0) { }
140 if (b != null) swap(b, lo,hi);
144 if (b != null) swap(b, lo,end);
145 sort(a, b, c, start, lo-1);
146 sort(a, b, c, lo+1, end);
149 private static final void swap(Array vec, int a, int b) {
151 Object tmp = vec.o[a];
157 public Object peek() {
158 if (size < 1) throw new IndexOutOfBoundsException("array is empty");
161 public Object pop() { return remove(size - 1); }
162 public void push(Object o) { add(o); }
165 //public class Tree implements RandomAccess { } FIXME
167 //public class IndexedTree extends Tree { } FIXME
169 /** Implementation of a hash table using Radke's quadratic residue
170 * linear probing. Uses a single array to store all entries.
172 * <p>See C. Radke, Communications of the ACM, 1970, 103-105</p>
174 * @author adam@ibex.org, crawshaw@ibex.org
176 public abstract class Hash implements Basket {
177 static final long serialVersionUID = 3948384093L;
179 /** Used internally to record used slots. */
180 final Object placeholder = new java.io.Serializable() { private static final long serialVersionUID = 1331L; };
182 /** When <tt>loadFactor * usedslots > num_slots</tt>, call
183 * <tt>rehash()</tt>. */
184 final float loadFactor;
186 /** Used to determine the number of array slots required by each
188 final int indexmultiple;
190 /** Number of currently active entries. */
191 private int size = 0;
193 /** Number of placeholders in entries[]. */
194 private int placeholders = 0;
196 /** Array of mappings.
198 * <p>Each map requires multiple slots in the array, and subclasses
199 * can vary the number of required slots without reimplementing all
200 * the functions of this class by changing the value of
201 * <tt>indexmultiple</tt>.</p>
203 * Default implementation uses <tt>indexmultiple == 1</tt>, and
204 * stores only the keys in <tt>entries</tt>.
206 private Object[] entries = null;
208 public Hash() { this(16, 0.75F); }
209 public Hash(int cap, float load) { this(2, cap, load); }
210 public Hash(int indexmultiple, int initialCapacity, float loadFactor) {
211 // using a pseudoprime in the form 4x+3 ensures full coverage
212 initialCapacity = (initialCapacity / 4) * 4 + 3;
213 this.loadFactor = loadFactor;
214 this.indexmultiple = indexmultiple;
215 this.entries = new Object[initialCapacity * indexmultiple];
218 public int size() { return size; }
219 public void clear() { for (int i = 0; i<entries.length; i++) entries[i] = null; size = 0; }
221 public boolean containsKey(Object k) { return indexOf(k) >= 0; }
223 /** <b>Warning:</b> This function is equivalent here to
224 * <tt>containsKey()</tt>. For a value map, use Basket.HashMap. */
225 public boolean containsValue(Object k) { return containsKey(k); }
229 public Object[] dumpkeys() {
230 Object[] ret = new Object[size];
232 for(int i=0; i<entries.length; i+=indexmultiple)
233 if (placeholder!=entries[i] && entries[i]!=null) {
234 ret[pos++] = entries[i];
239 public void remove(Object k) { remove(indexOf(k)); }
240 public void remove(int dest) {
241 if (dest < 0) return;
242 // instead of removing, insert a placeholder
243 entries[dest] = placeholder;
244 for (int inc=1; inc < indexmultiple; inc++) entries[dest + inc] = null;
249 public Object get(Object key) { return get(key, 0); }
250 public Object get(Object key, int whichval) {
251 int i = indexOf(key);
252 return i >= 0 ? entries[i + 1 + whichval] : null;
255 public Object put(Object key, Object value) { return put(key, value, 0); }
256 public Object put(Object key, Object value, int whichval) {
257 if (loadFactor * (size + placeholders) > entries.length) rehash();
258 int dest = indexOf(key);
261 dest = -1 * (dest + 1);
262 if (entries[dest] != placeholder) size++;
264 for(int i=1; i<indexmultiple; i++) entries[dest+i] = null;
266 old = entries[dest + 1 + whichval];
268 entries[dest + 1 + whichval] = value;
273 public boolean containsKey(Object k) { return super.containsValue(k); }
274 public boolean containsValue(Object v) {
275 for (int i=0; i < entries.length/indexmultiple; i++)
276 if ((i == 0 || entries[i * indexmultiple] != null) && // exception for null key
277 !equals(placeholder, entries[i * indexmultiple]) &&
278 v == null ? entries[i + 1] == null : v.equals(entries[i + 1]))
284 /** Compares two keys for equality. <tt>userKey</tt> is the key
285 * passed to the map, <tt>storedKey</tt> is from the map.
287 * <p>Default implementation provides standard Java equality
288 * testing, <tt>k1 == null ? k2 == null : k1.equals(k2)</tt>.</p>
290 protected boolean equals(Object userKey, Object storedKey) {
291 return userKey == null ? storedKey == null : userKey.equals(storedKey);
294 /** Returns the array position in <tt>entries</tt>, adjusted for
295 * <tt>indexmultiple</tt>, where <tt>k</tt> is/should be stored
296 * using Radke's quadratic residue linear probing.
298 * <p><tt>entries[0]</tt> is a hard coded exception for the null
301 * <p>If the key is not found, this function returns
302 * <tt>(-1 * indexPosition) - 1</tt>, where <tt>indexPosition</tt>
303 * is the array position where the mapping should be stored.</p>
305 * <p>Uses <tt>placeholder</tt> as a placeholder object, and
306 * compares keys using <tt>equals(Object, Object)</tt>.</p>
308 private int indexOf(Object k) {
309 // special case null key
310 if (k == null) return equals(placeholder, entries[0]) ? -1 : 0;
312 int hash = k == null ? 0 : k.hashCode();
313 final int orig = Math.abs(hash) % (entries.length / indexmultiple);
314 int dest = orig * indexmultiple;
318 while (entries[dest] != null) {
319 if (equals(k, entries[dest])) return dest;
320 dest = Math.abs((orig + (plus ? 1 : -1) * tries * tries) % (entries.length / indexmultiple)) * indexmultiple;
324 return -1 * dest - 1;
327 /** Doubles the available entry space, first by packing the data
328 * set (removes <tt>placeholder</tt> references) and if necessary
329 * by increasing the size of the <tt>entries</tt> array.
331 private void rehash() {
332 Object[] oldentries = entries;
333 entries = new Object[oldentries.length * indexmultiple];
335 for (int i=0; i < (oldentries.length/indexmultiple); i++) {
336 int pos = i * indexmultiple;
337 if (pos > 0 && oldentries[pos] == null) continue;
338 if (oldentries[pos] == placeholder) continue;
340 // dont adjust any of the support entries
341 int dest = indexOf(oldentries[pos]);
342 dest = -1 * dest - 1; size++; // always new entry
343 for (int inc=0; inc < indexmultiple; inc++)
344 entries[dest + inc] = oldentries[pos + inc];
350 public class HashMap extends Hash implements Map {
351 public HashMap() { super(); }
352 public HashMap(int x, float y) { super(x,y); }
353 public HashMap(int x, int z, float y) { super(x,z,y); }