Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lots of changes #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
node_modules
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.6
- 0.8
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
# BitArray: A simple bit array/bit field library in pure JavaScript
[![Build Status](https://secure.travis-ci.org/microjs/bitarray.png?branch=master)](https://travis-ci.org/microjs/bitarray)
# BitArray

BitArray is a simple bit array/bit field library in pure JavaScript

BitArray is based on the Ruby library BitArray by Peter Cooper (https://github.com/peterc/bitarray).

Basic, pure JavaScript bit field.
Basic, pure JavaScript bit field.

## Installation

Server:

$ npm install bitarray

Client:

$ component install microjs/bitarray

## Examples

Expand All @@ -19,10 +32,17 @@ ba.set(100, 1);
ba.get(100); // => 1

ba.set(100, 0);
ba.get(100); // = > 0
ba.get(100); // => 0
```

Create a BitArray where all the bits default to 1:

```javascript
var ba = new BitArray(100, 1);
ba.get(50); // => 1
```

More:
toString:

```javascript
var ba = new BitArray(20);
Expand Down
10 changes: 8 additions & 2 deletions bitarray.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"use strict";

function BitArray(size, value){
if(value === undefined) value = 0;
if (typeof this === 'undefined') return new BitArray(size, value);
if (typeof value === 'undefined') value = 0;
this.size = size;
this.field = new Array(~~((size - 1) / BitArray.ELEMENT_WIDTH) + 1);
for(var i = 0; i < this.field.length; i++)
Expand Down Expand Up @@ -35,4 +38,7 @@ BitArray.prototype.toString = function(){
return binary;
}).reverse().join('');
return string.split('').reverse().join('').slice(0,this.size);
}
}

if (typeof module != 'undefined')
module.exports = BitArray;
15 changes: 15 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "bitarray",
"repo": "microjs/bitarray",
"description": "Pure JavaScript bit array/bitfield implementation",
"version": "1.0.0",
"keywords": [],
"dependencies": {},
"development": {},
"scripts": [
"bitarray.js"
],
"main": "bitarray.js",
"twitter": "@ForbesLindesay",
"license": "MIT"
}
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "bitarray",
"version": "1.0.0",
"description": "Pure JavaScript bit array/bitfield implementation",
"main": "./bitarray.js",
"scripts": {
"test": "mocha -R spec"
},
"repository": {
"type": "git",
"url": "https://github.com/ForbesLindesay/bitarray.git"
},
"license": "MIT",
"devDependencies": {
"mocha": "*",
"better-assert": "*"
},
"dependencies": {}
}
42 changes: 27 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
var fs = require('fs'), filedata = fs.readFileSync('bitarray.js','utf8');
eval(filedata);
var assert = require('better-assert');
var BitArray = require('./');

var ba = new BitArray(200);
ba.set(100, 1);
console.log(ba.field);
console.log(ba.get(100), 'should be 1');
describe('a bit array of length 200', function () {
var ba = BitArray(200);
it('lets you set a value to 1', function () {
ba.set(100, 1);
assert(ba.get(100) === 1);
});
it('lets you set that value back to 0', function () {
ba.set(100, 0);
assert(ba.get(100) === 0);
});
});

ba.set(100, 0);
console.log(ba.get(100), 'should be 0');
describe('a bit array of length 20', function () {
it('lets you set lots of values independently', function () {
var ba = BitArray(20);
[1,3,5,9,11,13,15].forEach(function(i){ ba.set(i, 1) });
assert(ba.toString() === '01010100010101010000');
});
});

ba = new BitArray(20);
[1,3,5,9,11,13,15].forEach(function(i){ ba.set(i, 1) });
console.log(ba.toString() == '01010100010101010000', 'should be true');

ba = new BitArray(33,1);
[0].forEach(function(i){ ba.set(i, 0) });
console.log(ba.toString() == '011111111111111111111111111111111', 'should be true');
describe('a bit array with a default of 1', function () {
it('lets you set a bit to 0', function () {
var ba = BitArray(33,1);
ba.set(0, 0)
assert(ba.toString() == '011111111111111111111111111111111');
});
});