Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jwerle
committed
Sep 27, 2013
0 parents
commit ec03247
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
test: | ||
node test.js | ||
|
||
.PHONY: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
bitcells | ||
===== | ||
|
||
Bitwise operations on mutable cells in a stack | ||
|
||
## install | ||
|
||
### npm | ||
|
||
```sh | ||
$ npm install bitcells --save | ||
``` | ||
|
||
### component | ||
|
||
```sh | ||
$ component install jwerle/bitcells --save | ||
``` | ||
|
||
## usage | ||
|
||
```js | ||
var Cell = require('bitcells').Cell | ||
, Stack = require('bitcells').Stack | ||
, assert = require('assert') | ||
|
||
|
||
var $ = Stack() | ||
, a = Cell(4) | ||
, b = Cell(8) | ||
, c = Cell(16) | ||
|
||
$.push(a).push(b) | ||
|
||
assert(a & $); | ||
assert(b & $); | ||
assert(12 == $); | ||
|
||
a.v = c; | ||
|
||
assert(24 == $) | ||
``` | ||
|
||
## why? | ||
|
||
I don't know, just for fun. | ||
|
||
## license | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
|
||
exports.Stack = Stack; | ||
function Stack () { | ||
if (!(this instanceof Stack)) return new Stack(); | ||
this.cells = []; | ||
} | ||
|
||
Stack.prototype.push = function (c) { | ||
this.cells.push(c); | ||
return this; | ||
}; | ||
|
||
Stack.prototype.valueOf = function () { | ||
if (1 === this.cells.length) return this.cells[0].valueOf(); | ||
else if (0 === this.cells.length) return 0; | ||
else return this.cells.reduce(function (c, n) { return c + n }); | ||
}; | ||
|
||
Stack.prototype.toString = function () { | ||
return String(Number(this)); | ||
}; | ||
|
||
|
||
exports.Cell = Cell; | ||
function Cell (v) { | ||
if (!(this instanceof Cell)) return new Cell(v); | ||
this.v = v; | ||
} | ||
|
||
Cell.prototype.valueOf = function () { | ||
return this.v.valueOf(); | ||
} | ||
|
||
Cell.prototype.toString = function () { | ||
return String(this.v); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
var Stack = require('./').Stack | ||
, Cell = require('./').Cell | ||
, assert = require('assert') | ||
|
||
var $1 = Stack() | ||
, $2 = Stack() | ||
|
||
var a = Cell(4) | ||
, b = Cell(a << 1) | ||
, c = Cell(b << 1) | ||
, d = Cell(c << 1) | ||
, e = Cell(d << 1) | ||
|
||
$1.push(a).push(b) | ||
$2.push(c).push(d) | ||
|
||
assert(12 == $1); | ||
assert(48 == $2); | ||
|
||
assert(4 == a); | ||
assert(8 == b); | ||
assert(16 == c); | ||
assert(32 == d); | ||
assert(64 == e); | ||
|
||
assert($1 & a); | ||
assert($1 & b); | ||
assert(($1 & c) == 0); | ||
|
||
assert(a == a << 0); | ||
assert(b == a << 1); | ||
assert(c == a << 2); | ||
assert(d == a << 3); | ||
assert(e == a << 4); | ||
|
||
var x = Cell(50) | ||
, y = Cell(75) | ||
|
||
var s = Stack() | ||
, t = Stack() | ||
|
||
s.push(x); | ||
t.push(y); | ||
|
||
assert(50 == x); | ||
assert(75 == y); | ||
|
||
assert(50 == s); | ||
assert(75 == t); | ||
assert(125 == s + t); | ||
|
||
x.v = 25; | ||
y.v = 50; | ||
assert(25 == s); | ||
assert(50 == t); | ||
assert(75 == s + t); | ||
|
||
assert(x & s); | ||
assert(y & t); | ||
|
||
s.push(t); | ||
|
||
assert(75 == s); | ||
assert(50 == t) | ||
assert(s & t); | ||
|
||
y.v = x; | ||
|
||
assert(50 == s); | ||
assert(25 == t); |