react-flexigrid
Version:
A React table component designed to allow presenting millions of rows of data.
230 lines (180 loc) • 5.75 kB
JavaScript
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import invariant from 'invariant';
var getParentIndex = function getParentIndex(index) {
return Math.floor(index / 2);
};
var Int32Array = global.Int32Array || function (size) {
var arr = [];
for (var i = size - 1; i >= 0; i -= 1) {
arr[i] = 0;
}
return arr;
};
/**
* Computes the next power of 2 after or equal to x.
*/
function ceilLog2(x) {
var y = 1;
while (y < x) {
y *= 2;
}
return y;
}
/**
* A prefix interval tree stores an numeric array and the partial sums of that
* array. It is optimized for updating the values of the array without
* recomputing all of the partial sums.
*
* - O(ln n) update
* - O(1) lookup
* - O(ln n) compute a partial sum
* - O(n) space
*
* Note that the sequence of partial sums is one longer than the array, so that
* the first partial sum is always 0, and the last partial sum is the sum of the
* entire array.
*/
var PrefixIntervalTree = function () {
function PrefixIntervalTree(arr) {
_classCallCheck(this, PrefixIntervalTree);
this.size = arr.length;
/**
* Half the size of the heap. It is also the number of non-leaf nodes, and
* the index of the first element in the heap. Always a power of 2.
*/
this.half = ceilLog2(this.size);
this.heap = new Int32Array(2 * this.half);
// 初始化数组
for (var i = 0; i < this.size; i += 1) {
this.heap[this.half + i] = arr[i];
}
// 初始化数组和
for (var _i = this.half - 1; _i > 0; _i -= 1) {
this.heap[_i] = this.heap[2 * _i] + this.heap[2 * _i + 1];
}
}
PrefixIntervalTree.uniform = function uniform(size, initialValue) {
var arr = [];
for (var i = size - 1; i >= 0; i -= 1) {
arr[i] = initialValue;
}
return new PrefixIntervalTree(arr);
};
PrefixIntervalTree.empty = function empty(size) {
return PrefixIntervalTree.uniform(size, 0);
};
PrefixIntervalTree.prototype.set = function set(index, value) {
invariant(index >= 0 && index < this.size, 'Index out of range %s', index);
// 更新数组项
var nodeIndex = this.half + index;
this.heap[nodeIndex] = value;
// 更新和
nodeIndex = getParentIndex(nodeIndex);
while (nodeIndex) {
this.heap[nodeIndex] = this.heap[2 * nodeIndex] + this.heap[2 * nodeIndex + 1];
nodeIndex = getParentIndex(nodeIndex);
}
};
PrefixIntervalTree.prototype.get = function get(index) {
invariant(index >= 0 && index < this.size, 'Index out of range %s', index);
var nodeIndex = this.half + index;
return this.heap[nodeIndex];
};
PrefixIntervalTree.prototype.getSize = function getSize() {
return this.size;
};
/**
* Returns the sum get(0) + get(1) + ... + get(end - 1).
*/
PrefixIntervalTree.prototype.sumUntil = function sumUntil(end) {
invariant(end >= 0 && end < this.size + 1, 'Index out of range %s', end);
if (end === 0) {
return 0;
}
var index = this.half + end - 1;
var sum = this.heap[index]; // the last item
while (index !== 1) {
if (index % 2 === 1) {
sum += this.heap[index - 1];
}
index = getParentIndex(index);
}
return sum;
};
/**
* Returns the sum get(0) + get(1) + ... + get(inclusiveEnd).
*/
PrefixIntervalTree.prototype.sumTo = function sumTo(inclusiveEnd) {
invariant(inclusiveEnd >= 0 && inclusiveEnd < this.size, 'Index out of range %s', inclusiveEnd);
return this.sumUntil(inclusiveEnd + 1);
};
/**
* Returns the sum get(begin) + get(begin + 1) + ... + get(end - 1).
*/
PrefixIntervalTree.prototype.sum = function sum(begin, end) {
invariant(begin <= end, 'Begin must precede end');
return this.sumUntil(end) - this.sumUntil(begin);
};
/**
* Returns the smallest i such that 0 <= i <= size and sumUntil(i) <= t, or
* -1 if no such i exists.
*/
PrefixIntervalTree.prototype.greatestLowerBound = function greatestLowerBound(target) {
if (target < 0) {
return -1;
}
var index = 1;
if (this.heap[index] <= target) {
return this.size;
}
while (index < this.half) {
var leftSum = this.heap[2 * index];
if (target < leftSum) {
index = 2 * index; // eslint-disable-line
} else {
index = 2 * index + 1;
target -= leftSum; // eslint-disable-line
}
}
return index - this.half;
};
/**
* Returns the smallest i such that 0 <= i <= size and sumUntil(i) < t, or
* -1 if no such i exists.
*/
PrefixIntervalTree.prototype.greatestStrictLowerBound = function greatestStrictLowerBound(target) {
if (target <= 0) {
return -1;
}
var index = 1;
if (this.heap[index] < target) {
return this.size;
}
while (index < this.half) {
var leftSum = this.heap[2 * index];
if (target <= leftSum) {
index = 2 * index; // eslint-disable-line
} else {
index = 2 * index + 1;
target -= leftSum; // eslint-disable-line
}
}
return index - this.half;
};
/**
* Returns the smallest i such that 0 <= i <= size and t <= sumUntil(i), or
* size + 1 if no such i exists.
*/
PrefixIntervalTree.prototype.leastUpperBound = function leastUpperBound(target) {
return this.greatestStrictLowerBound(target) + 1;
};
/**
* Returns the smallest i such that 0 <= i <= size and t < sumUntil(i), or
* size + 1 if no such i exists.
*/
PrefixIntervalTree.prototype.leastStrictUpperBound = function leastStrictUpperBound(target) {
return this.greatestLowerBound(target) + 1;
};
return PrefixIntervalTree;
}();
export default PrefixIntervalTree;