Class CodePointMap<V>
- Type Parameters:
V
- the value type
- All Implemented Interfaces:
FormatClient
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.
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.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks 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>
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.void
Performs an action for the each value of this map.(package private) void
forEachKey
(IntConsumer action) Performs an action for all explicitly recognized code points.<F> F
format
(FormatServer<F> server) Returns a human-readable representation of this map.<F> F
format
(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) int
Returns the number of hashtable collision in this map.(package private) void
Changes 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.static <V> CodePointMap<V>
Returns a map containing the given entries.static String
quote
(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.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 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.
-
Method Details
-
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 collision 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.- 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
-
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
null
if 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, ornull
- 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 the 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
-
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:
format
in 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
ClassCastException
may 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:
true
if this map contains a value that satisfies the given predicate;false
otherwise
-
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)
. It may, however, react differently to the methodforEachKey(java.util.function.IntConsumer)
.- Returns:
- a map that differs from this map at most by discarding
explicit entries that are already implicit; can be
this
if no clean-up is required
-
forEachKey
Performs an action for all explicitly recognized 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)
, but not code points that have been assigned a value implicitly usingconstant(V)
ortotalize(V)
.- Parameters:
action
- the action to perform
-