semantic-typescript 0.3.8 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/map.d.ts ADDED
@@ -0,0 +1,76 @@
1
+ import { type BiConsumer, type BiFunctional, type Comparator, type MaybeInvalid, type MaybeUndefined, type Supplier, type TriConsumer } from "./utility";
2
+ export interface Entry<K, V> {
3
+ key: K;
4
+ value: V;
5
+ }
6
+ export interface SemanticMap<K, V> extends globalThis.Map<K, V> {
7
+ clear(): void;
8
+ compute(key: K, remapping: BiFunctional<K, MaybeInvalid<V>, MaybeInvalid<V>>): MaybeInvalid<V>;
9
+ computeIfAbsent(key: K, remapping: Supplier<V>): V;
10
+ computeIfPresent(key: K, remapping: BiFunctional<K, V, MaybeInvalid<V>>): MaybeInvalid<V>;
11
+ delete(key: K): boolean;
12
+ entries(): MapIterator<[K, V]>;
13
+ forEach(consumer: BiConsumer<V, K>): void;
14
+ forEach(consumer: TriConsumer<V, K, Map<K, V>>): void;
15
+ get(key: K): MaybeUndefined<V>;
16
+ get(key: K, defaultValue: V): V;
17
+ has(key: K): boolean;
18
+ replace(key: K, value: V): MaybeInvalid<V>;
19
+ replace(key: K, oldValue: V, newValue: V): boolean;
20
+ replaceAll(operator: BiFunctional<K, V, MaybeInvalid<V>>): void;
21
+ set(key: K, value: V): this;
22
+ size: number;
23
+ values(): IterableIterator<V>;
24
+ }
25
+ export declare abstract class AbstractSemanticMap<K, V> implements SemanticMap<K, V> {
26
+ protected internal: bigint;
27
+ size: number;
28
+ protected readonly SemanticMap: symbol;
29
+ constructor();
30
+ abstract clear(): void;
31
+ compute(key: K, remapping: BiFunctional<K, MaybeInvalid<V>, MaybeInvalid<V>>): MaybeInvalid<V>;
32
+ computeIfAbsent(key: K, remapping: Supplier<V>): V;
33
+ computeIfPresent(key: K, remapping: BiFunctional<K, V, MaybeInvalid<V>>): MaybeInvalid<V>;
34
+ abstract delete(key: K): boolean;
35
+ abstract entries(): MapIterator<[K, V]>;
36
+ forEach(consumer: BiConsumer<V, K>): void;
37
+ forEach(consumer: TriConsumer<V, K, Map<K, V>>): void;
38
+ abstract get(key: K): MaybeUndefined<V>;
39
+ abstract get(key: K, defaultValue: V): V;
40
+ abstract keys(): MapIterator<K>;
41
+ has(key: K): boolean;
42
+ replace(key: K, value: V): MaybeInvalid<V>;
43
+ replace(key: K, oldValue: V, newValue: V): boolean;
44
+ replaceAll(operator: BiFunctional<K, V, MaybeInvalid<V>>): void;
45
+ abstract set(key: K, value: V): this;
46
+ [Symbol.iterator](): IterableIterator<[K, V]>;
47
+ [Symbol.toStringTag]: string;
48
+ abstract values(): IterableIterator<V>;
49
+ }
50
+ export declare class HashMap<K, V> extends AbstractSemanticMap<K, V> {
51
+ protected buckets: Map<bigint, Array<Entry<K, V>>>;
52
+ protected threashold: number;
53
+ protected comparator: Comparator<K>;
54
+ protected capacity: number;
55
+ protected readonly HashMap: symbol;
56
+ constructor();
57
+ constructor(comparator: Comparator<K>);
58
+ constructor(threashold: number, initialCapacity: number);
59
+ constructor(comparator: Comparator<K>, threashold: number, capacity: number);
60
+ clear(): void;
61
+ delete(key: K): boolean;
62
+ entries(): MapIterator<[K, V]>;
63
+ has(key: K): boolean;
64
+ protected hash(key: K): bigint;
65
+ protected findEntry(key: K, hash: bigint): MaybeInvalid<Entry<K, V>>;
66
+ get(key: K): MaybeUndefined<V>;
67
+ get(key: K, defaultValue: V): V;
68
+ keys(): MapIterator<K>;
69
+ protected rehash(): void;
70
+ set(key: K, value: V): this;
71
+ values(): IterableIterator<V>;
72
+ }
73
+ export declare class TreeMap<K, V> {
74
+ key: K;
75
+ value: V;
76
+ }
package/dist/map.js ADDED
@@ -0,0 +1,253 @@
1
+ import { isFunction, isNumber } from "./guard";
2
+ import { useHash } from "./hash";
3
+ import { useCompare } from "./hook";
4
+ import { SemanticMapSymbol } from "./symbol";
5
+ import { invalidate, validate } from "./utility";
6
+ ;
7
+ ;
8
+ export class AbstractSemanticMap {
9
+ internal = 0n;
10
+ size = 0;
11
+ SemanticMap = SemanticMapSymbol;
12
+ constructor() {
13
+ Object.defineProperty(this, "SemanticMap", {
14
+ value: SemanticMapSymbol,
15
+ enumerable: false,
16
+ writable: false,
17
+ configurable: false
18
+ });
19
+ Object.freeze(this);
20
+ }
21
+ compute(key, remapping) {
22
+ let value = this.get(key);
23
+ return remapping(key, value);
24
+ }
25
+ computeIfAbsent(key, remapping) {
26
+ let value = this.get(key);
27
+ if (invalidate(value)) {
28
+ return remapping();
29
+ }
30
+ return value;
31
+ }
32
+ computeIfPresent(key, remapping) {
33
+ let value = this.get(key);
34
+ if (validate(value)) {
35
+ return remapping(key, value);
36
+ }
37
+ return value;
38
+ }
39
+ forEach(consumer) {
40
+ for (let entry of this.entries()) {
41
+ consumer(entry[1], entry[0], this);
42
+ }
43
+ }
44
+ has(key) {
45
+ return this.get(key) !== undefined;
46
+ }
47
+ replace(argument1, argument2, argument3) {
48
+ if (validate(argument1) && validate(argument2) && validate(argument3)) {
49
+ let key = argument1;
50
+ let oldValue = argument2;
51
+ let newValue = argument3;
52
+ if (this.get(key) === oldValue) {
53
+ this.set(key, newValue);
54
+ return true;
55
+ }
56
+ return this.get(key);
57
+ }
58
+ if (validate(argument1) && validate(argument2) && invalidate(argument3)) {
59
+ let key = argument1;
60
+ let value = argument2;
61
+ if (this.get(key) === value) {
62
+ this.delete(key);
63
+ return true;
64
+ }
65
+ return this.get(key);
66
+ }
67
+ }
68
+ replaceAll(operator) {
69
+ for (let entry of this.entries()) {
70
+ let key = entry[0];
71
+ let value = entry[1];
72
+ let newValue = operator(key, value);
73
+ if (validate(newValue)) {
74
+ this.set(key, newValue);
75
+ }
76
+ else {
77
+ this.delete(key);
78
+ }
79
+ }
80
+ }
81
+ [Symbol.iterator]() {
82
+ return this.entries();
83
+ }
84
+ [Symbol.toStringTag] = "SemanticMap";
85
+ }
86
+ ;
87
+ Object.freeze(AbstractSemanticMap);
88
+ Object.freeze(AbstractSemanticMap.prototype);
89
+ Object.freeze(Object.getPrototypeOf(AbstractSemanticMap));
90
+ export class HashMap extends AbstractSemanticMap {
91
+ buckets = new Map();
92
+ threashold;
93
+ comparator;
94
+ capacity;
95
+ HashMap = SemanticMapSymbol;
96
+ constructor(argument1, argument2, argument3) {
97
+ super();
98
+ if (isFunction(argument1) && isNumber(argument2) && isNumber(argument3)) {
99
+ this.comparator = argument1;
100
+ this.threashold = argument2;
101
+ this.capacity = argument3;
102
+ }
103
+ else if (isFunction(argument1) && isNumber(argument2) && invalidate(argument3)) {
104
+ this.comparator = argument1;
105
+ this.threashold = argument2;
106
+ this.capacity = 16;
107
+ }
108
+ else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
109
+ this.comparator = argument1;
110
+ this.threashold = 0.75;
111
+ this.capacity = 16;
112
+ }
113
+ else {
114
+ this.comparator = useCompare;
115
+ this.threashold = 0.75;
116
+ this.capacity = 16;
117
+ }
118
+ this.buckets = new Map();
119
+ Object.defineProperty(this, "size", {
120
+ get: () => this.internal,
121
+ set: (value) => this.internal = isNumber(value) ? BigInt(value) : 0n,
122
+ enumerable: true,
123
+ configurable: false
124
+ });
125
+ Object.defineProperty(this, "HashMap", {
126
+ value: SemanticMapSymbol,
127
+ enumerable: false,
128
+ writable: false,
129
+ configurable: false
130
+ });
131
+ Object.freeze(this);
132
+ }
133
+ clear() {
134
+ this.buckets.clear();
135
+ this.size = 0;
136
+ }
137
+ delete(key) {
138
+ let hashCode = this.hash(key);
139
+ let bucket = this.buckets.get(hashCode) || [];
140
+ if (bucket.length > 0) {
141
+ for (let index = 0; index < bucket.length; index++) {
142
+ let entry = bucket[index];
143
+ if (this.comparator(entry.key, key) === 0) {
144
+ bucket.splice(index, 1);
145
+ this.size--;
146
+ if (bucket.length === 0) {
147
+ this.buckets.delete(hashCode);
148
+ }
149
+ return true;
150
+ }
151
+ }
152
+ }
153
+ this.buckets.delete(hashCode);
154
+ return false;
155
+ }
156
+ *entries() {
157
+ for (let bucket of this.buckets.values()) {
158
+ for (let entry of bucket) {
159
+ yield [entry.key, entry.value];
160
+ }
161
+ }
162
+ }
163
+ has(key) {
164
+ let hashCode = this.hash(key);
165
+ return validate(this.findEntry(key, hashCode));
166
+ }
167
+ hash(key) {
168
+ return useHash(key);
169
+ }
170
+ ;
171
+ findEntry(key, hash) {
172
+ let candidates = (this.buckets.get(hash) || []);
173
+ for (let candidate of candidates) {
174
+ if (this.comparator(candidate.key, key) === 0) {
175
+ return candidate;
176
+ }
177
+ }
178
+ return (void 0);
179
+ }
180
+ get(argument1, argument2) {
181
+ if (validate(argument1) && validate(argument2)) {
182
+ let key = argument1;
183
+ let defaultValue = argument2;
184
+ let hashCode = this.hash(key);
185
+ let entry = this.findEntry(key, hashCode);
186
+ if (validate(entry)) {
187
+ return entry.value;
188
+ }
189
+ return defaultValue;
190
+ }
191
+ let key = argument1;
192
+ let hashCode = this.hash(key);
193
+ let entry = this.findEntry(key, hashCode);
194
+ if (validate(entry)) {
195
+ return entry.value;
196
+ }
197
+ return (void 0);
198
+ }
199
+ *keys() {
200
+ for (let bucket of this.buckets.values()) {
201
+ for (let entry of bucket) {
202
+ yield entry.key;
203
+ }
204
+ }
205
+ }
206
+ rehash() {
207
+ let oldBuckets = this.buckets;
208
+ this.buckets = new Map();
209
+ this.size = 0;
210
+ for (let bucket of oldBuckets.values()) {
211
+ for (let entry of bucket) {
212
+ let hashCode = this.hash(entry.key);
213
+ let bucket = this.buckets.get(hashCode) || [];
214
+ bucket.push(entry);
215
+ this.size++;
216
+ }
217
+ }
218
+ }
219
+ set(key, value) {
220
+ let hashCode = this.hash(key);
221
+ let bucket = this.buckets.get(hashCode) || [];
222
+ let found = this.findEntry(key, hashCode);
223
+ if (validate(found)) {
224
+ found.value = value;
225
+ }
226
+ else {
227
+ bucket.push({
228
+ key: key,
229
+ value: value
230
+ });
231
+ this.size++;
232
+ if (this.size > this.capacity * this.threashold) {
233
+ this.rehash();
234
+ }
235
+ }
236
+ return this;
237
+ }
238
+ *values() {
239
+ for (let bucket of this.buckets.values()) {
240
+ for (let entry of bucket) {
241
+ yield entry.value;
242
+ }
243
+ }
244
+ }
245
+ }
246
+ ;
247
+ Object.freeze(HashMap);
248
+ Object.freeze(HashMap.prototype);
249
+ Object.freeze(Object.getPrototypeOf(HashMap));
250
+ export class TreeMap {
251
+ key = (void 0);
252
+ value = (void 0);
253
+ }
package/dist/node.d.ts ADDED
@@ -0,0 +1,182 @@
1
+ import { Optional } from "./optional";
2
+ import { type Comparator } from "./utility";
3
+ export interface Node<E, N extends Node<E, N>> extends Iterable<N> {
4
+ ancestors(): Iterable<N>;
5
+ children(): Iterable<N>;
6
+ descendants(): Iterable<N>;
7
+ siblings(): Iterable<N>;
8
+ parent(): Optional<N>;
9
+ root(): N;
10
+ previousSibling(): Optional<N>;
11
+ nextSibling(): Optional<N>;
12
+ preorder(): Generator<N>;
13
+ inorder(): Generator<N>;
14
+ postorder(): Generator<N>;
15
+ breadthfirst(): Generator<N>;
16
+ depth(): bigint;
17
+ height(): bigint;
18
+ width(): bigint;
19
+ level(): bigint;
20
+ isLeaf(): boolean;
21
+ isRoot(): boolean;
22
+ [Symbol.iterator](): Iterator<N>;
23
+ }
24
+ export declare abstract class AbstractNode<E, N extends Node<E, N>> implements Node<E, N> {
25
+ protected readonly Node: symbol;
26
+ constructor();
27
+ ancestors(): Iterable<N>;
28
+ abstract children(): Iterable<N>;
29
+ descendants(): Iterable<N>;
30
+ root(): N;
31
+ siblings(): Iterable<N>;
32
+ abstract parent(): Optional<N>;
33
+ previousSibling(): Optional<N>;
34
+ nextSibling(): Optional<N>;
35
+ preorder(): Generator<N>;
36
+ inorder(): Generator<N>;
37
+ postorder(): Generator<N>;
38
+ breadthfirst(): Generator<N>;
39
+ [Symbol.iterator](): Iterator<N>;
40
+ depth(): bigint;
41
+ height(): bigint;
42
+ width(): bigint;
43
+ level(): bigint;
44
+ isLeaf(): boolean;
45
+ isRoot(): boolean;
46
+ }
47
+ export declare class LinearNode<E> extends AbstractNode<E, LinearNode<E>> {
48
+ protected previous: Optional<LinearNode<E>>;
49
+ protected next: Optional<LinearNode<E>>;
50
+ protected element: Optional<E>;
51
+ protected readonly LinearNode: symbol;
52
+ constructor(element: E);
53
+ constructor(element: E, previous: Optional<LinearNode<E>>, next: Optional<LinearNode<E>>);
54
+ ancestors(): Iterable<LinearNode<E>>;
55
+ descendants(): Iterable<LinearNode<E>>;
56
+ root(): LinearNode<E>;
57
+ siblings(): Iterable<LinearNode<E>>;
58
+ children(): Iterable<LinearNode<E>>;
59
+ parent(): Optional<LinearNode<E>>;
60
+ [Symbol.iterator](): Iterator<LinearNode<E>>;
61
+ getElement(): Optional<E>;
62
+ setElement(element: E): void;
63
+ getPrevious(): Optional<LinearNode<E>>;
64
+ setPrevious(previous: LinearNode<E>): void;
65
+ setPrevious(previous: Optional<LinearNode<E>>): void;
66
+ getNext(): Optional<LinearNode<E>>;
67
+ setNext(next: LinearNode<E>): void;
68
+ setNext(next: Optional<LinearNode<E>>): void;
69
+ linkPrevious(previous: LinearNode<E>): void;
70
+ unlinkPrevious(): void;
71
+ linkNext(next: LinearNode<E>): void;
72
+ unlinkNext(): void;
73
+ }
74
+ export declare abstract class BinaryNode<E, N extends BinaryNode<E, N>> extends AbstractNode<E, N> {
75
+ BinaryNode: symbol;
76
+ protected ancestor: Optional<N>;
77
+ protected left: Optional<N>;
78
+ protected right: Optional<N>;
79
+ protected element: Optional<E>;
80
+ constructor(element: E);
81
+ constructor(element: E, ancestor: Optional<N>, left: Optional<N>, right: Optional<N>);
82
+ ancestors(): Iterable<N>;
83
+ compare(other: N): bigint;
84
+ compare(other: N, comparator: Comparator<E>): bigint;
85
+ descendants(): Iterable<N>;
86
+ abstract identity(): N;
87
+ root(): N;
88
+ siblings(): Iterable<N>;
89
+ children(): Iterable<N>;
90
+ parent(): Optional<N>;
91
+ [Symbol.iterator](): Iterator<N>;
92
+ preorder(): Generator<N>;
93
+ inorder(): Generator<N>;
94
+ postorder(): Generator<N>;
95
+ breadthfirst(): Generator<N>;
96
+ getElement(): Optional<E>;
97
+ setElement(element: E): void;
98
+ getLeft(): Optional<N>;
99
+ setLeft(left: N): void;
100
+ setLeft(left: Optional<N>): void;
101
+ getRight(): Optional<N>;
102
+ setRight(right: N): void;
103
+ setRight(right: Optional<N>): void;
104
+ getAncestor(): Optional<N>;
105
+ setAncestor(ancestor: N): void;
106
+ setAncestor(ancestor: Optional<N>): void;
107
+ linkLeft(left: N): void;
108
+ unlinkLeft(): Optional<N>;
109
+ linkRight(right: N): void;
110
+ unlinkRight(): Optional<N>;
111
+ isLeftChild(): boolean;
112
+ isRightChild(): boolean;
113
+ detach(): void;
114
+ isBanlanced(): boolean;
115
+ isFull(): boolean;
116
+ isComplete(): boolean;
117
+ isPerfect(): boolean;
118
+ invert(): void;
119
+ invert(deep: boolean): void;
120
+ }
121
+ type Color = "RED" | "BLACK";
122
+ export declare class RedBlackNode<E> extends BinaryNode<E, RedBlackNode<E>> {
123
+ protected readonly RedBlackNode: symbol;
124
+ protected color: Color;
125
+ constructor(element: E);
126
+ constructor(element: E, color: Color);
127
+ constructor(element: E, color: Color, ancestor: Optional<RedBlackNode<E>>, left: Optional<RedBlackNode<E>>, right: Optional<RedBlackNode<E>>);
128
+ identity(): RedBlackNode<E>;
129
+ getElement(): Optional<E>;
130
+ setElement(element: E): void;
131
+ getLeft(): Optional<RedBlackNode<E>>;
132
+ getRight(): Optional<RedBlackNode<E>>;
133
+ getColor(): Color;
134
+ setColor(color: Color): void;
135
+ isRed(): boolean;
136
+ isBlack(): boolean;
137
+ turnRed(): void;
138
+ turnBlack(): void;
139
+ toggle(): void;
140
+ compareAndLink(other: RedBlackNode<E>): void;
141
+ compareAndLink(other: RedBlackNode<E>, comparator: Comparator<E>): void;
142
+ rotate(): void;
143
+ rotateLeft(): RedBlackNode<E>;
144
+ rotateRight(): RedBlackNode<E>;
145
+ fix(): void;
146
+ uncle(): Optional<RedBlackNode<E>>;
147
+ findMinimum(): RedBlackNode<E>;
148
+ findMaximum(): RedBlackNode<E>;
149
+ }
150
+ export declare class AverageLevelNode<E> extends BinaryNode<E, AverageLevelNode<E>> {
151
+ constructor(element: E);
152
+ constructor(element: E, ancestor: Optional<AverageLevelNode<E>>, left: Optional<AverageLevelNode<E>>, right: Optional<AverageLevelNode<E>>);
153
+ [Symbol.iterator](): Iterator<AverageLevelNode<E>>;
154
+ identity(): AverageLevelNode<E>;
155
+ getElement(): Optional<E>;
156
+ setElement(element: E): void;
157
+ getLeft(): Optional<AverageLevelNode<E>>;
158
+ getRight(): Optional<AverageLevelNode<E>>;
159
+ }
160
+ export declare class BinarySearchNode<E> extends AbstractNode<E, BinarySearchNode<E>> {
161
+ protected left: Optional<BinarySearchNode<E>>;
162
+ protected right: Optional<BinarySearchNode<E>>;
163
+ protected element: Optional<E>;
164
+ constructor(element: E);
165
+ constructor(element: E, left: Optional<BinarySearchNode<E>>, right: Optional<BinarySearchNode<E>>);
166
+ ancestors(): Iterable<BinarySearchNode<E>>;
167
+ descendants(): Iterable<BinarySearchNode<E>>;
168
+ root(): BinarySearchNode<E>;
169
+ siblings(): Iterable<BinarySearchNode<E>>;
170
+ children(): Iterable<BinarySearchNode<E>>;
171
+ parent(): Optional<BinarySearchNode<E>>;
172
+ [Symbol.iterator](): Iterator<BinarySearchNode<E>>;
173
+ inorder(): Generator<BinarySearchNode<E>>;
174
+ preorder(): Generator<BinarySearchNode<E>>;
175
+ postorder(): Generator<BinarySearchNode<E>>;
176
+ breadthfirst(): Generator<BinarySearchNode<E>>;
177
+ getElement(): Optional<E>;
178
+ setElement(element: E): void;
179
+ getLeft(): Optional<BinarySearchNode<E>>;
180
+ getRight(): Optional<BinarySearchNode<E>>;
181
+ }
182
+ export {};