mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-19 10:58:10 +08:00
Merge pull request #178 from chanzuckerberg/bkmartinjr/crlf-cleanup
clean up CRLF
This commit is contained in:
@@ -1,31 +1,31 @@
|
||||
// jshint esversion: 6
|
||||
import { scaleRGB } from "./scaleRGB";
|
||||
|
||||
// maintain a cache of already parsed RGB names, as it is reasonably expensive
|
||||
// to do this operation. This lets us have speed, but keep the pleasant ability
|
||||
// to talk about colors by their text description eg, 'rgb(0,0,1)'
|
||||
//
|
||||
const colorCache = new Object(null); // no prototype
|
||||
|
||||
function parseColorName(c) {
|
||||
if (c[0] !== "#") {
|
||||
const _c = c.replace(/[^\d,.]/g, "").split(",");
|
||||
return [scaleRGB(+_c[0]), scaleRGB(+_c[1]), scaleRGB(+_c[2])];
|
||||
} else {
|
||||
var parsedHex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(c);
|
||||
return [
|
||||
scaleRGB(parseInt(parsedHex[1], 16)),
|
||||
scaleRGB(parseInt(parsedHex[2], 16)),
|
||||
scaleRGB(parseInt(parsedHex[3], 16))
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export const parseRGB = c => {
|
||||
var cv = colorCache[c];
|
||||
if (!cv) {
|
||||
cv = parseColorName(c);
|
||||
colorCache[c] = cv;
|
||||
}
|
||||
return cv;
|
||||
};
|
||||
// jshint esversion: 6
|
||||
import { scaleRGB } from "./scaleRGB";
|
||||
|
||||
// maintain a cache of already parsed RGB names, as it is reasonably expensive
|
||||
// to do this operation. This lets us have speed, but keep the pleasant ability
|
||||
// to talk about colors by their text description eg, 'rgb(0,0,1)'
|
||||
//
|
||||
const colorCache = new Object(null); // no prototype
|
||||
|
||||
function parseColorName(c) {
|
||||
if (c[0] !== "#") {
|
||||
const _c = c.replace(/[^\d,.]/g, "").split(",");
|
||||
return [scaleRGB(+_c[0]), scaleRGB(+_c[1]), scaleRGB(+_c[2])];
|
||||
} else {
|
||||
var parsedHex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(c);
|
||||
return [
|
||||
scaleRGB(parseInt(parsedHex[1], 16)),
|
||||
scaleRGB(parseInt(parsedHex[2], 16)),
|
||||
scaleRGB(parseInt(parsedHex[3], 16))
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export const parseRGB = c => {
|
||||
var cv = colorCache[c];
|
||||
if (!cv) {
|
||||
cv = parseColorName(c);
|
||||
colorCache[c] = cv;
|
||||
}
|
||||
return cv;
|
||||
};
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
// jshint esversion: 6
|
||||
|
||||
// Substitute for a d3 linear scale - less flexible, more performant.
|
||||
// Returns a function which will scale a value.
|
||||
//
|
||||
// Example will scale [0,1] to [-1,1]
|
||||
// var myScale = scaleLinear([0, 1], [-1, 1]);
|
||||
// myScale(0) === -1
|
||||
// this is is equivalent to d3.scaleLinear().domain([0,1]).range([-1,1])
|
||||
|
||||
export const scaleLinear = (domain, range) => {
|
||||
const domainStart = domain[0];
|
||||
const scale = (range[1] - range[0]) / (domain[1] - domain[0]);
|
||||
const rangeStart = range[0];
|
||||
return function(value) {
|
||||
return (value - domainStart) * scale + rangeStart;
|
||||
};
|
||||
};
|
||||
// jshint esversion: 6
|
||||
|
||||
// Substitute for a d3 linear scale - less flexible, more performant.
|
||||
// Returns a function which will scale a value.
|
||||
//
|
||||
// Example will scale [0,1] to [-1,1]
|
||||
// var myScale = scaleLinear([0, 1], [-1, 1]);
|
||||
// myScale(0) === -1
|
||||
// this is is equivalent to d3.scaleLinear().domain([0,1]).range([-1,1])
|
||||
|
||||
export const scaleLinear = (domain, range) => {
|
||||
const domainStart = domain[0];
|
||||
const scale = (range[1] - range[0]) / (domain[1] - domain[0]);
|
||||
const rangeStart = range[0];
|
||||
return function(value) {
|
||||
return (value - domainStart) * scale + rangeStart;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
// jshint esversion: 6
|
||||
|
||||
// In the case where the REST server does not implement data schema
|
||||
// declaration, we attempt to deduce it by sniffing the data.
|
||||
//
|
||||
export function createSchemaByDataSniffing(ranges) {
|
||||
let schema = {};
|
||||
_.forEach(ranges, (value, key) => {
|
||||
schema[key] = {
|
||||
displayname: key,
|
||||
variabletype: value.options ? "categorical" : "continuous"
|
||||
};
|
||||
|
||||
// Metadata field type is inferred by sniffing the data. This has some risks.
|
||||
// Caveats:
|
||||
// * Values have been converted to native JS objects by the JSON parser.
|
||||
// * Lots of assumptions about he REST API behaving properly (eg, min/max
|
||||
// are the same type, etc).
|
||||
let type;
|
||||
if (schema[key].variabletype === "continuous" && value.range) {
|
||||
// Use min/max as a proxy for all data.
|
||||
const min = value.range.min;
|
||||
const max = value.range.max;
|
||||
type =
|
||||
typeof min !== "number" || typeof max !== "number"
|
||||
? "string"
|
||||
: Number.isSafeInteger(min) && Number.isSafeInteger(max)
|
||||
? "int"
|
||||
: "float";
|
||||
} else {
|
||||
// use an option value as a proxy for all data
|
||||
const aVal = value.options[0];
|
||||
type =
|
||||
typeof aVal !== "number"
|
||||
? "string"
|
||||
: Number.isSafeInteger(aVal) ? "int" : "float";
|
||||
}
|
||||
schema[key].type = type;
|
||||
});
|
||||
return schema;
|
||||
}
|
||||
// jshint esversion: 6
|
||||
|
||||
// In the case where the REST server does not implement data schema
|
||||
// declaration, we attempt to deduce it by sniffing the data.
|
||||
//
|
||||
export function createSchemaByDataSniffing(ranges) {
|
||||
let schema = {};
|
||||
_.forEach(ranges, (value, key) => {
|
||||
schema[key] = {
|
||||
displayname: key,
|
||||
variabletype: value.options ? "categorical" : "continuous"
|
||||
};
|
||||
|
||||
// Metadata field type is inferred by sniffing the data. This has some risks.
|
||||
// Caveats:
|
||||
// * Values have been converted to native JS objects by the JSON parser.
|
||||
// * Lots of assumptions about he REST API behaving properly (eg, min/max
|
||||
// are the same type, etc).
|
||||
let type;
|
||||
if (schema[key].variabletype === "continuous" && value.range) {
|
||||
// Use min/max as a proxy for all data.
|
||||
const min = value.range.min;
|
||||
const max = value.range.max;
|
||||
type =
|
||||
typeof min !== "number" || typeof max !== "number"
|
||||
? "string"
|
||||
: Number.isSafeInteger(min) && Number.isSafeInteger(max)
|
||||
? "int"
|
||||
: "float";
|
||||
} else {
|
||||
// use an option value as a proxy for all data
|
||||
const aVal = value.options[0];
|
||||
type =
|
||||
typeof aVal !== "number"
|
||||
? "string"
|
||||
: Number.isSafeInteger(aVal) ? "int" : "float";
|
||||
}
|
||||
schema[key].type = type;
|
||||
});
|
||||
return schema;
|
||||
}
|
||||
|
||||
@@ -1,254 +1,254 @@
|
||||
"use strict";
|
||||
// jshint esversion: 6
|
||||
|
||||
// BitArray is a 2D bitarray with size [length, nBitWidth].
|
||||
// Each bit is referred to as a `dimension`. Dimensions may be
|
||||
// dynamically allocated and deallocated. The overall length
|
||||
// of the BitArray is fixed at creation time (for simplicity).
|
||||
//
|
||||
// Organization of the bitarray is dimension-major. As dimensions
|
||||
// are added, the underlying store is grown 32 bits at a time.
|
||||
// NOTE: currently does not deallocate / shrink.
|
||||
//
|
||||
// Primary operations on the BitArray are:
|
||||
// - set & clear dimension
|
||||
// - test dimension
|
||||
// - various performance or convenience operations to optimize bulk ops
|
||||
//
|
||||
// The underlying data structure uses TypedArrays for performance.
|
||||
//
|
||||
class BitArray {
|
||||
constructor(length) {
|
||||
// Initially allocate a 32 bit wide array. allocDimension() will expand
|
||||
// as necessary.
|
||||
//
|
||||
// Int32Array is (counterintuitively) used to accomadate JS numeric casting
|
||||
// (to/from primitive number type).
|
||||
//
|
||||
|
||||
// Fixed for the life of this object.
|
||||
this.length = length;
|
||||
|
||||
// Bitarray width. width is always greater than 32*dimensionCount.
|
||||
this.width = 1; // underlying number of 32 bit arrays
|
||||
this.dimensionCount = 0; // num allocated dimensions
|
||||
|
||||
this.bitmask = new Int32Array(this.width); // dimension allocation mask
|
||||
this.bitarray = new Int32Array(this.width * this.length);
|
||||
}
|
||||
|
||||
// Return the number of records that are selected, ie, have a one bit in
|
||||
// all allocated dimensions.
|
||||
//
|
||||
get selectionCount() {
|
||||
return this.countAllOnes();
|
||||
}
|
||||
|
||||
// Count all records that have a 'one' bit in allocated dimensions.
|
||||
//
|
||||
countAllOnes() {
|
||||
let count = 0;
|
||||
for (let i = 0; i < this.width; i++) {
|
||||
const bitmask = this.bitmask[i];
|
||||
for (let j = i * this.length, len = j + this.length; j < len; j++) {
|
||||
if (this.bitarray[i * this.length + j] === bitmask) count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// count trailing zeros - hard to do fast in JS!
|
||||
// https://en.wikipedia.org/wiki/Find_first_set#CTZ
|
||||
static ctz(v) {
|
||||
let c = 32;
|
||||
v &= -v; // isolate lowest non-zero bit
|
||||
if (v) c--;
|
||||
if (v & 0x0000ffff) c -= 16;
|
||||
if (v & 0x00ff00ff) c -= 8;
|
||||
if (v & 0x0f0f0f0f) c -= 4;
|
||||
if (v & 0x33333333) c -= 2;
|
||||
if (v & 0x55555555) c -= 1;
|
||||
return c;
|
||||
}
|
||||
|
||||
// find a free dimension. Return undefined if none
|
||||
_findFreeDimension() {
|
||||
let dim;
|
||||
for (let col = 0; col < this.width; col++) {
|
||||
const bitmask = this.bitmask[col];
|
||||
const lowestZeroBit = ~this.bitmask[col] & -~this.bitmask[col];
|
||||
if (lowestZeroBit) {
|
||||
this.bitmask[col] |= lowestZeroBit;
|
||||
dim = 32 * col + BitArray.ctz(lowestZeroBit);
|
||||
}
|
||||
}
|
||||
return dim;
|
||||
}
|
||||
|
||||
// allocate and return the dimension ID (bit position)
|
||||
//
|
||||
allocDimension() {
|
||||
let dim = this._findFreeDimension();
|
||||
|
||||
// if we did not find free dimension, expand the bitarray.
|
||||
if (dim === undefined) {
|
||||
this.width++;
|
||||
|
||||
const biggerBitArray = new Int32Array(this.width * this.length);
|
||||
biggerBitArray.set(this.bitarray);
|
||||
this.bitarray = biggerBitArray;
|
||||
|
||||
const biggerBitmask = new Int32Array(this.width);
|
||||
biggerBitmask.set(this.bitmask);
|
||||
this.bitmask = biggerBitmask;
|
||||
|
||||
dim = this._findFreeDimension();
|
||||
}
|
||||
|
||||
this.dimensionCount++;
|
||||
return dim;
|
||||
}
|
||||
|
||||
// free a dimension for later use. MUST deselect the dimension, as other
|
||||
// code assume the column will be zero valued.
|
||||
//
|
||||
freeDimension(dim) {
|
||||
// all selection tests assume unallocated dimensions are zero valued.
|
||||
this.deselectAll(dim);
|
||||
const col = dim >>> 5;
|
||||
this.bitmask[col] &= ~(1 << dim % 32);
|
||||
this.dimensionCount--;
|
||||
}
|
||||
|
||||
// return true if this index is selected in ALL dimensions.
|
||||
//
|
||||
isSelected(index) {
|
||||
const width = this.width;
|
||||
const length = this.length;
|
||||
const bitarray = this.bitarray;
|
||||
|
||||
for (let w = 0; w < width; w++) {
|
||||
const bitmask = this.bitmask[w];
|
||||
if (!bitmask || bitarray[w * length + index] !== bitmask) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// return true if this index is selected in ALL dimensions IGNORING dim
|
||||
//
|
||||
isSelectedIgnoringDim(index, dim) {
|
||||
const ignoreOffset = dim >>> 5;
|
||||
const ignoreMask = ~(1 << dim % 32);
|
||||
|
||||
const width = this.width;
|
||||
const length = this.length;
|
||||
const bitarray = this.bitarray;
|
||||
|
||||
for (let w = 0; w < width; w++) {
|
||||
const bitmask = this.bitmask[w];
|
||||
if (w === ignoreOffset) {
|
||||
if (
|
||||
bitmask &&
|
||||
(bitarray[w * length + index] & ignoreMask) !== (bitmask & ignoreMask)
|
||||
)
|
||||
return false;
|
||||
} else {
|
||||
if (bitmask && bitarray[w * length + index] !== bitmask) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// select index on dimension
|
||||
//
|
||||
selectOne(dim, index) {
|
||||
const col = dim >>> 5;
|
||||
const before = this.bitarray[col * this.length + index];
|
||||
const after = before | (1 << dim % 32);
|
||||
this.bitarray[col * this.length + index] = after;
|
||||
}
|
||||
|
||||
// deselect index on dimension
|
||||
//
|
||||
deselectOne(dim, index) {
|
||||
const col = dim >>> 5;
|
||||
const before = this.bitarray[col * this.length + index];
|
||||
const after = before & ~(1 << dim % 32);
|
||||
this.bitarray[col * this.length + index] = after;
|
||||
}
|
||||
|
||||
// select all indices on dimension.
|
||||
//
|
||||
selectAll(dim) {
|
||||
let col = dim >> 5;
|
||||
const bitmask = this.bitmask[col];
|
||||
const bitarray = this.bitarray;
|
||||
const one = 1 << dim % 32;
|
||||
for (let i = col * this.length, len = i + this.length; i < len; i++) {
|
||||
bitarray[i] |= one;
|
||||
}
|
||||
}
|
||||
|
||||
// deselect all indices on dimension
|
||||
//
|
||||
deselectAll(dim) {
|
||||
let col = dim >> 5;
|
||||
const bitmask = this.bitmask[col];
|
||||
const bitarray = this.bitarray;
|
||||
const zero = ~(1 << dim % 32);
|
||||
for (let i = col * this.length, len = i + this.length; i < len; i++) {
|
||||
bitarray[i] &= zero;
|
||||
}
|
||||
}
|
||||
|
||||
// select range of indices on a dimension, indirect through a sort map.
|
||||
// Indirect functions are used to map between sort and natural order.
|
||||
//
|
||||
selectIndirectFromRange(dim, indirect, range) {
|
||||
const col = dim >>> 5;
|
||||
const first = range[0];
|
||||
const last = range[1];
|
||||
const bitarray = this.bitarray;
|
||||
const one = 1 << dim % 32;
|
||||
const offset = col * this.length;
|
||||
for (let i = first; i < last; i++) {
|
||||
bitarray[offset + indirect[i]] |= one;
|
||||
}
|
||||
}
|
||||
|
||||
// deselect range of indices on a dimension, indirect through a sort map.
|
||||
//
|
||||
deselectIndirectFromRange(dim, indirect, range) {
|
||||
const col = dim >>> 5;
|
||||
const first = range[0];
|
||||
const last = range[1];
|
||||
const bitarray = this.bitarray;
|
||||
const zero = ~(1 << dim % 32);
|
||||
const offset = col * this.length;
|
||||
for (let i = first; i < last; i++) {
|
||||
bitarray[offset + indirect[i]] &= zero;
|
||||
}
|
||||
}
|
||||
|
||||
// Fill the array with selected|deselected value based upon the
|
||||
// current selection state.
|
||||
//
|
||||
fillBySelection(result, selectedValue, deselectedValue) {
|
||||
// special case (width === 1) for performance
|
||||
if (this.width === 1) {
|
||||
const bitmask = this.bitmask[0];
|
||||
const bitarray = this.bitarray;
|
||||
for (let i = 0, len = this.length; i < len; i++) {
|
||||
result[i] =
|
||||
bitmask && bitarray[i] === bitmask ? selectedValue : deselectedValue;
|
||||
}
|
||||
} else {
|
||||
for (let i = 0, len = this.length; i < len; i++) {
|
||||
result[i] = this.isSelected(i) ? selectedValue : deselectedValue;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export default BitArray;
|
||||
"use strict";
|
||||
// jshint esversion: 6
|
||||
|
||||
// BitArray is a 2D bitarray with size [length, nBitWidth].
|
||||
// Each bit is referred to as a `dimension`. Dimensions may be
|
||||
// dynamically allocated and deallocated. The overall length
|
||||
// of the BitArray is fixed at creation time (for simplicity).
|
||||
//
|
||||
// Organization of the bitarray is dimension-major. As dimensions
|
||||
// are added, the underlying store is grown 32 bits at a time.
|
||||
// NOTE: currently does not deallocate / shrink.
|
||||
//
|
||||
// Primary operations on the BitArray are:
|
||||
// - set & clear dimension
|
||||
// - test dimension
|
||||
// - various performance or convenience operations to optimize bulk ops
|
||||
//
|
||||
// The underlying data structure uses TypedArrays for performance.
|
||||
//
|
||||
class BitArray {
|
||||
constructor(length) {
|
||||
// Initially allocate a 32 bit wide array. allocDimension() will expand
|
||||
// as necessary.
|
||||
//
|
||||
// Int32Array is (counterintuitively) used to accomadate JS numeric casting
|
||||
// (to/from primitive number type).
|
||||
//
|
||||
|
||||
// Fixed for the life of this object.
|
||||
this.length = length;
|
||||
|
||||
// Bitarray width. width is always greater than 32*dimensionCount.
|
||||
this.width = 1; // underlying number of 32 bit arrays
|
||||
this.dimensionCount = 0; // num allocated dimensions
|
||||
|
||||
this.bitmask = new Int32Array(this.width); // dimension allocation mask
|
||||
this.bitarray = new Int32Array(this.width * this.length);
|
||||
}
|
||||
|
||||
// Return the number of records that are selected, ie, have a one bit in
|
||||
// all allocated dimensions.
|
||||
//
|
||||
get selectionCount() {
|
||||
return this.countAllOnes();
|
||||
}
|
||||
|
||||
// Count all records that have a 'one' bit in allocated dimensions.
|
||||
//
|
||||
countAllOnes() {
|
||||
let count = 0;
|
||||
for (let i = 0; i < this.width; i++) {
|
||||
const bitmask = this.bitmask[i];
|
||||
for (let j = i * this.length, len = j + this.length; j < len; j++) {
|
||||
if (this.bitarray[i * this.length + j] === bitmask) count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// count trailing zeros - hard to do fast in JS!
|
||||
// https://en.wikipedia.org/wiki/Find_first_set#CTZ
|
||||
static ctz(v) {
|
||||
let c = 32;
|
||||
v &= -v; // isolate lowest non-zero bit
|
||||
if (v) c--;
|
||||
if (v & 0x0000ffff) c -= 16;
|
||||
if (v & 0x00ff00ff) c -= 8;
|
||||
if (v & 0x0f0f0f0f) c -= 4;
|
||||
if (v & 0x33333333) c -= 2;
|
||||
if (v & 0x55555555) c -= 1;
|
||||
return c;
|
||||
}
|
||||
|
||||
// find a free dimension. Return undefined if none
|
||||
_findFreeDimension() {
|
||||
let dim;
|
||||
for (let col = 0; col < this.width; col++) {
|
||||
const bitmask = this.bitmask[col];
|
||||
const lowestZeroBit = ~this.bitmask[col] & -~this.bitmask[col];
|
||||
if (lowestZeroBit) {
|
||||
this.bitmask[col] |= lowestZeroBit;
|
||||
dim = 32 * col + BitArray.ctz(lowestZeroBit);
|
||||
}
|
||||
}
|
||||
return dim;
|
||||
}
|
||||
|
||||
// allocate and return the dimension ID (bit position)
|
||||
//
|
||||
allocDimension() {
|
||||
let dim = this._findFreeDimension();
|
||||
|
||||
// if we did not find free dimension, expand the bitarray.
|
||||
if (dim === undefined) {
|
||||
this.width++;
|
||||
|
||||
const biggerBitArray = new Int32Array(this.width * this.length);
|
||||
biggerBitArray.set(this.bitarray);
|
||||
this.bitarray = biggerBitArray;
|
||||
|
||||
const biggerBitmask = new Int32Array(this.width);
|
||||
biggerBitmask.set(this.bitmask);
|
||||
this.bitmask = biggerBitmask;
|
||||
|
||||
dim = this._findFreeDimension();
|
||||
}
|
||||
|
||||
this.dimensionCount++;
|
||||
return dim;
|
||||
}
|
||||
|
||||
// free a dimension for later use. MUST deselect the dimension, as other
|
||||
// code assume the column will be zero valued.
|
||||
//
|
||||
freeDimension(dim) {
|
||||
// all selection tests assume unallocated dimensions are zero valued.
|
||||
this.deselectAll(dim);
|
||||
const col = dim >>> 5;
|
||||
this.bitmask[col] &= ~(1 << dim % 32);
|
||||
this.dimensionCount--;
|
||||
}
|
||||
|
||||
// return true if this index is selected in ALL dimensions.
|
||||
//
|
||||
isSelected(index) {
|
||||
const width = this.width;
|
||||
const length = this.length;
|
||||
const bitarray = this.bitarray;
|
||||
|
||||
for (let w = 0; w < width; w++) {
|
||||
const bitmask = this.bitmask[w];
|
||||
if (!bitmask || bitarray[w * length + index] !== bitmask) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// return true if this index is selected in ALL dimensions IGNORING dim
|
||||
//
|
||||
isSelectedIgnoringDim(index, dim) {
|
||||
const ignoreOffset = dim >>> 5;
|
||||
const ignoreMask = ~(1 << dim % 32);
|
||||
|
||||
const width = this.width;
|
||||
const length = this.length;
|
||||
const bitarray = this.bitarray;
|
||||
|
||||
for (let w = 0; w < width; w++) {
|
||||
const bitmask = this.bitmask[w];
|
||||
if (w === ignoreOffset) {
|
||||
if (
|
||||
bitmask &&
|
||||
(bitarray[w * length + index] & ignoreMask) !== (bitmask & ignoreMask)
|
||||
)
|
||||
return false;
|
||||
} else {
|
||||
if (bitmask && bitarray[w * length + index] !== bitmask) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// select index on dimension
|
||||
//
|
||||
selectOne(dim, index) {
|
||||
const col = dim >>> 5;
|
||||
const before = this.bitarray[col * this.length + index];
|
||||
const after = before | (1 << dim % 32);
|
||||
this.bitarray[col * this.length + index] = after;
|
||||
}
|
||||
|
||||
// deselect index on dimension
|
||||
//
|
||||
deselectOne(dim, index) {
|
||||
const col = dim >>> 5;
|
||||
const before = this.bitarray[col * this.length + index];
|
||||
const after = before & ~(1 << dim % 32);
|
||||
this.bitarray[col * this.length + index] = after;
|
||||
}
|
||||
|
||||
// select all indices on dimension.
|
||||
//
|
||||
selectAll(dim) {
|
||||
let col = dim >> 5;
|
||||
const bitmask = this.bitmask[col];
|
||||
const bitarray = this.bitarray;
|
||||
const one = 1 << dim % 32;
|
||||
for (let i = col * this.length, len = i + this.length; i < len; i++) {
|
||||
bitarray[i] |= one;
|
||||
}
|
||||
}
|
||||
|
||||
// deselect all indices on dimension
|
||||
//
|
||||
deselectAll(dim) {
|
||||
let col = dim >> 5;
|
||||
const bitmask = this.bitmask[col];
|
||||
const bitarray = this.bitarray;
|
||||
const zero = ~(1 << dim % 32);
|
||||
for (let i = col * this.length, len = i + this.length; i < len; i++) {
|
||||
bitarray[i] &= zero;
|
||||
}
|
||||
}
|
||||
|
||||
// select range of indices on a dimension, indirect through a sort map.
|
||||
// Indirect functions are used to map between sort and natural order.
|
||||
//
|
||||
selectIndirectFromRange(dim, indirect, range) {
|
||||
const col = dim >>> 5;
|
||||
const first = range[0];
|
||||
const last = range[1];
|
||||
const bitarray = this.bitarray;
|
||||
const one = 1 << dim % 32;
|
||||
const offset = col * this.length;
|
||||
for (let i = first; i < last; i++) {
|
||||
bitarray[offset + indirect[i]] |= one;
|
||||
}
|
||||
}
|
||||
|
||||
// deselect range of indices on a dimension, indirect through a sort map.
|
||||
//
|
||||
deselectIndirectFromRange(dim, indirect, range) {
|
||||
const col = dim >>> 5;
|
||||
const first = range[0];
|
||||
const last = range[1];
|
||||
const bitarray = this.bitarray;
|
||||
const zero = ~(1 << dim % 32);
|
||||
const offset = col * this.length;
|
||||
for (let i = first; i < last; i++) {
|
||||
bitarray[offset + indirect[i]] &= zero;
|
||||
}
|
||||
}
|
||||
|
||||
// Fill the array with selected|deselected value based upon the
|
||||
// current selection state.
|
||||
//
|
||||
fillBySelection(result, selectedValue, deselectedValue) {
|
||||
// special case (width === 1) for performance
|
||||
if (this.width === 1) {
|
||||
const bitmask = this.bitmask[0];
|
||||
const bitarray = this.bitarray;
|
||||
for (let i = 0, len = this.length; i < len; i++) {
|
||||
result[i] =
|
||||
bitmask && bitarray[i] === bitmask ? selectedValue : deselectedValue;
|
||||
}
|
||||
} else {
|
||||
for (let i = 0, len = this.length; i < len; i++) {
|
||||
result[i] = this.isSelected(i) ? selectedValue : deselectedValue;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export default BitArray;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,132 +1,132 @@
|
||||
"use strict";
|
||||
// jshint esversion: 6
|
||||
|
||||
// Interval operations - very simple version of interval set relationship
|
||||
// operators. An interval is a multi-interval list of [min, max),
|
||||
// where min and max are mandatory. Constraints:
|
||||
// * min <= max, min >= 0
|
||||
// * empty interval groups are OK, ie, []
|
||||
// * Legal intervals: [], [ [0, 1], ... ]
|
||||
// * Not legal: [ [] ]
|
||||
//
|
||||
// All intervals are represented by simple JS arrays/numbers.
|
||||
//
|
||||
// Code assumes intervals have a low cardinality; many operations are done
|
||||
// with a brute force scan. Little attempt to reduce GC pressure.
|
||||
//
|
||||
class PositiveIntervals {
|
||||
// Canonicalize - ensure that:
|
||||
// 1. no overlapping intervals
|
||||
// 2. sorted in order of interval min.
|
||||
//
|
||||
static canonicalize(A) {
|
||||
if (A.length <= 1) return A;
|
||||
let copy = A.slice();
|
||||
copy.sort((a, b) => a[0] - b[0]);
|
||||
const res = [];
|
||||
res.push(copy[0]);
|
||||
for (let i = 1, len = copy.length; i < len; i++) {
|
||||
if (copy[i][0] > res[res.length - 1][1]) {
|
||||
// non-overlapping, add to result
|
||||
res.push(copy[i]);
|
||||
} else if (copy[i][1] > res[res.length - 1][1]) {
|
||||
// merge this into previous
|
||||
res[res.length - 1][1] = copy[i][1];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// Return interval with values belonging to both A and B. Essentially
|
||||
// a set union operation.
|
||||
//
|
||||
static union(A, B) {
|
||||
return PositiveIntervals.canonicalize([...A, ...B]);
|
||||
}
|
||||
|
||||
static _flatten(A, B) {
|
||||
let points = []; /* point, A, start */
|
||||
for (let a = 0; a < A.length; a++) {
|
||||
points.push([A[a][0], true, true]);
|
||||
points.push([A[a][1], true, false]);
|
||||
}
|
||||
for (let b = 0; b < B.length; b++) {
|
||||
points.push([B[b][0], false, true]);
|
||||
points.push([B[b][1], false, false]);
|
||||
}
|
||||
// Sort order: point, then start
|
||||
points.sort((a, b) => (a[0] !== b[0] ? a[0] - b[0] : a[2] ? 1 : -1));
|
||||
return points;
|
||||
}
|
||||
|
||||
// A - B, ie, the interval with all values in A that are not in B. Essentially
|
||||
// a set difference operation.
|
||||
//
|
||||
static difference(A, B) {
|
||||
// Corner cases
|
||||
if (A.length === 0 || B.length === 0) {
|
||||
return PositiveIntervals.canonicalize(A);
|
||||
}
|
||||
|
||||
A = PositiveIntervals.canonicalize(A);
|
||||
B = PositiveIntervals.canonicalize(B);
|
||||
|
||||
const points = PositiveIntervals._flatten(A, B);
|
||||
const res = [];
|
||||
let aDepth = 0;
|
||||
let depth = 0;
|
||||
let intervalStart;
|
||||
let prevPoint;
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
const p = points[i];
|
||||
const before = depth;
|
||||
const delta = p[2] ? 1 : -1;
|
||||
depth += delta;
|
||||
if (p[1]) aDepth += delta;
|
||||
|
||||
if (i === points.length - 1 || p[0] !== points[i + 1][0]) {
|
||||
if (aDepth === 1 && depth === 1) {
|
||||
intervalStart = p[0];
|
||||
} else if (intervalStart !== undefined) {
|
||||
res.push([intervalStart, p[0]]);
|
||||
intervalStart = undefined;
|
||||
}
|
||||
}
|
||||
prevPoint = p[0];
|
||||
}
|
||||
// guaranteed to be in canonical form
|
||||
return res;
|
||||
}
|
||||
|
||||
// Return interval with values belonging to A or B. Essentially a set
|
||||
// intersection.
|
||||
//
|
||||
static intersection(A, B) {
|
||||
if (A.length === 0 || B.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
A = PositiveIntervals.canonicalize(A);
|
||||
B = PositiveIntervals.canonicalize(B);
|
||||
|
||||
const points = PositiveIntervals._flatten(A, B);
|
||||
const res = [];
|
||||
let depth = 0;
|
||||
let intervalStart;
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
const p = points[i];
|
||||
const before = depth;
|
||||
depth += p[2] ? 1 : -1;
|
||||
if (depth === 2) {
|
||||
intervalStart = p[0];
|
||||
} else if (intervalStart !== undefined) {
|
||||
res.push([intervalStart, p[0]]);
|
||||
intervalStart = undefined;
|
||||
}
|
||||
}
|
||||
// guaranteed to be in canonical form
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export default PositiveIntervals;
|
||||
"use strict";
|
||||
// jshint esversion: 6
|
||||
|
||||
// Interval operations - very simple version of interval set relationship
|
||||
// operators. An interval is a multi-interval list of [min, max),
|
||||
// where min and max are mandatory. Constraints:
|
||||
// * min <= max, min >= 0
|
||||
// * empty interval groups are OK, ie, []
|
||||
// * Legal intervals: [], [ [0, 1], ... ]
|
||||
// * Not legal: [ [] ]
|
||||
//
|
||||
// All intervals are represented by simple JS arrays/numbers.
|
||||
//
|
||||
// Code assumes intervals have a low cardinality; many operations are done
|
||||
// with a brute force scan. Little attempt to reduce GC pressure.
|
||||
//
|
||||
class PositiveIntervals {
|
||||
// Canonicalize - ensure that:
|
||||
// 1. no overlapping intervals
|
||||
// 2. sorted in order of interval min.
|
||||
//
|
||||
static canonicalize(A) {
|
||||
if (A.length <= 1) return A;
|
||||
let copy = A.slice();
|
||||
copy.sort((a, b) => a[0] - b[0]);
|
||||
const res = [];
|
||||
res.push(copy[0]);
|
||||
for (let i = 1, len = copy.length; i < len; i++) {
|
||||
if (copy[i][0] > res[res.length - 1][1]) {
|
||||
// non-overlapping, add to result
|
||||
res.push(copy[i]);
|
||||
} else if (copy[i][1] > res[res.length - 1][1]) {
|
||||
// merge this into previous
|
||||
res[res.length - 1][1] = copy[i][1];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// Return interval with values belonging to both A and B. Essentially
|
||||
// a set union operation.
|
||||
//
|
||||
static union(A, B) {
|
||||
return PositiveIntervals.canonicalize([...A, ...B]);
|
||||
}
|
||||
|
||||
static _flatten(A, B) {
|
||||
let points = []; /* point, A, start */
|
||||
for (let a = 0; a < A.length; a++) {
|
||||
points.push([A[a][0], true, true]);
|
||||
points.push([A[a][1], true, false]);
|
||||
}
|
||||
for (let b = 0; b < B.length; b++) {
|
||||
points.push([B[b][0], false, true]);
|
||||
points.push([B[b][1], false, false]);
|
||||
}
|
||||
// Sort order: point, then start
|
||||
points.sort((a, b) => (a[0] !== b[0] ? a[0] - b[0] : a[2] ? 1 : -1));
|
||||
return points;
|
||||
}
|
||||
|
||||
// A - B, ie, the interval with all values in A that are not in B. Essentially
|
||||
// a set difference operation.
|
||||
//
|
||||
static difference(A, B) {
|
||||
// Corner cases
|
||||
if (A.length === 0 || B.length === 0) {
|
||||
return PositiveIntervals.canonicalize(A);
|
||||
}
|
||||
|
||||
A = PositiveIntervals.canonicalize(A);
|
||||
B = PositiveIntervals.canonicalize(B);
|
||||
|
||||
const points = PositiveIntervals._flatten(A, B);
|
||||
const res = [];
|
||||
let aDepth = 0;
|
||||
let depth = 0;
|
||||
let intervalStart;
|
||||
let prevPoint;
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
const p = points[i];
|
||||
const before = depth;
|
||||
const delta = p[2] ? 1 : -1;
|
||||
depth += delta;
|
||||
if (p[1]) aDepth += delta;
|
||||
|
||||
if (i === points.length - 1 || p[0] !== points[i + 1][0]) {
|
||||
if (aDepth === 1 && depth === 1) {
|
||||
intervalStart = p[0];
|
||||
} else if (intervalStart !== undefined) {
|
||||
res.push([intervalStart, p[0]]);
|
||||
intervalStart = undefined;
|
||||
}
|
||||
}
|
||||
prevPoint = p[0];
|
||||
}
|
||||
// guaranteed to be in canonical form
|
||||
return res;
|
||||
}
|
||||
|
||||
// Return interval with values belonging to A or B. Essentially a set
|
||||
// intersection.
|
||||
//
|
||||
static intersection(A, B) {
|
||||
if (A.length === 0 || B.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
A = PositiveIntervals.canonicalize(A);
|
||||
B = PositiveIntervals.canonicalize(B);
|
||||
|
||||
const points = PositiveIntervals._flatten(A, B);
|
||||
const res = [];
|
||||
let depth = 0;
|
||||
let intervalStart;
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
const p = points[i];
|
||||
const before = depth;
|
||||
depth += p[2] ? 1 : -1;
|
||||
if (depth === 2) {
|
||||
intervalStart = p[0];
|
||||
} else if (intervalStart !== undefined) {
|
||||
res.push([intervalStart, p[0]]);
|
||||
intervalStart = undefined;
|
||||
}
|
||||
}
|
||||
// guaranteed to be in canonical form
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export default PositiveIntervals;
|
||||
|
||||
@@ -1,98 +1,98 @@
|
||||
"use strict";
|
||||
// jshint esversion: 6
|
||||
|
||||
/*
|
||||
Utility functions, private to this module.
|
||||
*/
|
||||
|
||||
// fill an array or typedarray with a sequential range of numbers,
|
||||
// starting with `start`
|
||||
//
|
||||
export function fillRange(arr, start = 0) {
|
||||
for (let i = 0, len = arr.length; i < len; i++) {
|
||||
arr[i] = i + start;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
// Search for `value` in the sorted array `arr`, in the range [first, last).
|
||||
// Return the first (left most) index where arr[index] >= value.
|
||||
//
|
||||
// In other words, return array index I where:
|
||||
// arr[i] < value for all tarr[lo:I]
|
||||
// arr[i] >= value for all tarr[I:last]
|
||||
//
|
||||
// The same semantics/behavior as:
|
||||
// C++: lower_bound()
|
||||
// Python: bisect.bisect_left()
|
||||
//
|
||||
// XXX: it is likely that there would be minimal performance hit from creating
|
||||
// a factory version of lowerBound that takes an accessor (rather than having
|
||||
// a special-cased version for lining the indirection).
|
||||
//
|
||||
export function lowerBound(valueArray, value, first, last) {
|
||||
// this is just a binary search
|
||||
while (first < last) {
|
||||
const middle = (first + last) >>> 1;
|
||||
if (valueArray[middle] < value) {
|
||||
first = middle + 1;
|
||||
} else {
|
||||
last = middle;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
// Inlined performance optimization - used to indirect through a sort map.
|
||||
//
|
||||
export function lowerBoundIndirect(valueArray, indexArray, value, first, last) {
|
||||
// this is just a binary search
|
||||
while (first < last) {
|
||||
const middle = (first + last) >>> 1;
|
||||
if (valueArray[indexArray[middle]] < value) {
|
||||
first = middle + 1;
|
||||
} else {
|
||||
last = middle;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
// Search for `value in the sorted array `arr`, in the range [first, last).
|
||||
// Return the first value where arr[index] > value.
|
||||
//
|
||||
// In other words, return array index I, where:
|
||||
// arr[i] <= value for all tarr[lo:I]
|
||||
// arr[i] > value for all tarr[I:last]
|
||||
//
|
||||
// The same semantics/behavior as:
|
||||
// C++: upper_bound()
|
||||
// Python: bisect.bisect_right()
|
||||
//
|
||||
export function upperBound(valueArray, value, first, last) {
|
||||
// this is just a binary search
|
||||
while (first < last) {
|
||||
const middle = (first + last) >>> 1;
|
||||
if (valueArray[middle] > value) {
|
||||
last = middle;
|
||||
} else {
|
||||
first = middle + 1;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
// Inline performance optimization
|
||||
//
|
||||
export function upperBoundIndirect(valueArray, indexArray, value, first, last) {
|
||||
// this is just a binary search
|
||||
while (first < last) {
|
||||
const middle = (first + last) >>> 1;
|
||||
if (valueArray[indexArray[middle]] > value) {
|
||||
last = middle;
|
||||
} else {
|
||||
first = middle + 1;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
"use strict";
|
||||
// jshint esversion: 6
|
||||
|
||||
/*
|
||||
Utility functions, private to this module.
|
||||
*/
|
||||
|
||||
// fill an array or typedarray with a sequential range of numbers,
|
||||
// starting with `start`
|
||||
//
|
||||
export function fillRange(arr, start = 0) {
|
||||
for (let i = 0, len = arr.length; i < len; i++) {
|
||||
arr[i] = i + start;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
// Search for `value` in the sorted array `arr`, in the range [first, last).
|
||||
// Return the first (left most) index where arr[index] >= value.
|
||||
//
|
||||
// In other words, return array index I where:
|
||||
// arr[i] < value for all tarr[lo:I]
|
||||
// arr[i] >= value for all tarr[I:last]
|
||||
//
|
||||
// The same semantics/behavior as:
|
||||
// C++: lower_bound()
|
||||
// Python: bisect.bisect_left()
|
||||
//
|
||||
// XXX: it is likely that there would be minimal performance hit from creating
|
||||
// a factory version of lowerBound that takes an accessor (rather than having
|
||||
// a special-cased version for lining the indirection).
|
||||
//
|
||||
export function lowerBound(valueArray, value, first, last) {
|
||||
// this is just a binary search
|
||||
while (first < last) {
|
||||
const middle = (first + last) >>> 1;
|
||||
if (valueArray[middle] < value) {
|
||||
first = middle + 1;
|
||||
} else {
|
||||
last = middle;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
// Inlined performance optimization - used to indirect through a sort map.
|
||||
//
|
||||
export function lowerBoundIndirect(valueArray, indexArray, value, first, last) {
|
||||
// this is just a binary search
|
||||
while (first < last) {
|
||||
const middle = (first + last) >>> 1;
|
||||
if (valueArray[indexArray[middle]] < value) {
|
||||
first = middle + 1;
|
||||
} else {
|
||||
last = middle;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
// Search for `value in the sorted array `arr`, in the range [first, last).
|
||||
// Return the first value where arr[index] > value.
|
||||
//
|
||||
// In other words, return array index I, where:
|
||||
// arr[i] <= value for all tarr[lo:I]
|
||||
// arr[i] > value for all tarr[I:last]
|
||||
//
|
||||
// The same semantics/behavior as:
|
||||
// C++: upper_bound()
|
||||
// Python: bisect.bisect_right()
|
||||
//
|
||||
export function upperBound(valueArray, value, first, last) {
|
||||
// this is just a binary search
|
||||
while (first < last) {
|
||||
const middle = (first + last) >>> 1;
|
||||
if (valueArray[middle] > value) {
|
||||
last = middle;
|
||||
} else {
|
||||
first = middle + 1;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
// Inline performance optimization
|
||||
//
|
||||
export function upperBoundIndirect(valueArray, indexArray, value, first, last) {
|
||||
// this is just a binary search
|
||||
while (first < last) {
|
||||
const middle = (first + last) >>> 1;
|
||||
if (valueArray[indexArray[middle]] > value) {
|
||||
last = middle;
|
||||
} else {
|
||||
first = middle + 1;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user