| 1 | export class Set<T extends AnyNotNil> { |
| 2 | public static [Symbol.species] = Set; |
| 3 | public [Symbol.toStringTag] = "Set"; |
| 4 | |
| 5 | public size = 0; |
| 6 | |
| 7 | private firstKey: T | undefined; |
| 8 | private lastKey: T | undefined; |
| 9 | private nextKey = new LuaTable<T, T>(); |
| 10 | private previousKey = new LuaTable<T, T>(); |
| 11 | |
| 12 | constructor(values?: Iterable<T> | T[]) { |
| 13 | if (values === undefined) return; |
| 14 | |
| 15 | const iterable = values as Iterable<T>; |
| 16 | if (iterable[Symbol.iterator]) { |
| 17 | // Iterate manually because Set is compiled with ES5 which doesn't support Iterables in for...of |
| 18 | const iterator = iterable[Symbol.iterator](); |
| 19 | while (true) { |
| 20 | const result = iterator.next(); |
| 21 | if (result.done) { |
| 22 | break; |
| 23 | } |
| 24 | this.add(result.value); |
| 25 | } |
| 26 | } else { |
| 27 | const array = values as T[]; |
| 28 | for (const value of array) { |
| 29 | this.add(value); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public add(value: T): Set<T> { |
| 35 | const isNewValue = !this.has(value); |
| 36 | if (isNewValue) { |
| 37 | this.size++; |
| 38 | } |
| 39 | |
| 40 | // Do order bookkeeping |
| 41 | if (this.firstKey === undefined) { |
| 42 | this.firstKey = value; |
| 43 | this.lastKey = value; |
| 44 | } else if (isNewValue) { |
| 45 | this.nextKey.set(this.lastKey!, value); |
| 46 | this.previousKey.set(value, this.lastKey!); |
| 47 | this.lastKey = value; |
| 48 | } |
| 49 | |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | public clear(): void { |
| 54 | this.nextKey = new LuaTable(); |
| 55 | this.previousKey = new LuaTable(); |
| 56 | this.firstKey = undefined; |
| 57 | this.lastKey = undefined; |
| 58 | this.size = 0; |
| 59 | } |
| 60 |
nothing calls this directly
no outgoing calls
no test coverage detected