Class CodePointMap<V>
- Type Parameters:
V- the value type
- All Implemented Interfaces:
FormatClient,Serializable
int values to
arbitrary values.
Since the number of code points is finite but large, this class provides
efficient support for total maps where nearly all points are mapped
to the same value. In fact, since null is generally allowed as a
value, every map is trivially total in this sense. See getDefaultValue().
The implementation builds on the assumption that maps are constructed in a first phase, and used for lookups in a second. The efficiency of the latter is strongly prioritized.
If an object of this class is to be serialized, make sure that all values are serializable. This cannot be enforced statically in a non-obtrusive way.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final intThe initial capacity of the backing hash table.static final intThe total number of Unicode code points. -
Method Summary
Modifier and TypeMethodDescriptionbooleanallKeysMatch(IntPredicate pred) Performs a check for all specifically mapped or unmapped code points.booleanChecks whether any value of this map satisfies a given predicate.static <V> CodePointMap<V> anyOf(int[] keys, V value) Returns a map containing several entries with the same value.static <V> CodePointMap<V> anyOf(Collection<Integer> keys, V value) Returns a map containing several entries with the same value.static <V> CodePointMap<V> anyOfRange(int first, int last, IntPredicate cond, V value) Returns a map containing parts of an interval of entries with the same value.static <V> CodePointMap<V> anyOfRange(int first, int last, V value) Returns a map containing an interval of entries with the same value.static <V> CodePointMap<V> constant(V value) Returns a total map containing one entry for every code point, all with the same value.static <V> CodePointMap<V> empty()Returns a map containing no entries.voidPerforms an action for each value of this map.voidforEach(ObjIntConsumer<? super V> keyAction, Consumer<? super V> otherwiseAction) Performs an action for all entries.(package private) voidforEachKey(IntConsumer action) Performs an action for all specifically mapped or unmapped code points.<F> Fformat(FormatServer<F> server) Returns a human-readable representation of this map.<F> Fformat(FormatServer<F> server, Function<? super V, ? extends F> onValue) Returns a human-readable representation of this map.get(int key) Returns the value associated with the given key.(package private) intReturns the number of hashtable entries in this map.(package private) intReturns the number of hashtable collisions in this map.(package private) VReturns the value assigned to keys for which nothing particular has been specified.(package private) voidChanges the content of this map according to a given substitution.static <V,W, X> CodePointMap <X> intersect(CodePointMap<V> a, CodePointMap<W> b, BiFunction<? super V, ? super W, ? extends X> intersector) Returns a map containing the intersected entries of two maps, with conflicting values combined.<W> CodePointMap<W> Returns a map containing the entries of this map, with all values transformed.static <V> CodePointMap<V> merge(CodePointMap<V> a, CodePointMap<V> b, BinaryOperator<V> merger) Returns a map containing the merged entries of two maps, with conflicting values combined.intminKey()Returns the smallest key that has a non-null entry in this map.static <V> CodePointMap<V> Returns a map containing the given entries.static Stringquote(int c) Returns a representation of the given code point that works in Java literals.simplify()Returns a map that differs from this map at most by discarding explicit entries that are already implicit.static <V> CodePointMap<V> singleton(int key, V value) Returns a map containing one entry.intsize()Returns the number of entries in this map.toString()Returns a human-readable representation of this map.Returns a map that differs from this map by having default entries for all keys previously mapped tonull.Returns a map where all keys of this map have been translated according to a relational image.Returns a map containing the entries of this map with one value added or overwritten.with(int key, V value, @Opt BinaryOperator<V> merge) Returns a map containing the entries of this map with one value added or merged.without(int key) Returns a map containing the entries of this map with one value removed.without(int... keys) Returns a map containing the entries of this map with some values removed.
-
Field Details
-
BOOT_CAPACITY
static final int BOOT_CAPACITYThe initial capacity of the backing hash table.- See Also:
-
MAX_SIZE
public static final int MAX_SIZEThe total number of Unicode code points.- See Also:
-
-
Method Details
-
getDefaultValue
V getDefaultValue()Returns the value assigned to keys for which nothing particular has been specified.- Returns:
- the value assigned to keys for which nothing particular has been
specified; possibly
null
-
empty
Returns a map containing no entries.- Type Parameters:
V- the value type- Returns:
- a map containing no entries
-
singleton
Returns a map containing one entry.- Type Parameters:
V- the value type- Parameters:
key- the code point keyvalue- the associated value, possiblynull- Returns:
- a map containing the given entry
- Throws:
IllegalArgumentException- if the given key is not a valid code point- See Also:
-
getCollisions
int getCollisions()Returns the number of hashtable collisions in this map. This method is for efficiency profiling. The lower the number, the more efficient the table layout.- Returns:
- the number of hashtable collisions in this map
-
getCapacity
int getCapacity()Returns the number of hashtable entries in this map. This method is for efficiency profiling. The lower the number, the more efficient the table layout.- Returns:
- the number of hashtable collision in this map
-
anyOf
Returns a map containing several entries with the same value.Repeating the same code point in the array of keys has no further effect.
- Type Parameters:
V- the value type- Parameters:
keys- an array of code point keysvalue- the value to associate with each of the keys- Returns:
- a map containing entries for all given keys with the given value
-
anyOf
Returns a map containing several entries with the same value.Repeating the same code point in the collection of keys has no further effect.
- Type Parameters:
V- the value type- Parameters:
keys- an array of code point keysvalue- the value to associate with each of the keys- Returns:
- a map containing entries for all given keys with the given value
-
anyOfRange
Returns a map containing an interval of entries with the same value.- Type Parameters:
V- the value type- Parameters:
first- the first code pointlast- the last code pointvalue- the value to associate with each of the keys- Returns:
- a map containing entries for all keys greater or equal to
firstand less or equal tolast
-
anyOfRange
Returns a map containing parts of an interval of entries with the same value.- Type Parameters:
V- the value type- Parameters:
first- the first code pointlast- the last code pointcond- an additional predicate that code points to be mapped must satisfyvalue- the value to associate with each of the keys- Returns:
- a map containing entries for all keys greater or equal to
firstand less or equal tolast, and for whichcondreturnstrue
-
constant
Returns a total map containing one entry for every code point, all with the same value.- Type Parameters:
V- the value type- Parameters:
value- the value to associate with all code point keys- Returns:
- a total map of all code point keys to the given value
-
of
Returns a map containing the given entries.All map entries are iterated over when this method is called. The method
Number.intValue()is used to convert keys to code points. Changes to the given map that occur after this method has returned will have no side-effect on the result.- Type Parameters:
V- the value type- Parameters:
map- the entries- Returns:
- a map containing the given entries
- Throws:
IllegalArgumentException- if any converted key is not a valid code point- See Also:
-
get
Returns the value associated with the given key.- Parameters:
key- the code point key- Returns:
- the value associated with the given key, or
nullif there is none
-
with
Returns a map containing the entries of this map with one value added or overwritten.- Parameters:
key- the code point key to add or overwritevalue- the value to associate with the given key- Returns:
- a map containing the entries of this map with the given entry added or overwritten
- Throws:
IllegalArgumentException- if the given key is not a valid code point- See Also:
-
with
Returns a map containing the entries of this map with one value added or merged.If there is no previous entry with the same key, then the value is just added. If there is one, then the old and new value are merged using the given operator. If the merge operator is
null, then the new value overwrites the old one.- Parameters:
key- the code point key to add or overwritevalue- the value to associate with the given keymerge- an operator for merging the old and the new value, ornullfor overwriting- Returns:
- a map containing the entries of this map with the given entry added or merged
- Throws:
IllegalArgumentException- if the given key is not a valid code point- See Also:
-
without
Returns a map containing the entries of this map with one value removed.Note that this operation is intended for use with
constant(V)to define maps that contain most, but not all keys. No attempt is made to shrink or simplify the internal data; hence it is not guaranteed to strictly undo a precedingwith(int,Object).- Parameters:
key- the code point key to remove- Returns:
- a map containing the entries of this map with the given entry removed
-
without
Returns a map containing the entries of this map with some values removed.Note that this operation is intended for use with
constant(V)to define maps that contain most, but not all keys. No attempt is made to shrink or simplify the internal data; hence it is not guaranteed to strictly undo a precedingwith(int,Object).- Parameters:
keys- the code point keys to remove- Returns:
- a map containing the entries of this map with the given entries removed
-
map
Returns a map containing the entries of this map, with all values transformed.- Type Parameters:
W- the target value type- Parameters:
fun- the function for transforming all values- Returns:
- a map containing the entries of this map, with all values transformed with the given function
-
merge
public static <V> CodePointMap<V> merge(CodePointMap<V> a, CodePointMap<V> b, BinaryOperator<V> merger) Returns a map containing the merged entries of two maps, with conflicting values combined.- Type Parameters:
V- the value type of the maps- Parameters:
a- the first mapb- the second mapmerger- the operation for combining conflicting values- Returns:
- a map containing the merged entries of two maps, with conflicting values combined
-
intersect
public static <V,W, CodePointMap<X> intersectX> (CodePointMap<V> a, CodePointMap<W> b, BiFunction<? super V, ? super W, ? extends X> intersector) Returns a map containing the intersected entries of two maps, with conflicting values combined.- Type Parameters:
V- the value type of the first mapW- the value type of the second mapX- the value type of the intersection map- Parameters:
a- the first mapb- the second mapintersector- the operation for combining conflicting values- Returns:
- a map containing the intersected entries of two maps, with conflicting values combined
-
forEach
Performs an action for each value of this map.If a value occurs multiple times for different keys, multiple actions may be performed. It is this strongly recommended that the given action be idempotent, such as adding the value to a set.
- Parameters:
action- the action to perform
-
forEach
Performs an action for all entries.- Parameters:
keyAction- the action to perform for explicitly set keysotherwiseAction- the action to perform for other keys
-
toString
Returns a human-readable representation of this map. -
quote
Returns a representation of the given code point that works in Java literals.In particular, standard backslash escapes apply.
- Parameters:
c- the code point to represent- Returns:
- a representation of the given code point that works in Java literals
-
format
Returns a human-readable representation of this map.- Specified by:
formatin interfaceFormatClient- Type Parameters:
F- the type of format objects to produce- Parameters:
server- a factory object that can produce format objects- Returns:
- a format object produced by the server
-
format
Returns a human-readable representation of this map.- Type Parameters:
F- the type of format objects to produce- Parameters:
server- a factory object that can produce format objectsonValue- the operation for computing representations of values- Returns:
- a human-readable representation of this map
-
hardPatch
Changes the content of this map according to a given substitution.The type of the substituted values is not checked either at compile time or at execution time. Latent causes for
ClassCastExceptionmay arise if this method is used carelessly.- Parameters:
subst- the substitution to apply; values not present as keys are left unchanged.
-
totalize
Returns a map that differs from this map by having default entries for all keys previously mapped tonull.Note that
otherwise(null)makes an equivalent copy.- Parameters:
otherwise- the default value to replacenull- Returns:
- a map that differs from this map by having entries for all keys
previously mapped to
null
-
anyMatch
Checks whether any value of this map satisfies a given predicate.- Parameters:
pred- the predicate to satisfy- Returns:
trueif this map contains a value that satisfies the given predicate;falseotherwise
-
simplify
Returns a map that differs from this map at most by discarding explicit entries that are already implicit. The resulting map may require less, but not more, space.This method can also be used to clean up redundant null-valued entries.
The resulting map will be undistinguishable from this map in terms of the observable map-like behavior, i.e., results of the method
get(int).- Returns:
- a map that differs from this map at most by discarding explicit
entries that are already implicit; can be
thisif no clean-up is required
-
forEachKey
Performs an action for all specifically mapped or unmapped code points.This includes all code points that have been assigned a non-null value explicitly using
of(java.util.Map<? extends java.lang.Number, ? extends V>),anyOf(int[], V)orwith(int, V), as well as code points that have been exceptionally assigned a null value, but not code points that have been assigned a value implicitly usingconstant(V)ortotalize(V).- Parameters:
action- the action to perform
-
allKeysMatch
Performs a check for all specifically mapped or unmapped code points.This includes all code points that have been assigned a non-null value explicitly using
of(java.util.Map<? extends java.lang.Number, ? extends V>),anyOf(int[], V)orwith(int, V), as well as code points that have been exceptionally assigned a null value, but not code points that have been assigned a value implicitly usingconstant(V)ortotalize(V).Checks may be performed only until a counterexample is found. The given predicate should not rely on side effects or a precise number of invocations.
- Parameters:
pred- the check to perform- Returns:
trueif the check has returnedtruefor each relevant key;falseif a counterexample has been found
-
size
public int size()Returns the number of entries in this map.Both positive (non-null) and negative (null) particular entries are counted, as well as a non-null default entry.
- Returns:
- the number of entries
-
minKey
public int minKey()Returns the smallest key that has a non-null entry in this map.- Returns:
- the smallest key that has a non-null entry in this map, or
Integer.MAX_VALUEif there is none
-
translate
Returns a map where all keys of this map have been translated according to a relational image.For each key k with associated value v in this map, the resulting map attaches v to every key t such that
trans.contains(k, t)returnstrue. Multiple colliding values for the same key are combined using the operatormerge, in unspecified order; it is therefore recommended thatmergebe commutative.- Parameters:
trans- the translating relationmerge- the operation for combining colliding values- Returns:
- a map where all keys of this map have been translated according to
trans - Throws:
NullPointerException- iftransormergeis null- Since:
- 1.1
-