Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bitset

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitset - npm Package Compare versions

Comparing version 0.3.0 to 4.1.0

.travis.yml

64

package.json
{
"author": "Tom de Grunt <tom@degrunt.nl>",
"name": "bitset",
"description": "BitSet implementation for JavaScript",
"keywords": ["bit", "bitset", "metrics"],
"version": "0.3.0",
"repository": {
"type": "git",
"url": "https://github.com/tdegrunt/bitset"
},
"main": "./lib/bitset.js",
"engines": {
"node": "~0.6.2"
},
"scripts": {
"test": "cake test"
},
"directories": {
"lib": "./lib"
},
"files": ["README.md", "./lib", "./src", "./support", "./test"],
"dependencies": {},
"devDependencies": {
"should": "0.3",
"mocha": "0.1",
"coffee-script": "1.1"
}
"name": "bitset",
"title": "BitSet",
"version": "4.1.0",
"homepage": "http://www.xarg.org/2014/03/javascript-bit-array/",
"description": "A performance optimized infinite bit vector library",
"keywords": [
"bit",
"set",
"twiddle",
"bit array",
"binary",
"bit vector",
"bitset",
"bitmap",
"bitstring",
"bitwise"
],
"author": "Robert Eisele <robert@xarg.org> (http://www.xarg.org/)",
"main": "bitset",
"private": false,
"readmeFilename": "README.md",
"license": "MIT OR GPL-2.0",
"repository": {
"type": "git",
"url": "http://github.com/infusion/bitset.js.git"
},
"engines": {
"node": "*"
},
"scripts": {
"test": "mocha tests/*.js"
},
"devDependencies": {
"mocha": "*",
"should": "*",
"google-closure-compiler": "*",
"gulp": "*"
}
}

@@ -1,71 +0,251 @@

[![Build Status](https://secure.travis-ci.org/tdegrunt/bitset.png)](http://travis-ci.org/tdegrunt/bitset)
# BitSet.js
BitSet
======
This module implements an array of bits, that grows as needed. It can be used from both JavaScript:
[![NPM Package](https://nodei.co/npm-dl/bitset.png?months=6&height=1)](https://npmjs.org/package/bitset)
var bs = new BitSet();
[![Build Status](https://travis-ci.org/infusion/BitSet.js.svg)](https://travis-ci.org/infusion/BitSet.js)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
for(var n = 0; n < 12; n++) {
bs.set(n);
}
BitSet.js is a infinite [Bit-Array](http://en.wikipedia.org/wiki/Bit_array) implementation in JavaScript. That means that if you invert a bit vector, the leading ones get remembered. As far as I can tell, BitSet.js is the only library which has this feature. It is also heavily benchmarked against other implementations and is the fastest implementation to date.
console.dir(bs);
*NOTE:* As of version v4.0.0, BitSet.js is mutable, which means that the object gets changed by method invocations. In order to work on explicit copies, use `clone()`.
as well as CoffeeScript:
Examples
===
bs = new BitSet
Basic usage
---
```javascript
var bs = new BitSet;
bs.set(128, 1); // Set bit at position 128
console.log(bs.toString(16)); // Print out a hex dump with one bit set
```
bs.set 1
bs.clear 1
Flipping bits
---
```javascript
var bs = new BitSet;
bs
.flip(0, 62)
.flip(29, 35);
console.log bs.toString()
Usage
-----
BitSets are very useful for realtime metrics. If you want to count the number of active users (CoffeeScript):
var str = bs.toString();
# Somewhere in application code
todaysUserCount = new Bitset
# The User class
class User
@login: (userName, password) ->
todaysUserCount.set @userId
# In reporting code
todaysUserCount.cardinality()
Documentation
-------------
if (str === "111111111111111111111111111000000011111111111111111111111111111") {
console.log("YES!");
}
```
* `BitSet#set(pos)` - sets the bit on position pos to true
* `BitSet#get(pos)` - returns whether the bit on position pos is set
* `BitSet#clear(pos)` - clears the bit on position pos
* `BitSet#length` - returns the logical length of the bitset
* `BitSet#wordLength` - returns the word-length of the bitset
* `BitSet#cardinality` - returns how many bits are set to true in the bitset
* `BitSet#toString` - returns a string representation of the bitset
* `BitSet#toBinaryString` - returns a binary string representation of the bitset
* `BitSet#or(bitset)` - OR's this bitset with the argument bitset
* `BitSet#and(bitset)` - AND's this bitset with the argument bitset
* `BitSet#andNot(bitset)` - ANDNOT's this bitset with the argument bitset
* `BitSet#xor(bitset)` - XOR's this bitset with the argument bitset
Range Set
---
```javascript
var bs = new BitSet;
bs.setRange(10, 18, 1); // Set a 1 between 10 and 18, inclusive
```
For details see src/bitset.coffee
User permissions
---
If you want to store user permissions in your database and use BitSet for the bit twiddling, you can start with the following Linux-style snippet:
```javascript
var P_READ = 2; // Bit pos
var P_WRITE = 1;
var P_EXEC = 0;
var user = new BitSet;
user.set(P_READ); // Give read perms
user.set(P_WRITE); // Give write perms
var group = new BitSet(P_READ);
var world = new BitSet(P_EXEC);
console.log("0" + user.toString(8) + group.toString(8) + world.toString(8));
```
Installation
------------
===
```
npm install bitset
```
or
```
bower install bitset.js
```
Performance
-----------
It performs pretty well, you can try the performance.js in the support folder. This is the output with 100.000.000 bits:
Using BitSet.js with the browser
===
```html
<script src="bitset.js"></script>
<script>
console.log(BitSet("111"));
</script>
```
set 100000000 bits: 3173ms
cardinality 100000000
cardinality: 949ms
Using BitSet.js with require.js
===
```html
<script src="require.js"></script>
<script>
requirejs(['bitset.js'],
function(BitSet) {
console.log(BitSet("1111"));
});
</script>
```
Tests
-----
Testing is done via the excellent [mocha](http://visionmedia.github.com/mocha) (cake test)
Parser
===
The parser accepts the following types of values in either function
Strings
- Binary strings "010101"
- Binary strings with prefix "0b100101"
- Hexadecimal strings with prefix "0xaffe"
Arrays
- The values of the array are the indizes to be set to 1
Uint8Array
- A binary representation in 8 bit form
Number
- A binary value
BitSet
- A BitSet object, which get copied over
Functions
===
The data type Mixed can be either a BitSet object, a String or an integer representing a native bitset with 31 bits.
BitSet set(ndx[, value=0)
---
Sets value 0 or 1 to index `ndx` of the bitset
int get(int ndx)
---
Gets the value at index ndx
BitSet setRange(from, to[, value=1])
---
Helper function for set, to set an entire range to a given value
BitSet clear([from[, to])
---
Sets a portion of a given bitset to zero
- If no param is given, the whole bitset gets cleared
- If one param is given, the bit at this index gets cleared
- If two params are given, the range is cleared
BitSet slice([from[, to])
---
Extracts a portion of a given bitset as a new bitset
- If no param is given, the bitset is getting cloned
- If one param is given, the index is used as offset
- If two params are given, the range is returned as new BitSet
BitSet flip([from[, to])
---
Toggles a portion of a given bitset
- If no param is given, the bitset is inverted
- If one param is given, the bit at the index is toggled
- If two params are given, the bits in the given range are toggled
BitSet not()
---
Calculates the bitwise not
BitSet and(Mixed x)
---
Calculates the bitwise and between two bitsets
BitSet or(Mixed x)
---
Calculates the bitwise or between two bitsets
BitSet xor(Mixed x)
---
Calculates the bitwise xor between two bitsets
BitSet andNot(Mixed x)
---
Calculates the bitwise andNot between two bitsets (this is not the nand operation!)
BitSet clone()
---
Clones the actual object
Array toArray()
---
Returns an array with all indexes set in the bitset
String toString([base=2])
---
Returns a string representation with respect to the base
int cardinality()
---
Calculates the number of bits set
int msb()
---
Calculates the most significant bit (the left most)
int ntz()
---
Calculates the number of trailing zeros (zeros on the right). If all digits are zero, `Infinity` is returned, since BitSet.js is an arbitrary large bit vector implementation.
int lsb()
---
Calculates the least significant bit (the right most)
bool isEmpty()
---
Checks if the bitset has all bits set to zero
bool equals()
---
Checks if two bitsets are the same
BitSet.fromBinaryString(str)
---
Alternative constructor to pass with a binary string
BitSet.fromHexString(str)
---
Alternative constructor to pass a hex string
BitSet.Random(n=32)
---
Create a random BitSet with a maximum length of n bits
Coding Style
===
As every library I publish, BitSet.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.
Build the library
===
Gulp is optional for minifying with Google Closure Compiler. After cloning the Git repository, do:
```
npm install
gulp
```
Run a test
===
Testing the source against the shipped test suite is as easy as
```
npm test
```
Copyright and licensing
===
Copyright (c) 2014-2018, [Robert Eisele](https://www.xarg.org/)
Dual licensed under the MIT or GPL Version 2 licenses.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc