• Stars
    star
    105
  • Rank 328,196 (Top 7 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 13 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pure Ruby bit array/bitfield implementation

BitArray: Pure Ruby bit-array/bitfield library

A simple, pure-Ruby 'bit field' object. Works well for Bloom filters (the use case for which I originally wrote it, although there are numerous good libraries for that task now).

Despite its age, BitArray has been updated to work within a typical, modern Ruby environment, but is only 'mildly' maintained.

Installation

bundle add bitarray

Examples

To use:

require 'bitarray'

Create a bit array 1000 bits wide:

ba = BitArray.new(1000)

Setting and reading bits:

ba[100] = 1
ba[100]
#=> 1

ba[100] = 0
ba[100]
#=> 0

More:

ba = BitArray.new(20)
[1,3,5,9,11,13,15].each { |i| ba[i] = 1 }
ba.to_s
#=> "01010100010101010000"
ba.total_set
#=> 7

Initializing BitArray with a custom field value:

ba = BitArray.new(16, ["0000111111110000"].pack('B*'))
ba.to_s # "1111000000001111"

BitArray by default stores the bits in reverse order for each byte. If for example, you are initializing BitArray with Redis raw value manipulated with setbit / getbit operations, you will need to tell BitArray to not reverse the bits in each byte using the reverse_byte: false option:

ba = BitArray.new(16, ["0000111111110000"].pack('B*'), reverse_byte: false)
ba.to_s # "0000111111110000"

History

  • 1.3 in 2022 (cleanups and a minor perf tweak)
  • 1.2 in 2018 (Added option to skip reverse the bits for each byte by @dalibor)
  • 1.1 in 2018 (fixed a significant bug)
  • 1.0 in 2017 (updated for modern Ruby, more efficient storage, and 10th birthday)
  • 0.0.1 in 2012 (original v5 released on GitHub)
  • v5 (added support for flags being on by default, instead of off)
  • v4 (fixed bug where setting 0 bits to 0 caused a set to 1)
  • v3 (supports dynamic bitwidths for array elements.. now doing 32 bit widths default)
  • v2 (now uses 1 << y, rather than 2 ** y .. it's 21.8 times faster!)
  • v1 (first release)

Thanks

Thanks to Michael Slade for encouraging me to update this library on its 10th birthday and for suggesting finally using String's getbyte and setbyte methods now that we're all on 1.9+ compatible implementations.

Further thanks to @tdeo, @JoshuaSP, @dalibor, @yegct and @m1lt0n for pull requests.

License

MIT licensed. Copyright 2007-2022 Peter Cooper.

More Repositories

1

pismo

Extracts machine-readable metadata and content from Web pages
Ruby
747
star
2

whatlanguage

A language detection library for Ruby that uses bloom filters for speed.
Ruby
685
star
3

testrocket

Super simple Ruby testing library
Ruby
237
star
4

engblogs

Engineering Blogs
Ruby
119
star
5

hackerslide

A sliding view of the Hacker News front page over time
JavaScript
76
star
6

trtl

Tk-powered Ruby turtle graphics
Ruby
65
star
7

chrome2gif

Dynamically create an animated GIF of a page running in Chrome
JavaScript
63
star
8

rsmaz

Ruby port of Smaz - a short string compression library
Ruby
44
star
9

multirb

Run Ruby code over multiple implementations/versions using RVM from a IRB-esque prompt
Ruby
43
star
10

potc-jruby

JRuby port of Prelude of the Chambered, a Java game
Ruby
41
star
11

videocr

Perform OCR upon entire videos to look for credentials or similar.
Python
39
star
12

webloc

Read and write .webloc (web link) files on macOS / OS X
Ruby
24
star
13

switchpipe

SwitchPipe is a backend process manager and HTTP proxy that makes (especially Ruby) web app deployment simple. NOW OBSOLETE. DO NOT USE.
Ruby
14
star
14

webassembly-simplest-demo

A simple example of compiling C to WebAssembly and running it
HTML
13
star
15

simredis

Redis simulator that allows you to use redis-rb without a Redis daemon running
Ruby
10
star
16

monos

My Ludum Dare 22 entry
Ruby
8
star
17

coffeebots

A programmable robot war game in CoffeeScript / JavaScript
CoffeeScript
4
star
18

illustrator-cc-scripting

Help and resources on scripting Illustrator CC on macOS
JavaScript
4
star
19

igsubscriber

Streams live market data from IG.com's Lightstreamer into a Redis data store
JavaScript
4
star
20

massiveattract

A game developed in a few hours for Ludum Dare 23
JavaScript
2
star
21

bits

Random bits of code I want to keep track of
Ruby
1
star
22

hntitles

Hacker News title edit tracker
Ruby
1
star
23

cardnut

Simple backend for pushing Twilio text messages over WebSocket to a client (not useful for many)
JavaScript
1
star
24

toto

MIDI proxying for great fun with keyboards
Ruby
1
star
25

superhighway

superhighway.dev
HTML
1
star
26

ruranopupore

An Ubuntu 12.04 LTS Ruby, Rails, Nginx, Node.js, Puma, Postgres, and Redis Ansible Playbook Collection
1
star
27

aoc2019solutions

My Advent of Code 2019 Solutions
Ruby
1
star
28

herokuexperiment

Running two processes (web and worker) on a single Heroku dyno
Ruby
1
star