public static void main(String[] args) { PrimeFinder primeFinder = new ConcretePrimeFinder(); Iterator<Integer> iterator = primeFinder.findPrimeNumber(2, 7); try { Thread.sleep(11000); } catch (InterruptedException e) { System.out.println("Thread interrupted " + e); } while (iterator.hasNext()) { System.out.println(iterator.next()); } }
@Test public void testConstructors() { OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); int[] capacity = new int[1]; double[] minLoadFactor = new double[1]; double[] maxLoadFactor = new double[1]; map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); assertEquals(AbstractSet.defaultCapacity, capacity[0]); assertEquals(AbstractSet.defaultMaxLoadFactor, maxLoadFactor[0], 0.001); assertEquals(AbstractSet.defaultMinLoadFactor, minLoadFactor[0], 0.001); int prime = PrimeFinder.nextPrime(907); map = new OpenDoubleObjectHashMap<TestClass>(prime); map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); assertEquals(prime, capacity[0]); assertEquals(AbstractSet.defaultMaxLoadFactor, maxLoadFactor[0], 0.001); assertEquals(AbstractSet.defaultMinLoadFactor, minLoadFactor[0], 0.001); map = new OpenDoubleObjectHashMap<TestClass>(prime, 0.4, 0.8); map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); assertEquals(prime, capacity[0]); assertEquals(0.4, minLoadFactor[0], 0.001); assertEquals(0.8, maxLoadFactor[0], 0.001); }
/** * Ensure that this hashtable has sufficient capacity to hold <tt>desiredCapacity<tt> * <b>additional</b> elements without requiring a rehash. This is a tuning method you can call * before doing a large insert. * * @param desiredCapacity an <code>int</code> value */ public void ensureCapacity(int desiredCapacity) { if (desiredCapacity > (_maxSize - size())) { rehash( PrimeFinder.nextPrime( HashFunctions.fastCeil((desiredCapacity + size()) / _loadFactor) + 1)); computeMaxSize(capacity()); } }
/** * initializes the hashtable to a prime capacity which is at least <tt>initialCapacity + 1</tt>. * * @param initialCapacity an <code>int</code> value * @return the actual capacity chosen */ protected int setUp(int initialCapacity) { int capacity; capacity = PrimeFinder.nextPrime(initialCapacity); computeMaxSize(capacity); computeNextAutoCompactionAmount(initialCapacity); return capacity; }
/** * Compresses the hashtable to the minimum prime size (as defined by PrimeFinder) that will hold * all of the elements currently in the table. If you have done a lot of <tt>remove</tt> * operations and plan to do a lot of queries or insertions or iteration, it is a good idea to * invoke this method. Doing so will accomplish two things: * * <ol> * <li>You'll free memory allocated to the table but no longer needed because of the remove()s. * <li>You'll get better query/insert/iterator performance because there won't be any * <tt>REMOVED</tt> slots to skip over when probing for indices in the table. * </ol> */ public void compact() { // need at least one free spot for open addressing rehash(PrimeFinder.nextPrime(HashFunctions.fastCeil(size() / _loadFactor) + 1)); computeMaxSize(capacity()); // If auto-compaction is enabled, re-determine the compaction interval if (_autoCompactionFactor != 0) { computeNextAutoCompactionAmount(size()); } }
@Test public void testEnsureCapacity() { OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); int prime = PrimeFinder.nextPrime(907); map.ensureCapacity(prime); int[] capacity = new int[1]; double[] minLoadFactor = new double[1]; double[] maxLoadFactor = new double[1]; map.getInternalFactors(capacity, minLoadFactor, maxLoadFactor); assertEquals(prime, capacity[0]); }
/** * After an insert, this hook is called to adjust the size/free values of the set and to perform * rehashing if necessary. */ protected final void postInsertHook(boolean usedFreeSlot) { if (usedFreeSlot) { _free--; } // rehash whenever we exhaust the available space in the table if (++_size > _maxSize || _free == 0) { // choose a new capacity suited to the new state of the table // if we've grown beyond our maximum size, double capacity; // if we've exhausted the free spots, rehash to the same capacity, // which will free up any stale removed slots for reuse. int newCapacity = _size > _maxSize ? PrimeFinder.nextPrime(capacity() << 1) : capacity(); rehash(newCapacity); computeMaxSize(capacity()); } }
@Test public void testIsPrime() { PrimeFinder finder = new PrimeFinder(); assertTrue(finder.isPrime(2)); assertTrue(finder.isPrime(3)); assertTrue(finder.isPrime(7)); assertFalse(finder.isPrime(1)); assertFalse(finder.isPrime(0)); assertFalse(finder.isPrime(-1)); assertFalse(finder.isPrime(4)); }
/** * Returns a prime number which is <code>>= desiredCapacity</code> and very close to <code> * desiredCapacity</code> (within 11% if <code>desiredCapacity >= 1000</code>). * * @param desiredCapacity the capacity desired by the user. * @return the capacity which should be used for a hashtable. */ protected int nextPrime(int desiredCapacity) { return PrimeFinder.nextPrime(desiredCapacity); }