UNPKG

sc4

Version:

A command line utility for automating SimCity 4 modding tasks & modifying savegames

66 lines (65 loc) 1.66 kB
// # MappedIndex() export default class MappedIndex { tgi = new Map(); ti = new Map(); t = new Map(); // ## constructor() constructor(entries = []) { for (let j = 0; j < entries.length; j++) { let entry = entries[j]; let { type: t, group: g, instance: i } = entry; set(this.tgi, hhh(t, g, i), j); set(this.ti, hh(t, i), j); set(this.t, h(t), j); } } // ## load(cache) // Loads an index from cache. load(cache) { this.tgi = new Map(cache.tgi); this.ti = new Map(cache.ti); this.t = new Map(cache.t); return this; } // ## findTGI() findTGI(t, g, i) { return get(this.tgi, hhh(t, g, i)); } // ## findType() findType(t) { return get(this.t, h(t)); } // ## findTI() findTI(t, i) { return get(this.ti, hh(t, i)); } // ## toJSON() toJSON() { return { tgi: [...this.tgi.entries()], ti: [...this.ti.entries()], t: [...this.t.entries()], }; } } // The hash functions we use for one, two or three values. const h = (t) => BigInt(t); const hh = (t, g) => BigInt(t) << 32n | BigInt(g); const hhh = (t, g, i) => BigInt(t) << 64n | BigInt(g) << 32n | BigInt(i); // # get(arr, dict, key) // Accessor function for easily reading values from our maps. function get(dict, key) { return dict.get(key) ?? []; } // # set(dict, key, value) function set(dict, key, value) { let arr = dict.get(key); if (arr) { arr.push(value); } else { dict.set(key, [value]); } }