Package com.github.tommyettinger.ds
Class OffsetBitSet
java.lang.Object
com.github.tommyettinger.ds.OffsetBitSet
- All Implemented Interfaces:
PrimitiveCollection<Integer>,PrimitiveCollection.OfInt
A bit set, which can be seen as a set of integer positions greater than some starting number,
that has changeable offset, or starting position. If you know the integer positions will all
be greater than or equal to some minimum value, such as -128, 0, or 1000, then you can use an offset
of that minimum value to save memory. This is important because every possible integer position, whether
contained in the bit set or not, takes up one bit of memory (rounded up to a multiple of 32), but
positions less than the offset simply aren't stored, and the bit set can grow to fit positions arbitrarily
higher than the offset. Allows comparison via bitwise operators to other bit sets, as long as the offsets
are the same.
This was originally Bits in libGDX. Many methods have been renamed to more-closely match the Collection API. This has also had the offset functionality added. It was changed from using
This was originally Bits in libGDX. Many methods have been renamed to more-closely match the Collection API. This has also had the offset functionality added. It 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.OfShort -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]The raw bits, each one representing the presence or absence of an integer at a position.protected OffsetBitSet.OffsetBitSetIteratorprotected OffsetBitSet.OffsetBitSetIteratorprotected intThis is the lowest integer position that this OffsetBitSet can store. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a bit set with an initial size that can store positions between 0 and 31, inclusive, without needing to resize.OffsetBitSet(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.OffsetBitSet(int[] toCopy) Creates a bit set from an entire int array.OffsetBitSet(int[] toCopy, int off, int length) Creates a bit set from an int array, starting reading at an offset and continuing for a given length.OffsetBitSet(int start, int end) Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the rangestartthroughend-1.OffsetBitSet(OffsetBitSet toCopy) Creates a bit set from another bit set. -
Method Summary
Modifier and TypeMethodDescriptionvoidactivate(int index) Sets the given int position to true, unless the position is less than theoffset(then it does nothing).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) voidand(OffsetBitSet other) Performs a logical AND of this target bit set with the argument bit set.voidandNot(OffsetBitSet 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 OffsetBitSet, without allocating a String.appendTo(StringBuilder builder) Given a StringBuilder, this appends the toString() representation of this OffsetBitSet, without allocating a String.<S extends CharSequence & Appendable>
SappendTo(S sb, String separator, boolean brackets, IntAppender appender) Appends to a StringBuilder from the contents of this PrimitiveCollection, but uses the givenIntAppenderto convert each item to a customizable representation and append them to a StringBuilder.voidchangeOffset(int addend) Addsaddendto the current offset, effectively adding to every int stored in this, in constant time.voidclear()Clears the entire bitset, removing all contained ints.booleancontains(int index) Returns true if the given position is contained in this bit set.booleancontainsAll(OffsetBitSet 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 less than theoffset(then it does nothing).booleanintGets the lowest integer position that this OffsetBitSet can store.int[]This gets the internalint[]used to store bits in bulk.inthashCode()booleanintersects(OffsetBitSet other) Returns true if the specified OffsetBitSet has any bits set to true that are also set to true in this OffsetBitSet.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(OffsetBitSet other) Performs a logical OR of this bit set with the bit set argument.static OffsetBitSetCallsparse(String, String, boolean)with brackets set to false.static OffsetBitSetCreates a new collection and fills it by callingPrimitiveCollection.OfInt.addLegible(String, String, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static OffsetBitSetCreates a new collection and fills it by callingPrimitiveCollection.OfInt.addLegible(String, String, int, int)with the given four parameters as-is.booleanremove(int index) Deactivates the given position and returns true if the bit set was modified in the process.voidsetOffset(int newOffset) Changes the offset without considering the previous value.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.voidtoggle(int index) Changes the given int position from true to false, or from false to true, unless the position is less than theoffset(then it does nothing).toString()static OffsetBitSetwith(int index) Static builder for an OffsetBitSet; this overload does not allocate an array for the index/indices, but only takes one index.static OffsetBitSetwith(int... indices) Static builder for an OffsetBitSet; this overload allocates an array for the indices unless given an array already, and can take many indices.voidxor(OffsetBitSet 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.OfInt
addAll, addDense, addDense, addLegible, addLegible, addVarargs, appendTo, containsAll, containsAll, containsAll, containsAll, containsAny, containsAny, containsAny, containsAny, denseAppendTo, equalContents, first, forEach, removeAll, removeAll, removeAll, removeAll, removeEach, removeEach, removeEach, removeEach, removeIf, retainAll, retainAll, retainAll, toArray, toArray, toDenseString, toDenseString, toString, toString, toString
-
Field Details
-
bits
protected int[] bitsThe raw bits, each one representing the presence or absence of an integer at a position. -
offset
protected int offsetThis is the lowest integer position that this OffsetBitSet can store. If all positions are at least equal to some value, using that for the offset can save space. -
iterator1
-
iterator2
-
-
Constructor Details
-
OffsetBitSet
public OffsetBitSet()Creates a bit set with an initial size that can store positions between 0 and 31, inclusive, without needing to resize. This has an offset of 0 and can resize to fit larger positions. -
OffsetBitSet
public OffsetBitSet(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 has an offset of 0 and can resize to fit larger positions.- Parameters:
bitCapacity- the initial size of the bit set
-
OffsetBitSet
public OffsetBitSet(int start, int end) Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the rangestartthroughend-1. This has an offset ofstartand can resize to fit larger positions.- Parameters:
start- the lowest value that can be stored in the bit setend- the initial end of the range of the bit set
-
OffsetBitSet
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
-
OffsetBitSet
Creates a bit set from any primitive int collection, such as aIntListorIntSet. The offset of the new bit set will be the lowest int in the collection, which you should be aware of if you intend to use the bitwise methods such asand(OffsetBitSet)andor(OffsetBitSet).- Parameters:
toCopy- the primitive int collection to copy
-
OffsetBitSet
public OffsetBitSet(int[] toCopy) Creates a bit set from an entire int array. The offset of the new bit set will be the lowest int in the collection, which you should be aware of if you intend to use the bitwise methods such asand(OffsetBitSet)andor(OffsetBitSet).- Parameters:
toCopy- the non-null int array to copy
-
OffsetBitSet
public OffsetBitSet(int[] toCopy, int off, int length) Creates a bit set from an int array, starting reading at an offset and continuing for a given length. The offset of the new bit set will be the lowest int in the collection, which you should be aware of if you intend to use the bitwise methods such asand(OffsetBitSet)andor(OffsetBitSet).- Parameters:
toCopy- the int array to copyoff- which index to start copying from toCopylength- how many items to copy from toCopy
-
-
Method Details
-
getOffset
public int getOffset()Gets the lowest integer position that this OffsetBitSet can store. If all positions are at least equal to some value, using that for the offset can save space. -
setOffset
public void setOffset(int newOffset) Changes the offset without considering the previous value. This effectively addsnewOffset - getOffset()to every int stored in this, in constant time. This also changes the minimum value in the process.- Parameters:
newOffset- the value to use instead of the current offset
-
changeOffset
public void changeOffset(int addend) Addsaddendto the current offset, effectively adding to every int stored in this, in constant time. This also changes the minimum value in the process.- Parameters:
addend- the value to add to the current offset
-
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(int index) Returns true if the given position is contained in this bit set. If the index is less than theoffset, this returns false.- Specified by:
containsin interfacePrimitiveCollection.OfInt- Parameters:
index- the index of the bit- Returns:
- whether the bit is set
-
remove
public boolean remove(int index) Deactivates the given position and returns true if the bit set was modified in the process. If the index is less than theoffset, this does not modify the bit set and returns false.- Specified by:
removein interfacePrimitiveCollection.OfInt- 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. If the index is less than theoffset, this does not modify the bit set and returns false.- Specified by:
addin interfacePrimitiveCollection.OfInt- Parameters:
index- the index of the bit- Returns:
- true if this modified the bit set
-
addAll
public boolean addAll(int[] indices) - Specified by:
addAllin interfacePrimitiveCollection.OfInt
-
addAll
public boolean addAll(int[] indices, int off, int length) - Specified by:
addAllin interfacePrimitiveCollection.OfInt
-
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) -
addAll
public boolean addAll(char[] indices, int off, int length) -
addAll
- Specified by:
addAllin interfacePrimitiveCollection.OfInt
-
iterator
Returns an iterator for the keys in the set. Remove is supported.Use the
OffsetBitSet.OffsetBitSetIteratorconstructor for nested or multithreaded iteration.- Specified by:
iteratorin interfacePrimitiveCollection<Integer>- Specified by:
iteratorin interfacePrimitiveCollection.OfInt
-
activate
public void activate(int index) Sets the given int position to true, unless the position is less than theoffset(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 less than theoffset(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 less than theoffset(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<Integer>
-
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. If this has any set bits, it will return an int at least equal tooffset. 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<Integer>- 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<Integer>- 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<Integer>- 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 thengetOffset() - 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() + getOffset()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. Both this OffsetBitSet andothermust have the same offset.- Parameters:
other- another OffsetBitSet; must have the same offset as this
-
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)that only works if both OffsetBitSet objects have the sameoffset. Both this OffsetBitSet andothermust have the same offset.- Parameters:
other- another OffsetBitSet; must have the same offset as this
-
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 in the bit set argument has the value true. Both this OffsetBitSet andothermust have the same offset.- Parameters:
other- another OffsetBitSet; must have the same offset as this
-
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.
othermust have the same offset.- Parameters:
other- another OffsetBitSet; must have the same offset as this
-
intersects
Returns true if the specified OffsetBitSet has any bits set to true that are also set to true in this OffsetBitSet. Both this OffsetBitSet andothermust have the same offset.- Parameters:
other- another OffsetBitSet; must have the same offset as this- Returns:
- boolean indicating whether this bit set intersects 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 OffsetBitSet. If this OffsetBitSet andotherhave the same offset, this is much more efficient, but it will work even if the offsets are different.- Parameters:
other- another OffsetBitSet- Returns:
- boolean indicating whether this bit set is a super set of the specified set
-
hashCode
public int hashCode()- Specified by:
hashCodein interfacePrimitiveCollection<Integer>- Overrides:
hashCodein classObject
-
equals
- Specified by:
equalsin interfacePrimitiveCollection<Integer>- Overrides:
equalsin classObject
-
appendContents
Given a StringBuilder, this appends part of the toString() representation of this OffsetBitSet, without allocating a String. This does not include the opening[and closing]chars, and only appends the int positions in this OffsetBitSet, 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 OffsetBitSet, 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, IntAppender appender) Appends to a StringBuilder from the contents of this PrimitiveCollection, but uses the givenIntAppenderto convert each item to a customizable representation and append them to a StringBuilder. To use the default String representation, you can useIntAppender.DEFAULTas an appender.- Specified by:
appendToin interfacePrimitiveCollection.OfInt- 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
-
with
Static builder for an OffsetBitSet; 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 position to place in the built bit set; must be non-negative- Returns:
- a new OffsetBitSet with the given item
-
with
Static builder for an OffsetBitSet; 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 OffsetBitSet 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.OfInt.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.OfInt.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
-