LibBitSet (0.0.1)

LibBitSet is a library for working with bitsets. It provides a memory-optimized alternative to using tables like id => true, making it a great option for handling large datasets or tracking boolean states. By utilizing bitwise operations, LibBitSet allows developers to store and manipulate flags efficiently.
Key Functions
Creating a BitSet:
local bitSet = LibBitSet:New()
Setting a Bit:
bitSet:SetBit(index)
Sets the bit at the specified index to 1.
Clearing a Bit:
bitSet:ClearBit(index)
Clears the bit at the specified index (sets it to 0).
Toggling a Bit:
bitSet:ToggleBit(index)
Toggles the bit at the specified index (switches between 1 and 0).
Getting a Bit Value:
local bitValue = bitSet:GetBit(index)
Returns the value of the bit (0 or 1) at the specified index.
Bitwise Operations
Bitwise OR:
local result = bitSet:BitwiseOr(otherBitSet)
Performs a bitwise OR operation with another bit set.
Bitwise AND:
local result = bitSet:BitwiseAnd(otherBitSet)
Performs a bitwise AND operation with another bit set.
Bitwise NOT:
local result = bitSet:BitwiseNot(maxIndex)
Returns the bitwise negation of the current bit set.
Converting to a string:
local bitString = bitSet:ToString(maxIndex)
Returns a string representation of the bit set.
Counting Set Bits:
local count = bitSet:CountSetBits()
Counts the number of bits set to 1 in the bitset.
Iterating Over Bits
LibBitSet provides an iterator to loop over the set bits:
for index, value in LibBitSetIterator(bitSet) do
— Your code here
end
This function will iterate through all set bits.
Integration with SavedVars
To store bit sets in SavedVars, LibBitSet can be easily integrated as shown below:
self.data = LibSimpleSavedVars:NewInstallationWide(“MyAddonData”, 1, {
items = LibBitSet.New(),
})
— Load BitSet from SavedVars
self.data.items = LibBitSetLoad(LibBitSet.New(), self.data.items)