001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2002 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.set;
020:
021: import bak.pcj.IntCollection;
022: import bak.pcj.IntIterator;
023: import bak.pcj.hash.IntHashFunction;
024: import bak.pcj.hash.DefaultIntHashFunction;
025: import bak.pcj.util.Exceptions;
026:
027: import java.io.Serializable;
028: import java.io.IOException;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectOutputStream;
031:
032: /**
033: * This class represents chained hash table based sets of int values.
034: * Unlike the Java Collections <tt>HashSet</tt> instances of this class
035: * are not backed up by a map. It is implemented using a simple chained
036: * hash table where the keys are stored directly as entries.
037: *
038: * @see IntOpenHashSet
039: * @see java.util.HashSet
040: *
041: * @author Søren Bak
042: * @version 1.4 21-08-2003 20:05
043: * @since 1.0
044: */
045: public class IntChainedHashSet extends AbstractIntSet implements
046: IntSet, Cloneable, Serializable {
047:
048: /** Constant indicating relative growth policy. */
049: private static final int GROWTH_POLICY_RELATIVE = 0;
050:
051: /** Constant indicating absolute growth policy. */
052: private static final int GROWTH_POLICY_ABSOLUTE = 1;
053:
054: /**
055: * The default growth policy of this set.
056: * @see #GROWTH_POLICY_RELATIVE
057: * @see #GROWTH_POLICY_ABSOLUTE
058: */
059: private static final int DEFAULT_GROWTH_POLICY = GROWTH_POLICY_RELATIVE;
060:
061: /** The default factor with which to increase the capacity of this set. */
062: public static final double DEFAULT_GROWTH_FACTOR = 1.0;
063:
064: /** The default chunk size with which to increase the capacity of this set. */
065: public static final int DEFAULT_GROWTH_CHUNK = 10;
066:
067: /** The default capacity of this set. */
068: public static final int DEFAULT_CAPACITY = 11;
069:
070: /** The default load factor of this set. */
071: public static final double DEFAULT_LOAD_FACTOR = 0.75;
072:
073: /**
074: * The hash function used to hash keys in this set.
075: * @serial
076: */
077: private IntHashFunction keyhash;
078:
079: /**
080: * The size of this set.
081: * @serial
082: */
083: private int size;
084:
085: /** The hash table backing up this set. Contains set values directly. */
086: private transient int[][] data;
087:
088: /**
089: * The growth policy of this set (0 is relative growth, 1 is absolute growth).
090: * @serial
091: */
092: private int growthPolicy;
093:
094: /**
095: * The growth factor of this set, if the growth policy is
096: * relative.
097: * @serial
098: */
099: private double growthFactor;
100:
101: /**
102: * The growth chunk size of this set, if the growth policy is
103: * absolute.
104: * @serial
105: */
106: private int growthChunk;
107:
108: /**
109: * The load factor of this set.
110: * @serial
111: */
112: private double loadFactor;
113:
114: /**
115: * The next size at which to expand the data[].
116: * @serial
117: */
118: private int expandAt;
119:
120: private IntChainedHashSet(IntHashFunction keyhash, int capacity,
121: int growthPolicy, double growthFactor, int growthChunk,
122: double loadFactor) {
123: if (keyhash == null)
124: Exceptions.nullArgument("hash function");
125: if (capacity < 0)
126: Exceptions.negativeArgument("capacity", String
127: .valueOf(capacity));
128: if (growthFactor < 0.0)
129: Exceptions.negativeArgument("growthFactor", String
130: .valueOf(growthFactor));
131: if (growthChunk < 0)
132: Exceptions.negativeArgument("growthChunk", String
133: .valueOf(growthChunk));
134: if (loadFactor <= 0.0)
135: Exceptions.negativeOrZeroArgument("loadFactor", String
136: .valueOf(loadFactor));
137: data = new int[capacity][];
138: size = 0;
139: expandAt = (int) Math.round(loadFactor * capacity);
140: this .growthPolicy = growthPolicy;
141: this .growthFactor = growthFactor;
142: this .growthChunk = growthChunk;
143: this .loadFactor = loadFactor;
144: this .keyhash = keyhash;
145: }
146:
147: private IntChainedHashSet(int capacity, int growthPolicy,
148: double growthFactor, int growthChunk, double loadFactor) {
149: this (DefaultIntHashFunction.INSTANCE, capacity, growthPolicy,
150: growthFactor, growthChunk, loadFactor);
151: }
152:
153: /**
154: * Creates a new hash set with capacity 11, a relative
155: * growth factor of 1.0, and a load factor of 75%.
156: */
157: public IntChainedHashSet() {
158: this (DEFAULT_CAPACITY);
159: }
160:
161: /**
162: * Creates a new hash set with the same elements as a specified
163: * collection.
164: *
165: * @param c
166: * the collection whose elements to add to the new
167: * set.
168: *
169: * @throws NullPointerException
170: * if <tt>c</tt> is <tt>null</tt>.
171: */
172: public IntChainedHashSet(IntCollection c) {
173: this ();
174: addAll(c);
175: }
176:
177: /**
178: * Creates a new hash set with the same elements as the specified
179: * array.
180: *
181: * @param a
182: * the array whose elements to add to the new
183: * set.
184: *
185: * @throws NullPointerException
186: * if <tt>a</tt> is <tt>null</tt>.
187: *
188: * @since 1.1
189: */
190: public IntChainedHashSet(int[] a) {
191: this ();
192: for (int i = 0; i < a.length; i++)
193: add(a[i]);
194: }
195:
196: /**
197: * Creates a new hash set with a specified capacity, a relative
198: * growth factor of 1.0, and a load factor of 75%.
199: *
200: * @param capacity
201: * the initial capacity of the set.
202: *
203: * @throws IllegalArgumentException
204: * if <tt>capacity</tt> is negative.
205: */
206: public IntChainedHashSet(int capacity) {
207: this (capacity, DEFAULT_GROWTH_POLICY, DEFAULT_GROWTH_FACTOR,
208: DEFAULT_GROWTH_CHUNK, DEFAULT_LOAD_FACTOR);
209: }
210:
211: /**
212: * Creates a new hash set with a capacity of 11, a relative
213: * growth factor of 1.0, and a specified load factor.
214: *
215: * @param loadFactor
216: * the load factor of the set.
217: *
218: * @throws IllegalArgumentException
219: * if <tt>loadFactor</tt> is negative.
220: */
221: public IntChainedHashSet(double loadFactor) {
222: this (DEFAULT_CAPACITY, DEFAULT_GROWTH_POLICY,
223: DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK, loadFactor);
224: }
225:
226: /**
227: * Creates a new hash set with a specified capacity and
228: * load factor, and a relative growth factor of 1.0.
229: *
230: * @param capacity
231: * the initial capacity of the set.
232: *
233: * @param loadFactor
234: * the load factor of the set.
235: *
236: * @throws IllegalArgumentException
237: * if <tt>capacity</tt> is negative;
238: * if <tt>loadFactor</tt> is not positive.
239: */
240: public IntChainedHashSet(int capacity, double loadFactor) {
241: this (capacity, DEFAULT_GROWTH_POLICY, DEFAULT_GROWTH_FACTOR,
242: DEFAULT_GROWTH_CHUNK, loadFactor);
243: }
244:
245: /**
246: * Creates a new hash set with a specified capacity,
247: * load factor, and relative growth factor.
248: *
249: * <p>The set capacity increases to <tt>capacity()*(1+growthFactor)</tt>.
250: * This strategy is good for avoiding many capacity increases, but
251: * the amount of wasted memory is approximately the size of the set.
252: *
253: * @param capacity
254: * the initial capacity of the set.
255: *
256: * @param loadFactor
257: * the load factor of the set.
258: *
259: * @param growthFactor
260: * the relative amount with which to increase the
261: * the capacity when a capacity increase is needed.
262: *
263: * @throws IllegalArgumentException
264: * if <tt>capacity</tt> is negative;
265: * if <tt>loadFactor</tt> is not positive;
266: * if <tt>growthFactor</tt> is not positive.
267: */
268: public IntChainedHashSet(int capacity, double loadFactor,
269: double growthFactor) {
270: this (capacity, GROWTH_POLICY_RELATIVE, growthFactor,
271: DEFAULT_GROWTH_CHUNK, loadFactor);
272: }
273:
274: /**
275: * Creates a new hash set with a specified capacity,
276: * load factor, and absolute growth factor.
277: *
278: * <p>The set capacity increases to <tt>capacity()+growthChunk</tt>.
279: * This strategy is good for avoiding wasting memory. However, an
280: * overhead is potentially introduced by frequent capacity increases.
281: *
282: * @param capacity
283: * the initial capacity of the set.
284: *
285: * @param loadFactor
286: * the load factor of the set.
287: *
288: * @param growthChunk
289: * the absolute amount with which to increase the
290: * the capacity when a capacity increase is needed.
291: *
292: * @throws IllegalArgumentException
293: * if <tt>capacity</tt> is negative;
294: * if <tt>loadFactor</tt> is not positive;
295: * if <tt>growthChunk</tt> is not positive.
296: */
297: public IntChainedHashSet(int capacity, double loadFactor,
298: int growthChunk) {
299: this (capacity, GROWTH_POLICY_ABSOLUTE, DEFAULT_GROWTH_FACTOR,
300: growthChunk, loadFactor);
301: }
302:
303: // ---------------------------------------------------------------
304: // Constructors with hash function argument
305: // ---------------------------------------------------------------
306:
307: /**
308: * Creates a new hash set with capacity 11, a relative
309: * growth factor of 1.0, and a load factor of 75%.
310: *
311: * @param keyhash
312: * the hash function to use when hashing keys.
313: *
314: * @throws NullPointerException
315: * if <tt>keyhash</tt> is <tt>null</tt>.
316: */
317: public IntChainedHashSet(IntHashFunction keyhash) {
318: this (keyhash, DEFAULT_CAPACITY, DEFAULT_GROWTH_POLICY,
319: DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK,
320: DEFAULT_LOAD_FACTOR);
321: }
322:
323: /**
324: * Creates a new hash set with a specified capacity, a relative
325: * growth factor of 1.0, and a load factor of 75%.
326: *
327: * @param keyhash
328: * the hash function to use when hashing keys.
329: *
330: * @param capacity
331: * the initial capacity of the set.
332: *
333: * @throws IllegalArgumentException
334: * if <tt>capacity</tt> is negative.
335: *
336: * @throws NullPointerException
337: * if <tt>keyhash</tt> is <tt>null</tt>.
338: */
339: public IntChainedHashSet(IntHashFunction keyhash, int capacity) {
340: this (keyhash, capacity, DEFAULT_GROWTH_POLICY,
341: DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK,
342: DEFAULT_LOAD_FACTOR);
343: }
344:
345: /**
346: * Creates a new hash set with a capacity of 11, a relative
347: * growth factor of 1.0, and a specified load factor.
348: *
349: * @param keyhash
350: * the hash function to use when hashing keys.
351: *
352: * @param loadFactor
353: * the load factor of the set.
354: *
355: * @throws IllegalArgumentException
356: * if <tt>loadFactor</tt> is negative.
357: *
358: * @throws NullPointerException
359: * if <tt>keyhash</tt> is <tt>null</tt>.
360: */
361: public IntChainedHashSet(IntHashFunction keyhash, double loadFactor) {
362: this (keyhash, DEFAULT_CAPACITY, DEFAULT_GROWTH_POLICY,
363: DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK, loadFactor);
364: }
365:
366: /**
367: * Creates a new hash set with a specified capacity and
368: * load factor, and a relative growth factor of 1.0.
369: *
370: * @param keyhash
371: * the hash function to use when hashing keys.
372: *
373: * @param capacity
374: * the initial capacity of the set.
375: *
376: * @param loadFactor
377: * the load factor of the set.
378: *
379: * @throws IllegalArgumentException
380: * if <tt>capacity</tt> is negative;
381: * if <tt>loadFactor</tt> is not positive.
382: *
383: * @throws NullPointerException
384: * if <tt>keyhash</tt> is <tt>null</tt>.
385: */
386: public IntChainedHashSet(IntHashFunction keyhash, int capacity,
387: double loadFactor) {
388: this (keyhash, capacity, DEFAULT_GROWTH_POLICY,
389: DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK, loadFactor);
390: }
391:
392: /**
393: * Creates a new hash set with a specified capacity,
394: * load factor, and relative growth factor.
395: *
396: * <p>The set capacity increases to <tt>capacity()*(1+growthFactor)</tt>.
397: * This strategy is good for avoiding many capacity increases, but
398: * the amount of wasted memory is approximately the size of the set.
399: *
400: * @param keyhash
401: * the hash function to use when hashing keys.
402: *
403: * @param capacity
404: * the initial capacity of the set.
405: *
406: * @param loadFactor
407: * the load factor of the set.
408: *
409: * @param growthFactor
410: * the relative amount with which to increase the
411: * the capacity when a capacity increase is needed.
412: *
413: * @throws IllegalArgumentException
414: * if <tt>capacity</tt> is negative;
415: * if <tt>loadFactor</tt> is not positive;
416: * if <tt>growthFactor</tt> is not positive.
417: *
418: * @throws NullPointerException
419: * if <tt>keyhash</tt> is <tt>null</tt>.
420: */
421: public IntChainedHashSet(IntHashFunction keyhash, int capacity,
422: double loadFactor, double growthFactor) {
423: this (keyhash, capacity, GROWTH_POLICY_RELATIVE, growthFactor,
424: DEFAULT_GROWTH_CHUNK, loadFactor);
425: }
426:
427: /**
428: * Creates a new hash set with a specified capacity,
429: * load factor, and absolute growth factor.
430: *
431: * <p>The set capacity increases to <tt>capacity()+growthChunk</tt>.
432: * This strategy is good for avoiding wasting memory. However, an
433: * overhead is potentially introduced by frequent capacity increases.
434: *
435: * @param keyhash
436: * the hash function to use when hashing keys.
437: *
438: * @param capacity
439: * the initial capacity of the set.
440: *
441: * @param loadFactor
442: * the load factor of the set.
443: *
444: * @param growthChunk
445: * the absolute amount with which to increase the
446: * the capacity when a capacity increase is needed.
447: *
448: * @throws IllegalArgumentException
449: * if <tt>capacity</tt> is negative;
450: * if <tt>loadFactor</tt> is not positive;
451: * if <tt>growthChunk</tt> is not positive.
452: *
453: * @throws NullPointerException
454: * if <tt>keyhash</tt> is <tt>null</tt>.
455: */
456: public IntChainedHashSet(IntHashFunction keyhash, int capacity,
457: double loadFactor, int growthChunk) {
458: this (keyhash, capacity, GROWTH_POLICY_ABSOLUTE,
459: DEFAULT_GROWTH_FACTOR, growthChunk, loadFactor);
460: }
461:
462: // ---------------------------------------------------------------
463: // Hash table management
464: // ---------------------------------------------------------------
465:
466: private void ensureCapacity(int elements) {
467: if (elements >= expandAt) {
468: int newcapacity;
469: if (growthPolicy == GROWTH_POLICY_RELATIVE)
470: newcapacity = (int) (data.length * (1.0 + growthFactor));
471: else
472: newcapacity = data.length + growthChunk;
473: if (newcapacity * loadFactor < elements)
474: newcapacity = (int) Math
475: .round(((double) elements / loadFactor));
476: newcapacity = bak.pcj.hash.Primes.nextPrime(newcapacity);
477: expandAt = (int) Math.round(loadFactor * newcapacity);
478:
479: int[][] newdata = new int[newcapacity][];
480:
481: // re-hash
482: for (int i = 0; i < data.length; i++) {
483: int[] list = data[i];
484: if (list != null) {
485: for (int n = 0; n < list.length; n++) {
486: int v = list[n];
487: int index = Math.abs(keyhash.hash(v))
488: % newdata.length;
489: newdata[index] = addList(newdata[index], v);
490: }
491: }
492: }
493:
494: data = newdata;
495: }
496: }
497:
498: private int[] addList(int[] list, int v) {
499: if (list == null)
500: return new int[] { v };
501: int[] newlist = new int[list.length + 1];
502: for (int i = 0; i < list.length; i++)
503: newlist[i] = list[i];
504: newlist[list.length] = v;
505: return newlist;
506: }
507:
508: private int[] removeList(int[] list, int index) {
509: if (list.length == 1)
510: return null;
511: int[] newlist = new int[list.length - 1];
512: int n = 0;
513: for (int i = 0; i < index; i++)
514: newlist[n++] = list[i];
515: for (int i = index + 1; i < list.length; i++)
516: newlist[n++] = list[i];
517: return newlist;
518: }
519:
520: private int searchList(int[] list, int v) {
521: for (int i = 0; i < list.length; i++)
522: if (list[i] == v)
523: return i;
524: return -1;
525: }
526:
527: // ---------------------------------------------------------------
528: // Operations not supported by abstract implementation
529: // ---------------------------------------------------------------
530:
531: public boolean add(int v) {
532: ensureCapacity(size + 1);
533:
534: int index = Math.abs(keyhash.hash(v)) % data.length;
535: int[] list = data[index];
536: if (list == null) {
537: data[index] = new int[] { v };
538: size++;
539: return true;
540: }
541: for (int i = 0; i < list.length; i++)
542: if (list[i] == v)
543: return false;
544: data[index] = addList(data[index], v);
545: size++;
546: return true;
547: }
548:
549: public IntIterator iterator() {
550: return new IntIterator() {
551: int currList = nextList(0);
552: int currInt = 0;
553: int lastList = -1;
554: int lastInt;
555: int lastValue;
556:
557: int nextList(int index) {
558: while (index < data.length && data[index] == null)
559: index++;
560: return index < data.length ? index : -1;
561: }
562:
563: public boolean hasNext() {
564: return currList != -1;
565: }
566:
567: public int next() {
568: if (currList == -1)
569: Exceptions.endOfIterator();
570: lastList = currList;
571: lastInt = currInt;
572: lastValue = data[currList][currInt];
573: if (currInt == data[currList].length - 1) {
574: currList = nextList(currList + 1);
575: currInt = 0;
576: } else {
577: currInt++;
578: }
579: return lastValue;
580: }
581:
582: public void remove() {
583: if (lastList == -1)
584: Exceptions.noElementToRemove();
585: if (currList == lastList)
586: currInt--;
587: data[lastList] = removeList(data[lastList], lastInt);
588: size--;
589: lastList = -1;
590: }
591: };
592: }
593:
594: public void trimToSize() {
595: }
596:
597: /**
598: * Returns a clone of this hash set.
599: *
600: * @return a clone of this hash set.
601: *
602: * @since 1.1
603: */
604: public Object clone() {
605: try {
606: IntChainedHashSet c = (IntChainedHashSet) super .clone();
607: c.data = new int[data.length][];
608: // Cloning each array is not necessary since they are immutable
609: System.arraycopy(data, 0, c.data, 0, data.length);
610: return c;
611: } catch (CloneNotSupportedException e) {
612: Exceptions.cloning();
613: throw new RuntimeException();
614: }
615: }
616:
617: // ---------------------------------------------------------------
618: // Operations overwritten for efficiency
619: // ---------------------------------------------------------------
620:
621: public int size() {
622: return size;
623: }
624:
625: public void clear() {
626: size = 0;
627: }
628:
629: public boolean contains(int v) {
630: int[] list = data[Math.abs(keyhash.hash(v)) % data.length];
631: if (list == null)
632: return false;
633: return searchList(list, v) != -1;
634: }
635:
636: public int hashCode() {
637: int h = 0;
638: for (int i = 0; i < data.length; i++) {
639: int[] list = data[i];
640: if (list != null) {
641: for (int n = 0; n < list.length; n++)
642: h += list[n];
643: }
644: }
645: return h;
646: }
647:
648: public boolean remove(int v) {
649: int index = Math.abs(keyhash.hash(v)) % data.length;
650: int[] list = data[index];
651: if (list != null) {
652: int lindex = searchList(list, v);
653: if (lindex == -1)
654: return false;
655: data[index] = removeList(list, lindex);
656: size--;
657: return true;
658: }
659: return false;
660: }
661:
662: public int[] toArray(int[] a) {
663: if (a == null || a.length < size)
664: a = new int[size];
665:
666: int p = 0;
667: for (int i = 0; i < data.length; i++) {
668: int[] list = data[i];
669: if (list != null) {
670: for (int n = 0; n < list.length; n++)
671: a[p++] = list[n];
672: }
673: }
674: return a;
675: }
676:
677: // ---------------------------------------------------------------
678: // Serialization
679: // ---------------------------------------------------------------
680:
681: /**
682: * @serialData Default fields; the capacity of the
683: * set (<tt>int</tt>); the set's elements
684: * (<tt>int</tt>).
685: *
686: * @since 1.1
687: */
688: private void writeObject(ObjectOutputStream s) throws IOException {
689: s.defaultWriteObject();
690: s.writeInt(data.length);
691: IntIterator i = iterator();
692: while (i.hasNext()) {
693: int x = i.next();
694: s.writeInt(x);
695: }
696: }
697:
698: /**
699: * @since 1.1
700: */
701: private void readObject(ObjectInputStream s) throws IOException,
702: ClassNotFoundException {
703: s.defaultReadObject();
704: data = new int[s.readInt()][];
705: for (int i = 0; i < size; i++) {
706: int v = s.readInt();
707: int index = Math.abs(keyhash.hash(v)) % data.length;
708: int[] list = data[index];
709: if (list == null)
710: data[index] = new int[] { v };
711: else
712: data[index] = addList(data[index], v);
713: }
714: }
715:
716: }
|