Class Map<K,V>

java.lang.Object
io.lacuna.bifurcan.IMap.Mixin<K,V>
io.lacuna.bifurcan.Map<K,V>
All Implemented Interfaces:
ICollection<IMap<K,V>,IEntry<K,V>>, IMap<K,V>, Iterable<IEntry<K,V>>, Function<K,V>

public class Map<K,V> extends IMap.Mixin<K,V>
An implementation of an immutable hash-map based on the general approach described by Steindorfer and Vinju in this paper. It allows for customized hashing and equality semantics, and due to its default reliance on Java's semantics, it is significantly faster than Clojure's PersistentHashMap for lookups and construction for collections smaller than 100k entries, and has equivalent performance for larger collections.

By ensuring that equivalent maps always have equivalent layout in memory, it can perform equality checks and set operations (union, difference, intersection) significantly faster than a more naive implementation. By keeping the memory layout of each node more compact, iteration is at least 2x faster than Clojure's implementation.

  • Field Details

    • EMPTY

      public static final Map EMPTY
  • Constructor Details

    • Map

      public Map(ToLongFunction<K> hashFn, BiPredicate<K,K> equalsFn)
      Creates a map.
      Parameters:
      hashFn - a function which yields the hash value of keys
      equalsFn - a function which checks equality of keys
    • Map

      public Map()
  • Method Details

    • from

      public static <K, V> Map<K,V> from(IMap<K,V> map)
      Parameters:
      map - another map
      Returns:
      an equivalent forked map, with the same equality semantics
    • from

      public static <K, V> Map<K,V> from(Map<K,V> map)
      Returns:
      a forked map with the same contents as map
    • from

      public static <K, V> Map<K,V> from(Iterator<IEntry<K,V>> entries)
      Parameters:
      entries - an iterator of IEntry objects
      Returns:
      a forked map containing the remaining entries
    • from

      public static <K, V> Map<K,V> from(IList<IEntry<K,V>> entries)
      Parameters:
      entries - a list of IEntry objects
      Returns:
      a forked map containing these entries
    • empty

      public static <K, V> Map<K,V> empty()
    • keys

      public Set<K> keys()
      Returns:
      a set representing all keys in the map
    • keyHash

      public ToLongFunction<K> keyHash()
      Returns:
      the hash function used by the map
    • keyEquality

      public BiPredicate<K,K> keyEquality()
      Returns:
      the key equality semantics used by the map
    • get

      public V get(K key, V defaultValue)
      Returns:
      the value under key, or defaultValue if there is no such key
    • put

      public Map<K,V> put(K key, V value, BinaryOperator<V> merge)
      Parameters:
      merge - a function which will be invoked if there is a pre-existing value under key, with the current value as the first argument and new value as the second, to determine the combined result
      Returns:
      an updated map with value under key
    • put

      public Map<K,V> put(K key, V value, BinaryOperator<V> merge, Object editor)
    • update

      public Map<K,V> update(K key, UnaryOperator<V> update)
      Parameters:
      update - a function which takes the existing value, or null if none exists, and returns an updated value.
      Returns:
      an updated map with update(value) under key.
    • update

      public Map<K,V> update(K key, UnaryOperator<V> update, Object editor)
    • remove

      public Map<K,V> remove(K key)
      Returns:
      an updated map that does not contain key
    • remove

      public Map<K,V> remove(K key, Object editor)
    • contains

      public boolean contains(K key)
      Returns:
      true if key is in the map, false otherwise
    • indexOf

      public OptionalLong indexOf(K key)
      Returns:
      the index of key within the collection, if it's present
    • nth

      public IEntry<K,V> nth(long idx)
      Returns:
      the element at idx
    • forked

      public Map<K,V> forked()
      Description copied from interface: ICollection
      This returns a data structure which is forked, which is equivalent to Clojure's persistent data structures, also sometimes called functional or immutable. This is called "forked" because it means that multiple functions can make divergent changes to the data structure without affecting each other.

      If only a single function or scope uses the data structure, it can be left as a linear data structure, which can have significant performance benefits.

      If the data structure is already forked, it will simply return itself.

      Returns:
      a forked form of the data structure
    • linear

      public Map<K,V> linear()
      Description copied from interface: ICollection
      This returns a data structure which is linear, or temporarily mutable. The term "linear", as used here, does not completely align with the formal definition of linear types as used in type theory. It is meant to describe the linear dataflow of the method calls, and as a converse to "forked" data structures.

      If ICollection.forked() is called on a linear collection, all references to that linear collection should be discarded.

      If the data structure is already linear, it will simply return itself.

      Returns:
      a linear form of this data structure
    • mapValues

      public <U> Map<K,U> mapValues(BiFunction<K,V,U> f)
      Type Parameters:
      U - the new type of the values
      Parameters:
      f - a function which transforms the values
      Returns:
      a transformed map which shares the same equality semantics
    • split

      public List<Map<K,V>> split(int parts)
      Description copied from interface: ICollection
      Splits the collection into roughly even pieces, for parallel processing. Depending on the size and contents of the collection, this function may not return exactly parts subsets.
      Parameters:
      parts - the target number of pieces
      Returns:
      a list containing subsets of the collection.
    • union

      public Map<K,V> union(IMap<K,V> m)
      Returns:
      a combined map, with the values from m shadowing those in this amp
    • merge

      public Map<K,V> merge(IMap<K,V> m, BinaryOperator<V> mergeFn)
      Parameters:
      m - another map
      mergeFn - a function which, in the case of key collisions, takes two values and returns the merged result
      Returns:
      a new map representing the merger of the two maps
    • difference

      public Map<K,V> difference(ISet<K> keys)
      Returns:
      a new map representing the current map, less the keys in keys
    • intersection

      public Map<K,V> intersection(ISet<K> keys)
      Returns:
      a new map representing the current map, but only with the keys in keys
    • difference

      public Map<K,V> difference(IMap<K,?> m)
      Returns:
      a new map representing the current map, less the keys in m
    • intersection

      public Map<K,V> intersection(IMap<K,?> m)
      Returns:
      a new map representing the current map, but only with the keys in m
    • size

      public long size()
      Returns:
      the number of elements in the collection
    • isLinear

      public boolean isLinear()
      Returns:
      true, if the collection is linear
    • iterator

      public Iterator<IEntry<K,V>> iterator()
    • equals

      public boolean equals(IMap<K,V> m, BiPredicate<V,V> valEquals)
      Parameters:
      m - another map
      valEquals - a predicate which checks value equalities
      Returns:
      true, if the maps are equivalent
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class IMap.Mixin<K,V>
    • clone

      public Map<K,V> clone()
      Specified by:
      clone in interface ICollection<K,V>
      Overrides:
      clone in class IMap.Mixin<K,V>
      Returns:
      a cloned copy of the collection