Package com.github.tommyettinger.ds
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
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.-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface com.github.tommyettinger.ds.PrimitiveCollection
PrimitiveCollection.OfBoolean, PrimitiveCollection.OfByte, PrimitiveCollection.OfChar, PrimitiveCollection.OfDouble, PrimitiveCollection.OfFloat, PrimitiveCollection.OfInt, PrimitiveCollection.OfLong, PrimitiveCollection.OfShortNested classes/interfaces inherited from interface com.github.tommyettinger.ds.PrimitiveSet
PrimitiveSet.SetOfChar, PrimitiveSet.SetOfInt, PrimitiveSet.SetOfLong -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]The raw bits, each one representing the presence or absence of a char at a position.protected CharBitSet.CharBitSetIteratorprotected CharBitSet.CharBitSetIterator -
Constructor Summary
ConstructorsConstructorDescriptionCreates a bit set with an initial size that can store positions between 0 and 65535, inclusive, without needing to resize.CharBitSet(char[] toCopy) Creates a bit set from an entire char array.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.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.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 withgetRawBits().CharBitSet(CharBitSet toCopy) Creates a bit set from another bit set.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.CharBitSet(CharSequence toCopy) -
Method Summary
Modifier and TypeMethodDescriptionvoidactivate(int index) Sets the given int position to true, unless the position is outside char range (then it does nothing).booleanadd(char index) Activates the given position and returns true if the bit set was modified in the process.booleanadd(int index) Activates the given position and returns true if the bit set was modified in the process.booleanaddAll(byte[] indices) booleanaddAll(byte[] indices, int off, int length) booleanaddAll(char[] indices) booleanaddAll(char[] indices, int off, int length) booleanaddAll(int[] indices) booleanaddAll(int[] indices, int off, int length) booleanaddAll(short[] indices) booleanaddAll(short[] indices, int off, int length) booleanaddAll(PrimitiveCollection.OfInt indices) booleanaddAll(CharSequence indices) booleanaddAll(CharSequence indices, int off, int length) voidand(CharBitSet other) Performs a logical AND of this target bit set with the argument bit set.voidandNot(CharBitSet other) Clears all the bits in this bit set whose corresponding bit is set in the specified bit set.appendContents(StringBuilder builder, String delimiter) Given a StringBuilder, this appends part of the toString() representation of this CharBitSet, without allocating a String.appendTo(StringBuilder builder) Given a StringBuilder, this appends the toString() representation of this CharBitSet, without allocating a String.<S extends CharSequence & Appendable>
SappendTo(S sb, String separator, boolean brackets, CharAppender appender) Appends to a StringBuilder from the contents of this PrimitiveCollection, but uses the givenCharAppenderto convert each item to a customizable representation and append them to a StringBuilder.voidclear()Clears the entire bitset, removing all contained ints.booleancontains(char index) Returns true if the given char is contained in this bit set.booleancontains(int index) Returns true if the given position is contained in this bit set.booleancontainsAll(CharBitSet other) Returns true if this bit set is a super set of the specified set, i.e.voiddeactivate(int index) Sets the given int position to false, unless the position is outside char range (then it does nothing).booleanint[]This gets the internalint[]used to store bits in bulk.inthashCode()booleanintersects(CharBitSet other) Returns true if the specified CharBitSet has any bits set to true that are also set to true in this CharBitSet.booleanisEmpty()Checks if there are no positions contained in this at all.iterator()Returns an iterator for the keys in the set.intlength()Returns the "logical extent" of this bitset: the index of the highest set bit in the bitset plus one.intnextClearBit(int fromIndex) Returns the index of the first bit that is set to false that occurs on or after the specified starting index.intnextSetBit(int fromIndex) Returns the index of the first bit that is set to true that occurs on or after the specified starting index.booleannotEmpty()Checks if there are any positions contained in this at all.intnumBits()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.voidor(CharBitSet other) Performs a logical OR of this bit set with the bit set argument.static CharBitSetCallsparse(String, String, boolean)with brackets set to false.static CharBitSetCreates a new collection and fills it by callingPrimitiveCollection.OfChar.addLegible(String, String, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static CharBitSetCreates a new collection and fills it by callingPrimitiveCollection.OfChar.addLegible(String, String, int, int)with the given four parameters as-is.booleanremove(char index) Deactivates the given position and returns true if the bit set was modified in the process.booleanremove(int index) Deactivates the given position and returns true if the bit set was modified in the process.voidsetRawBits(int[] bits) This allows setting the internalint[]used to store bits in bulk.intsize()Returns the size of the set, or its cardinality; this is the count of distinct activated positions in the set.booleantest(char value) Evaluates this predicate on the given argument.voidtoggle(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).A convenience method that returns a String of Java source that constructs this CharBitSet directly from its raw bits, without any extra steps involved.toString()static CharBitSetwith(char index) Static builder for a CharBitSet; this overload does not allocate an array for the index/indices, but only takes one index.static CharBitSetwith(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.voidxor(CharBitSet other) Performs a logical XOR of this bit set with the bit set argument.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.PrimitiveCollection.OfChar
addAll, addAll, addDense, addDense, addLegible, addLegible, addVarargs, appendTo, containsAll, containsAll, containsAll, containsAll, containsAny, containsAny, containsAny, containsAny, denseAppendTo, first, forEach, removeAll, removeAll, removeAll, removeAll, removeEach, removeEach, removeEach, removeEach, removeIf, retainAll, retainAll, retainAll, toArray, toArray, toDenseString, toDenseString, toString, toString, toStringMethods inherited from interface com.github.tommyettinger.ds.PrimitiveSet.SetOfChar
equalContents
-
Field Details
-
bits
protected int[] bitsThe raw bits, each one representing the presence or absence of a char at a position. -
iterator1
-
iterator2
-
-
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
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
- 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 copyoff- which index to start copying from toCopylength- 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 likeCharacter.isLetter(char)on GWT.- Parameters:
predicate- a CharPredicate, which could be a method reference likeCharacter::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 withgetRawBits(). Note thatintsshould always have a length of 1 or more; otherwise, it won't be used directly (or ifuseAsRawBitsis false, it won't have any contents copied out).- Parameters:
ints- depending onuseAsRawBits, this will be used as either char items or raw bitsuseAsRawBits- if true,intswill be used as raw bits and used directly, not copied as char items
-
-
Method Details
-
getRawBits
public int[] getRawBits()This gets the internalint[]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 internalint[]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 fromgetRawBits()
-
contains
public boolean contains(char index) Returns true if the given char is contained in this bit set.- Specified by:
containsin interfacePrimitiveCollection.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:
removein interfacePrimitiveCollection.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:
addin interfacePrimitiveCollection.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:
addAllin interfacePrimitiveCollection.OfChar
-
addAll
public boolean addAll(char[] indices, int off, int length) - Specified by:
addAllin interfacePrimitiveCollection.OfChar
-
addAll
-
addAll
-
addAll
-
test
public boolean test(char value) Evaluates this predicate on the given argument.- Specified by:
testin interfacecom.github.tommyettinger.function.CharPredicate- Parameters:
value- the input argument- Returns:
trueif the input argument matches the predicate, otherwisefalse
-
iterator
Returns an iterator for the keys in the set. Remove is supported.Use the
CharBitSet.CharBitSetIteratorconstructor for nested or multithreaded iteration.- Specified by:
iteratorin interfacePrimitiveCollection<Character>- Specified by:
iteratorin interfacePrimitiveCollection.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:
clearin interfacePrimitiveCollection<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:
sizein interfacePrimitiveCollection<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:
notEmptyin interfacePrimitiveCollection<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:
isEmptyin interfacePrimitiveCollection<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-1is 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 thennumBits()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
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
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 ofPrimitiveCollection.OfInt.removeAll(OfInt).- Parameters:
other- another CharBitSet
-
or
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 inotherhas the value true.- Parameters:
other- another CharBitSet
-
xor
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
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
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:
hashCodein interfacePrimitiveCollection<Character>- Specified by:
hashCodein interfacePrimitiveSet<Character>- Overrides:
hashCodein classObject
-
equals
- Specified by:
equalsin interfacePrimitiveCollection<Character>- Specified by:
equalsin interfacePrimitiveSet<Character>- Overrides:
equalsin classObject
-
appendContents
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 returneddelimiter- the String that separates every pair of integers in the result- Returns:
- the given StringBuilder, after modifications
-
appendTo
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 givenCharAppenderto convert each item to a customizable representation and append them to a StringBuilder. To use the default String representation, you can useCharAppender.DEFAULTas an appender.- Specified by:
appendToin interfacePrimitiveCollection.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 toseparator- how to separate items, such as", "brackets- true to wrap the output in square brackets, or false to omit themappender- a function that takes a StringBuilder and an int, and returns the modified StringBuilder- Returns:
sb, with the appended items of this PrimitiveCollection
-
toString
-
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 asCharacter.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
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
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
Callsparse(String, String, boolean)with brackets set to false.- Parameters:
str- a String that will be parsed in fulldelimiter- the delimiter between items in str- Returns:
- a new collection parsed from str
-
parse
Creates a new collection and fills it by callingPrimitiveCollection.OfChar.addLegible(String, String, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true). Each item is expected to be separated bydelimiter.- Parameters:
str- a String that will be parsed in full (depending on brackets)delimiter- the delimiter between items in strbrackets- if true, the first and last chars in str will be ignored- Returns:
- a new collection parsed from str
-
parse
Creates a new collection and fills it by callingPrimitiveCollection.OfChar.addLegible(String, String, int, int)with the given four parameters as-is.- Parameters:
str- a String that will have the given section parseddelimiter- the delimiter between items in stroffset- the first position to parse in str, inclusivelength- how many chars to parse, starting from offset- Returns:
- a new collection parsed from str
-