1.1.0 • Published 11 months ago

dyn-bitfield v1.1.0

Weekly downloads
-
License
GPL3
Repository
-
Last release
11 months ago

# Simple Bitfields class

Bitfields are efficient for representing a set of small positive integers. They are functionally equivalent to Set<unsigned_integer> or Array<boolean>

This specific implementation puts no theoretical limit on the integer (beyond memory)

import { BitField } from 'dyn-bitfield'

const field = new BitField // {}

field.set(10) // {10}
field.set(20) // {10, 20}
console.assert(field.has(10) && field.has(20))

field.unset(10) // {20}
console.assert(!field.has(10))

const field2 = new BitField
field2.set(5) // {5}

field.xor(field2) // {5, 20}
console.assert(field.has(5))

field.xor(field2) // {20}
console.assert(!field.has(5))

field.set(30) // {20,30}
// Getting items in order
console.assert(field.popFirst() == 20)
console.assert(field.popFirst() == 30)

// Serialize it
const json = JSON.stringify(BitField)
console.log(json) // "[1048576]"
const parsedField = BitField.from(JSON.parse(json))
console.assert(parsedField.has(20))

Available methods

//
interface BitField{
	// Functions for initalizing & clearing BitFields
		// Create a new bitfield with no bits set
		constructor(): BitField

		// Create a new bitfield with the specified positions set
		static of(...n: number[])

		// Mutates a JSONified arr into a BitField
		static parse(arr: any[]): BitField

		// Copies a JSONified arr into a new BitField
		static fromCopy(arr: any[]): BitField

		// Remove all bits
		clear(): void

	// Functions for directly accessing a BitField
		// Set the bit at a position
		set(pos: number): void

		// Unset the bit at a position
		unset(pos: number): void

		// Toggle a bit from on to off and vice versa
		toggle(pos: number): void

		// Returns if the bit at that position was set
		has(pos: number): boolean

		// Unsets the bit if it was set and returns if it was changed (i.e was it originally set?)
		pop(pos: number): boolean

		// Sets the bit if it wasn't and returns if it was changed (i.e was it originally unset?)
		put(pos: number): boolean
	
	// Functions for large binary operations on the whole BitField
		// Flips all of the bits on this BitField that are set in other BitField
		// Equivalent to: this ^= other
		xor(other: BitField): void

		// Only keep bits on this BitField that are set in other BitField
		// Equivalent to: this &= other
		and(other: BitField): void

		// Sets all the bits on this BitField that are set in other BitField
		// Equivalent to: this |= other
		or(other: BitField): void
	
	// Functions for finding first / last set bits
		// Find the first (lowest) position that is unset
		firstUnset(): number
		// Find the first (lowest) position that is set
		firstSet(): number
		// Find the last (highest) position that is set
		lastSet(): number
		// Find, unset and return the first (lowest) position that is set
		popFirst(): number
		// Find, unset and return the last (highest) position that is set
		popLast(): number
}
1.1.0

11 months ago

1.0.1

1 year ago

1.0.0

1 year ago