pax_global_header00006660000000000000000000000064130566544320014522gustar00rootroot0000000000000052 comment=922ad0e94c4134fa1de17ae456551f950eef528f sparse-bitfield-3.0.3/000077500000000000000000000000001305665443200146025ustar00rootroot00000000000000sparse-bitfield-3.0.3/.gitignore000066400000000000000000000000151305665443200165660ustar00rootroot00000000000000node_modules sparse-bitfield-3.0.3/.travis.yml000066400000000000000000000001051305665443200167070ustar00rootroot00000000000000language: node_js node_js: - '0.10' - '0.12' - '4.0' - '5.0' sparse-bitfield-3.0.3/LICENSE000066400000000000000000000020671305665443200156140ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Mathias Buus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. sparse-bitfield-3.0.3/README.md000066400000000000000000000033551305665443200160670ustar00rootroot00000000000000# sparse-bitfield Bitfield implementation that allocates a series of 1kb buffers to support sparse bitfields without allocating a massive buffer. If you want to simple implementation of a flat bitfield see the [bitfield](https://github.com/fb55/bitfield) module. This module is mostly useful if you need a big bitfield where you won't nessecarily set every bit. ``` npm install sparse-bitfield ``` [![build status](http://img.shields.io/travis/mafintosh/sparse-bitfield.svg?style=flat)](http://travis-ci.org/mafintosh/sparse-bitfield) ## Usage ``` js var bitfield = require('sparse-bitfield') var bits = bitfield() bits.set(0, true) // set first bit bits.set(1, true) // set second bit bits.set(1000000000000, true) // set the 1.000.000.000.000th bit ``` Running the above example will allocate two 1kb buffers internally. Each 1kb buffer can hold information about 8192 bits so the first one will be used to store information about the first two bits and the second will be used to store the 1.000.000.000.000th bit. ## API #### `var bits = bitfield([options])` Create a new bitfield. Options include ``` js { pageSize: 1024, // how big should the partial buffers be buffer: anExistingBitfield, trackUpdates: false // track when pages are being updated in the pager } ``` #### `bits.set(index, value)` Set a bit to true or false. #### `bits.get(index)` Get the value of a bit. #### `bits.pages` A [memory-pager](https://github.com/mafintosh/memory-pager) instance that is managing the underlying memory. If you set `trackUpdates` to true in the constructor you can use `.lastUpdate()` on this instance to get the last updated memory page. #### `var buffer = bits.toBuffer()` Get a single buffer representing the entire bitfield. ## License MIT sparse-bitfield-3.0.3/index.js000066400000000000000000000043771305665443200162620ustar00rootroot00000000000000var pager = require('memory-pager') module.exports = Bitfield function Bitfield (opts) { if (!(this instanceof Bitfield)) return new Bitfield(opts) if (!opts) opts = {} if (Buffer.isBuffer(opts)) opts = {buffer: opts} this.pageOffset = opts.pageOffset || 0 this.pageSize = opts.pageSize || 1024 this.pages = opts.pages || pager(this.pageSize) this.byteLength = this.pages.length * this.pageSize this.length = 8 * this.byteLength if (!powerOfTwo(this.pageSize)) throw new Error('The page size should be a power of two') this._trackUpdates = !!opts.trackUpdates this._pageMask = this.pageSize - 1 if (opts.buffer) { for (var i = 0; i < opts.buffer.length; i += this.pageSize) { this.pages.set(i / this.pageSize, opts.buffer.slice(i, i + this.pageSize)) } this.byteLength = opts.buffer.length this.length = 8 * this.byteLength } } Bitfield.prototype.get = function (i) { var o = i & 7 var j = (i - o) / 8 return !!(this.getByte(j) & (128 >> o)) } Bitfield.prototype.getByte = function (i) { var o = i & this._pageMask var j = (i - o) / this.pageSize var page = this.pages.get(j, true) return page ? page.buffer[o + this.pageOffset] : 0 } Bitfield.prototype.set = function (i, v) { var o = i & 7 var j = (i - o) / 8 var b = this.getByte(j) return this.setByte(j, v ? b | (128 >> o) : b & (255 ^ (128 >> o))) } Bitfield.prototype.toBuffer = function () { var all = alloc(this.pages.length * this.pageSize) for (var i = 0; i < this.pages.length; i++) { var next = this.pages.get(i, true) var allOffset = i * this.pageSize if (next) next.buffer.copy(all, allOffset, this.pageOffset, this.pageOffset + this.pageSize) } return all } Bitfield.prototype.setByte = function (i, b) { var o = i & this._pageMask var j = (i - o) / this.pageSize var page = this.pages.get(j, false) o += this.pageOffset if (page.buffer[o] === b) return false page.buffer[o] = b if (i >= this.byteLength) { this.byteLength = i + 1 this.length = this.byteLength * 8 } if (this._trackUpdates) this.pages.updated(page) return true } function alloc (n) { if (Buffer.alloc) return Buffer.alloc(n) var b = new Buffer(n) b.fill(0) return b } function powerOfTwo (x) { return !(x & (x - 1)) } sparse-bitfield-3.0.3/package.json000066400000000000000000000013341305665443200170710ustar00rootroot00000000000000{ "name": "sparse-bitfield", "version": "3.0.3", "description": "Bitfield that allocates a series of small buffers to support sparse bits without allocating a massive buffer", "main": "index.js", "dependencies": { "memory-pager": "^1.0.2" }, "devDependencies": { "buffer-alloc": "^1.1.0", "standard": "^9.0.0", "tape": "^4.6.3" }, "scripts": { "test": "standard && tape test.js" }, "repository": { "type": "git", "url": "https://github.com/mafintosh/sparse-bitfield.git" }, "author": "Mathias Buus (@mafintosh)", "license": "MIT", "bugs": { "url": "https://github.com/mafintosh/sparse-bitfield/issues" }, "homepage": "https://github.com/mafintosh/sparse-bitfield" } sparse-bitfield-3.0.3/test.js000066400000000000000000000036701305665443200161250ustar00rootroot00000000000000var alloc = require('buffer-alloc') var tape = require('tape') var bitfield = require('./') tape('set and get', function (t) { var bits = bitfield() t.same(bits.get(0), false, 'first bit is false') bits.set(0, true) t.same(bits.get(0), true, 'first bit is true') t.same(bits.get(1), false, 'second bit is false') bits.set(0, false) t.same(bits.get(0), false, 'first bit is reset') t.end() }) tape('set large and get', function (t) { var bits = bitfield() t.same(bits.get(9999999999999), false, 'large bit is false') bits.set(9999999999999, true) t.same(bits.get(9999999999999), true, 'large bit is true') t.same(bits.get(9999999999999 + 1), false, 'large bit + 1 is false') bits.set(9999999999999, false) t.same(bits.get(9999999999999), false, 'large bit is reset') t.end() }) tape('get and set buffer', function (t) { var bits = bitfield({trackUpdates: true}) t.same(bits.pages.get(0, true), undefined) t.same(bits.pages.get(Math.floor(9999999999999 / 8 / 1024), true), undefined) bits.set(9999999999999, true) var bits2 = bitfield() var upd = bits.pages.lastUpdate() bits2.pages.set(Math.floor(upd.offset / 1024), upd.buffer) t.same(bits2.get(9999999999999), true, 'bit is set') t.end() }) tape('toBuffer', function (t) { var bits = bitfield() t.same(bits.toBuffer(), alloc(0)) bits.set(0, true) t.same(bits.toBuffer(), bits.pages.get(0).buffer) bits.set(9000, true) t.same(bits.toBuffer(), Buffer.concat([bits.pages.get(0).buffer, bits.pages.get(1).buffer])) t.end() }) tape('pass in buffer', function (t) { var bits = bitfield() bits.set(0, true) bits.set(9000, true) var clone = bitfield(bits.toBuffer()) t.same(clone.get(0), true) t.same(clone.get(9000), true) t.end() }) tape('set small buffer', function (t) { var buf = alloc(1) buf[0] = 255 var bits = bitfield(buf) t.same(bits.get(0), true) t.same(bits.pages.get(0).buffer.length, bits.pageSize) t.end() })