private void writeObject(java.io.ObjectOutputStream s) throws IOException { s.defaultWriteObject(); // Write nothing for( long K : keySet() ) { final Object V = get(K); // Do an official 'get' s.writeLong (K); // Write the <long,TypeV> pair s.writeObject(V); } s.writeLong(NO_KEY); // Sentinel to indicate end-of-data s.writeObject(null); }
HM.put(0, "Cliff0"); HM.remove(0); int sz = HM.size(); while (sz + 1024 < _table_size) { int idx = R.nextInt(); for (int i = 0; i < 1024; i++) { int k = idx & (KEYS.length - 1); HM.put(k, KEYS[k]); idx++; sz = HM.size(); String key = KEYS[k]; if (sz < _table_size) { if (HM.put(k, key) == null) { sz++; break; if (HM.remove(k) != null) { sz--; break; if (trip > 1024 * 1024 + 100) throw new RuntimeException( "barf trip " + sz + " " + HM.size() + " numkeys=" + KEYS.length); System.out.println(key); if (sz != HM.size()) {
/** Auto-boxing version of {@link #putIfAbsent}. */ public TypeV putIfAbsent( Long key, TypeV val ) { return putIfAbsent( key.longValue(), val ); } /** Auto-boxing version of {@link #replace}. */
private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); // Read nothing initialize(MIN_SIZE); for (;;) { final long K = s.readLong(); final TypeV V = (TypeV) s.readObject(); if( K == NO_KEY && V == null ) break; put(K,V); // Insert with an offical put } }
assertTrue(_nbhml.isEmpty()); assertThat(_nbhml.put(1, "v1"), nullValue()); checkSizes(1); assertThat(_nbhml.putIfAbsent(2, "v2"), nullValue()); checkSizes(2); assertTrue(_nbhml.containsKey(2)); assertThat(_nbhml.put(1, "v1a"), is("v1")); assertThat(_nbhml.put(2, "v2a"), is("v2")); checkSizes(2); assertThat(_nbhml.putIfAbsent(2, "v2b"), is("v2a")); assertThat(_nbhml.remove(1), is("v1a")); assertFalse(_nbhml.containsKey(1)); checkSizes(1); assertThat(_nbhml.remove(1), nullValue()); assertThat(_nbhml.remove(2), is("v2a")); checkSizes(0); assertThat(_nbhml.remove(2), nullValue()); assertThat(_nbhml.remove("k3"), nullValue()); assertTrue(_nbhml.isEmpty()); assertThat(_nbhml.put(0, "v0"), nullValue()); assertTrue(_nbhml.containsKey(0)); checkSizes(1); assertThat(_nbhml.remove(0), is("v0")); assertFalse(_nbhml.containsKey(0)); checkSizes(0); assertThat(_nbhml.replace(0, "v0"), nullValue()); assertFalse(_nbhml.containsKey(0));
assertThat(_nbhml.size(), is(0)); for (int i = 0; i < CNT; i++) _nbhml.put(i, "v" + i); assertThat(_nbhml.size(), is(CNT)); for (long x : _nbhml.keySet()) assertThat("Found all integers in list", sum, is(CNT * (CNT - 1) / 2)); assertThat("can remove 3", _nbhml.remove(3), is("v3")); assertThat("can remove 4", _nbhml.remove(4), is("v4")); sz = 0; sum = 0; for (long x : _nbhml.keySet()) String v = _nbhml.get(x); assertThat("", v.charAt(0), is('v')); assertThat("", x, is(Long.parseLong(v.substring(1)))); _nbhml.clear();
static void run_till_stable(int num_threads, int num_trials) { NonBlockingHashMapLong<String> HM = new NonBlockingHashMapLong<String>(true); String name = "NonBlockingHashMapLong"; System.out.printf("=== %10.10s %3d cnts/sec=", name, num_threads); HM.put(i, KEYS[i]); for (int j = 0; j < i; j++) { if (HM.get(j) != KEYS[j]) { throw new Error("Broken table, put " + i + " but cannot find #" + j); System.out.printf(" (+/-%2d%%) %d", p, HM.size());
@Test public void testIteration() { assertTrue(_nbhml.isEmpty()); assertThat(_nbhml.put(1, "v1"), nullValue()); assertThat(_nbhml.put(2, "v2"), nullValue()); String str1 = ""; for (Map.Entry<Long, String> e : _nbhml.entrySet()) { str1 += e.getKey(); } assertThat("found all entries", str1, anyOf(is("12"), is("21"))); String str2 = ""; for (Long key : _nbhml.keySet()) { str2 += key; } assertThat("found all keys", str2, anyOf(is("12"), is("21"))); String str3 = ""; for (String val : _nbhml.values()) { str3 += val; } assertThat("found all vals", str3, anyOf(is("v1v2"), is("v2v1"))); assertThat("toString works", _nbhml.toString(), anyOf(is("{1=v1, 2=v2}"), is("{2=v2, 1=v1}"))); _nbhml.clear(); }
if (x < _gr) { get_ops++; String val = _hash.get(k); if (val != null && !val.equals(key)) throw new IllegalArgumentException("Mismatched key=" + key + " and val=" + val); _hash.putIfAbsent(k, key); _hash.remove(k);
@Test public final void testNonBlockingHashMapSize() NonBlockingHashMapLong<String> items = new NonBlockingHashMapLong<>(); items.put(100L, "100"); items.put(101L, "101"); assertEquals("keySet().size()", 2, items.keySet().size()); assertTrue("keySet().contains(100)", items.keySet().contains(100L)); assertTrue("keySet().contains(101)", items.keySet().contains(101L)); assertEquals("values().size()", 2, items.values().size()); assertTrue("values().contains(\"100\")", items.values().contains("100")); assertTrue("values().contains(\"101\")", items.values().contains("101")); assertEquals("entrySet().size()", 2, items.entrySet().size()); boolean found100 = false; boolean found101 = false; for (Map.Entry<Long, String> entry : items.entrySet())
private void checkSizes(int expectedSize) { assertEquals("size()", _nbhml.size(), expectedSize); Collection<String> vals = _nbhml.values(); checkSizes("values()", vals.size(), vals.iterator(), expectedSize); Set<Long> keys = _nbhml.keySet(); checkSizes("keySet()", keys.size(), keys.iterator(), expectedSize); Set<Map.Entry<Long, String>> ents = _nbhml.entrySet(); checkSizes("entrySet()", ents.size(), ents.iterator(), expectedSize); }
@Test public void testConcurrentSimple() throws InterruptedException final NonBlockingHashMapLong<String> nbhml = new NonBlockingHashMapLong<>(); buf.append("Should be emptyset but has these elements: {"); boolean found = false; for (long x : nbhml.keySet()) assertThat("concurrent size=0", nbhml.size(), is(0)); assertThat("keySet size==0", nbhml.keySet().size(), is(0));
public boolean remove ( Object k ) { return NonBlockingHashMapLong.this.remove (k) != null; } public IteratorLong iterator() { return new IteratorLong(); }
void work_helper(NonBlockingHashMapLong<String> nbhml, int d, int num_thrds) { String thrd = "T" + d; final int ITERS = 20000; for (int j = 0; j < 10; j++) { //long start = System.nanoTime(); for (int i = d; i < ITERS; i += num_thrds) { assertThat("key " + i + " not in there, so putIfAbsent must work", nbhml.putIfAbsent((long) i, thrd), is((String) null)); } for (int i = d; i < ITERS; i += num_thrds) { assertTrue(nbhml.remove((long) i, thrd)); } //double delta_nanos = System.nanoTime()-start; //double delta_secs = delta_nanos/1000000000.0; //double ops = ITERS*2; //System.out.println("Thrd"+thrd+" "+(ops/delta_secs)+" ops/sec size="+nbhml.size()); } }
@Test public void testNonBlockingHashMapIteratorMultithreaded() throws InterruptedException, ExecutionException { TestKeyFeeder feeder = getTestKeyFeeder(); final int itemCount = feeder.size(); // validate results final NonBlockingHashMapLong<TestKey> items = feeder.getMapMultithreaded(); assertEquals("size()", itemCount, items.size()); assertEquals("values().size()", itemCount, items.values().size()); assertEquals("entrySet().size()", itemCount, items.entrySet().size()); int iteratorCount = 0; for (TestKey m : items.values()) { iteratorCount++; } // sometimes a different result comes back the second time int iteratorCount2 = 0; for (TestKey m2 : items.values()) { iteratorCount2++; } assertEquals("iterator counts differ", iteratorCount, iteratorCount2); assertEquals("values().iterator() count", itemCount, iteratorCount); }
/** Tests if the key in the table. * @return <tt>true</tt> if the key is in the table */ public boolean containsKey( long key ) { return get(key) != null; }
public int size ( ) { return NonBlockingHashMapLong.this.size ( ); } public boolean contains( Object v ) { return NonBlockingHashMapLong.this.containsValue(v); }