Package gnu.trove.map

Examples of gnu.trove.map.TIntLongMap


    }


    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 ) );
        }
    }
View Full Code Here


    }


    public void testRetainEntries() {
        int element_count = 20;
        TIntLongMap map = new TIntLongHashMap();
        for ( int i = 1; i <= element_count; i++ ) {
            map.put( i, i * i );
        }

        class ForEach implements TIntLongProcedure {
            TIntLongMap built = new TIntLongHashMap();


            // Evens in one map, odds in another.
            public boolean execute( int key, long value ) {
                if ( key % 2 == 1 ) {
                    built.put( key, value );
                    return false;
                }
                return true;
            }

            TIntLongMap getBuilt() {
                return built;
            }
        }

        ForEach foreach = new ForEach();
        map.retainEntries( foreach );
        TIntLongMap built = foreach.getBuilt();

        for ( int i = 1; i <= element_count; i++ ) {
            if ( i % 2 == 0 ) {
                assertTrue( map.containsKey( i ) );
                assertFalse( built.containsKey( i ) );
                assertTrue( map.containsValue( i * i ) );
                assertFalse( built.containsValue( i * i ) );
                assertEquals( i * i, map.get( i ) );
            } else {
                assertFalse( map.containsKey( i ) );
                assertTrue( built.containsKey( i ) );
                assertFalse( map.containsValue( i * i ) );
                assertTrue( built.containsValue( i * i ) );
                assertEquals( i * i, built.get( i ) );
            }
        }
    }
View Full Code Here

    }


    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 ) );
            }
        }
    }
View Full Code Here

//        assertSame( map, ( (TByteIntHashMapDecorator) decorator ).getMap() );
    }


    public void testIterator() {
        TIntLongMap map = new TIntLongHashMap();

        TIntLongIterator iterator = map.iterator();
        assertFalse( iterator.hasNext() );

        map.put( KEY_ONE, 1 );
        map.put( KEY_TWO, 2 );

        iterator = map.iterator();
        assertTrue( iterator.hasNext() );
        iterator.advance();
    boolean found_one;
    if ( iterator.value() == 1 ) {
      assertEquals( KEY_ONE, iterator.key() );
      found_one = true;
    }
    else {
      assertEquals( 2, iterator.value() );
      assertEquals( KEY_TWO, iterator.key() );
      found_one = false;
    }


        assertTrue( iterator.hasNext() );
        iterator.advance();
    if ( found_one ) {
      assertEquals( 2, iterator.value() );
      assertEquals( KEY_TWO, iterator.key() );
    }
    else {
      assertEquals( 1, iterator.value() );
      assertEquals( KEY_ONE, iterator.key() );
    }

        assertFalse( iterator.hasNext() );

        int key = iterator.key();
        long old_value = iterator.value();

    if ( found_one ) {
      assertEquals( 2, old_value );
      assertEquals( KEY_TWO, iterator.key() );
    }
    else {
      assertEquals( 1, old_value );
      assertEquals( KEY_ONE, iterator.key() );
    }

        assertEquals( old_value, iterator.setValue( old_value * 10 ) );
        assertEquals( old_value * 10, iterator.value() );

        assertFalse( map.containsValue( old_value ) );
        assertTrue( map.containsValue( old_value * 10 ) );
        assertEquals( old_value * 10, map.get( key ) );

        iterator.remove();
        assertFalse( map.containsValue( old_value * 10 ) );
        assertEquals( map.getNoEntryValue(), map.get( key ) );
        assertEquals( 1, map.size() );
    }
View Full Code Here

        assertFalse( map.containsKey( KEY_TWO ) );
    }


    public void testAdjustOrPutValue() {
        TIntLongMap map = new TIntLongHashMap();

        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 ) );
    }
View Full Code Here

//        assertEquals( pp_float_map.get( negative_zero_float ), 0 );
    }


    public void testPutIfAbsent() {
        TIntLongMap map = new TIntLongHashMap();

        map.put( 1, 10 );
        map.put( 2, 20 );
        map.put( 3, 30 );

        assertEquals( 10, map.putIfAbsent( 1, 111 ) );
        assertEquals( 10, map.get( 1 ) );
        assertEquals( map.getNoEntryValue(), map.putIfAbsent( 9, 90 ) );
        assertEquals( 90, map.get( 9 ) );
    }
View Full Code Here

        assertEquals( 90, map.get( 9 ) );
    }


    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;
View Full Code Here

    public void testEquals() {
        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] );
        }
        assertEquals( map, map );

        TIntIntMap int_map = new TIntIntHashMap();
        for ( int i = 0; i < keys.length; i++ ) {
            int_map.put( keys[i], (int) vals[i] );
        }
        assertFalse( map.equals( int_map ) );

        // Change a value..
        TIntLongMap unequal = new TIntLongHashMap( map );
        map.put( keys[3], vals[3] + 1 );
        assertFalse( map.equals( unequal ) );

        // Change length
        unequal = new TIntLongHashMap( map );
View Full Code Here

    public void testHashCode() {
        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] );
        }

        TIntLongMap other = new TIntLongHashMap();
        other.putAll( map );
        assertTrue( "hashcodes incorrectly not equal: " + map + ", " + other,
                map.hashCode() == other.hashCode() );

        TIntLongMap unequal = new TIntLongHashMap();
        for ( int i = 0; i < keys.length; i++ ) {
            unequal.put( keys[i], keys[i] );
        }
        assertFalse( "hashcodes unlikely equal: " + map + ", " + unequal,
                map.hashCode() == unequal.hashCode() );

        int[] mismatched = {72, 49, 53, 1024, 999};
        TIntLongMap mismatched_map = new TIntLongHashMap();
        for ( int i = 0; i < mismatched.length; i++ ) {
            mismatched_map.put( mismatched[i], mismatched[i] * 37 );
        }
        assertFalse( "hashcodes unlikely equal: " + map + ", " + unequal,
                map.hashCode() == unequal.hashCode() );

    }
View Full Code Here

    }



    public void testToString() {
        TIntLongMap m = new TIntLongHashMap();
        m.put( 11, 1 );
        m.put( 22, 2 );

        String to_string = m.toString();
        assertTrue( to_string,
      to_string.equals( "{11=1, 22=2}" ) || to_string.equals( "{22=2, 11=1}" ) );
    }
View Full Code Here

TOP

Related Classes of gnu.trove.map.TIntLongMap

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.