cspell-trie-lib 5.12.6 → 5.13.3

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.
@@ -4,6 +4,7 @@ exports.TrieBuilder = exports.buildTrieFast = exports.buildTrie = void 0;
4
4
  const trie_1 = require("./trie");
5
5
  const consolidate_1 = require("./consolidate");
6
6
  const util_1 = require("./util");
7
+ const secondChanceCache_1 = require("./secondChanceCache");
7
8
  /**
8
9
  * Builds an optimized Trie from a Iterable<string>. It attempts to reduce the size of the trie
9
10
  * by finding common endings.
@@ -24,12 +25,16 @@ function buildTrieFast(words, trieOptions) {
24
25
  return new trie_1.Trie(root, undefined);
25
26
  }
26
27
  exports.buildTrieFast = buildTrieFast;
28
+ // cspell:words sigs
29
+ const MAX_NUM_SIGS = 100000;
30
+ const MAX_TRANSFORMS = 1000000;
31
+ const MAX_CACHE_SIZE = 1000000;
27
32
  class TrieBuilder {
28
33
  constructor(words, trieOptions) {
29
34
  this.count = 0;
30
- this.signatures = new Map();
31
- this.cached = new Map();
32
- this.transforms = new Map();
35
+ this.signatures = new secondChanceCache_1.SecondChanceCache(MAX_NUM_SIGS);
36
+ this.cached = new secondChanceCache_1.SecondChanceCache(MAX_CACHE_SIZE);
37
+ this.transforms = new secondChanceCache_1.SecondChanceCache(MAX_TRANSFORMS);
33
38
  this._eow = Object.freeze({ f: 1 });
34
39
  /** position 0 of lastPath is always the root */
35
40
  this.lastPath = [{ s: '', n: { f: undefined, c: undefined } }];
@@ -63,8 +68,9 @@ class TrieBuilder {
63
68
  return true;
64
69
  }
65
70
  tryCacheFrozen(n) {
66
- if (this.cached.has(n))
71
+ if (this.cached.has(n)) {
67
72
  return n;
73
+ }
68
74
  this.cached.set(n, this.count++);
69
75
  return n;
70
76
  }
@@ -196,6 +202,9 @@ class TrieBuilder {
196
202
  this._root = (0, util_1.createTrieRoot)(this.trieOptions);
197
203
  this.cached.clear();
198
204
  this.signatures.clear();
205
+ this.signatures.set(this.signature(this._eow), this._eow);
206
+ this.count = 0;
207
+ this.cached.set(this._eow, this.count++);
199
208
  }
200
209
  build(consolidateSuffixes = false) {
201
210
  const root = this._root;
@@ -3,7 +3,7 @@ export { TrieNode, FLAG_WORD, ChildMap, TrieRoot } from './TrieNode';
3
3
  export * from './util';
4
4
  export * from './walker';
5
5
  export * from './importExport';
6
- export * from './TrieBuilder';
6
+ export { buildTrie, buildTrieFast, TrieBuilder } from './TrieBuilder';
7
7
  export * from './consolidate';
8
8
  export { SuggestionResult, MaxCost, suggestionCollector, SuggestionCollector } from './suggestCollector';
9
9
  export { parseDictionaryLines, parseDictionary } from './SimpleDictionaryParser';
package/dist/lib/index.js CHANGED
@@ -10,7 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.parseDictionary = exports.parseDictionaryLines = exports.suggestionCollector = exports.ChildMap = exports.FLAG_WORD = void 0;
13
+ exports.parseDictionary = exports.parseDictionaryLines = exports.suggestionCollector = exports.TrieBuilder = exports.buildTrieFast = exports.buildTrie = exports.ChildMap = exports.FLAG_WORD = void 0;
14
14
  __exportStar(require("./trie"), exports);
15
15
  var TrieNode_1 = require("./TrieNode");
16
16
  Object.defineProperty(exports, "FLAG_WORD", { enumerable: true, get: function () { return TrieNode_1.FLAG_WORD; } });
@@ -18,7 +18,10 @@ Object.defineProperty(exports, "ChildMap", { enumerable: true, get: function ()
18
18
  __exportStar(require("./util"), exports);
19
19
  __exportStar(require("./walker"), exports);
20
20
  __exportStar(require("./importExport"), exports);
21
- __exportStar(require("./TrieBuilder"), exports);
21
+ var TrieBuilder_1 = require("./TrieBuilder");
22
+ Object.defineProperty(exports, "buildTrie", { enumerable: true, get: function () { return TrieBuilder_1.buildTrie; } });
23
+ Object.defineProperty(exports, "buildTrieFast", { enumerable: true, get: function () { return TrieBuilder_1.buildTrieFast; } });
24
+ Object.defineProperty(exports, "TrieBuilder", { enumerable: true, get: function () { return TrieBuilder_1.TrieBuilder; } });
22
25
  __exportStar(require("./consolidate"), exports);
23
26
  var suggestCollector_1 = require("./suggestCollector");
24
27
  Object.defineProperty(exports, "suggestionCollector", { enumerable: true, get: function () { return suggestCollector_1.suggestionCollector; } });
@@ -0,0 +1,16 @@
1
+ export declare class SecondChanceCache<Key, Value> {
2
+ readonly maxL0Size: number;
3
+ private map0;
4
+ private map1;
5
+ constructor(maxL0Size: number);
6
+ has(key: Key): boolean;
7
+ get(key: Key): Value | undefined;
8
+ set(key: Key, value: Value): this;
9
+ get size(): number;
10
+ get size0(): number;
11
+ get size1(): number;
12
+ clear(): this;
13
+ private get1;
14
+ toArray(): [Key, Value][];
15
+ }
16
+ //# sourceMappingURL=secondChanceCache.d.ts.map
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SecondChanceCache = void 0;
4
+ class SecondChanceCache {
5
+ constructor(maxL0Size) {
6
+ this.maxL0Size = maxL0Size;
7
+ this.map0 = new Map();
8
+ this.map1 = new Map();
9
+ }
10
+ has(key) {
11
+ if (this.map0.has(key))
12
+ return true;
13
+ if (this.map1.has(key)) {
14
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
15
+ this.set(key, this.get1(key));
16
+ return true;
17
+ }
18
+ return false;
19
+ }
20
+ get(key) {
21
+ var _a;
22
+ return (_a = this.map0.get(key)) !== null && _a !== void 0 ? _a : this.get1(key);
23
+ }
24
+ set(key, value) {
25
+ if (this.map0.size >= this.maxL0Size && !this.map0.has(key)) {
26
+ this.map1 = this.map0;
27
+ this.map0 = new Map();
28
+ }
29
+ this.map0.set(key, value);
30
+ return this;
31
+ }
32
+ get size() {
33
+ return this.map0.size + this.map1.size;
34
+ }
35
+ get size0() {
36
+ return this.map0.size;
37
+ }
38
+ get size1() {
39
+ return this.map1.size;
40
+ }
41
+ clear() {
42
+ this.map0.clear();
43
+ this.map1.clear();
44
+ return this;
45
+ }
46
+ get1(key) {
47
+ if (this.map1.has(key)) {
48
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
49
+ const v = this.map1.get(key);
50
+ this.map1.delete(key);
51
+ this.set(key, v);
52
+ return v;
53
+ }
54
+ return undefined;
55
+ }
56
+ toArray() {
57
+ return [...this.map1, ...this.map0];
58
+ }
59
+ }
60
+ exports.SecondChanceCache = SecondChanceCache;
61
+ //# sourceMappingURL=secondChanceCache.js.map
@@ -43,7 +43,7 @@ export declare const normalizeWord: (text: string) => string;
43
43
  * converts text to lower case and removes any accents.
44
44
  * @param text - text to convert
45
45
  * @returns lowercase word without accents
46
- * @deprecated
46
+ * @deprecated true
47
47
  */
48
48
  export declare const normalizeWordToLowercase: (text: string) => string;
49
49
  /**
package/dist/lib/util.js CHANGED
@@ -202,7 +202,7 @@ exports.normalizeWord = normalizeWord;
202
202
  * converts text to lower case and removes any accents.
203
203
  * @param text - text to convert
204
204
  * @returns lowercase word without accents
205
- * @deprecated
205
+ * @deprecated true
206
206
  */
207
207
  const normalizeWordToLowercase = (text) => text.toLowerCase().normalize('NFD').replace(/\p{M}/gu, '');
208
208
  exports.normalizeWordToLowercase = normalizeWordToLowercase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-trie-lib",
3
- "version": "5.12.6",
3
+ "version": "5.13.3",
4
4
  "description": "Trie Data Structure to support cspell.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -43,12 +43,12 @@
43
43
  "node": ">=12.13.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@cspell/dict-en_us": "^2.1.3",
47
- "@cspell/dict-es-es": "^2.0.2",
46
+ "@cspell/dict-en_us": "^2.1.4",
47
+ "@cspell/dict-es-es": "^2.1.0",
48
48
  "@types/fs-extra": "^9.0.13",
49
- "@types/node": "^16.11.6",
50
- "jest": "^27.3.1",
49
+ "@types/node": "^16.11.10",
50
+ "jest": "^27.4.0",
51
51
  "rimraf": "^3.0.2"
52
52
  },
53
- "gitHead": "430bb6103b03793d5f0b746e7c643d12fa313216"
53
+ "gitHead": "f49cb2d98eed1f7274b1e6ad1ef076e7140347b5"
54
54
  }