semantic-typescript 0.3.8 → 0.4.1

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,72 @@
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
+ }
package/dist/map.js ADDED
@@ -0,0 +1,247 @@
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
+ }
20
+ compute(key, remapping) {
21
+ let value = this.get(key);
22
+ return remapping(key, value);
23
+ }
24
+ computeIfAbsent(key, remapping) {
25
+ let value = this.get(key);
26
+ if (invalidate(value)) {
27
+ return remapping();
28
+ }
29
+ return value;
30
+ }
31
+ computeIfPresent(key, remapping) {
32
+ let value = this.get(key);
33
+ if (validate(value)) {
34
+ return remapping(key, value);
35
+ }
36
+ return value;
37
+ }
38
+ forEach(consumer) {
39
+ for (let entry of this.entries()) {
40
+ consumer(entry[1], entry[0], this);
41
+ }
42
+ }
43
+ has(key) {
44
+ return this.get(key) !== undefined;
45
+ }
46
+ replace(argument1, argument2, argument3) {
47
+ if (validate(argument1) && validate(argument2) && validate(argument3)) {
48
+ let key = argument1;
49
+ let oldValue = argument2;
50
+ let newValue = argument3;
51
+ if (this.get(key) === oldValue) {
52
+ this.set(key, newValue);
53
+ return true;
54
+ }
55
+ return this.get(key);
56
+ }
57
+ if (validate(argument1) && validate(argument2) && invalidate(argument3)) {
58
+ let key = argument1;
59
+ let value = argument2;
60
+ if (this.get(key) === value) {
61
+ this.delete(key);
62
+ return true;
63
+ }
64
+ return this.get(key);
65
+ }
66
+ }
67
+ replaceAll(operator) {
68
+ for (let entry of this.entries()) {
69
+ let key = entry[0];
70
+ let value = entry[1];
71
+ let newValue = operator(key, value);
72
+ if (validate(newValue)) {
73
+ this.set(key, newValue);
74
+ }
75
+ else {
76
+ this.delete(key);
77
+ }
78
+ }
79
+ }
80
+ [Symbol.iterator]() {
81
+ return this.entries();
82
+ }
83
+ [Symbol.toStringTag] = "SemanticMap";
84
+ }
85
+ ;
86
+ Object.freeze(AbstractSemanticMap);
87
+ Object.freeze(AbstractSemanticMap.prototype);
88
+ Object.freeze(Object.getPrototypeOf(AbstractSemanticMap));
89
+ export class HashMap extends AbstractSemanticMap {
90
+ buckets = new Map();
91
+ threashold;
92
+ comparator;
93
+ capacity;
94
+ HashMap = SemanticMapSymbol;
95
+ constructor(argument1, argument2, argument3) {
96
+ super();
97
+ if (isFunction(argument1) && isNumber(argument2) && isNumber(argument3)) {
98
+ this.comparator = argument1;
99
+ this.threashold = argument2;
100
+ this.capacity = argument3;
101
+ }
102
+ else if (isFunction(argument1) && isNumber(argument2) && invalidate(argument3)) {
103
+ this.comparator = argument1;
104
+ this.threashold = argument2;
105
+ this.capacity = 16;
106
+ }
107
+ else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
108
+ this.comparator = argument1;
109
+ this.threashold = 0.75;
110
+ this.capacity = 16;
111
+ }
112
+ else {
113
+ this.comparator = useCompare;
114
+ this.threashold = 0.75;
115
+ this.capacity = 16;
116
+ }
117
+ this.buckets = new Map();
118
+ Object.defineProperty(this, "size", {
119
+ get: () => this.internal,
120
+ set: (value) => this.internal = isNumber(value) ? BigInt(value) : 0n,
121
+ enumerable: true,
122
+ configurable: false
123
+ });
124
+ Object.defineProperty(this, "HashMap", {
125
+ value: SemanticMapSymbol,
126
+ enumerable: false,
127
+ writable: false,
128
+ configurable: false
129
+ });
130
+ }
131
+ clear() {
132
+ this.buckets.clear();
133
+ this.size = 0;
134
+ }
135
+ delete(key) {
136
+ let hashCode = this.hash(key);
137
+ let bucket = this.buckets.get(hashCode) || [];
138
+ if (bucket.length > 0) {
139
+ for (let index = 0; index < bucket.length; index++) {
140
+ let entry = bucket[index];
141
+ if (this.comparator(entry.key, key) === 0) {
142
+ bucket.splice(index, 1);
143
+ this.size--;
144
+ if (bucket.length === 0) {
145
+ this.buckets.delete(hashCode);
146
+ }
147
+ return true;
148
+ }
149
+ }
150
+ }
151
+ this.buckets.delete(hashCode);
152
+ return false;
153
+ }
154
+ *entries() {
155
+ for (let bucket of this.buckets.values()) {
156
+ for (let entry of bucket) {
157
+ yield [entry.key, entry.value];
158
+ }
159
+ }
160
+ }
161
+ has(key) {
162
+ let hashCode = this.hash(key);
163
+ return validate(this.findEntry(key, hashCode));
164
+ }
165
+ hash(key) {
166
+ return useHash(key);
167
+ }
168
+ ;
169
+ findEntry(key, hash) {
170
+ let candidates = (this.buckets.get(hash) || []);
171
+ for (let candidate of candidates) {
172
+ if (this.comparator(candidate.key, key) === 0) {
173
+ return candidate;
174
+ }
175
+ }
176
+ return (void 0);
177
+ }
178
+ get(argument1, argument2) {
179
+ if (validate(argument1) && validate(argument2)) {
180
+ let key = argument1;
181
+ let defaultValue = argument2;
182
+ let hashCode = this.hash(key);
183
+ let entry = this.findEntry(key, hashCode);
184
+ if (validate(entry)) {
185
+ return entry.value;
186
+ }
187
+ return defaultValue;
188
+ }
189
+ let key = argument1;
190
+ let hashCode = this.hash(key);
191
+ let entry = this.findEntry(key, hashCode);
192
+ if (validate(entry)) {
193
+ return entry.value;
194
+ }
195
+ return (void 0);
196
+ }
197
+ *keys() {
198
+ for (let bucket of this.buckets.values()) {
199
+ for (let entry of bucket) {
200
+ yield entry.key;
201
+ }
202
+ }
203
+ }
204
+ rehash() {
205
+ let oldBuckets = this.buckets;
206
+ this.buckets = new Map();
207
+ this.size = 0;
208
+ for (let bucket of oldBuckets.values()) {
209
+ for (let entry of bucket) {
210
+ let hashCode = this.hash(entry.key);
211
+ let bucket = this.buckets.get(hashCode) || [];
212
+ bucket.push(entry);
213
+ this.size++;
214
+ }
215
+ }
216
+ }
217
+ set(key, value) {
218
+ let hashCode = this.hash(key);
219
+ let bucket = this.buckets.get(hashCode) || [];
220
+ let found = this.findEntry(key, hashCode);
221
+ if (validate(found)) {
222
+ found.value = value;
223
+ }
224
+ else {
225
+ bucket.push({
226
+ key: key,
227
+ value: value
228
+ });
229
+ this.size++;
230
+ if (this.size > this.capacity * this.threashold) {
231
+ this.rehash();
232
+ }
233
+ }
234
+ return this;
235
+ }
236
+ *values() {
237
+ for (let bucket of this.buckets.values()) {
238
+ for (let entry of bucket) {
239
+ yield entry.value;
240
+ }
241
+ }
242
+ }
243
+ }
244
+ ;
245
+ Object.freeze(HashMap);
246
+ Object.freeze(HashMap.prototype);
247
+ Object.freeze(Object.getPrototypeOf(HashMap));
package/dist/optional.js CHANGED
@@ -8,14 +8,14 @@ export class Optional {
8
8
  constructor(value) {
9
9
  this.value = value;
10
10
  Object.defineProperties(this, {
11
- "value": {
12
- value: this.value,
11
+ "Optional": {
12
+ value: OptionalSymbol,
13
13
  writable: false,
14
- enumerable: true,
14
+ enumerable: false,
15
15
  configurable: false
16
16
  },
17
- "Optional": {
18
- value: this.Optional,
17
+ "value": {
18
+ value: value,
19
19
  writable: false,
20
20
  enumerable: false,
21
21
  configurable: false
@@ -44,20 +44,10 @@ export class Optional {
44
44
  }
45
45
  ifPresent(action, elseAction) {
46
46
  if (this.isPresent() && isFunction(action)) {
47
- try {
48
- action(this.value);
49
- }
50
- catch (error) {
51
- throw new Error("Uncaught error on ifPresent.");
52
- }
47
+ action(this.value);
53
48
  }
54
49
  else if (isFunction(elseAction)) {
55
- try {
56
- elseAction();
57
- }
58
- catch (error) {
59
- throw new Error("Uncaught error on ifPresent elseAction.");
60
- }
50
+ elseAction();
61
51
  }
62
52
  }
63
53
  isEmpty() {
@@ -68,12 +58,7 @@ export class Optional {
68
58
  }
69
59
  map(mapper) {
70
60
  if (this.isPresent() && isFunction(mapper)) {
71
- try {
72
- return new Optional(mapper(this.value));
73
- }
74
- catch (error) {
75
- throw new Error("Uncaught error on map.");
76
- }
61
+ return new Optional(mapper(this.value));
77
62
  }
78
63
  return new Optional(null);
79
64
  }
package/dist/semantic.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Collectable, OrderedCollectable, UnorderedCollectable } from "./collectable";
2
2
  import { isBigInt, isCollectable, isFunction, isIterable, isNumber, isSemantic } from "./guard";
3
- import { useCompare, useRandom } from "./hook";
3
+ import { useHash } from "./hash";
4
+ import { useCompare } from "./hook";
4
5
  import { BigIntStatistics, NumericStatistics } from "./statistics";
5
6
  import { SemanticSymbol } from "./symbol";
6
7
  import { validate } from "./utility";
@@ -11,14 +12,14 @@ export class Semantic {
11
12
  constructor(generator) {
12
13
  this.generator = generator;
13
14
  Object.defineProperties(this, {
14
- "generator": {
15
- value: generator,
15
+ "Semantic": {
16
+ value: SemanticSymbol,
16
17
  writable: false,
17
- enumerable: true,
18
+ enumerable: false,
18
19
  configurable: false
19
20
  },
20
- "Semantic": {
21
- value: SemanticSymbol,
21
+ "generator": {
22
+ value: generator,
22
23
  writable: false,
23
24
  enumerable: false,
24
25
  configurable: false
@@ -269,7 +270,7 @@ export class Semantic {
269
270
  }
270
271
  });
271
272
  }
272
- throw new TypeError("Invalid arguments on redirect.");
273
+ throw new TypeError("Invalid arguments.");
273
274
  }
274
275
  reverse() {
275
276
  return new Semantic((accept, interrupt) => {
@@ -299,7 +300,7 @@ export class Semantic {
299
300
  return new Semantic((accept, interrupt) => {
300
301
  try {
301
302
  this.generator((element, index) => {
302
- accept(element, useRandom(index));
303
+ accept(element, useHash([element, index]));
303
304
  }, interrupt);
304
305
  }
305
306
  catch (error) {
@@ -345,7 +346,7 @@ export class Semantic {
345
346
  }
346
347
  });
347
348
  }
348
- throw new TypeError("Invalid arguments on skip.");
349
+ throw new TypeError("Invalid arguments.");
349
350
  }
350
351
  sorted(comparator) {
351
352
  if (isFunction(comparator)) {
@@ -364,44 +365,38 @@ export class Semantic {
364
365
  }
365
366
  }
366
367
  sub(start, end) {
367
- if (isBigInt(start) && isBigInt(end)) {
368
- return new Semantic((accept, interrupt) => {
369
- try {
370
- let count = 0n;
371
- this.generator((element, index) => {
372
- if (count < end) {
373
- count++;
374
- if (count >= start) {
375
- accept(element, index);
376
- }
368
+ return new Semantic((accept, interrupt) => {
369
+ try {
370
+ let count = 0n;
371
+ this.generator((element, index) => {
372
+ if (count < end) {
373
+ count++;
374
+ if (count >= start) {
375
+ accept(element, index);
377
376
  }
378
- }, interrupt);
379
- }
380
- catch (error) {
381
- throw new Error("Uncaught error on sub.");
382
- }
383
- });
384
- }
385
- throw new TypeError("Invalid arguments on sub.");
377
+ }
378
+ }, interrupt);
379
+ }
380
+ catch (error) {
381
+ throw new Error("Uncaught error on sub.");
382
+ }
383
+ });
386
384
  }
387
385
  takeWhile(predicate) {
388
- if (isFunction(predicate)) {
389
- return new Semantic((accept, interrupt) => {
390
- try {
391
- this.generator((element, index) => {
392
- if (!predicate(element, index)) {
393
- interrupt(element, index);
394
- return;
395
- }
396
- accept(element, index);
397
- }, interrupt);
398
- }
399
- catch (error) {
400
- throw new Error("Uncaught error on takeWhile.");
401
- }
402
- });
403
- }
404
- throw new TypeError("Invalid arguments.");
386
+ return new Semantic((accept, interrupt) => {
387
+ try {
388
+ this.generator((element, index) => {
389
+ if (!predicate(element, index)) {
390
+ interrupt(element, index);
391
+ return;
392
+ }
393
+ accept(element, index);
394
+ }, interrupt);
395
+ }
396
+ catch (error) {
397
+ throw new Error("Uncaught error on takeWhile.");
398
+ }
399
+ });
405
400
  }
406
401
  toCollectable(mapper) {
407
402
  if (isFunction(mapper)) {
@@ -476,7 +471,7 @@ export class Semantic {
476
471
  }
477
472
  });
478
473
  }
479
- if (isBigInt(argument1)) {
474
+ else if (isBigInt(argument1)) {
480
475
  let offset = argument1;
481
476
  return new Semantic((accept, interrupt) => {
482
477
  try {
@@ -489,7 +484,7 @@ export class Semantic {
489
484
  }
490
485
  });
491
486
  }
492
- if (isFunction(argument1)) {
487
+ else if (isFunction(argument1)) {
493
488
  let translator = argument1;
494
489
  return new Semantic((accept, interrupt) => {
495
490
  try {
@@ -508,4 +503,4 @@ export class Semantic {
508
503
  ;
509
504
  Object.freeze(Semantic);
510
505
  Object.freeze(Semantic.prototype);
511
- Object.freeze(Object.getPrototypeOf(Semantic.prototype));
506
+ Object.freeze(Object.getPrototypeOf(Semantic));
package/dist/set.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { HashMap } from "./map";
2
+ import type { BiConsumer, Consumer, TriConsumer } from "./utility";
3
+ export declare class HashSet<E> implements Set<E> {
4
+ protected map: HashMap<E, boolean>;
5
+ size: number;
6
+ constructor();
7
+ add(value: E): this;
8
+ clear(): void;
9
+ delete(value: E): boolean;
10
+ entries(): SetIterator<[E, E]>;
11
+ forEach(consumer: Consumer<E>): void;
12
+ forEach(consumer: BiConsumer<E, E>): void;
13
+ forEach(consumer: TriConsumer<E, E, Set<E>>): void;
14
+ has(value: E): boolean;
15
+ keys(): IterableIterator<E>;
16
+ values(): IterableIterator<E>;
17
+ [Symbol.iterator](): IterableIterator<E>;
18
+ [Symbol.toStringTag]: string;
19
+ }
package/dist/set.js ADDED
@@ -0,0 +1,64 @@
1
+ import { HashMap } from "./map";
2
+ export class HashSet {
3
+ map = new HashMap();
4
+ size = 0;
5
+ constructor() {
6
+ Object.defineProperty(this, "size", {
7
+ get: () => this.map.size,
8
+ set: (value) => {
9
+ this.map.size = value;
10
+ },
11
+ enumerable: true,
12
+ configurable: true
13
+ });
14
+ }
15
+ add(value) {
16
+ this.map.set(value, true);
17
+ return this;
18
+ }
19
+ clear() {
20
+ this.map.clear();
21
+ }
22
+ delete(value) {
23
+ return this.map.delete(value);
24
+ }
25
+ *entries() {
26
+ for (let [key, value] of this.map.entries()) {
27
+ if (value) {
28
+ yield [key, key];
29
+ }
30
+ }
31
+ }
32
+ forEach(consumer) {
33
+ for (let [key, value] of this.map.entries()) {
34
+ if (value) {
35
+ consumer(key, key);
36
+ }
37
+ }
38
+ }
39
+ has(value) {
40
+ return this.map.has(value);
41
+ }
42
+ *keys() {
43
+ for (let [key, value] of this.map.entries()) {
44
+ if (value) {
45
+ yield key;
46
+ }
47
+ }
48
+ }
49
+ *values() {
50
+ for (let [key, value] of this.map.entries()) {
51
+ if (value) {
52
+ yield key;
53
+ }
54
+ }
55
+ }
56
+ [Symbol.iterator]() {
57
+ return this.values();
58
+ }
59
+ [Symbol.toStringTag] = "HashSet";
60
+ }
61
+ ;
62
+ Object.freeze(HashSet);
63
+ Object.freeze(HashSet.prototype);
64
+ Object.freeze(Object.getPrototypeOf(HashSet));