-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneToOneWeakMap.mts
203 lines (177 loc) · 5.55 KB
/
OneToOneWeakMap.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* @file
* This is generated code. Do not edit.
*
* Generator: https://github.com/ajvincent/composite-collection/
* Template: OneToOne/Map
* @license MPL-2.0
* @author Alexander J. Vincent <[email protected]>
* @copyright © 2021-2022 Alexander J. Vincent
*/
import WeakWeakMap from "./WeakWeakMap.mjs"
class OneToOneWeakMap<
__MK1__ extends object,
__V__ extends object
>
{
/** @constant */
#baseMap = new WeakWeakMap<object, __MK1__, __V__>();
/** @type {WeakMap<object, object>} @constant */
#weakValueToInternalKeyMap: WeakMap<__V__, object> = new WeakMap;
/**
* Bind two sets of keys and values together.
*
* @param {object} key2_1 The second key.
* @param {object} value_1 The value.
* @param {object} key2_2 The second key.
* @param {object} value_2 The value.
* @public
*/
bindOneToOne(key2_1: __MK1__, value_1: __V__, key2_2: __MK1__, value_2: __V__) : void
{
this.#requireValidKey("(key2_1)", key2_1);
this.#requireValidValue("value_1", value_1);
this.#requireValidKey("(key2_2)", key2_2);
this.#requireValidValue("value_2", value_2);
let key1 = this.#weakValueToInternalKeyMap.get(value_1);
const __otherWeakKey__ = this.#weakValueToInternalKeyMap.get(value_2);
if (!key1) {
key1 = __otherWeakKey__ || {};
}
else if (__otherWeakKey__ && (__otherWeakKey__ !== key1)) {
throw new Error("value_1 and value_2 are already in different one-to-one mappings!");
}
const __hasKeySet1__ = this.#baseMap.has(key1, key2_1);
const __hasKeySet2__ = this.#baseMap.has(key1, key2_2);
if (__hasKeySet1__ && (this.#baseMap.get(key1, key2_1) !== value_1))
throw new Error("value_1 mismatch!");
if (__hasKeySet2__ && (this.#baseMap.get(key1, key2_2) !== value_2))
throw new Error("value_2 mismatch!");
this.#weakValueToInternalKeyMap.set(value_1, key1);
this.#weakValueToInternalKeyMap.set(value_2, key1);
if (!__hasKeySet1__)
this.#baseMap.set(key1, key2_1, value_1);
if (!__hasKeySet2__)
this.#baseMap.set(key1, key2_2, value_2);
}
/**
* Delete a target value.
*
* @param {object} value The value.
* @param {object} key2 The second key.
* @returns {boolean} True if the target value was deleted.
* @public
*/
delete(value: __V__, key2: __MK1__) : boolean
{
const key1 = this.#weakValueToInternalKeyMap.get(value);
if (!key1)
return false;
const __target__ = this.#baseMap.get(key1, key2);
if (!__target__)
return false;
const __returnValue__ = this.#baseMap.delete(key1, key2);
if (__returnValue__)
this.#weakValueToInternalKeyMap.delete(__target__);
return __returnValue__;
}
/**
* Get a target value.
*
* @param {object} value The value.
* @param {object} key2 The second key.
* @returns {*} The target value.
* @public
*/
get(value: __V__, key2: __MK1__) : __V__ | undefined
{
const key1 = this.#weakValueToInternalKeyMap.get(value);
return key1 ? this.#baseMap.get(key1, key2) : undefined;
}
/**
* Determine if a target value exists.
*
* @param {object} value The value.
* @param {object} key2 The second key.
* @returns {boolean} True if the target value exists.
* @public
*/
has(value: __V__, key2: __MK1__) : boolean
{
const key1 = this.#weakValueToInternalKeyMap.get(value);
return key1 ? this.#baseMap.has(key1, key2) : false;
}
/**
* Determine if a target value is an identity in this map.
*
* @param {object} value The value.
* @param {object} key2 The second key.
* @param {boolean} allowNotDefined If true, treat the absence of the value as an identity.
* @returns {boolean} True if the target value exists.
* @public
*/
hasIdentity(value: __V__, key2: __MK1__, allowNotDefined: boolean) : boolean
{
const key1 = this.#weakValueToInternalKeyMap.get(value);
if (!key1) {
return Boolean(allowNotDefined);
}
return this.#baseMap.get(key1, key2) === value;
}
/**
* Determine if a key is valid.
*
* @param {object} key2 The second key.
* @returns {boolean} True if the key is valid.
* @see the base map class for further constraints.
* @public
*/
isValidKey(key2: __MK1__) : boolean
{
return this.#isValidKey(key2);
}
#isValidKey(key2: __MK1__) : boolean
{
const key1 = {};
return this.#baseMap.isValidKey(key1, key2);
}
/**
* Determine if a value is valid.
*
* @param {object} value The value.
* @returns {boolean} True if the value is valid.
* @see the base map class for further constraints.
* @public
*/
isValidValue(value: __V__) : boolean
{
return Object(value) === value;
}
#requireValidKey(__argNames__: string, key2: __MK1__) : void
{
if (!this.#isValidKey(key2))
throw new Error("Invalid key tuple: " + __argNames__);
}
#requireValidValue(argName: string, value: __V__) : void
{
if (!this.isValidValue(value))
throw new Error(argName + " is not a valid value!");
}
[Symbol.toStringTag] = "OneToOneWeakMap";
}
Object.freeze(OneToOneWeakMap);
Object.freeze(OneToOneWeakMap.prototype);
export type ReadonlyOneToOneWeakMap<
__MK1__ extends object,
__V__ extends object
> =
Pick<
OneToOneWeakMap<__MK1__, __V__>,
"get" | "has" | "hasIdentity" | "isValidKey" | "isValidValue"
>
export default OneToOneWeakMap;