Class IntMap<V>

All Implemented Interfaces:
ICollection<IMap<Long,V>,IEntry<Long,V>>, IMap<Long,V>, ISortedMap<Long,V>, Iterable<IEntry<Long,V>>, Function<Long,V>

public class IntMap<V> extends ISortedMap.Mixin<Long,V>
A map which has integer keys, which is an combination of Okasaki and Gill's Fast Mergeable Integer Maps with the memory layout suggested by Steindorfer and Vinju used in Map, with which it shares the same broad performance characteristics.

This collection keeps the keys in sorted order, and can thought of as either a map of integers or a sparse vector. It provides slice(long, long), inclusiveFloorIndex(Long), and ceilIndex(Long) methods which allow for lookups and filtering on its keys.

  • Constructor Details

    • IntMap

      public IntMap()
  • Method Details

    • from

      public static <V> IntMap<V> from(IMap<Number,V> m)
      Parameters:
      m - another map
      Returns:
      a forked copy of the map
    • from

      public static <V> IntMap<V> from(Map<Number,V> m)
      Returns:
      a forked copy of m
    • from

      public static <V> IntMap<V> from(Collection<Map.Entry<Number,V>> collection)
      Parameters:
      collection - a collection of Map.Entry objects
      Returns:
      an IntMap representing the entries in the collection
    • from

      public static <V> IntMap<V> from(IList<IEntry<Number,V>> list)
      Parameters:
      list - a list of IEntry objects
      Returns:
      an IntMap representing the entries in the list
    • comparator

      public Comparator<Long> comparator()
    • keyHash

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

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

      public IntMap<V> slice(long min, long max)
      Parameters:
      min - the inclusive minimum key value
      max - the exclusive maximum key value
      Returns:
      a map representing all entries within [min, max)
    • slice

      public IntMap<V> slice(long min, ISortedSet.Bound minBound, long max, ISortedSet.Bound maxBound)
    • merge

      public IntMap<V> merge(IMap<Long,V> b, BinaryOperator<V> mergeFn)
      Parameters:
      b - 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 IntMap<V> difference(ISet<Long> keys)
      Returns:
      a new map representing the current map, less the keys in keys
    • intersection

      public IntMap<V> intersection(ISet<Long> keys)
      Returns:
      a new map representing the current map, but only with the keys in keys
    • union

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

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

      public IntMap<V> intersection(IMap<Long,?> b)
      Returns:
      a new map representing the current map, but only with the keys in m
    • put

      public IntMap<V> put(long key, V value)
      Parameters:
      key - a primitive long key
      value - a value
      Returns:
      an updated IntMap with value under key
    • put

      public IntMap<V> put(long key, V value, Object editor)
    • put

      public IntMap<V> put(long key, V value, BinaryOperator<V> merge)
      Parameters:
      key - a primitive long key
      value - a value
      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
    • put

      public IntMap<V> put(long key, V value, BinaryOperator<V> merge, Object editor)
    • put

      public IntMap<V> put(Long key, V value)
      Returns:
      an updated map with value stored under key
    • put

      public IntMap<V> put(Long 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
    • remove

      public IntMap<V> remove(long key)
      Returns:
      an updated map that does not contain key
    • remove

      public IntMap<V> remove(long key, Object editor)
    • remove

      public IntMap<V> remove(Long key)
      Returns:
      an updated map that does not contain key
    • mapValues

      public <U> IntMap<U> mapValues(BiFunction<Long,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
    • get

      public Optional<V> get(long key)
    • get

      public V get(long key, V defaultValue)
    • get

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

      public IntMap<V> update(Long 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 IntMap<V> update(long key, UnaryOperator<V> update)
    • update

      public IntMap<V> update(long key, UnaryOperator<V> update, Object editor)
    • contains

      public boolean contains(long key)
    • contains

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

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

      public OptionalLong indexOf(long key)
    • nth

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

      public Iterator<IEntry<Long,V>> iterator()
    • floorIndex

      public OptionalLong floorIndex(long key)
      Returns:
      the entry whose key is either equal to key, or just below it. If key is less than the minimum value in the map, returns null.
    • inclusiveFloorIndex

      public OptionalLong inclusiveFloorIndex(Long key)
    • ceilIndex

      public OptionalLong ceilIndex(long key)
      Returns:
      the entry whose key is either equal to key, or just above it. If key is greater than the maximum value in the map, returns null.
    • ceilIndex

      public OptionalLong ceilIndex(Long key)
    • size

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

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

      public IntMap<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 IntMap<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
    • split

      public List<IntMap<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.
    • equals

      public boolean equals(IMap<Long,V> o, BiPredicate<V,V> valEquals)
      Parameters:
      o - 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<Long,V>
    • clone

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