loudo-ds-lmap 0.0.1 → 0.0.4

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Entry, MapChange, MapRemove } from "loudo-ds-map-interfaces";
1
+ import { Entry, MapChange, MapInput, MapRemove } from "loudo-ds-map-interfaces";
2
2
  interface Bucket<K, V> {
3
3
  key: K;
4
4
  value: V;
@@ -8,20 +8,21 @@ interface Bucket<K, V> {
8
8
  chain: B<K, V>;
9
9
  }
10
10
  type B<K, V> = Bucket<K, V> | null;
11
- export interface HashConfig<K extends {}, V extends {}> {
11
+ export interface LMapConfig<K extends {}, V extends {}> {
12
12
  readonly load?: number;
13
13
  hashCode(key: K): number;
14
14
  keyEq?(key1: K, key2: K): boolean;
15
15
  valueEq?(value1: V, value2: V): boolean;
16
16
  }
17
17
  export declare class LMap<K extends {}, V extends {}> {
18
- #private;
19
- readonly config: HashConfig<K, V>;
20
- private buckets;
21
- firstB: B<K, V>;
22
- lastB: B<K, V>;
23
- constructor(config: HashConfig<K, V>);
18
+ protected _config: LMapConfig<K, V>;
19
+ protected buckets: B<K, V>[];
20
+ protected firstB: B<K, V>;
21
+ protected lastB: B<K, V>;
22
+ protected _size: number;
23
+ constructor(input: MapInput<K, V>, config: LMapConfig<K, V>);
24
24
  get size(): number;
25
+ get config(): LMapConfig<K, V>;
25
26
  get first(): Entry<K, V> | undefined;
26
27
  get last(): Entry<K, V> | undefined;
27
28
  get only(): Entry<K, V>;
@@ -30,11 +31,12 @@ export declare class LMap<K extends {}, V extends {}> {
30
31
  get load(): number;
31
32
  hasKey(key: K): boolean;
32
33
  get(key: K): V | undefined;
34
+ protected afterGet(bucket: B<K, V>): void;
33
35
  [Symbol.iterator](): IterableIterator<Entry<K, V>>;
34
36
  private grow;
35
37
  private bucket;
36
38
  put(key: K, value: V): V | undefined;
37
- remove(key: K): V | undefined;
39
+ removeKey(key: K): V | undefined;
38
40
  clear(): void;
39
41
  }
40
42
  export interface LMap<K extends {}, V extends {}> extends MapChange<K, V>, MapRemove<K, V> {
package/dist/index.js CHANGED
@@ -1,33 +1,23 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
- if (kind === "m") throw new TypeError("Private method is not writable");
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
- };
12
- var _LMap_size;
13
1
  import { mixin, toTin, OnlyError } from "loudo-ds-core";
14
2
  import { MapChange, MapRemove } from "loudo-ds-map-interfaces";
15
3
  export class LMap {
16
- constructor(config) {
4
+ constructor(input, config) {
17
5
  this.buckets = [];
18
6
  this.firstB = null;
19
7
  this.lastB = null;
20
- _LMap_size.set(this, 0);
21
- this.config = config;
8
+ this._size = 0;
9
+ this._config = config;
22
10
  for (let i = 0; i < 4; i++) {
23
11
  this.buckets.push(null);
24
12
  }
13
+ this.putAll(input);
25
14
  }
26
- get size() { return __classPrivateFieldGet(this, _LMap_size, "f"); }
15
+ get size() { return this._size; }
16
+ get config() { return this._config; }
27
17
  get first() { var _a; return (_a = this.firstB) !== null && _a !== void 0 ? _a : undefined; }
28
18
  get last() { var _a; return (_a = this.lastB) !== null && _a !== void 0 ? _a : undefined; }
29
19
  get only() {
30
- if (__classPrivateFieldGet(this, _LMap_size, "f") !== 1)
20
+ if (this._size !== 1)
31
21
  throw new OnlyError();
32
22
  return this.firstB;
33
23
  }
@@ -38,10 +28,12 @@ export class LMap {
38
28
  return this.bucket(key) !== null;
39
29
  }
40
30
  get(key) {
41
- var _a;
42
- return (_a = this.bucket(key)) === null || _a === void 0 ? void 0 : _a.value;
31
+ const bucket = this.bucket(key);
32
+ this.afterGet(bucket);
33
+ return bucket === null || bucket === void 0 ? void 0 : bucket.value;
43
34
  }
44
- *[(_LMap_size = new WeakMap(), Symbol.iterator)]() {
35
+ afterGet(bucket) { }
36
+ *[Symbol.iterator]() {
45
37
  for (let bucket = this.firstB; bucket !== null; bucket = bucket.next) {
46
38
  yield bucket;
47
39
  }
@@ -69,8 +61,7 @@ export class LMap {
69
61
  return null;
70
62
  }
71
63
  put(key, value) {
72
- var _a;
73
- if (__classPrivateFieldGet(this, _LMap_size, "f") / this.buckets.length >= this.load)
64
+ if (this._size / this.buckets.length >= this.load)
74
65
  this.grow();
75
66
  const hashCode = this.config.hashCode(key);
76
67
  const bucket = {
@@ -82,6 +73,7 @@ export class LMap {
82
73
  if (b.hashCode === hashCode && this.keyEq(key, b.key)) {
83
74
  const r = b.value;
84
75
  b.value = value;
76
+ this.afterGet(b);
85
77
  const elements = toTin([bucket]);
86
78
  this.fire({
87
79
  cleared: false,
@@ -103,12 +95,11 @@ export class LMap {
103
95
  bucket.prev = this.lastB;
104
96
  this.lastB = bucket;
105
97
  }
106
- __classPrivateFieldSet(this, _LMap_size, (_a = __classPrivateFieldGet(this, _LMap_size, "f"), _a++, _a), "f");
98
+ this._size++;
107
99
  this.fire({ cleared: false, added: { elements: toTin([bucket]), at: undefined, count: 1 } });
108
100
  return undefined;
109
101
  }
110
- remove(key) {
111
- var _a;
102
+ removeKey(key) {
112
103
  const hash = this.config.hashCode(key);
113
104
  let prev = null;
114
105
  const i = Math.abs(hash) % this.buckets.length;
@@ -121,7 +112,7 @@ export class LMap {
121
112
  }
122
113
  if (bucket === null)
123
114
  return undefined;
124
- __classPrivateFieldSet(this, _LMap_size, (_a = __classPrivateFieldGet(this, _LMap_size, "f"), _a--, _a), "f");
115
+ this._size--;
125
116
  if (prev === null)
126
117
  this.buckets[i] = bucket.chain;
127
118
  else
@@ -145,7 +136,7 @@ export class LMap {
145
136
  return;
146
137
  this.firstB = null;
147
138
  this.lastB = null;
148
- __classPrivateFieldSet(this, _LMap_size, 0, "f");
139
+ this._size = 0;
149
140
  this.fire({ cleared: true });
150
141
  }
151
142
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "loudo-ds-lmap",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.4",
5
5
  "description": "Flexible loud map.",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -33,6 +33,6 @@
33
33
  "vitest": "^2.1.4"
34
34
  },
35
35
  "dependencies": {
36
- "loudo-ds-map-interfaces": "^0.0.4"
36
+ "loudo-ds-map-interfaces": "^0.0.12"
37
37
  }
38
38
  }