Class LinearMap<K,V>
- All Implemented Interfaces:
ICollection<IMap<K,
,V>, IEntry<K, V>> IMap<K,
,V> Iterable<IEntry<K,
,V>> Function<K,
V>
HashMap
for lookups and construction, and superior in the
case of poor hash distribution. Because entries are stored contiguously, performance of clone()
and
iteration is significantly better than HashMap
. Unlike Map
, it can only hold
Integer.MAX_VALUE
elements.
The IMap.entries()
method is O(1) and allows random access, returning an IList that proxies through
to an
underlying array. Partitioning this list is the most efficient way to process the collection in parallel.
However, LinearMap
also exposes O(N) split(int)
and merge(IMap, BinaryOperator)
methods, which despite their asymptotic complexity can be quite fast in practice. The appropriate way to split this
collection will depend on the use case.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.lacuna.bifurcan.IMap
IMap.Mixin<K,
V> -
Field Summary
FieldsFields inherited from class io.lacuna.bifurcan.IMap.Mixin
hash
-
Constructor Summary
ConstructorsConstructorDescriptionLinearMap
(int initialCapacity) LinearMap
(int initialCapacity, ToLongFunction<K> hashFn, BiPredicate<K, K> equalsFn) LinearMap
(ToLongFunction<K> hashFn, BiPredicate<K, K> equalsFn) -
Method Summary
Modifier and TypeMethodDescriptionclear()
clone()
boolean
containsAll
(IMap<K, ?> map) boolean
containsAll
(ISet<K> set) boolean
containsAny
(IMap<K, ?> map) boolean
containsAny
(ISet<K> set) difference
(IMap<K, ?> m) difference
(ISet<K> keys) boolean
forked()
This returns a data structure which is forked, which is equivalent to Clojure's persistent data structures, also sometimes called functional or immutable.static <K,
V> LinearMap <K, V> static <K,
V> LinearMap <K, V> from
(Collection<Map.Entry<K, V>> collection) static <K,
V> LinearMap <K, V> static <K,
V> LinearMap <K, V> int
hashCode()
intersection
(IMap<K, ?> m) intersection
(ISet<K> keys) boolean
isLinear()
keyHash()
Accessorskeys()
linear()
This returns a data structure which is linear, or temporarily mutable.mapValues
(BiFunction<K, V, U> f) nth
(long index) put
(K key, V value, BinaryOperator<V> merge) long
size()
split
(int parts) Splits the collection into roughly even pieces, for parallel processing.update
(K key, UnaryOperator<V> update) Methods inherited from class io.lacuna.bifurcan.IMap.Mixin
toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.lacuna.bifurcan.ICollection
iterator, nth
Methods inherited from interface io.lacuna.bifurcan.IMap
apply, contains, entries, equals, get, getOrCreate, iterator, sliceIndices, spliterator, stream, toMap, values
-
Field Details
-
MAX_CAPACITY
public static final int MAX_CAPACITYFields- See Also:
-
-
Constructor Details
-
LinearMap
public LinearMap() -
LinearMap
public LinearMap(int initialCapacity) - Parameters:
initialCapacity
- the initial capacity of the map
-
LinearMap
- Parameters:
hashFn
- a function which yields the hash value of keysequalsFn
- a function which checks equality of keys
-
LinearMap
- Parameters:
initialCapacity
- the initial capacity of the maphashFn
- a function which yields the hash value of keysequalsFn
- a function which checks equality of keys
-
-
Method Details
-
from
-
from
-
from
-
from
- Returns:
- a map representing the entries in
collection
-
keyHash
-
keyEquality
- Returns:
- the key equality semantics used by the map
-
put
-
put
- Parameters:
merge
- a function which will be invoked if there is a pre-existing value underkey
, 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
underkey
-
remove
-
clear
-
mapValues
- 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
-
update
-
keys
-
indexOf
- Returns:
- the index of
key
within the collection, if it's present
-
nth
-
size
public long size()- Returns:
- the number of elements in the collection
-
isLinear
public boolean isLinear()- Returns:
- true, if the collection is linear
-
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
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
-
equals
- Overrides:
equals
in classIMap.Mixin<K,
V>
-
clone
-
hashCode
public int hashCode()- Overrides:
hashCode
in classIMap.Mixin<K,
V>
-
split
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 exactlyparts
subsets.- Parameters:
parts
- the target number of pieces- Returns:
- a list containing subsets of the collection.
-
union
-
merge
-
difference
-
difference
-
intersection
-
intersection
-
containsAll
-
containsAll
-
containsAny
-
containsAny
-