Skip to content

Commit f1461fd

Browse files
committed
impr(polyglot): Populate PolyglotWordset.words.
- Add strView to not duplicate data. (@IliyaZinoviev)
1 parent 5f47b5d commit f1461fd

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { WordGenError } from "../../utils/word-gen-error";
2424
import { FunboxName, KeymapLayout, Layout } from "@monkeytype/schemas/configs";
2525
import { Language, LanguageObject } from "@monkeytype/schemas/languages";
2626
import { qs } from "../../utils/dom";
27+
import { strView } from "./strings-view";
2728

2829
export type FunboxFunctions = {
2930
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
@@ -146,24 +147,22 @@ class PseudolangWordGenerator extends Wordset {
146147
}
147148

148149
export class PolyglotWordset extends Wordset {
149-
readonly wordsMap: Map<Language, Wordset>;
150+
readonly wordsetMap: Map<Language, Wordset>;
150151
readonly languageProperties: Map<Language, JSONData.LanguageProperties>;
151152
private currLang: Language;
152153
readonly langs: Language[];
153154

154155
constructor(
155-
wordsMap: Map<Language, Wordset>,
156+
words: string[],
157+
wordsetMap: Map<Language, Wordset>,
156158
languageProperties: Map<Language, JSONData.LanguageProperties>,
157159
) {
158-
super([]);
160+
super(words);
159161
this.languageProperties = languageProperties;
160162
this.langs = Array.from(languageProperties.keys());
161-
this.wordsMap = wordsMap;
163+
this.wordsetMap = wordsetMap;
162164
this.resetIndexes();
163-
this.length = Array.from(this.wordsMap.values()).reduce(
164-
(sum, ws) => sum + ws.length,
165-
0,
166-
);
165+
this.length = words.length;
167166
this.currLang = this.langs[0] as Language;
168167
}
169168

@@ -172,7 +171,7 @@ export class PolyglotWordset extends Wordset {
172171
}
173172

174173
override resetIndexes(): void {
175-
this.wordsMap.forEach((ws) => {
174+
this.wordsetMap.forEach((ws) => {
176175
ws.resetIndexes();
177176
});
178177
}
@@ -185,7 +184,7 @@ export class PolyglotWordset extends Wordset {
185184

186185
private getWordset(): Wordset {
187186
const lang = this.uniformLang();
188-
return this.wordsMap.get(lang) as Wordset;
187+
return this.wordsetMap.get(lang) as Wordset;
189188
}
190189

191190
override randomWord(mode: FunboxWordsFrequency): string {
@@ -802,11 +801,16 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
802801
},
803802
]),
804803
);
805-
806-
const wordsMap: Map<Language, Wordset> = new Map(
807-
languages.map((lang) => [lang.name, new Wordset(lang.words)]),
808-
);
809-
return new PolyglotWordset(wordsMap, languageProperties);
804+
const wordsetMap = new Map<Language, Wordset>();
805+
let end = 0;
806+
const words: string[] = [];
807+
for (const lang of languages) {
808+
const start = end;
809+
end += lang.words.length;
810+
words.push(...lang.words);
811+
wordsetMap.set(lang.name, new Wordset(strView(words, start, end)));
812+
}
813+
return new PolyglotWordset(words, wordsetMap, languageProperties);
810814
},
811815
},
812816
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export function strView(
2+
arr: string[],
3+
start: number,
4+
end: number = arr.length,
5+
): string[] {
6+
const s = Math.max(0, start);
7+
const e = Math.min(arr.length, end);
8+
9+
return new Proxy(arr, {
10+
get(target, prop) {
11+
if (prop === "length") return e - s;
12+
13+
const i = Number(prop);
14+
if (Number.isInteger(i) && i >= 0 && i < e - s) {
15+
return target[s + i];
16+
}
17+
return undefined;
18+
},
19+
20+
set(target, prop, value: string) {
21+
const i = Number(prop);
22+
if (Number.isInteger(i) && i >= 0 && i < e - s) {
23+
target[s + i] = value;
24+
return true;
25+
}
26+
return false;
27+
},
28+
});
29+
}

0 commit comments

Comments
 (0)