[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * @template {any[]} T
|
---|
| 10 | */
|
---|
| 11 | class TupleSet {
|
---|
| 12 | /**
|
---|
| 13 | * @param {Iterable<T>=} init init
|
---|
| 14 | */
|
---|
| 15 | constructor(init) {
|
---|
| 16 | /** @type {Map<T, TODO>} */
|
---|
| 17 | this._map = new Map();
|
---|
| 18 | this.size = 0;
|
---|
| 19 | if (init) {
|
---|
| 20 | for (const tuple of init) {
|
---|
| 21 | this.add(...tuple);
|
---|
| 22 | }
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * @param {T} args tuple
|
---|
| 28 | * @returns {void}
|
---|
| 29 | */
|
---|
| 30 | add(...args) {
|
---|
| 31 | let map = this._map;
|
---|
| 32 | for (let i = 0; i < args.length - 2; i++) {
|
---|
| 33 | const arg = args[i];
|
---|
| 34 | const innerMap = map.get(arg);
|
---|
| 35 | if (innerMap === undefined) {
|
---|
| 36 | map.set(arg, (map = new Map()));
|
---|
| 37 | } else {
|
---|
| 38 | map = innerMap;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | const beforeLast = args[args.length - 2];
|
---|
| 43 | let set = map.get(beforeLast);
|
---|
| 44 | if (set === undefined) {
|
---|
| 45 | map.set(beforeLast, (set = new Set()));
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | const last = args[args.length - 1];
|
---|
| 49 | this.size -= set.size;
|
---|
| 50 | set.add(last);
|
---|
| 51 | this.size += set.size;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * @param {T} args tuple
|
---|
| 56 | * @returns {boolean} true, if the tuple is in the Set
|
---|
| 57 | */
|
---|
| 58 | has(...args) {
|
---|
| 59 | let map = this._map;
|
---|
| 60 | for (let i = 0; i < args.length - 2; i++) {
|
---|
| 61 | const arg = args[i];
|
---|
| 62 | map = map.get(arg);
|
---|
| 63 | if (map === undefined) {
|
---|
| 64 | return false;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | const beforeLast = args[args.length - 2];
|
---|
| 69 | const set = map.get(beforeLast);
|
---|
| 70 | if (set === undefined) {
|
---|
| 71 | return false;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | const last = args[args.length - 1];
|
---|
| 75 | return set.has(last);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * @param {T} args tuple
|
---|
| 80 | * @returns {void}
|
---|
| 81 | */
|
---|
| 82 | delete(...args) {
|
---|
| 83 | let map = this._map;
|
---|
| 84 | for (let i = 0; i < args.length - 2; i++) {
|
---|
| 85 | const arg = args[i];
|
---|
| 86 | map = map.get(arg);
|
---|
| 87 | if (map === undefined) {
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | const beforeLast = args[args.length - 2];
|
---|
| 93 | const set = map.get(beforeLast);
|
---|
| 94 | if (set === undefined) {
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | const last = args[args.length - 1];
|
---|
| 99 | this.size -= set.size;
|
---|
| 100 | set.delete(last);
|
---|
| 101 | this.size += set.size;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * @returns {Iterator<T>} iterator
|
---|
| 106 | */
|
---|
| 107 | [Symbol.iterator]() {
|
---|
| 108 | /** @type {TODO[]} */
|
---|
| 109 | const iteratorStack = [];
|
---|
| 110 | /** @type {T[]} */
|
---|
| 111 | const tuple = [];
|
---|
| 112 | /** @type {Iterator<T> | undefined} */
|
---|
| 113 | let currentSetIterator;
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | * @param {TODO} it iterator
|
---|
| 117 | * @returns {boolean} result
|
---|
| 118 | */
|
---|
| 119 | const next = it => {
|
---|
| 120 | const result = it.next();
|
---|
| 121 | if (result.done) {
|
---|
| 122 | if (iteratorStack.length === 0) return false;
|
---|
| 123 | tuple.pop();
|
---|
| 124 | return next(iteratorStack.pop());
|
---|
| 125 | }
|
---|
| 126 | const [key, value] = result.value;
|
---|
| 127 | iteratorStack.push(it);
|
---|
| 128 | tuple.push(key);
|
---|
| 129 | if (value instanceof Set) {
|
---|
| 130 | currentSetIterator = value[Symbol.iterator]();
|
---|
| 131 | return true;
|
---|
| 132 | }
|
---|
| 133 | return next(value[Symbol.iterator]());
|
---|
| 134 | };
|
---|
| 135 |
|
---|
| 136 | next(this._map[Symbol.iterator]());
|
---|
| 137 |
|
---|
| 138 | return {
|
---|
| 139 | next() {
|
---|
| 140 | while (currentSetIterator) {
|
---|
| 141 | const result = currentSetIterator.next();
|
---|
| 142 | if (result.done) {
|
---|
| 143 | tuple.pop();
|
---|
| 144 | if (!next(iteratorStack.pop())) {
|
---|
| 145 | currentSetIterator = undefined;
|
---|
| 146 | }
|
---|
| 147 | } else {
|
---|
| 148 | return {
|
---|
| 149 | done: false,
|
---|
| 150 | value: /** @type {T} */ (tuple.concat(result.value))
|
---|
| 151 | };
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | return { done: true, value: undefined };
|
---|
| 155 | }
|
---|
| 156 | };
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | module.exports = TupleSet;
|
---|