Skip to content

Commit

Permalink
Inline util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aesy committed Jul 2, 2024
1 parent e5079ae commit b0ab7a6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 97 deletions.
36 changes: 14 additions & 22 deletions src/BitArray.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { assertTrue, isInteger } from './util';

/**
* A {@link BitSet} implementation with no limit due to bits being stored in an array. Also known as bit set, bit map
* or bit vector. This implementation will never throw out of bounds errors.
Expand All @@ -24,8 +22,9 @@ class BitArray {
* @throws {Error} In case 'minLength' is equals to or smaller than zero.
*/
constructor(minLength) {
assertTrue(minLength === undefined || minLength > 0,
'Illegal argument: parameter \'minLength\' must be larger than 0');
if (minLength !== undefined && minLength <= 0) {
throw Error('Illegal argument: parameter \'minLength\' must be larger than 0');
}

minLength = minLength || 1;

Expand Down Expand Up @@ -118,15 +117,13 @@ class BitArray {
}

get(index) {
assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer');

return this.value[index] || false;
}

getRange(from, to) {
assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer');
assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer');
assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
if (to <= from) {
throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
}

const length = to - from;
const bitArray = new BitArray();
Expand Down Expand Up @@ -168,7 +165,6 @@ class BitArray {
}

testAt(value, index) {
assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer');

if (index > this.length) {
return Boolean(value) === false;
Expand Down Expand Up @@ -212,8 +208,6 @@ class BitArray {
}

setAt(value, index) {
assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer');

while (index >= this.length) {
this.value.push(false);
}
Expand All @@ -224,9 +218,9 @@ class BitArray {
}

setRange(value, from, to) {
assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer');
assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer');
assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
if (to <= from) {
throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
}

for (let i = from; i < to; i++) {
this.setAt(value, i);
Expand Down Expand Up @@ -258,8 +252,6 @@ class BitArray {
}

flipAt(index) {
assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer');

while (index >= this.length) {
this.value.push(false);
}
Expand All @@ -270,9 +262,9 @@ class BitArray {
}

flipRange(from, to) {
assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer');
assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer');
assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
if (to <= from) {
throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
}

for (let i = from; i < to; i++) {
this.flipAt(i);
Expand Down Expand Up @@ -337,13 +329,13 @@ class BitArray {
* @returns {BitArray} A new instance.
*/
static deserialize(input) {
const array = input.split('');
const array = input.split('').map(Number);

if (array.some(isNaN)) {
throw new Error('Failed to deserialize input');
}

return BitArray.fromArray(array.map(Number));
return BitArray.fromArray(array);
}

clone() {
Expand Down
75 changes: 48 additions & 27 deletions src/BitField.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { assertTrue, isInteger, withinRange } from './util';

/**
* A {@link BitSet} implementation limited to 31 bits due to bits being stored in a Number type.
* This implementation is about 25% faster than a BitArray.
Expand Down Expand Up @@ -37,8 +35,9 @@ class BitField {
* @throws {Error} In case 'minLength' is equals to or smaller than zero.
*/
constructor(minLength) {
assertTrue(minLength === undefined || minLength > 0,
'Illegal argument: parameter \'minLength\' must be larger than 0');
if (minLength !== undefined && minLength <= 0) {
throw Error('Illegal argument: parameter \'minLength\' must be larger than 0');
}

this.minLength = minLength || 1;

Expand Down Expand Up @@ -143,18 +142,25 @@ class BitField {
}

get(index) {
assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer');
assertTrue(withinRange(index, 0, 31), 'Illegal argument: parameter \'index\' is out of bounds');
if (index < 0 || index > 31) {
throw Error('Illegal argument: parameter \'index\' is out of bounds');
}

return Boolean((this.value >> index) & 1);
}

getRange(from, to) {
assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer');
assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer');
assertTrue(withinRange(from, 0, 31), 'Illegal argument: parameter \'from\' is out of bounds');
assertTrue(withinRange(to, 0, 31), 'Illegal argument: parameter \'to\' is out of bounds');
assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
if (from < 0 || from > 31) {
throw Error('Illegal argument: parameter \'from\' is out of bounds');
}

if (to < 0 || to > 31) {
throw Error('Illegal argument: parameter \'to\' is out of bounds');
}

if (to <= from) {
throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
}

const length = to - from;
const mask = (1 << length) - 1;
Expand All @@ -177,8 +183,9 @@ class BitField {
}

testAt(value, index) {
assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer');
assertTrue(withinRange(index, 0, 31), 'Illegal argument: parameter \'index\' is out of bounds');
if (index < 0 || index > 31) {
throw Error('Illegal argument: parameter \'index\' is out of bounds');
}

return this.get(index) === Boolean(value);
}
Expand Down Expand Up @@ -220,20 +227,27 @@ class BitField {
}

setAt(value, index) {
assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer');
assertTrue(withinRange(index, 0, 31), 'Illegal argument: parameter \'index\' is out of bounds');
if (index < 0 || index > 31) {
throw Error('Illegal argument: parameter \'index\' is out of bounds');
}

const mask = 1 << index;

return this.set(value, mask);
}

setRange(value, from, to) {
assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer');
assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer');
assertTrue(withinRange(from, 0, 31), 'Illegal argument: parameter \'from\' is out of bounds');
assertTrue(withinRange(to, 0, 31), 'Illegal argument: parameter \'to\' is out of bounds');
assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
if (from < 0 || from > 31) {
throw Error('Illegal argument: parameter \'from\' is out of bounds');
}

if (to < 0 || to > 31) {
throw Error('Illegal argument: parameter \'to\' is out of bounds');
}

if (to <= from) {
throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
}

let mask = (1 << (to - from)) - 1;

Expand All @@ -257,20 +271,27 @@ class BitField {
}

flipAt(index) {
assertTrue(isInteger(index), 'Illegal argument: parameter \'index\' is not an integer');
assertTrue(withinRange(index, 0, 31), 'Illegal argument: parameter \'index\' is out of bounds');
if (index < 0 || index > 31) {
throw Error('Illegal argument: parameter \'index\' is out of bounds');
}

const mask = 1 << index;

return this.flip(mask);
}

flipRange(from, to) {
assertTrue(isInteger(from), 'Illegal argument: parameter \'from\' is not an integer');
assertTrue(isInteger(to), 'Illegal argument: parameter \'to\' is not an integer');
assertTrue(withinRange(from, 0, 31), 'Illegal argument: parameter \'from\' is out of bounds');
assertTrue(withinRange(to, 0, 31), 'Illegal argument: parameter \'to\' is out of bounds');
assertTrue(to > from, 'Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
if (from < 0 || from > 31) {
throw Error('Illegal argument: parameter \'from\' is out of bounds');
}

if (to < 0 || to > 31) {
throw Error('Illegal argument: parameter \'to\' is out of bounds');
}

if (to <= from) {
throw Error('Illegal argument: parameter \'to\' must be larger than parameter \'from\'');
}

let mask = (1 << (to - from)) - 1;

Expand Down
1 change: 1 addition & 0 deletions src/BitFlags.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BitArray from './BitArray';
import BitField from './BitField';
import EnumConstant from './EnumConstant';
import EnumLike from './EnumLike';

/**
* A BitFlags instance consists of a set of predefined values, each representing a binary digit that acts as a flag
Expand Down
1 change: 1 addition & 0 deletions src/Enum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EnumConstant from './EnumConstant';
import EnumLike from './EnumLike';

/**
* An Enum consists of a set of predefined constants. These constants are accessible directly on the instance.
Expand Down
2 changes: 2 additions & 0 deletions src/EnumLike.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

/**
* The amount of values/constants in this instance.
*
* @readonly
* @member {Number}
* @name EnumLike#length
Expand Down
48 changes: 0 additions & 48 deletions src/util.js

This file was deleted.

0 comments on commit b0ab7a6

Please sign in to comment.