bitlist

A bit-set/bit-array/bit-vector implementation which allows inserting and removing values

Usage no npm install needed!

<script type="module">
  import bitlist from 'https://cdn.skypack.dev/bitlist';
</script>

README

BitList

An implementation of a bit array (also known as bit vector, bit mask or bit set), which allows the set to grow or shrink as necessary, and allows inserting or removing values from arbitrary positions in the list.

Install

npm install bitlist

Usage

var BitList = require('bitlist');
var list = new BitList();

list.insert(0, true);
list.insert(1, false);
list.insert(0, false);
list.insert(2, true);
// list is now [false, true, false, true]

list.get(0); // false
list.get(1); // true

list.remove(0);
// list is now [true, false, true]

list.size; // 3

// lists can be instantiated with an array of values
var otherList = new BitList([false, true, true]);

// AND otherList onto list
list.applyAnd([otherList]);
// list is now [false, false, true]

API

  • get(index)

    • Get the value at the given index.
  • set(index, value)

    • Update the value at the given index.
  • insert(index, value)

    • Insert a new value into the list at the given position.
  • remove(index)

    • Remove a value from the list.
  • applyAnd(lists)

    • Apply one or more other BitLists to the current one, using AND operations (treats each as a mask).
  • clearRange(lowIndex, highIndex)

    • Set a range of values to false. Indexes are inclusive.
  • clone()

    • Create a new BitList which has the same values as this one.
  • getIndexes(matchValue)

    • Turn the BitList into an array of indexes which have the given value.

        [1,0,1,1,0].getIndexes(false) -> [1, 4]
        [1,0,1,1,0].getIndexes(true)  -> [0, 2, 3]
      
  • countValuesInRange(value, lowIndex, highIndex)

    • Count the number of items in the given range which have the given value. Indexes are inclusive.

License

The MIT License (MIT)

Copyright (c) 2015 SoundCloud

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.