[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const isWeakKey = thing => typeof thing === "object" && thing !== null;
|
---|
| 9 |
|
---|
| 10 | class WeakTupleNode {
|
---|
| 11 | constructor() {
|
---|
| 12 | this.f = 0;
|
---|
| 13 | /** @type {any} */
|
---|
| 14 | this.v = undefined;
|
---|
| 15 | /** @type {Map<object, WeakTupleNode> | undefined} */
|
---|
| 16 | this.m = undefined;
|
---|
| 17 | /** @type {WeakMap<object, WeakTupleNode> | undefined} */
|
---|
| 18 | this.w = undefined;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | getValue() {
|
---|
| 22 | return this.v;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | hasValue() {
|
---|
| 26 | return (this.f & 1) === 1;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | setValue(v) {
|
---|
| 30 | this.f |= 1;
|
---|
| 31 | this.v = v;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | deleteValue() {
|
---|
| 35 | this.f &= 6;
|
---|
| 36 | this.v = undefined;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | peek(thing) {
|
---|
| 40 | if (isWeakKey(thing)) {
|
---|
| 41 | if ((this.f & 4) !== 4) return undefined;
|
---|
| 42 | return this.w.get(thing);
|
---|
| 43 | } else {
|
---|
| 44 | if ((this.f & 2) !== 2) return undefined;
|
---|
| 45 | return this.m.get(thing);
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | get(thing) {
|
---|
| 50 | if (isWeakKey(thing)) {
|
---|
| 51 | if ((this.f & 4) !== 4) {
|
---|
| 52 | const newMap = new WeakMap();
|
---|
| 53 | this.f |= 4;
|
---|
| 54 | const newNode = new WeakTupleNode();
|
---|
| 55 | (this.w = newMap).set(thing, newNode);
|
---|
| 56 | return newNode;
|
---|
| 57 | }
|
---|
| 58 | const entry = this.w.get(thing);
|
---|
| 59 | if (entry !== undefined) {
|
---|
| 60 | return entry;
|
---|
| 61 | }
|
---|
| 62 | const newNode = new WeakTupleNode();
|
---|
| 63 | this.w.set(thing, newNode);
|
---|
| 64 | return newNode;
|
---|
| 65 | } else {
|
---|
| 66 | if ((this.f & 2) !== 2) {
|
---|
| 67 | const newMap = new Map();
|
---|
| 68 | this.f |= 2;
|
---|
| 69 | const newNode = new WeakTupleNode();
|
---|
| 70 | (this.m = newMap).set(thing, newNode);
|
---|
| 71 | return newNode;
|
---|
| 72 | }
|
---|
| 73 | const entry = this.m.get(thing);
|
---|
| 74 | if (entry !== undefined) {
|
---|
| 75 | return entry;
|
---|
| 76 | }
|
---|
| 77 | const newNode = new WeakTupleNode();
|
---|
| 78 | this.m.set(thing, newNode);
|
---|
| 79 | return newNode;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * @template {any[]} T
|
---|
| 86 | * @template V
|
---|
| 87 | */
|
---|
| 88 | class WeakTupleMap {
|
---|
| 89 | constructor() {
|
---|
| 90 | this._node = new WeakTupleNode();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * @param {[...T, V]} args tuple
|
---|
| 95 | * @returns {void}
|
---|
| 96 | */
|
---|
| 97 | set(...args) {
|
---|
| 98 | let node = this._node;
|
---|
| 99 | for (let i = 0; i < args.length - 1; i++) {
|
---|
| 100 | node = node.get(args[i]);
|
---|
| 101 | }
|
---|
| 102 | node.setValue(args[args.length - 1]);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * @param {T} args tuple
|
---|
| 107 | * @returns {boolean} true, if the tuple is in the Set
|
---|
| 108 | */
|
---|
| 109 | has(...args) {
|
---|
| 110 | let node = this._node;
|
---|
| 111 | for (let i = 0; i < args.length; i++) {
|
---|
| 112 | node = node.peek(args[i]);
|
---|
| 113 | if (node === undefined) return false;
|
---|
| 114 | }
|
---|
| 115 | return node.hasValue();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * @param {T} args tuple
|
---|
| 120 | * @returns {V} the value
|
---|
| 121 | */
|
---|
| 122 | get(...args) {
|
---|
| 123 | let node = this._node;
|
---|
| 124 | for (let i = 0; i < args.length; i++) {
|
---|
| 125 | node = node.peek(args[i]);
|
---|
| 126 | if (node === undefined) return undefined;
|
---|
| 127 | }
|
---|
| 128 | return node.getValue();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * @param {[...T, function(): V]} args tuple
|
---|
| 133 | * @returns {V} the value
|
---|
| 134 | */
|
---|
| 135 | provide(...args) {
|
---|
| 136 | let node = this._node;
|
---|
| 137 | for (let i = 0; i < args.length - 1; i++) {
|
---|
| 138 | node = node.get(args[i]);
|
---|
| 139 | }
|
---|
| 140 | if (node.hasValue()) return node.getValue();
|
---|
| 141 | const fn = args[args.length - 1];
|
---|
| 142 | const newValue = fn(...args.slice(0, -1));
|
---|
| 143 | node.setValue(newValue);
|
---|
| 144 | return newValue;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * @param {T} args tuple
|
---|
| 149 | * @returns {void}
|
---|
| 150 | */
|
---|
| 151 | delete(...args) {
|
---|
| 152 | let node = this._node;
|
---|
| 153 | for (let i = 0; i < args.length; i++) {
|
---|
| 154 | node = node.peek(args[i]);
|
---|
| 155 | if (node === undefined) return;
|
---|
| 156 | }
|
---|
| 157 | node.deleteValue();
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * @returns {void}
|
---|
| 162 | */
|
---|
| 163 | clear() {
|
---|
| 164 | this._node = new WeakTupleNode();
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | module.exports = WeakTupleMap;
|
---|