-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneToOneStrongMap.mjs
156 lines (156 loc) · 5.69 KB
/
OneToOneStrongMap.mjs
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
/*
* 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 WeakStrongMap from "./WeakStrongMap.mjs";
class OneToOneStrongMap {
/** @constant */
#baseMap = new WeakStrongMap();
/** @type {WeakMap<object, object>} @constant */
#weakValueToInternalKeyMap = new WeakMap;
/**
* Bind two sets of keys and values together.
*
* @param {*} strongKey_1 The strongly held key.
* @param {object} value_1 The value.
* @param {*} strongKey_2 The strongly held key.
* @param {object} value_2 The value.
* @public
*/
bindOneToOne(strongKey_1, value_1, strongKey_2, value_2) {
this.#requireValidKey("(strongKey_1)", strongKey_1);
this.#requireValidValue("value_1", value_1);
this.#requireValidKey("(strongKey_2)", strongKey_2);
this.#requireValidValue("value_2", value_2);
let weakKey = this.#weakValueToInternalKeyMap.get(value_1);
const __otherWeakKey__ = this.#weakValueToInternalKeyMap.get(value_2);
if (!weakKey) {
weakKey = __otherWeakKey__ || {};
}
else if (__otherWeakKey__ && (__otherWeakKey__ !== weakKey)) {
throw new Error("value_1 and value_2 are already in different one-to-one mappings!");
}
const __hasKeySet1__ = this.#baseMap.has(weakKey, strongKey_1);
const __hasKeySet2__ = this.#baseMap.has(weakKey, strongKey_2);
if (__hasKeySet1__ && (this.#baseMap.get(weakKey, strongKey_1) !== value_1))
throw new Error("value_1 mismatch!");
if (__hasKeySet2__ && (this.#baseMap.get(weakKey, strongKey_2) !== value_2))
throw new Error("value_2 mismatch!");
this.#weakValueToInternalKeyMap.set(value_1, weakKey);
this.#weakValueToInternalKeyMap.set(value_2, weakKey);
if (!__hasKeySet1__)
this.#baseMap.set(weakKey, strongKey_1, value_1);
if (!__hasKeySet2__)
this.#baseMap.set(weakKey, strongKey_2, value_2);
}
/**
* Delete a target value.
*
* @param {object} value The value.
* @param {*} strongKey The strongly held key.
* @returns {boolean} True if the target value was deleted.
* @public
*/
delete(value, strongKey) {
const weakKey = this.#weakValueToInternalKeyMap.get(value);
if (!weakKey)
return false;
const __target__ = this.#baseMap.get(weakKey, strongKey);
if (!__target__)
return false;
const __returnValue__ = this.#baseMap.delete(weakKey, strongKey);
if (__returnValue__)
this.#weakValueToInternalKeyMap.delete(__target__);
return __returnValue__;
}
/**
* Get a target value.
*
* @param {object} value The value.
* @param {*} strongKey The strongly held key.
* @returns {*} The target value.
* @public
*/
get(value, strongKey) {
const weakKey = this.#weakValueToInternalKeyMap.get(value);
return weakKey ? this.#baseMap.get(weakKey, strongKey) : undefined;
}
/**
* Determine if a target value exists.
*
* @param {object} value The value.
* @param {*} strongKey The strongly held key.
* @returns {boolean} True if the target value exists.
* @public
*/
has(value, strongKey) {
const weakKey = this.#weakValueToInternalKeyMap.get(value);
return weakKey ? this.#baseMap.has(weakKey, strongKey) : false;
}
/**
* Determine if a target value is an identity in this map.
*
* @param {object} value The value.
* @param {*} strongKey The strongly held 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, strongKey, allowNotDefined) {
const weakKey = this.#weakValueToInternalKeyMap.get(value);
if (!weakKey) {
return Boolean(allowNotDefined);
}
return this.#baseMap.get(weakKey, strongKey) === value;
}
/**
* Determine if a key is valid.
*
* @param {*} strongKey The strongly held key.
* @returns {boolean} True if the key is valid.
* @see the base map class for further constraints.
* @public
*/
isValidKey(strongKey) {
return this.#isValidKey(strongKey);
}
#isValidKey(strongKey) {
const weakKey = {};
return this.#baseMap.isValidKey(weakKey, strongKey);
}
/**
* 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) {
return Object(value) === value;
}
#requireValidKey(__argNames__, strongKey) {
if (!this.#isValidKey(strongKey))
throw new Error("Invalid key tuple: " + __argNames__);
}
#requireValidValue(argName, value) {
if (!this.isValidValue(value))
throw new Error(argName + " is not a valid value!");
}
[Symbol.toStringTag] = "OneToOneStrongMap";
}
Object.freeze(OneToOneStrongMap);
Object.freeze(OneToOneStrongMap.prototype);
export default OneToOneStrongMap;
//# sourceMappingURL=OneToOneStrongMap.mjs.map