Class CharBitSet

java.lang.Object
com.github.tommyettinger.ds.CharBitSet
All Implemented Interfaces:
PrimitiveCollection<Character>, PrimitiveCollection.OfChar, PrimitiveSet<Character>, PrimitiveSet.SetOfChar, com.github.tommyettinger.function.CharPredicate

public class CharBitSet extends Object implements PrimitiveSet.SetOfChar, com.github.tommyettinger.function.CharPredicate
A bit set, which can be seen as a set of char positions in the Unicode Basic Multilingual Plane (the first 65536 chars in Unicode). Allows comparison via bitwise operators to other bit sets.
This was originally Bits in libGDX. Many methods have been renamed to more-closely match the Collection API. This was changed from using long to store 64 bits in one value, to int to store 32 bits in one value, because GWT is so slow at handling long.
  • Field Details

  • Constructor Details

    • CharBitSet

      public CharBitSet()
      Creates a bit set with an initial size that can store positions between 0 and 65535, inclusive, without needing to resize. This won't ever need to resize for any char input.
    • CharBitSet

      public CharBitSet(int bitCapacity)
      Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through bitCapacity-1. This can resize to fit larger positions.
      Parameters:
      bitCapacity - the initial size of the bit set
    • CharBitSet

      public CharBitSet(CharBitSet toCopy)
      Creates a bit set from another bit set. This will copy the raw bits and will have the same offset.
      Parameters:
      toCopy - bitset to copy
    • CharBitSet

      public CharBitSet(CharSequence toCopy)
      Creates a bit set from any primitive char collection, such as a CharList or CharDeque.
      Parameters:
      toCopy - the primitive int collection to copy
    • CharBitSet

      public CharBitSet(char[] toCopy)
      Creates a bit set from an entire char array.
      Parameters:
      toCopy - the non-null char array to copy
    • CharBitSet

      public CharBitSet(char[] toCopy, int off, int length)
      Creates a bit set from a char array, starting reading at an offset and continuing for a given length.
      Parameters:
      toCopy - the char array to copy
      off - which index to start copying from toCopy
      length - how many items to copy from toCopy
    • CharBitSet

      public CharBitSet(com.github.tommyettinger.function.CharPredicate predicate)
      Meant primarily for offline use to store the results of a CharPredicate on one target platform so those results can be recalled identically on all platforms. This can be relevant because of changing Unicode versions on newer JDK versions, or partial implementations of JDK predicates like Character.isLetter(char) on GWT.
      Parameters:
      predicate - a CharPredicate, which could be a method reference like Character::isLetter
      See Also:
    • CharBitSet

      public CharBitSet(int[] ints, boolean useAsRawBits)
      Allows passing an int array either to be treated as char contents to enter (ignoring any ints outside the valid char range) or as the raw bits that are used internally (which can be accessed with getRawBits(). Note that ints should always have a length of 1 or more; otherwise, it won't be used directly (or if useAsRawBits is false, it won't have any contents copied out).
      Parameters:
      ints - depending on useAsRawBits, this will be used as either char items or raw bits
      useAsRawBits - if true, ints will be used as raw bits and used directly, not copied as char items
  • Method Details

    • getRawBits

      public int[] getRawBits()
      This gets the internal int[] used to store bits in bulk. This is not meant for typical usage; it may be useful for serialization or other code that would typically need reflection to access the internals here. This may and often does include padding at the end.
      Returns:
      the raw int array used to store positions, one bit per on and per off position
    • setRawBits

      public void setRawBits(int[] bits)
      This allows setting the internal int[] used to store bits in bulk. This is not meant for typical usage; it may be useful for serialization or other code that would typically need reflection to access the internals here. Be very careful with this method. If bits is null or empty, it is ignored; this is the only error validation this does.
      Parameters:
      bits - a non-null, non-empty int array storing positions, typically obtained from getRawBits()
    • contains

      public boolean contains(char index)
      Returns true if the given char is contained in this bit set.
      Specified by:
      contains in interface PrimitiveCollection.OfChar
      Parameters:
      index - the index of the bit
      Returns:
      whether the bit is set
    • contains

      public boolean contains(int index)
      Returns true if the given position is contained in this bit set.
      Parameters:
      index - the index of the bit
      Returns:
      whether the bit is set
    • remove

      public boolean remove(char index)
      Deactivates the given position and returns true if the bit set was modified in the process.
      Specified by:
      remove in interface PrimitiveCollection.OfChar
      Parameters:
      index - the index of the bit
      Returns:
      true if this modified the bit set
    • remove

      public boolean remove(int index)
      Deactivates the given position and returns true if the bit set was modified in the process.
      Parameters:
      index - the index of the bit
      Returns:
      true if this modified the bit set
    • add

      public boolean add(char index)
      Activates the given position and returns true if the bit set was modified in the process.
      Specified by:
      add in interface PrimitiveCollection.OfChar
      Parameters:
      index - the index of the bit
      Returns:
      true if this modified the bit set
    • add

      public boolean add(int index)
      Activates the given position and returns true if the bit set was modified in the process.
      Parameters:
      index - the index of the bit
      Returns:
      true if this modified the bit set
    • addAll

      public boolean addAll(int[] indices)
    • addAll

      public boolean addAll(int[] indices, int off, int length)
    • addAll

      public boolean addAll(short[] indices)
    • addAll

      public boolean addAll(short[] indices, int off, int length)
    • addAll

      public boolean addAll(byte[] indices)
    • addAll

      public boolean addAll(byte[] indices, int off, int length)
    • addAll

      public boolean addAll(char[] indices)
      Specified by:
      addAll in interface PrimitiveCollection.OfChar
    • addAll

      public boolean addAll(char[] indices, int off, int length)
      Specified by:
      addAll in interface PrimitiveCollection.OfChar
    • addAll

      public boolean addAll(CharSequence indices)
    • addAll

      public boolean addAll(CharSequence indices, int off, int length)
    • addAll

      public boolean addAll(PrimitiveCollection.OfInt indices)
    • test

      public boolean test(char value)
      Evaluates this predicate on the given argument.
      Specified by:
      test in interface com.github.tommyettinger.function.CharPredicate
      Parameters:
      value - the input argument
      Returns:
      true if the input argument matches the predicate, otherwise false
    • iterator

      public CharBitSet.CharBitSetIterator iterator()
      Returns an iterator for the keys in the set. Remove is supported.

      Use the CharBitSet.CharBitSetIterator constructor for nested or multithreaded iteration.

      Specified by:
      iterator in interface PrimitiveCollection<Character>
      Specified by:
      iterator in interface PrimitiveCollection.OfChar
    • activate

      public void activate(int index)
      Sets the given int position to true, unless the position is outside char range (then it does nothing).
      Parameters:
      index - the index of the bit to set
    • deactivate

      public void deactivate(int index)
      Sets the given int position to false, unless the position is outside char range (then it does nothing).
      Parameters:
      index - the index of the bit to clear
    • toggle

      public void toggle(int index)
      Changes the given int position from true to false, or from false to true, unless the position is outside char range (then it does nothing).
      Parameters:
      index - the index of the bit to flip
    • clear

      public void clear()
      Clears the entire bitset, removing all contained ints. Doesn't change the capacity.
      Specified by:
      clear in interface PrimitiveCollection<Character>
    • numBits

      public int numBits()
      Gets the capacity in bits, including both true and false values, and including any false values that may be after the last contained position, but does not include the offset. Runs in O(1) time.
      Returns:
      the number of bits currently stored, not the highest set bit; doesn't include offset either
    • length

      public int length()
      Returns the "logical extent" of this bitset: the index of the highest set bit in the bitset plus one. Returns zero if the bitset contains no set bits. Runs in O(n) time.
      Returns:
      the logical extent of this bitset
    • size

      public int size()
      Returns the size of the set, or its cardinality; this is the count of distinct activated positions in the set. Note that unlike most Collection types, which typically have O(1) size() runtime, this runs in O(n) time, where n is on the order of the capacity.
      Specified by:
      size in interface PrimitiveCollection<Character>
      Returns:
      the count of distinct activated positions in the set.
    • notEmpty

      public boolean notEmpty()
      Checks if there are any positions contained in this at all. Run in O(n) time, but usually takes less.
      Specified by:
      notEmpty in interface PrimitiveCollection<Character>
      Returns:
      true if this bitset contains at least one bit set to true
    • isEmpty

      public boolean isEmpty()
      Checks if there are no positions contained in this at all. Run in O(n) time, but usually takes less.
      Specified by:
      isEmpty in interface PrimitiveCollection<Character>
      Returns:
      true if this bitset contains no bits that are set to true
    • nextSetBit

      public int nextSetBit(int fromIndex)
      Returns the index of the first bit that is set to true that occurs on or after the specified starting index. If no such bit exists then -1 is returned.
      Parameters:
      fromIndex - the index to start looking at
      Returns:
      the first position that is set to true that occurs on or after the specified starting index
    • nextClearBit

      public int nextClearBit(int fromIndex)
      Returns the index of the first bit that is set to false that occurs on or after the specified starting index. If no such bit exists then numBits() is returned.
      Parameters:
      fromIndex - the index to start looking at
      Returns:
      the first position that is set to true that occurs on or after the specified starting index
    • and

      public void and(CharBitSet other)
      Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the bit set argument also had the value true.
      Parameters:
      other - another CharBitSet
    • andNot

      public void andNot(CharBitSet other)
      Clears all the bits in this bit set whose corresponding bit is set in the specified bit set. This can be seen as an optimized version of PrimitiveCollection.OfInt.removeAll(OfInt).
      Parameters:
      other - another CharBitSet
    • or

      public void or(CharBitSet other)
      Performs a logical OR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if it either already had the value true or the corresponding bit in other has the value true.
      Parameters:
      other - another CharBitSet
    • xor

      public void xor(CharBitSet other)
      Performs a logical XOR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if one of the following statements holds:
      • The bit initially has the value true, and the corresponding bit in the argument has the value false.
      • The bit initially has the value false, and the corresponding bit in the argument has the value true.
      Parameters:
      other - another CharBitSet
    • intersects

      public boolean intersects(CharBitSet other)
      Returns true if the specified CharBitSet has any bits set to true that are also set to true in this CharBitSet.
      Parameters:
      other - another CharBitSet
      Returns:
      true if this bit set shares any set bits with the specified bit set
    • containsAll

      public boolean containsAll(CharBitSet other)
      Returns true if this bit set is a super set of the specified set, i.e. it has all bits set to true that are also set to true in the specified CharBitSet.
      Parameters:
      other - another CharBitSet
      Returns:
      boolean indicating whether this bit set is a super set of the specified set
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface PrimitiveCollection<Character>
      Specified by:
      hashCode in interface PrimitiveSet<Character>
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface PrimitiveCollection<Character>
      Specified by:
      equals in interface PrimitiveSet<Character>
      Overrides:
      equals in class Object
    • appendContents

      public StringBuilder appendContents(StringBuilder builder, String delimiter)
      Given a StringBuilder, this appends part of the toString() representation of this CharBitSet, without allocating a String. This does not include the opening [ and closing ] chars, and only appends the int positions in this CharBitSet, each pair separated by the given delimiter String. You can use this to choose a different delimiter from what toString() uses.
      Parameters:
      builder - a StringBuilder that will be modified in-place and returned
      delimiter - the String that separates every pair of integers in the result
      Returns:
      the given StringBuilder, after modifications
    • appendTo

      public StringBuilder appendTo(StringBuilder builder)
      Given a StringBuilder, this appends the toString() representation of this CharBitSet, without allocating a String. This includes the opening [ and closing ] chars; it uses ", " as its delimiter.
      Parameters:
      builder - a StringBuilder that will be modified in-place and returned
      Returns:
      the given StringBuilder, after modifications
    • appendTo

      public <S extends CharSequence & Appendable> S appendTo(S sb, String separator, boolean brackets, CharAppender appender)
      Appends to a StringBuilder from the contents of this PrimitiveCollection, but uses the given CharAppender to convert each item to a customizable representation and append them to a StringBuilder. To use the default String representation, you can use CharAppender.DEFAULT as an appender.
      Specified by:
      appendTo in interface PrimitiveCollection.OfChar
      Type Parameters:
      S - any type that is both a CharSequence and an Appendable, such as StringBuilder, StringBuffer, CharBuffer, or CharList
      Parameters:
      sb - a StringBuilder that this can append to
      separator - how to separate items, such as ", "
      brackets - true to wrap the output in square brackets, or false to omit them
      appender - a function that takes a StringBuilder and an int, and returns the modified StringBuilder
      Returns:
      sb, with the appended items of this PrimitiveCollection
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toJavaCode

      public String toJavaCode()
      A convenience method that returns a String of Java source that constructs this CharBitSet directly from its raw bits, without any extra steps involved.
      This is intended to allow tests on one platform to set up CharBitSet values that store the results of some test, such as Character.isLetter(char), and to load those results on any platform without having to recalculate the results (potentially with incorrect results on other platforms). Notably, GWT doesn't calculate many Unicode queries correctly (at least according to their JVM documentation), and this can store their results for a recent Unicode version by running on the most recent desktop JDK, and storing to be loaded on other platforms.
      Returns:
      a String of Java code that can be used to construct an exact copy of this CharBitSet
    • with

      public static CharBitSet with(char index)
      Static builder for a CharBitSet; this overload does not allocate an array for the index/indices, but only takes one index. This always has an offset of 0.
      Parameters:
      index - the one char to place in the built bit set
      Returns:
      a new CharBitSet with the given item
    • with

      public static CharBitSet with(char... indices)
      Static builder for a CharBitSet; this overload allocates an array for the indices unless given an array already, and can take many indices. This always has an offset of 0.
      Parameters:
      indices - the positions to place in the built bit set; must be non-negative
      Returns:
      a new CharBitSet with the given items
    • parse

      public static CharBitSet parse(String str, String delimiter)
      Calls parse(String, String, boolean) with brackets set to false.
      Parameters:
      str - a String that will be parsed in full
      delimiter - the delimiter between items in str
      Returns:
      a new collection parsed from str
    • parse

      public static CharBitSet parse(String str, String delimiter, boolean brackets)
      Creates a new collection and fills it by calling PrimitiveCollection.OfChar.addLegible(String, String, int, int) on either all of str (if brackets is false) or str without its first and last chars (if brackets is true). Each item is expected to be separated by delimiter.
      Parameters:
      str - a String that will be parsed in full (depending on brackets)
      delimiter - the delimiter between items in str
      brackets - if true, the first and last chars in str will be ignored
      Returns:
      a new collection parsed from str
    • parse

      public static CharBitSet parse(String str, String delimiter, int offset, int length)
      Creates a new collection and fills it by calling PrimitiveCollection.OfChar.addLegible(String, String, int, int) with the given four parameters as-is.
      Parameters:
      str - a String that will have the given section parsed
      delimiter - the delimiter between items in str
      offset - the first position to parse in str, inclusive
      length - how many chars to parse, starting from offset
      Returns:
      a new collection parsed from str