UNPKG

typescript-algorithms-and-datastructures

Version:
59 lines 2.34 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "./Utils"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Utils_1 = require("./Utils"); class ObjectArray { constructor(size, properties) { this.columns = []; this.nameToColumn = {}; this.freePointer = 0; this.properties = properties; this.columns = properties.map(property => { var TypedArrayConstructor = property.type; return new TypedArrayConstructor(size); }); properties.forEach((property, i) => this.nameToColumn[property.name] = i); this.nextFreeList = new Float64Array(Utils_1.Utils.range(1, size + 1)); this.nextFreeList[size - 1] = -1; } add(obj) { if (this.freePointer === -1) { throw new RangeError('Array size exceeded.'); } var index = this.freePointer; this.freePointer = this.nextFreeList[this.freePointer]; this.nextFreeList[index] = -1; obj.forEach((value, columnIndex) => this.columns[columnIndex][index] = value); return index; } remove(index) { var currentPointer = this.freePointer; this.freePointer = index; this.nextFreeList[this.freePointer] = currentPointer; } get(index) { if (this.nextFreeList[index] !== -1) { return null; } return this.columns.map(column => column[index]); } getHash(index) { var output = {}; if (this.nextFreeList[index] !== -1) { return null; } this.columns.map((column, columnIndex) => output[this.properties[columnIndex].name] = column[index]); return output; } } exports.ObjectArray = ObjectArray; }); //# sourceMappingURL=ObjectArray.js.map