Skip to content

Bit array (or bit set, bit string, boolean vector, ..., whatever) data structure for Lua.

License

Notifications You must be signed in to change notification settings

cleoold/bitarray

Repository files navigation

LuaRocks dev-build-ci

A simple bit array (or bit set, bit string, boolean vector, ... whatever) data structure for Lua written in pure C.

The data structure Bitarray stores bits (booleans) in an array in a memory-saving manner internally. One can assign value (false or true) to each element as well as extract values from the array.

  • Array bit access using overloaded [] operators, concatenation with .., as well as &, |, ~ for bit operations (5.3+).
  • Object-oriented access. Method chaining is available.
  • Conversion between bitarray and unsigned integers (big-endian).

Install from luarocks

It will install the release version.

luarocks install bitarray

Build the library manually

make all LUA_VERSION=5.3 # or 5.1, 5.2

The generated shared library will reside in out folder. Unfortunately, I understand the difficulty of finding the right install path for libraries for different platforms so it is your responsibility to copy the file there. Typically it can be /usr/local/lib/lua/5.3/.

FreeBSD users will need to use gmake.

Requiring ldoc, available by issuing

make doc

It is also available at here.

Basic usage

Array access

local Bitarray = require'bitarray'
local a = Bitarray.new(10)
a[1] = true
for i = 1, #a do print(a[i]) end
-- true false false false false false false false false false

Method chaining

local b = Bitarray.new(10)
b:fill(true):set(1, false):set(#b, false)
print(b)
-- Bitarray[0,1,1,1,1,1,1,1,1,0]

Storing integers

local c1 = Bitarray.new(16):from_uint16(65535)
print(c1)
-- Bitarray[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
local c2 = Bitarray.new(16):from_uint16(4015)
print(c2)
-- Bitarray[0,0,0,0,1,1,1,1,1,0,1,0,1,1,1,1]
local c3 = c1 .. c2
print(c3)
-- Bitarray[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,1,0,1,1,1,1]
print(c3:at_uint16(1))
-- 65535
print(c3:at_uint16(17))
-- 4015
print(c3:at_uint32(1))
-- 4294905775

License

MIT

About

Bit array (or bit set, bit string, boolean vector, ..., whatever) data structure for Lua.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published