UNPKG

encompass-gc-optimized-collections

Version:

Provides GC optimized collections in TypeScript and Lua for use with Encompass-TS

109 lines 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class GCOptimizedList { constructor(...entries) { this.items = new Map(); this.indices = new Map(); this._size = 0; for (const k of entries) { this.add(k); } } get size() { return this._size; } forEach(callback) { for (let i = 0; i < this.size; i++) { callback(this.get(i)); } } entries() { return this.items; } values() { return this.items.values(); } add(value) { this.items.set(this._size, value); this.indices.set(value, this._size); this._size += 1; } clear() { for (const [k, v] of this.entries()) { this.items.delete(k); this.indices.delete(v); } this._size = 0; } delete(index) { if (this.hasIndex(index)) { const value = this.get(index); this.items.delete(index); this.indices.delete(value); let k = index; this._size -= 1; while (k < this.size) { const one_up_value = this.items.get(k + 1); if (one_up_value !== undefined) { this.items.set(k, one_up_value); this.indices.set(one_up_value, k); } k += 1; } this.items.delete(this.size); return true; } return false; } deleteValue(value) { const index = this.indexOf(value); if (index !== null) { this.delete(index); return true; } return false; } indexOf(value) { if (this.indices.has(value)) { return this.indices.get(value); } else { return null; } } get(index) { return this.items.get(index); } empty() { return this.size === 0; } hasIndex(index) { return this.items.has(index); } hasValue(value) { return this.indices.has(value); } shift() { if (!this.empty()) { const item = this.items.get(0); this.delete(0); return item; } else { return undefined; } } pop() { if (!this.empty()) { const item = this.items.get(this.size - 1); this.delete(this.size - 1); return item; } else { return undefined; } } } GCOptimizedList.Empty = new GCOptimizedList(); exports.GCOptimizedList = GCOptimizedList; //# sourceMappingURL=gc_optimized_list.js.map