Skip to content

Commit

Permalink
refactor: Remove genSequence from trie-lib API (#4477)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed May 11, 2023
1 parent 5c908a0 commit 8407f4b
Show file tree
Hide file tree
Showing 44 changed files with 129 additions and 121 deletions.
6 changes: 3 additions & 3 deletions packages/cspell-tools/src/compiler/wordListCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('Validate Larger Dictionary', () => {
const trie = normalizeWordsToTrie(words);
expect(isCircular(trie)).toBe(false);
const nWords = toArray(legacyNormalizeWords(words)).sort().filter(uniqueFilter(1000));
const results = iteratorTrieWords(trie).toArray().sort().filter(uniqueFilter(1000));
const results = [...iteratorTrieWords(trie)].sort().filter(uniqueFilter(1000));
expect(results).toEqual(nWords);
}, 60000);

Expand All @@ -167,11 +167,11 @@ describe('Validate Larger Dictionary', () => {
const trie = Trie.consolidate(normalizeWordsToTrie(words));
expect(isCircular(trie)).toBe(false);
const nWords = toArray(legacyNormalizeWords(words)).sort().filter(uniqueFilter(1000));
const results = iteratorTrieWords(trie).toArray().sort();
const results = [...iteratorTrieWords(trie)].sort();
expect(results).toEqual(nWords);
const data = serializeTrie(trie, { base: 40 });
const trie2 = importTrie(data);
const results2 = iteratorTrieWords(trie2).toArray();
const results2 = [...iteratorTrieWords(trie2)];
expect(results2).toEqual(results);
}, 60000);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';

import { createTriFromList } from '../trie-util.js';
import { createTriFromList } from '../TrieNode/trie-util.js';
import { FastTrieBlob } from './FastTrieBlob.js';

describe('FastTrieBlob', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/src/lib/TrieBlob/FastTrieBlob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TrieNode, TrieRoot } from '../TrieNode.js';
import type { TrieNode, TrieRoot } from '../TrieNode/TrieNode.js';
import { TrieBlob } from './TrieBlob.js';

type FastTrieBlobNode = number[];
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/src/lib/TrieBlob/test/en.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'vitest';

import { readTrie } from '../../../test/dictionaries.test.helper.js';
import type { TrieNode } from '../../TrieNode.js';
import type { TrieNode } from '../../TrieNode/TrieNode.js';

function getTrie() {
return readTrie('@cspell/dict-en_us/cspell-ext.json');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ function hasWords(words: string[], method: (word: string) => boolean): boolean {

export async function measureFastBlob(which: string | undefined, method: string | undefined) {
const trie = await getTrie();
const words = trie.words().toArray();
const words = [...trie.words()];

if (filterTest(which, 'blob')) {
const ft = measure('blob.FastTrieBlob \t\t', () => FastTrieBlob.fromTrieRoot(trie.root));
const ft = measure('blob.FastTrieBlob.fromTrieRoot \t', () => FastTrieBlob.fromTrieRoot(trie.root));
const trieBlob = measure('blob.FastTrieBlob.toTrieBlob \t', () => ft.toTrieBlob());

switch (method) {
Expand All @@ -52,7 +52,7 @@ export async function measureFastBlob(which: string | undefined, method: string
}

if (filterTest(which, 'fast')) {
const ft = measure('fast.FastTrieBlob \t\t', () => FastTrieBlob.fromWordList(words));
const ft = measure('fast.FastTrieBlob.fromWordList \t', () => FastTrieBlob.fromWordList(words));

switch (method) {
case 'has':
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/src/lib/TrieBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'vitest';

import { countNodes, isCircular } from './trie-util.js';
import { buildTrie, TrieBuilder } from './TrieBuilder.js';
import { countNodes, isCircular } from './TrieNode/trie-util.js';

describe('Validate TrieBuilder', () => {
test('builder explicit consolidateSuffixes', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cspell-trie-lib/src/lib/TrieBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { consolidate } from './consolidate.js';
import type { PartialTrieOptions, TrieOptions } from './trie.js';
import { Trie } from './trie.js';
import { createTrieRoot, createTriFromList, trieNodeToRoot } from './trie-util.js';
import type { TrieNode, TrieRoot } from './TrieNode.js';
import { createTrieRoot, createTriFromList, trieNodeToRoot } from './TrieNode/trie-util.js';
import type { TrieNode, TrieRoot } from './TrieNode/TrieNode.js';
import { mergeOptionalWithDefaults } from './utils/mergeOptionalWithDefaults.js';
import { SecondChanceCache } from './utils/secondChanceCache.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PartialWithUndefined } from './types.js';
import type { PartialWithUndefined } from '../types.js';

export const FLAG_WORD = 1;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, expect, test } from 'vitest';

import { parseDictionary } from '../SimpleDictionaryParser.js';
import type { Trie } from '../trie.js';
import type { WalkItem, WalkNext } from './compoundWalker.js';
import { compoundWalker, compoundWords } from './compoundWalker.js';
import { findWord } from './find.js';
import { parseDictionary } from './SimpleDictionaryParser.js';
import type { Trie } from './trie.js';

// cspell:ignore errorerror

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Trie } from './trie.js';
import type { Trie } from '../trie.js';
import type { TrieNode } from './TrieNode.js';

export interface WalkItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import * as fs from 'fs/promises';
import { describe, expect, test } from 'vitest';
import * as zlib from 'zlib';

import { resolveGlobalDict } from '../test/samples.js';
import { resolveGlobalDict } from '../../test/samples.js';
import { importTrie } from '../io/importExport.js';
import { normalizeWordToLowercase } from '../utils/normalizeWord.js';
import type { FindFullResult, PartialFindOptions } from './find.js';
import { findWord } from './find.js';
import { importTrie } from './io/importExport.js';
import type { TrieNode } from './TrieNode.js';
import { normalizeWordToLowercase } from './utils/normalizeWord.js';

const dutchDictionary = resolveGlobalDict('nl_nl.trie.gz');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, test } from 'vitest';

import { parseDictionary } from '../SimpleDictionaryParser.js';
import { Trie } from '../trie.js';
import type { FindFullResult, PartialFindOptions } from './find.js';
import { __testing__, createFindOptions, findLegacyCompound, findWord } from './find.js';
import { parseDictionary } from './SimpleDictionaryParser.js';
import { Trie } from './trie.js';

const findLegacyCompoundWord = __testing__.findLegacyCompoundWord;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CASE_INSENSITIVE_PREFIX, COMPOUND_FIX, FORBID_PREFIX } from './constants.js';
import { CASE_INSENSITIVE_PREFIX, COMPOUND_FIX, FORBID_PREFIX } from '../constants.js';
import type { PartialWithUndefined } from '../types.js';
import { memorizeLastCall } from '../utils/memorizeLastCall.js';
import { mergeDefaults } from '../utils/mergeDefaults.js';
import type { TrieNode, TrieRoot } from './TrieNode.js';
import { FLAG_WORD } from './TrieNode.js';
import type { PartialWithUndefined } from './types.js';
import { memorizeLastCall } from './utils/memorizeLastCall.js';
import { mergeDefaults } from './utils/mergeDefaults.js';

type Root = PartialWithUndefined<TrieRoot>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, test } from 'vitest';

import { mergeDefaults } from '../utils/mergeDefaults.js';
import {
countNodes,
countWords,
Expand All @@ -9,7 +10,6 @@ import {
isCircular,
iteratorTrieWords,
} from './trie-util.js';
import { mergeDefaults } from './utils/mergeDefaults.js';

describe('Validate Util Functions', () => {
test('createTriFromList', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { Sequence } from 'gensequence';
import { genSequence } from 'gensequence';
import { opFilter, opMap, pipe } from '@cspell/cspell-pipe/sync';

import { mergeOptionalWithDefaults } from '../utils/mergeOptionalWithDefaults.js';
import { walker } from '../walker/walker.js';
import type { YieldResult } from '../walker/walkerTypes.js';
import type { PartialTrieOptions, TrieNode, TrieRoot } from './TrieNode.js';
import { FLAG_WORD } from './TrieNode.js';
import { mergeOptionalWithDefaults } from './utils/mergeOptionalWithDefaults.js';
import { walker } from './walker/walker.js';
import type { YieldResult } from './walker/walkerTypes.js';

export function insert(text: string, root: TrieNode = {}): TrieNode {
let node = root;
Expand Down Expand Up @@ -40,19 +39,21 @@ export function orderTrie(node: TrieNode): void {
/**
* Generator an iterator that will walk the Trie parent then children in a depth first fashion that preserves sorted order.
*/
export function walk(node: TrieNode): Sequence<YieldResult> {
return genSequence(walker(node));
export function walk(node: TrieNode): Iterable<YieldResult> {
return walker(node);
}

export const iterateTrie = walk;

/**
* Generate a Iterator that can walk a Trie and yield the words.
*/
export function iteratorTrieWords(node: TrieNode): Sequence<string> {
return walk(node)
.filter((r) => isWordTerminationNode(r.node))
.map((r) => r.text);
export function iteratorTrieWords(node: TrieNode): Iterable<string> {
return pipe(
walk(node),
opFilter((r) => isWordTerminationNode(r.node)),
opMap((r) => r.text)
);
}

export function createTrieRoot(options: PartialTrieOptions): TrieRoot {
Expand Down
4 changes: 2 additions & 2 deletions packages/cspell-trie-lib/src/lib/consolidate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { describe, expect, test } from 'vitest';

import { resolveGlobalSample } from '../test/samples.js';
import { consolidate } from './consolidate.js';
import { countNodes, createTrieRoot, createTriFromList, iteratorTrieWords } from './trie-util.js';
import { buildTrie } from './TrieBuilder.js';
import type { TrieNode } from './TrieNode.js';
import { countNodes, createTrieRoot, createTriFromList, iteratorTrieWords } from './TrieNode/trie-util.js';
import type { TrieNode } from './TrieNode/TrieNode.js';

const samples = resolveGlobalSample('dicts');
const sampleEnglish = path.join(samples, 'en_US.txt');
Expand Down
6 changes: 3 additions & 3 deletions packages/cspell-trie-lib/src/lib/consolidate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { trieNodeToRoot } from './trie-util.js';
import type { TrieNode, TrieRoot } from './TrieNode.js';
import { FLAG_WORD } from './TrieNode.js';
import { trieNodeToRoot } from './TrieNode/trie-util.js';
import type { TrieNode, TrieRoot } from './TrieNode/TrieNode.js';
import { FLAG_WORD } from './TrieNode/TrieNode.js';

/**
* Consolidate to DAWG
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TrieOptions } from './TrieNode.js';
import type { TrieOptions } from './TrieNode/TrieNode.js';

export const COMPOUND_FIX = '+';
export const OPTIONAL_COMPOUND_FIX = '*';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, test } from 'vitest';

import { consolidate } from './consolidate.js';
import { convertToTrieRefNodes } from './convertToTrieRefNodes.js';
import { createTriFromList } from './trie-util.js';
import { createTriFromList } from './TrieNode/trie-util.js';
import type { TrieRefNode } from './trieRef.js';

describe('Validate convertToTrieRefNodes', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cspell-trie-lib/src/lib/convertToTrieRefNodes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { genSequence } from 'gensequence';

import type { TrieNode } from './TrieNode.js';
import { FLAG_WORD } from './TrieNode.js';
import type { TrieNode } from './TrieNode/TrieNode.js';
import { FLAG_WORD } from './TrieNode/TrieNode.js';
import type { TrieRefNode } from './trieRef.js';

const MinReferenceCount = 3;
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/src/lib/flatten.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { genSequence } from 'gensequence';
import { describe, expect, test } from 'vitest';

import { flattenToTrieRefNodeArray, flattenToTrieRefNodeIterable } from './flatten.js';
import { createTriFromList } from './trie-util.js';
import { createTriFromList } from './TrieNode/trie-util.js';
import type { TrieRefNode } from './trieRef.js';

describe('Validate Flatten', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/src/lib/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TrieNode } from './TrieNode.js';
import type { TrieNode } from './TrieNode/TrieNode.js';
import type { TrieRefNode } from './trieRef.js';

export interface Emitter {
Expand Down
8 changes: 4 additions & 4 deletions packages/cspell-trie-lib/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { consolidate } from './consolidate.js';
export type { WeightMap } from './distance/index.js';
export { createWeightedMap, editDistance, editDistanceWeighted } from './distance/index.js';
export type { FindFullResult } from './find.js';
export { ExportOptions, importTrie, serializeTrie } from './io/importExport.js';
export { mapDictionaryInformationToWeightMap } from './mappers/mapDictionaryInfoToWeightMap.js';
export type { SuggestionCostMapDef } from './models/suggestionCostsDef.js';
Expand All @@ -25,6 +24,8 @@ export {
OPTIONAL_COMPOUND_FIX,
Trie,
} from './trie.js';
export { buildTrie, buildTrieFast, TrieBuilder } from './TrieBuilder.js';
export type { FindFullResult } from './TrieNode/find.js';
export {
countNodes,
countWords,
Expand All @@ -40,9 +41,8 @@ export {
orderTrie,
trieNodeToRoot,
walk,
} from './trie-util.js';
export { buildTrie, buildTrieFast, TrieBuilder } from './TrieBuilder.js';
export { ChildMap, FLAG_WORD, TrieNode, TrieRoot } from './TrieNode.js';
} from './TrieNode/trie-util.js';
export { ChildMap, FLAG_WORD, TrieNode, TrieRoot } from './TrieNode/TrieNode.js';
export { isDefined } from './utils/isDefined.js';
export { mergeDefaults } from './utils/mergeDefaults.js';
export { mergeOptionalWithDefaults } from './utils/mergeOptionalWithDefaults.js';
Expand Down
15 changes: 7 additions & 8 deletions packages/cspell-trie-lib/src/lib/io/importExport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { toDistributableIterable } from '@cspell/cspell-pipe';
import type { Sequence } from 'gensequence';
import { genSequence } from 'gensequence';

import type { TrieRoot } from '../TrieNode.js';
import type { TrieRoot } from '../TrieNode/TrieNode.js';
import * as iv1 from './importExportV1.js';
import * as iv2 from './importExportV2.js';
import * as iv3 from './importExportV3.js';
Expand All @@ -15,7 +13,7 @@ export interface ExportOptions {
addLineBreaksToImproveDiffs?: boolean;
}

type Serializer = (root: TrieRoot, options?: number | ExportOptions) => Sequence<string>;
type Serializer = (root: TrieRoot, options?: number | ExportOptions) => Iterable<string>;

const serializers: readonly Serializer[] = [
iv1.serializeTrie,
Expand All @@ -35,7 +33,7 @@ const DEFAULT_VERSION = 3;
* Even though it is possible to preserve the trie, dealing with very large tries can consume a lot of memory.
* Considering this is the last step before exporting, it was decided to let this be destructive.
*/
export function serializeTrie(root: TrieRoot, options: ExportOptions | number = 16): Sequence<string> {
export function serializeTrie(root: TrieRoot, options: ExportOptions | number = 16): Iterable<string> {
const version = typeof options !== 'number' && options.version ? options.version : DEFAULT_VERSION;
const method = serializers[version];
if (!method) {
Expand All @@ -44,7 +42,8 @@ export function serializeTrie(root: TrieRoot, options: ExportOptions | number =
return method(root, options);
}

export function importTrie(lines: Iterable<string> | IterableIterator<string>): TrieRoot {
export function importTrie(lines: Iterable<string> | IterableIterator<string> | string[]): TrieRoot {
const aLines = Array.isArray(lines) ? lines : [...lines];
function parseHeaderRows(headerRows: string[]): number {
const header = headerRows.join('\n');
const headerReg = /^\s*TrieXv(\d+)/m;
Expand All @@ -65,10 +64,10 @@ export function importTrie(lines: Iterable<string> | IterableIterator<string>):
return headerRows;
}

const input = toDistributableIterable(lines);
const input = toDistributableIterable(aLines);
const headerLines = readHeader(input);
const version = parseHeaderRows(headerLines);
const stream = genSequence(headerLines).concat(input);
const stream = [...headerLines, ...input];
const method = deserializers[version];
if (!method) {
throw new Error(`Unsupported version: ${version}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/src/lib/io/importExportV1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, test } from 'vitest';
import { resolveSample } from '../../test/samples.js';
import { consolidate } from '../consolidate.js';
import * as Trie from '../index.js';
import { iteratorTrieWords } from '../trie-util.js';
import { iteratorTrieWords } from '../TrieNode/trie-util.js';
import { importTrie, serializeTrie } from './importExportV1.js';

describe('Import/Export', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/cspell-trie-lib/src/lib/io/importExportV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { Sequence } from 'gensequence';
import { genSequence } from 'gensequence';

import { convertToTrieRefNodes } from '../convertToTrieRefNodes.js';
import { trieNodeToRoot } from '../trie-util.js';
import type { TrieNode, TrieRoot } from '../TrieNode.js';
import { FLAG_WORD } from '../TrieNode.js';
import { trieNodeToRoot } from '../TrieNode/trie-util.js';
import type { TrieNode, TrieRoot } from '../TrieNode/TrieNode.js';
import { FLAG_WORD } from '../TrieNode/TrieNode.js';
import type { TrieRefNode } from '../trieRef.js';

const EOW = '*';
Expand Down
6 changes: 3 additions & 3 deletions packages/cspell-trie-lib/src/lib/io/importExportV2.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Sequence } from 'gensequence';
import { genSequence } from 'gensequence';

import { trieNodeToRoot } from '../trie-util.js';
import type { TrieNode, TrieRoot } from '../TrieNode.js';
import { FLAG_WORD } from '../TrieNode.js';
import { trieNodeToRoot } from '../TrieNode/trie-util.js';
import type { TrieNode, TrieRoot } from '../TrieNode/TrieNode.js';
import { FLAG_WORD } from '../TrieNode/TrieNode.js';

const EOW = '*';
export const DATA = '__DATA__';
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/src/lib/io/importExportV3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { describe, expect, test } from 'vitest';
import { resolveSample as resolveSamplePath } from '../../test/samples.js';
import { consolidate } from '../consolidate.js';
import * as Trie from '../index.js';
import type { TrieNode } from '../TrieNode.js';
import type { TrieNode } from '../TrieNode/TrieNode.js';
import { importTrie, serializeTrie } from './importExportV3.js';

const sampleFile = resolveSamplePath('sampleV3.trie');
Expand Down
Loading

0 comments on commit 8407f4b

Please sign in to comment.