Class HamtPMap<K,​V>

  • All Implemented Interfaces:
    PMap<K,​V>, java.io.Serializable

    public final class HamtPMap<K,​V>
    extends java.lang.Object
    implements PMap<K,​V>, java.io.Serializable
    An immutable sorted map with efficient (persistent) updates.

    Uses a hash array mapped trie: http://en.wikipedia.org/wiki/Hash_array_mapped_trie.

    This implementation stores the bare minimum in each node: a key (with its hash), value, mask, and children. It is also optimized to take maximum advantage of binary operations on shared trees. Specifically, reconcile(com.google.javascript.rhino.PMap<K, V>, com.google.javascript.rhino.PMap.Reconciler<K, V>) avoids recursing into entire subtrees if they are identical objects. Null keys and values are not allowed. The implementation special-cases away the EMPTY map as soon as possible, using 'null' instead for all the logic (since EMPTY violates the invariant that key and value are non-null). Finally, we maintain an invariant that the entry with the smallest hash code is always at the root of the tree, which avoids almost all extra tree rebuilding during binary operations.

    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface com.google.javascript.rhino.PMap

        PMap.Reconciler<K,​V>
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <K,​V>
      HamtPMap<K,​V>
      empty()
      Returns an empty map.
      boolean equivalent​(PMap<K,​V> that, java.util.function.BiPredicate<V,​V> equivalence)
      Checks equality recursively based on the given equivalence.
      V get​(K key)
      Retrieves the value associated with the given key from the map, or returns null if it is not present.
      boolean isEmpty()
      Returns whether this map is empty.
      java.lang.Iterable<K> keys()
      Returns an iterable for a (possibly null) tree.
      HamtPMap<K,​V> minus​(K key)
      Returns a new map with the given key removed.
      HamtPMap<K,​V> plus​(K key, V value)
      Returns a new map with the given key-value pair added.
      HamtPMap<K,​V> reconcile​(PMap<K,​V> that, PMap.Reconciler<K,​V> joiner)
      Performs a reconcile operation to merge this and that.
      java.lang.String toString()  
      java.lang.Iterable<V> values()
      Returns an iterable for a (possibly null) tree.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • empty

        public static <K,​V> HamtPMap<K,​V> empty()
        Returns an empty map.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • isEmpty

        public boolean isEmpty()
        Returns whether this map is empty.
        Specified by:
        isEmpty in interface PMap<K,​V>
      • values

        public java.lang.Iterable<V> values()
        Returns an iterable for a (possibly null) tree.
        Specified by:
        values in interface PMap<K,​V>
      • keys

        public java.lang.Iterable<K> keys()
        Returns an iterable for a (possibly null) tree.
        Specified by:
        keys in interface PMap<K,​V>
      • get

        public V get​(K key)
        Retrieves the value associated with the given key from the map, or returns null if it is not present.
        Specified by:
        get in interface PMap<K,​V>
      • plus

        public HamtPMap<K,​V> plus​(K key,
                                        V value)
        Returns a new map with the given key-value pair added. If the value is already present, then this same map will be returned.
        Specified by:
        plus in interface PMap<K,​V>
      • minus

        public HamtPMap<K,​V> minus​(K key)
        Returns a new map with the given key removed. If the key was not present in the first place, then this same map will be returned.
        Specified by:
        minus in interface PMap<K,​V>
      • reconcile

        public HamtPMap<K,​V> reconcile​(PMap<K,​V> that,
                                             PMap.Reconciler<K,​V> joiner)
        Description copied from interface: PMap
        Performs a reconcile operation to merge this and that.

        joiner is called for each pair of entries, one from each map, which share the same key and whose values are not Object.equals(java.lang.Object). This includes entries that are absent from one of the maps, for which null is passed as the absent value.

        The return of calling joiner will appear in the merged map at the key of the original entry pair. The return may not be null. If the values in a pair of entries are Object.equals(java.lang.Object), that value will be used directly in the result without calling joiner.

        The first value passed to joiner comes from this, and the second value comes from that. There are no guarantees on the source of key. Note that that map must be the same implementation.

        Specified by:
        reconcile in interface PMap<K,​V>
      • equivalent

        public boolean equivalent​(PMap<K,​V> that,
                                  java.util.function.BiPredicate<V,​V> equivalence)
        Checks equality recursively based on the given equivalence. Short-circuits as soon as a 'false' result is found.
        Specified by:
        equivalent in interface PMap<K,​V>