private IntTreePMap<V> withRoot(final IntTree<V> root) { if(root==this.root) return this; return new IntTreePMap<V>(root); }
public TreePVector<E> minus(final Object e) { for(Entry<Integer,E> entry : map.entrySet()) if(entry.getValue().equals(e)) return minus((int)entry.getKey()); return this; }
/** * @param <V> * @param map * @return empty().plusAll(map) */ @SuppressWarnings("unchecked") public static <V> IntTreePMap<V> from(final Map<? extends Integer, ? extends V> map) { if(map instanceof IntTreePMap) return (IntTreePMap<V>)map; //(actually we only know it's IntTreePMap<? extends V>) // but that's good enough for an immutable // (i.e. we can't mess someone else up by adding the wrong type to it) return IntTreePMap.<V>empty().plusAll(map); }
public TreePVector<E> minus(final int i) { if(i<0 || i>=size()) throw new IndexOutOfBoundsException(); return new TreePVector<E>( map.minus(i).withKeysChangedAbove(i,-1) ); }
public TreePVector<E> plus(final int i, final E e) { if(i<0 || i>size()) throw new IndexOutOfBoundsException(); return new TreePVector<E>( map.withKeysChangedAbove(i,1).plus(i,e) ); }
public PVector<E> with(final int i, final E e) { if(i<0 || i>=size()) throw new IndexOutOfBoundsException(); IntTreePMap<E> map = this.map.plus(i, e); if(map==this.map) return this; return new TreePVector<E>( map ); } }
@Override public boolean contains(final Object e) { if(!(e instanceof Entry)) return false; V value = get(((Entry<?,?>)e).getKey()); return value!=null && value.equals(((Entry<?,?>)e).getValue()); } };
@Override public int size() { // same as Map return IntTreePMap.this.size(); } @Override
@Override public Iterator<E> iterator() { return map.values().iterator(); }
PMap<Integer,Integer> pmap = IntTreePMap.empty(); Map<Integer,Integer> map = new HashMap<Integer,Integer>(); Random r = new Random(); UtilityTest.assertEqualsAndHash(map.entrySet(), pmap.entrySet()); UtilityTest.assertEqualsAndHash(pmap, IntTreePMap.from(pmap)); UtilityTest.assertEqualsAndHash(IntTreePMap.empty(), pmap.minusAll(pmap.keySet())); UtilityTest.assertEqualsAndHash(pmap, pmap.plusAll(pmap));
public void testEmpty() { PMap<?,?> empty = IntTreePMap.empty(); UtilityTest.assertEqualsAndHash(new HashMap<Object,Object>(), empty); assertEquals(0, empty.size()); assertTrue(empty.isEmpty()); for(@SuppressWarnings("unused") Object e : empty.entrySet()) fail(); }
public TreePVector<E> plus(final int i, final E e) { if(i<0 || i>size()) throw new IndexOutOfBoundsException(); return new TreePVector<E>( map.withKeysChangedAbove(i,1).plus(i,e) ); }
/** * @param <V> * @param map * @return empty().plusAll(map) */ @SuppressWarnings("unchecked") public static <V> IntTreePMap<V> from(final Map<? extends Integer, ? extends V> map) { if(map instanceof IntTreePMap) return (IntTreePMap<V>)map; //(actually we only know it's IntTreePMap<? extends V>) // but that's good enough for an immutable // (i.e. we can't mess someone else up by adding the wrong type to it) return IntTreePMap.<V>empty().plusAll(map); }
public TreePVector<E> minus(final int i) { if(i<0 || i>=size()) throw new IndexOutOfBoundsException(); return new TreePVector<E>( map.minus(i).withKeysChangedAbove(i,-1) ); }
public PVector<E> with(final int i, final E e) { if(i<0 || i>=size()) throw new IndexOutOfBoundsException(); IntTreePMap<E> map = this.map.plus(i, e); if(map==this.map) return this; return new TreePVector<E>( map ); } }
@Override public boolean contains(final Object e) { if(!(e instanceof Entry)) return false; V value = get(((Entry<?,?>)e).getKey()); return value!=null && value.equals(((Entry<?,?>)e).getValue()); } };
@Override public int size() { // same as Map return IntTreePMap.this.size(); } @Override
@Override public Iterator<E> iterator() { return map.values().iterator(); }