structurae
Advanced tools
Comparing version 0.0.23 to 0.0.24
@@ -33,2 +33,3 @@ // Type definitions for structurae | ||
set(row: number, column: number, value: any): this; | ||
setArray(array: Collection, offset: number): void; | ||
getCoordinates(index: number): Coordinates; | ||
@@ -216,4 +217,5 @@ toArrays(withPadding?: boolean): any[][]; | ||
constructor(options: BinaryGridOptions, ...args: any); | ||
getBit(row: number, column: number): Bit; | ||
setBit(row: number, column: number, value?: Bit): this; | ||
get(row: number, column: number): Bit; | ||
set(row: number, column: number, value?: Bit): this; | ||
setArray(array: Collection, offset: number): void; | ||
getRow(row: number): Bit[]; | ||
@@ -228,2 +230,3 @@ getColumn(column: number): Bit[]; | ||
rows: number; | ||
columns: number; | ||
pad: any; | ||
@@ -235,2 +238,3 @@ lastCoordinates: Coordinates; | ||
set(row: number, column: number, value: any): this; | ||
setArray(array: Collection, offset: number): void; | ||
getCoordinates(index: number): Coordinates; | ||
@@ -237,0 +241,0 @@ toArrays(withPadding?: boolean): any[][]; |
@@ -29,5 +29,5 @@ /** | ||
Object.defineProperties(this, { | ||
offset: { value: offset, writable: true }, | ||
columns: { value: columns, writable: true }, | ||
rows: { value: rows, writable: true }, | ||
offset: { value: offset }, | ||
columns: { value: columns }, | ||
rows: { value: rows }, | ||
lastPosition: { value: Object.seal({ bucket: 0, position: 0 }) }, | ||
@@ -44,3 +44,3 @@ }); | ||
*/ | ||
getBit(row, column) { | ||
get(row, column) { | ||
const { bucket, position } = this.getBitPosition(row, column); | ||
@@ -58,3 +58,3 @@ return (this[bucket] >> position) & 1; | ||
*/ | ||
setBit(row, column, value = 1) { | ||
set(row, column, value = 1) { | ||
const { bucket, position } = this.getBitPosition(row, column); | ||
@@ -66,2 +66,13 @@ this[bucket] = (this[bucket] & ~(1 << position)) | (value << position); | ||
/** | ||
* A proxy to Uint16Array#set method. | ||
* | ||
* @param {Collection} array | ||
* @param {number} [offset] | ||
* @returns {void} | ||
*/ | ||
setArray(array, offset) { | ||
super.set(array, offset); | ||
} | ||
/** | ||
* @private | ||
@@ -118,8 +129,6 @@ * @param {number} row | ||
/** | ||
* @private | ||
* @param {number} columns | ||
* @returns {number} | ||
* @type {Uint16ArrayConstructor} | ||
*/ | ||
static getOffset(columns) { | ||
return Math.ceil(Math.log2(columns)); | ||
static get [Symbol.species]() { | ||
return Uint16Array; | ||
} | ||
@@ -139,6 +148,8 @@ | ||
/** | ||
* @type {Uint16ArrayConstructor} | ||
* @private | ||
* @param {number} columns | ||
* @returns {number} | ||
*/ | ||
static get [Symbol.species]() { | ||
return Uint16Array; | ||
static getOffset(columns) { | ||
return Math.ceil(Math.log2(columns)); | ||
} | ||
@@ -145,0 +156,0 @@ } |
@@ -168,2 +168,21 @@ /** | ||
/** | ||
* Implements in-place replacement of the grid elements if it's based on Array. | ||
* Proxies to TypedArray#set if the grid is based on a TypedArray. | ||
* | ||
* @param {Collection} array | ||
* @param {number} [offset] | ||
* @returns {void} | ||
*/ | ||
setArray(array, offset) { | ||
if (super.set) { | ||
super.set(array, offset); | ||
} else { | ||
this.length = array.length; | ||
for (let i = 0; i < array.length; i++) { | ||
this[i] = array[i]; | ||
} | ||
} | ||
} | ||
/** | ||
* Gets coordinates of an element at specified index. | ||
@@ -227,19 +246,19 @@ * | ||
/** | ||
* @private | ||
* Returns the length of underlying Array required to hold the grid. | ||
* | ||
* @param {number} rows | ||
* @param {number} columns | ||
* @returns {number} | ||
*/ | ||
static getOffset(columns) { | ||
return Math.ceil(Math.log2(columns)); | ||
static getLength(rows, columns) { | ||
return rows << this.getOffset(columns); | ||
} | ||
/** | ||
* Returns the length of underlying Array required to hold the grid. | ||
* | ||
* @param {number} rows | ||
* @private | ||
* @param {number} columns | ||
* @returns {number} | ||
*/ | ||
static getLength(rows, columns) { | ||
return rows << this.getOffset(columns); | ||
static getOffset(columns) { | ||
return Math.ceil(Math.log2(columns)); | ||
} | ||
@@ -246,0 +265,0 @@ |
@@ -49,2 +49,4 @@ /** | ||
pad: { value: pad, writable: true }, | ||
columns: { value: rows }, | ||
rows: { value: rows }, | ||
lastCoordinates: { value: Object.seal({ row: 0, column: 0 }) }, | ||
@@ -54,26 +56,3 @@ }); | ||
get rows() { | ||
return (Math.sqrt((this.length << 3) + 1) - 1) >> 1; | ||
} | ||
/** | ||
* Returns an array index of an element at given coordinates. | ||
* | ||
* @param {number} row | ||
* @param {number} column | ||
* @returns {*} | ||
* @example | ||
* | ||
* const a = SymmetricGrid({ rows: 3 }); | ||
* a.get(1, 0); | ||
* //=> 1 | ||
* a.get(0, 1); | ||
* //=> 1 | ||
*/ | ||
static getIndex(row, column) { | ||
const [x, y] = row >= column ? [column, row] : [row, column]; | ||
return x + (((y + 1) * y) >> 1); | ||
} | ||
/** | ||
* Returns an element from given coordinates. | ||
@@ -114,2 +93,21 @@ * | ||
/** | ||
* Implements in-place replacement of the grid elements if it's based on Array. | ||
* Proxies to TypedArray#set if the grid is based on a TypedArray. | ||
* | ||
* @param {Collection} array | ||
* @param {number} [offset] | ||
* @returns {void} | ||
*/ | ||
setArray(array, offset) { | ||
if (super.set) { | ||
super.set(array, offset); | ||
} else { | ||
this.length = array.length; | ||
for (let i = 0; i < array.length; i++) { | ||
this[i] = array[i]; | ||
} | ||
} | ||
} | ||
/** | ||
* Gets coordinates of an element at specified index. | ||
@@ -166,2 +164,31 @@ * | ||
/** | ||
* Returns an array index of an element at given coordinates. | ||
* | ||
* @param {number} row | ||
* @param {number} column | ||
* @returns {*} | ||
* @example | ||
* | ||
* const a = SymmetricGrid({ rows: 3 }); | ||
* a.get(1, 0); | ||
* //=> 1 | ||
* a.get(0, 1); | ||
* //=> 1 | ||
*/ | ||
static getIndex(row, column) { | ||
const [x, y] = row >= column ? [column, row] : [row, column]; | ||
return x + (((y + 1) * y) >> 1); | ||
} | ||
/** | ||
* Returns the length of underlying Array required to hold the grid. | ||
* | ||
* @param {number} rows | ||
* @returns {number} | ||
*/ | ||
static getLength(rows) { | ||
return ((rows + 1) * rows) >> 1; | ||
} | ||
/** | ||
* Creates a grid from an array of arrays. | ||
@@ -192,12 +219,2 @@ * | ||
} | ||
/** | ||
* Returns the length of underlying Array required to hold the grid. | ||
* | ||
* @param {number} rows | ||
* @returns {number} | ||
*/ | ||
static getLength(rows) { | ||
return ((rows + 1) * rows) >> 1; | ||
} | ||
} | ||
@@ -204,0 +221,0 @@ |
@@ -34,4 +34,4 @@ const BinaryGrid = require('./binary-grid'); | ||
addEdge(x, y) { | ||
this.setBit(x, y); | ||
if (!this.directed) this.setBit(y, x); | ||
this.set(x, y); | ||
if (!this.directed) this.set(y, x); | ||
return this; | ||
@@ -48,4 +48,4 @@ } | ||
removeEdge(x, y) { | ||
this.setBit(x, y, 0); | ||
if (!this.directed) this.setBit(y, x, 0); | ||
this.set(x, y, 0); | ||
if (!this.directed) this.set(y, x, 0); | ||
return this; | ||
@@ -62,3 +62,3 @@ } | ||
hasEdge(x, y) { | ||
return !!this.getBit(x, y); | ||
return !!this.get(x, y); | ||
} | ||
@@ -103,3 +103,3 @@ | ||
isGray(x) { | ||
return !!this.colors.getBit(0, x); | ||
return !!this.colors.get(0, x); | ||
} | ||
@@ -114,3 +114,3 @@ | ||
setGray(x) { | ||
this.colors.setBit(0, x); | ||
this.colors.set(0, x); | ||
return this; | ||
@@ -126,3 +126,3 @@ } | ||
isBlack(x) { | ||
return !!this.colors.getBit(1, x); | ||
return !!this.colors.get(1, x); | ||
} | ||
@@ -137,3 +137,3 @@ | ||
setBlack(x) { | ||
this.colors.setBit(1, x); | ||
this.colors.set(1, x); | ||
return this; | ||
@@ -140,0 +140,0 @@ } |
@@ -123,3 +123,3 @@ const BinaryGrid = require('./binary-grid'); | ||
isGray(x) { | ||
return !!this.colors.getBit(0, x); | ||
return !!this.colors.get(0, x); | ||
} | ||
@@ -134,3 +134,3 @@ | ||
setGray(x) { | ||
this.colors.setBit(0, x); | ||
this.colors.set(0, x); | ||
return this; | ||
@@ -146,3 +146,3 @@ } | ||
isBlack(x) { | ||
return !!this.colors.getBit(1, x); | ||
return !!this.colors.get(1, x); | ||
} | ||
@@ -157,3 +157,3 @@ | ||
setBlack(x) { | ||
this.colors.setBit(1, x); | ||
this.colors.set(1, x); | ||
return this; | ||
@@ -160,0 +160,0 @@ } |
{ | ||
"name": "structurae", | ||
"version": "0.0.23", | ||
"version": "0.0.24", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -284,8 +284,8 @@ # Structurae | ||
const bitGrid = new BinaryGrid({ rows: 2, columns: 8 }); | ||
bitGrid.setBit(0, 0).setBit(0, 2).setBit(0, 5); | ||
bitGrid.getBit(0, 0); | ||
bitGrid.set(0, 0).set(0, 2).set(0, 5); | ||
bitGrid.get(0, 0); | ||
//=> 1 | ||
bitGrid.getBit(0, 1); | ||
bitGrid.get(0, 1); | ||
//=> 0 | ||
bitGrid.getBit(0, 2); | ||
bitGrid.get(0, 2); | ||
//=> 1 | ||
@@ -292,0 +292,0 @@ bitGrid.getRow(0); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
126564
3484