/** * Creates a new <code>TIntLongHashMap</code> instance containing * all of the entries in the map passed in. * * @param map a <tt>TIntLongMap</tt> that will be duplicated. */ public TIntLongHashMap( TIntLongMap map ) { super( map.size() ); if ( map instanceof TIntLongHashMap ) { TIntLongHashMap hashmap = ( TIntLongHashMap ) map; this._loadFactor = hashmap._loadFactor; this.no_entry_key = hashmap.no_entry_key; this.no_entry_value = hashmap.no_entry_value; //noinspection RedundantCast if ( this.no_entry_key != ( int ) 0 ) { Arrays.fill( _set, this.no_entry_key ); } //noinspection RedundantCast if ( this.no_entry_value != ( long ) 0 ) { Arrays.fill( _values, this.no_entry_value ); } setUp( (int) Math.ceil( DEFAULT_CAPACITY / _loadFactor ) ); } putAll( map ); }
/** Be sure that size is large enough to force a resize or two. */ public void testRehash() { int size = 1000; int[] keys = new int[size]; long[] vals = new long[size]; for ( int i = 0; i < size; i++ ) { keys[i] = i + 1; vals[i] = keys[i] * 2; } TIntLongMap map = new TIntLongHashMap(); for ( int i = 0; i < keys.length; i++ ) { map.put( keys[i], vals[i] ); } assertEquals( keys.length, map.size() ); for ( int i = 0; i < keys.length; i++ ) { int key = keys[i]; long val = vals[i]; assertEquals( "got incorrect value for index " + i + ", map: " + map, val, map.get( key ) ); } }
public void testClear() { int[] keys = {1138, 42, 86, 99, 101}; long[] vals = {1138, 42, 86, 99, 101}; TIntLongMap map = new TIntLongHashMap(); for ( int i = 0; i < keys.length; i++ ) { map.put( keys[i], vals[i] * 2 ); } assertEquals( keys.length, map.size() ); map.clear(); assertEquals( 0, map.size() ); assertTrue( map.isEmpty() ); TIntLongMap empty = new TIntLongHashMap(); assertEquals( empty, map ); // Map<String,String> jmap = new HashMap<String, String>(); // jmap.isEmpty() }
public void testBug2037709() { TIntLongMap m = new TIntLongHashMap(); for ( int i = 0; i < 10; i++ ) { m.put( i, i ); } int sz = m.size(); assertEquals( 10, sz ); int[] keys = new int[sz]; m.keys( keys ); boolean[] seen = new boolean[sz]; Arrays.fill( seen, false ); for ( int i = 0; i < 10; i++ ) { seen[keys[i]] = true; } for ( int i = 0; i < 10; i++ ) { if ( !seen[i] ) { TestCase.fail( "Missing key for: " + i ); } } }
public void testTransformValues() { int element_count = 20; TIntLongMap map = new TIntLongHashMap(); for ( int i = 1; i <= element_count; i++ ) { map.put( i, i ); } class TransformValues implements TLongFunction { @Override public long execute( long value ) { return value * value; } } TransformValues foreach = new TransformValues(); map.transformValues( foreach ); for ( int i = 1; i <= element_count; i++ ) { assertEquals( i * i, map.get( i ) ); } }
public void testIncrement() { int element_count = 20; TIntLongMap map = new TIntLongHashMap(); for ( int i = 1; i <= element_count; i++ ) { map.put( i, i * i ); } for ( int i = 1; i <= element_count; i++ ) { if ( i % 2 == 0 ) { map.increment( i ); } } for ( int i = 1; i <= element_count; i++ ) { if ( i % 2 == 0 ) { assertEquals( i * i + 1, map.get( i ) ); } else { assertEquals( i * i, map.get( i ) ); } } }
public void testSerialize() throws Exception { int[] keys = {1138, 42, 86, 99, 101, 727, 117}; long[] vals = new long[keys.length]; TIntLongMap map = new TIntLongHashMap(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( map ); ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() ); ObjectInputStream ois = new ObjectInputStream( bias ); TIntLongMap deserialized = (TIntLongMap) ois.readObject(); assertEquals( map, deserialized ); }
/** Be sure that size is large enough to force a resize or two. */ public void testRehash() { int size = 1000; int[] keys = new int[size]; long[] vals = new long[size]; for ( int i = 0; i < size; i++ ) { keys[i] = i + 1; vals[i] = keys[i] * 2; } TIntLongMap map = new TIntLongOffheapHashMap(); for ( int i = 0; i < keys.length; i++ ) { map.put( keys[i], vals[i] ); } assertEquals( keys.length, map.size() ); for ( int i = 0; i < keys.length; i++ ) { int key = keys[i]; long val = vals[i]; assertEquals( "got incorrect value for index " + i + ", map: " + map, val, map.get( key ) ); } }
public void testClear() { int[] keys = {1138, 42, 86, 99, 101}; long[] vals = {1138, 42, 86, 99, 101}; TIntLongMap map = new TIntLongOffheapHashMap(); for ( int i = 0; i < keys.length; i++ ) { map.put( keys[i], vals[i] * 2 ); } assertEquals( keys.length, map.size() ); map.clear(); assertEquals( 0, map.size() ); assertTrue( map.isEmpty() ); TIntLongMap empty = new TIntLongOffheapHashMap(); assertEquals( empty, map ); // Map<String,String> jmap = new HashMap<String, String>(); // jmap.isEmpty() }
public void testRemove() { int[] keys = {1138, 42, 86, 99, 101, 727, 117}; long[] vals = new long[keys.length]; TIntLongMap map = new TIntLongOffheapHashMap(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } assertEquals( keys.length, map.size() ); for ( int i = 0; i < keys.length; i++ ) { assertEquals( vals[i], map.get( keys[i] ) ); } assertEquals( vals[0], map.remove( keys[0] ) ); assertEquals( vals[3], map.remove( keys[3] ) ); assertEquals( map.getNoEntryValue(), map.remove( keys[0] ) ); assertEquals( vals[5], map.remove( keys[5] ) ); assertEquals( map.getNoEntryValue(), map.remove( 11010110 ) ); }
@Override public void forEach(QuartIntLongConsumer action) { for (int i = 0; i < values.length; i++) for (int j = 0; j < values[i].length; j++) for (int k = 0; k < values[i][j].length; k++) { final int f1 = i; final int f2 = j; final TIntLongMap v = values[i][j][k]; if (v != null) { final int f3 = k; v.forEachEntry( new TIntLongProcedure() { @Override public boolean execute(int a, long b) { if (b > 0) action.accept(f1, f2, f3, a, b); return true; } }); } } }
public void testValueCollectionHashCode() { int[] keys = {1138, 42, 86, 99, 101, 727, 117}; long[] vals = new long[keys.length]; TIntLongMap map = new TIntLongOffheapHashMap(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } TLongCollection values = map.valueCollection(); assertEquals( map.size(), values.size() ); assertFalse( values.isEmpty() ); assertEquals( "hashcodes incorrectly not equal: " + map + ", " + values, values.hashCode(), values.hashCode() ); assertFalse( "hashcodes incorrectly equal: " + map + ", " + values, map.hashCode() == values.hashCode() ); }
public void testTransformValues() { int element_count = 20; TIntLongMap map = new TIntLongOffheapHashMap(); for ( int i = 1; i <= element_count; i++ ) { map.put( i, i ); } class TransformValues implements TLongFunction { @Override public long execute( long value ) { return value * value; } } TransformValues foreach = new TransformValues(); map.transformValues( foreach ); for ( int i = 1; i <= element_count; i++ ) { assertEquals( i * i, map.get( i ) ); } }
public void testAdjustValue() { TIntLongMap map = new TIntLongOffheapHashMap(); map.put( KEY_ONE, 1 ); boolean changed = map.adjustValue( KEY_ONE, 1 ); assertTrue( changed ); assertEquals( 2, map.get( KEY_ONE ) ); changed = map.adjustValue( KEY_ONE, 5 ); assertTrue( changed ); assertEquals( 7, map.get( KEY_ONE ) ); changed = map.adjustValue( KEY_ONE, -3 ); assertTrue( changed ); assertEquals( 4, map.get( KEY_ONE ) ); changed = map.adjustValue( KEY_TWO, 1 ); assertFalse( changed ); assertFalse( map.containsKey( KEY_TWO ) ); }
public void testAdjustOrPutValue() { TIntLongMap map = new TIntLongOffheapHashMap(); map.put( KEY_ONE, 1 ); long new_value = map.adjustOrPutValue( KEY_ONE, 1, 100 ); assertEquals( 2, new_value ); assertEquals( 2, map.get( KEY_ONE ) ); new_value = map.adjustOrPutValue( KEY_ONE, 5, 100 ); assertEquals( 7, new_value ); assertEquals( 7, map.get( KEY_ONE ) ); new_value = map.adjustOrPutValue( KEY_ONE, -3, 100 ); assertEquals( 4, new_value ); assertEquals( 4, map.get( KEY_ONE ) ); new_value = map.adjustOrPutValue( KEY_TWO, 1, 100 ); assertEquals( 100, new_value ); assertTrue( map.containsKey( KEY_TWO ) ); assertEquals( 100, map.get( KEY_TWO ) ); new_value = map.adjustOrPutValue( KEY_TWO, 1, 100 ); assertEquals( 101, new_value ); assertEquals( 101, map.get( KEY_TWO ) ); }
public void testEquals() { int[] keys = {1138, 42, 86, 99, 101, 727, 117}; long[] vals = new long[keys.length]; TIntLongMap map = new TIntLongOffheapHashMap(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } assertEquals( map, map ); TIntIntMap int_map = new TIntIntOffheapHashMap(); for ( int i = 0; i < keys.length; i++ ) { int_map.put( keys[i], (int) vals[i] ); } assertFalse( map.equals( int_map ) ); }
public void testSerialize() throws Exception { int[] keys = {1138, 42, 86, 99, 101, 727, 117}; long[] vals = new long[keys.length]; TIntLongMap map = new TIntLongOffheapHashMap(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( map ); ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() ); ObjectInputStream ois = new ObjectInputStream( bias ); TIntLongMap deserialized = (TIntLongMap) ois.readObject(); assertEquals( map, deserialized ); }
public void testTPrimitivePrimitveHashMapConstructors() { int cap = 20; TIntLongMap cap_and_factor = new TIntLongHashMap( cap, 0.75f ); TPrimitiveHash cap_and_factor_hash = (TPrimitiveHash) cap_and_factor; assertTrue( "capacity not sufficient: " + cap + ", " + cap_and_factor_hash.capacity(), cap <= cap_and_factor_hash.capacity() ); assertEquals( 0.75f, cap_and_factor_hash._loadFactor ); TIntLongMap fully_specified = new TIntLongHashMap( cap, 0.5f, Integer.MIN_VALUE, Long.MIN_VALUE ); TPrimitiveHash fully_specified_hash = (TPrimitiveHash) fully_specified; assertTrue( "capacity not sufficient: " + cap + ", " + fully_specified_hash.capacity(), cap <= fully_specified_hash.capacity() ); assertEquals( 0.5f, fully_specified_hash._loadFactor ); assertEquals( Integer.MIN_VALUE, fully_specified.getNoEntryKey() ); assertEquals( Long.MIN_VALUE, fully_specified.getNoEntryValue() ); }
public void testGet() { int element_count = 20; int[] keys = new int[element_count]; Long[] vals = new Long[element_count]; TIntLongMap raw_map = new TIntLongHashMap(); for ( int i = 0; i < element_count; i++ ) { keys[i] = i + 1; vals[i] = Long.valueOf( i + 1 ); raw_map.put( keys[i], vals[i] ); } Map<Integer,Long> map = TDecorators.wrap( raw_map ); assertEquals( vals[10], map.get( Integer.valueOf( keys[10] ) ) ); assertNull( map.get( Integer.valueOf( 1138 ) ) ); }
/** Be sure that size is large enough to force a resize or two. */ public void testRehash() { int size = 1000; int[] keys = new int[size]; long[] vals = new long[size]; for ( int i = 0; i < size; i++ ) { keys[i] = i + 1; vals[i] = keys[i] * 2; } TIntLongMap raw_map = new TIntLongHashMap(); for ( int i = 0; i < keys.length; i++ ) { raw_map.put( keys[i], vals[i] ); } Map<Integer,Long> map = TDecorators.wrap( raw_map ); assertEquals( keys.length, map.size() ); for ( int i = 0; i < keys.length; i++ ) { Integer key = keys[i]; Long val = vals[i]; assertEquals( "got incorrect value for index " + i + ", map: " + map, val, map.get( key ) ); } }
public void testTransformValues() { int element_count = 20; TIntLongMap map = new TIntLongHashMap(); for ( int i = 1; i <= element_count; i++ ) { map.put( i, i ); } class TransformValues implements TLongFunction { public long execute( long value ) { return value * value; } } TransformValues foreach = new TransformValues(); map.transformValues( foreach ); for ( int i = 1; i <= element_count; i++ ) { assertEquals( i * i, map.get( i ) ); } }
public void testKeySetHashCode() { int[] keys = {1138, 42, 86, 99, 101, 727, 117}; Integer[] integer_keys = new Integer[keys.length]; long[] vals = new long[keys.length]; TIntLongMap raw_map = new TIntLongHashMap(); for ( int i = 0; i < keys.length; i++ ) { integer_keys[i] = Integer.valueOf( keys[i] ); vals[i] = keys[i] * 2; raw_map.put( keys[i], vals[i] ); } Map<Integer,Long> map = TDecorators.wrap( raw_map ); Set<Integer> set = map.keySet(); assertEquals( map.size(), set.size() ); assertFalse( set.isEmpty() ); Set<Integer> other = new HashSet<Integer>(); other.addAll( Arrays.asList( integer_keys ) ); assertTrue( "hashcodes incorrectly not equal: " + set + ", " + other, set.hashCode() == other.hashCode() ); }
public void testSerialize() throws Exception { int[] keys = {1138, 42, 86, 99, 101, 727, 117}; long[] vals = new long[keys.length]; TIntLongMap raw_map = new TIntLongHashMap(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; raw_map.put( keys[i], vals[i] ); } Map<Integer,Long> map = TDecorators.wrap( raw_map ); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( map ); ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() ); ObjectInputStream ois = new ObjectInputStream( bias ); //noinspection unchecked Map<Integer,Long> deserialized = (Map<Integer,Long>) ois.readObject(); assertEquals( map, deserialized ); }
/** {@inheritDoc} */ public void putAll( TIntLongMap map ) { ensureCapacity( map.size() ); TIntLongIterator iter = map.iterator(); while ( iter.hasNext() ) { iter.advance(); this.put( iter.key(), iter.value() ); } }
/** {@inheritDoc} */ @Override public boolean equals( Object other ) { if ( ! ( other instanceof TIntLongMap ) ) { return false; } TIntLongMap that = ( TIntLongMap ) other; if ( that.size() != this.size() ) { return false; } long[] values = _values; byte[] states = _states; long this_no_entry_value = getNoEntryValue(); long that_no_entry_value = that.getNoEntryValue(); for ( int i = values.length; i-- > 0; ) { if ( states[i] == FULL ) { int key = _set[i]; long that_value = that.get( key ); long this_value = values[i]; if ( ( this_value != that_value ) && ( this_value != this_no_entry_value ) && ( that_value != that_no_entry_value ) ) { return false; } } } return true; }
public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { // VERSION in.readByte(); // MAP _map = ( TIntLongMap ) in.readObject(); }
public void testValueCollectionRemoveAllArray() { int[] keys = {1138, 42, 86, 99, 101, 727, 117}; long[] vals = new long[keys.length]; TIntLongMap map = new TIntLongHashMap(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } TLongCollection values = map.valueCollection(); assertEquals( map.size(), values.size() ); assertFalse( values.isEmpty() ); // test raw array map = new TIntLongHashMap(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } values = map.valueCollection(); vals[3] = vals[3] + 1; assertTrue( values.removeAll( vals ) ); vals[3] = vals[3] - 1; assertEquals( "removed: " + keys[3] + ", values: " + values, 1, values.size() ); assertEquals( "removed: " + keys[3] + ", values: " + values + "\nmap: " + map, values.size(), map.size() ); }