Package gnu.trove.map

Examples of gnu.trove.map.TIntLongMap


    public void testValueCollectionRemoveAllTCollection() {
        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() );

        assertTrue( values.removeAll( values ) );
        assertTrue( values.isEmpty() );

        // repopulate the set.
        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();

        // With empty set
        TLongSet tlongset = new TLongHashSet();
        assertFalse( "values: " + values + ", collection: " + tlongset,
                values.removeAll( tlongset ) );

        // With partial set
        tlongset = new TLongHashSet( vals );
        tlongset.remove( 42 * 2 );
        assertTrue( "values: " + values + ", collection: " + tlongset,
                values.removeAll( tlongset ) );
        assertEquals( "set: " + values, 1, values.size() );
        assertEquals( "set: " + values, 1, map.size() );
        for ( int i = 0; i < keys.length; i++ ) {
            if ( keys[i] == 42 ) {
                assertTrue( values.contains( vals[i] ) );
                assertTrue( map.containsValue( vals[i] ) );
            } else {
                assertFalse( values.contains( vals[i] ) );
                assertFalse( map.containsValue( vals[i] ) );
            }
        }

        // repopulate the set.
        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();

        // Empty list
        TLongCollection collection = new TLongArrayList();
        assertFalse( "values: " + values + ", collection: " + collection,
                values.removeAll( collection ) );

        // partial list
        collection = new TLongArrayList( vals );
        collection.remove( 42 * 2 );
        assertTrue( "values: " + values + ", collection: " + collection,
                values.removeAll( collection ) );
        assertEquals( "values: " + values, 1, values.size() );
        assertEquals( "values: " + values, 1, map.size() );
        for ( int i = 0; i < keys.length; i++ ) {
            if ( vals[i] == 42 * 2 ) {
                assertTrue( values.contains( vals[i] ) );
                assertTrue( map.containsValue( vals[i] ) );
            } else {
                assertFalse( values.contains( vals[i] ) );
                assertFalse( map.containsValue( vals[i] ) );
            }
        }
    }
View Full Code Here


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

    public void testValueCollectionForEach() {
        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() );

        class ForEach implements TLongProcedure {
            TLongList built = new TLongArrayList();

View Full Code Here

    public void testValueCollectionEquals() {
        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() );
        assertEquals( values, values );
        TLongList values_list = new TLongArrayList( values );
        assertFalse( "collections should not be equal: " + values + ", " + values_list,
                values.equals( values_list ) );

        TLongList list = new TLongArrayList( vals );
        values_list.sort();
        list.sort();
        assertTrue( "collections incorrectly not equal: " + values_list + ", " + list,
                values_list.equals( list ) );
        assertTrue( "collections incorrectly not equal: " + values_list + ", " + list,
                values_list.equals( list ) );


        long[] mismatched = {72, 49, 53, 1024, 999};
        TLongCollection unequal = new TLongArrayList();
        unequal.addAll( mismatched );

        assertFalse( "collections incorrectly equal: " + values_list + ", " + unequal,
                values_list.equals( unequal ) );

        // Change length, different code branch
        unequal.add( 1 );
        assertFalse( "collections incorrectly equal: " + values_list + ", " + unequal,
                values_list.equals( unequal ) );

        assertFalse( "values incorrectly equals a random object",
                values_list.equals( new Object() ) );

        // value in map twice, in list twice.
        list = new TLongArrayList( vals );
        map.put( 1, vals[0] );
        values_list = new TLongArrayList( map.valueCollection() );
        list.add( vals[0] );
        values_list.sort();
        list.sort();
        assertTrue( "collections incorrectly not equal: " + values_list + ", " + list,
                values_list.equals( list ) );
View Full Code Here

    public void testValueCollectionHashCode() {
        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() );
        assertEquals( "hashcodes incorrectly not equal: " + map + ", " + values,
                values.hashCode(), values.hashCode() );
        assertFalse( "hashcodes incorrectly equal: " + map + ", " + values,
                map.hashCode() == values.hashCode() );
    }
View Full Code Here

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

        TLongList list = new TLongArrayList( vals );
        TLongCollection set = map.valueCollection();
        assertEquals( map.getNoEntryValue(), set.getNoEntryValue() );

        // test basic iterator function.
        TLongIterator iter = set.iterator();
        while ( iter.hasNext() ) {
            long val = iter.next();
            assertTrue( "value collection should only contain values: " + val + ", set; " + set,
                    list.contains( val ) );
        }

        assertFalse( iter.hasNext() );
        try {
            iter.next();
            fail( "Expect NoSuchElementException" );
        }
        catch ( NoSuchElementException ex ) {
            // Expected.
        }

        // Start over with new iterator -- test iter.remove()
        iter = set.iterator();
        while ( iter.hasNext() ) {
            long val = iter.next();
            assertTrue( "value collection should only contain values: " + val + ", set; " + set,
                    list.contains( val ) );
            if ( val == vals[3] ) {
                iter.remove();
                assertFalse( "set contains removed element: " + val + ", set: " + set,
                        set.contains( val ) );
            }
        }
        assertEquals( map.size(), set.size() );
        assertEquals( keys.length - 1, map.size() );
        assertEquals( map.getNoEntryValue(), map.get( keys[3] ) );
    }
View Full Code Here

        assertEquals( map.getNoEntryValue(), map.get( keys[3] ) );
    }


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

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

        assertEquals( 2, map.size() );

        long[] values = map.values( new long[map.size()] );
        assertEquals( 2, values.length );
        TLongList values_list = new TLongArrayList( values );

        assertTrue( values_list.contains( 1 ) );
        assertTrue( values_list.contains( 2 ) );

        long[] values2 = map.values();
        assertEquals( 2, values2.length );
        TLongList keys_list2 = new TLongArrayList( values2 );

        assertTrue( keys_list2.contains( 1 ) );
        assertTrue( keys_list2.contains( 2 ) );

        int element_count = 20;
        map = new TIntLongHashMap();
        for ( int i = 0; i < element_count; i++ ) {
            map.put( i, i * i );
        }
        assertEquals( element_count, map.size() );
        long[] vals = map.values( new long[0] );
        Arrays.sort( vals );
        assertEquals( element_count, vals.length );
        for ( int i = 0; i < element_count; i++ ) {
            assertEquals( "expected: " + i * i + " got: " + vals[i] + ", i: " + i +
                          ", vals: " + Arrays.toString( vals ),  i * i, vals[i] );
            assertEquals( i * i, map.get( i ) );
        }
    }
View Full Code Here

    }


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

        class ForEach implements TIntProcedure {
            TIntList built = new TIntArrayList();


            public boolean execute( int value ) {
                built.add( value );
                return true;
            }

            TIntList getBuilt() {
                return built;
            }
        }

        ForEach foreach = new ForEach();
        map.forEachKey( foreach );
        TIntList built = foreach.getBuilt();
        TIntList keys = new TIntArrayList( map.keys() );
        assertEquals( keys, built );

        built.sort();
        keys.sort();
        assertEquals( keys, built );


        class ForEachFalse implements TIntProcedure {
            TIntList built = new TIntArrayList();


            public boolean execute( int value ) {
                built.add( value );
                return false;
            }

            TIntList getBuilt() {
                return built;
            }
        }

        ForEachFalse foreach_false = new ForEachFalse();
        map.forEachKey( foreach_false );
        built = foreach_false.getBuilt();
        keys = new TIntArrayList( map.keys() );
        assertEquals( 1, built.size() );
        assertEquals( keys.get( 0 ), built.get( 0 ) );
    }
View Full Code Here

    }


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

        class ForEach implements TLongProcedure {
            TLongList built = new TLongArrayList();


            public boolean execute( long value ) {
                built.add( value );
                return true;
            }

            TLongList getBuilt() {
                return built;
            }
        }

        ForEach foreach = new ForEach();
        map.forEachValue( foreach );
        TLongList built = foreach.getBuilt();
        TLongList vals = new TLongArrayList( map.values() );
        assertEquals( vals, built );

        built.sort();
        vals.sort();
        assertEquals( vals, built );


        class ForEachFalse implements TLongProcedure {
            TLongList built = new TLongArrayList();


            public boolean execute( long value ) {
                built.add( value );
                return false;
            }

            TLongList getBuilt() {
                return built;
            }
        }

        ForEachFalse foreach_false = new ForEachFalse();
        map.forEachValue( foreach_false );
        built = foreach_false.getBuilt();
        vals = new TLongArrayList( map.values() );
        assertEquals( 1, built.size() );
        assertEquals( vals.get( 0 ), built.get( 0 ) );
    }
View Full Code Here

    }


    public void testForEachEntry() {
        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();


            public boolean execute( int key, long value ) {
                built.put( key, value );
                return true;
            }

            TIntLongMap getBuilt() {
                return built;
            }
        }

        ForEach foreach = new ForEach();
        map.forEachEntry( foreach );
        TIntLongMap built = foreach.getBuilt();
        assertEquals( map, built );
    }
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.