Giter Site home page Giter Site logo

cleoold / bitarray Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 2.0 71 KB

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

Home Page: https://cleoold.github.io/bitarray/

License: MIT License

Makefile 2.75% Lua 23.49% C 73.77%
bitarray bitset lua

bitarray's Introduction

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

bitarray's People

Contributors

cleoold avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

hero1s deining

bitarray's Issues

Why is there no `count` method?

For counting the number of 1 bits in the array. Like for example

local c = Bitarray.new(16):from_uint16(4015):count() --> 10 '1' bits on

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.