Class MutableCodePointTrie

java.lang.Object
com.ibm.icu.util.CodePointMap
com.ibm.icu.util.MutableCodePointTrie
All Implemented Interfaces:
Cloneable, Iterable<CodePointMap.Range>

public final class MutableCodePointTrie extends CodePointMap implements Cloneable
Mutable Unicode code point trie. Fast map from Unicode code points (U+0000..U+10FFFF) to 32-bit integer values. For details see https://icu.unicode.org/design/struct/utrie

Setting values (especially ranges) and lookup is fast. The mutable trie is only somewhat space-efficient. It builds a compacted, immutable CodePointTrie.

This trie can be modified while iterating over its contents. For example, it is possible to merge its values with those from another set of ranges (e.g., another @{link CodePointMap}): Iterate over those source ranges; for each of them iterate over this trie; add the source value into the value of each trie range.

  • Constructor Details

    • MutableCodePointTrie

      public MutableCodePointTrie(int initialValue, int errorValue)
      Constructs a mutable trie that initially maps each Unicode code point to the same value. It uses 32-bit data values until buildImmutable(com.ibm.icu.util.CodePointTrie.Type, com.ibm.icu.util.CodePointTrie.ValueWidth) is called. buildImmutable() takes a valueWidth parameter which determines the number of bits in the data value in the resulting CodePointTrie.
      Parameters:
      initialValue - the initial value that is set for all code points
      errorValue - the value for out-of-range code points and ill-formed UTF-8/16
  • Method Details

    • clone

      public MutableCodePointTrie clone()
      Clones this mutable trie.
      Overrides:
      clone in class Object
      Returns:
      the clone
    • fromCodePointMap

      public static MutableCodePointTrie fromCodePointMap(CodePointMap map)
      Creates a mutable trie with the same contents as the CodePointMap.
      Parameters:
      map - the source map or trie
      Returns:
      the mutable trie
    • get

      public int get(int c)
      Returns the value for a code point as stored in the map, with range checking. Returns an implementation-defined error value if c is not in the range 0..U+10FFFF.
      Specified by:
      get in class CodePointMap
      Parameters:
      c - the code point
      Returns:
      the map value, or an implementation-defined error value if the code point is not in the range 0..U+10FFFF
    • getRange

      public boolean getRange(int start, CodePointMap.ValueFilter filter, CodePointMap.Range range)
      Sets the range object to a range of code points beginning with the start parameter. The range start is the same as the start input parameter (even if there are preceding code points that have the same value). The range end is the last code point such that all those from start to there have the same value. Returns false if start is not 0..U+10FFFF. Can be used to efficiently iterate over all same-value ranges in a map. (This is normally faster than iterating over code points and get()ting each value, but may be much slower than a data structure that stores ranges directly.)

      If the CodePointMap.ValueFilter parameter is not null, then the value to be delivered is passed through that filter, and the return value is the end of the range where all values are modified to the same actual value. The value is unchanged if that parameter is null.

      Example:

       int start = 0;
       CodePointMap.Range range = new CodePointMap.Range();
       while (map.getRange(start, null, range)) {
           int end = range.getEnd();
           int value = range.getValue();
           // Work with the range start..end and its value.
           start = end + 1;
       }
       

      The trie can be modified between calls to this function.

      Specified by:
      getRange in class CodePointMap
      Parameters:
      start - range start
      filter - an object that may modify the map data value, or null if the values from the map are to be used unmodified
      range - the range object that will be set to the code point range and value
      Returns:
      true if start is 0..U+10FFFF; otherwise no new range is fetched
    • set

      public void set(int c, int value)
      Sets a value for a code point.
      Parameters:
      c - the code point
      value - the value
    • setRange

      public void setRange(int start, int end, int value)
      Sets a value for each code point [start..end]. Faster and more space-efficient than setting the value for each code point separately.
      Parameters:
      start - the first code point to get the value
      end - the last code point to get the value (inclusive)
      value - the value
    • buildImmutable

      public CodePointTrie buildImmutable(CodePointTrie.Type type, CodePointTrie.ValueWidth valueWidth)
      Compacts the data and builds an immutable CodePointTrie according to the parameters. After this, the mutable trie will be empty.

      The mutable trie stores 32-bit values until buildImmutable() is called. If values shorter than 32 bits are to be stored in the immutable trie, then the upper bits are discarded. For example, when the mutable trie contains values 0x81, -0x7f, and 0xa581, and the value width is 8 bits, then each of these is stored as 0x81 and the immutable trie will return that as an unsigned value. (Some implementations may want to make productive temporary use of the upper bits until buildImmutable() discards them.)

      Not every possible set of mappings can be built into a CodePointTrie, because of limitations resulting from speed and space optimizations. Every Unicode assigned character can be mapped to a unique value. Typical data yields data structures far smaller than the limitations.

      It is possible to construct extremely unusual mappings that exceed the data structure limits. In such a case this function will throw an exception.

      Parameters:
      type - selects the trie type
      valueWidth - selects the number of bits in a trie data value; if smaller than 32 bits, then the values stored in the trie will be truncated first
      See Also: