Skip to content

Commit 4bd8802

Browse files
committed
Split search function into an individual file.
1 parent e459040 commit 4bd8802

File tree

3 files changed

+73
-70
lines changed

3 files changed

+73
-70
lines changed

examples/engine.mjs

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,8 @@ import path from "node:path";
22
import { readFile } from "node:fs/promises";
33
import { argv, stdin, stdout, exit } from "node:process";
44
import { createInterface } from "node:readline/promises";
5-
import { buffer_size, rule_t } from "../tsds/tsds.mts";
6-
7-
function* search(input_strings, buffer_limit, callback) {
8-
buffer_size(buffer_limit);
9-
10-
const rules = {};
11-
const facts = {};
12-
13-
let cycle = -1;
14-
15-
for (const input_string of input_strings) {
16-
const rule = new rule_t(input_string);
17-
if (rule.length() !== 0) {
18-
rules[rule.key()] = [rule, cycle];
19-
} else {
20-
facts[rule.key()] = [rule, cycle];
21-
}
22-
}
23-
24-
while (true) {
25-
const temp_rules = {};
26-
const temp_facts = {};
27-
let something_new = false;
28-
29-
for (const r_hash in rules) {
30-
for (const f_hash in facts) {
31-
const [rule, r_cycle] = rules[r_hash];
32-
const [fact, f_cycle] = facts[f_hash];
33-
if (r_cycle !== cycle && f_cycle !== cycle) {
34-
continue;
35-
}
36-
const candidate = rule.match(fact);
37-
if (candidate === null) {
38-
continue;
39-
}
40-
const candidate_hash = candidate.key();
41-
if (candidate.length() !== 0) {
42-
// rule
43-
if (candidate_hash in rules || candidate_hash in temp_rules) {
44-
continue;
45-
}
46-
temp_rules[candidate_hash] = candidate;
47-
} else {
48-
// fact
49-
if (candidate_hash in facts || candidate_hash in temp_facts) {
50-
continue;
51-
}
52-
temp_facts[candidate_hash] = candidate;
53-
}
54-
callback(candidate);
55-
something_new = true;
56-
}
57-
}
58-
59-
cycle++;
60-
for (const r_hash in temp_rules) {
61-
const rule = temp_rules[r_hash];
62-
rules[rule.key()] = [rule, cycle];
63-
}
64-
for (const f_hash in temp_facts) {
65-
const fact = temp_facts[f_hash];
66-
facts[fact.key()] = [fact, cycle];
67-
}
68-
if (!something_new) {
69-
return;
70-
}
71-
72-
yield;
73-
}
74-
}
5+
import { rule_t } from "../tsds/tsds.mts";
6+
import { search } from "./search.mjs";
757

768
async function read_file_to_string_array(file_path) {
779
const content = await readFile(file_path, "utf-8");

examples/search.mjs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { buffer_size, rule_t } from "../tsds/tsds.mts";
2+
3+
export function* search(input_strings, buffer_limit, callback) {
4+
buffer_size(buffer_limit);
5+
6+
const rules = {};
7+
const facts = {};
8+
9+
let cycle = -1;
10+
11+
for (const input_string of input_strings) {
12+
const rule = new rule_t(input_string);
13+
if (rule.length() !== 0) {
14+
rules[rule.key()] = [rule, cycle];
15+
} else {
16+
facts[rule.key()] = [rule, cycle];
17+
}
18+
}
19+
20+
while (true) {
21+
const temp_rules = {};
22+
const temp_facts = {};
23+
let something_new = false;
24+
25+
for (const r_hash in rules) {
26+
for (const f_hash in facts) {
27+
const [rule, r_cycle] = rules[r_hash];
28+
const [fact, f_cycle] = facts[f_hash];
29+
if (r_cycle !== cycle && f_cycle !== cycle) {
30+
continue;
31+
}
32+
const candidate = rule.match(fact);
33+
if (candidate === null) {
34+
continue;
35+
}
36+
const candidate_hash = candidate.key();
37+
if (candidate.length() !== 0) {
38+
// rule
39+
if (candidate_hash in rules || candidate_hash in temp_rules) {
40+
continue;
41+
}
42+
temp_rules[candidate_hash] = candidate;
43+
} else {
44+
// fact
45+
if (candidate_hash in facts || candidate_hash in temp_facts) {
46+
continue;
47+
}
48+
temp_facts[candidate_hash] = candidate;
49+
}
50+
callback(candidate);
51+
something_new = true;
52+
}
53+
}
54+
55+
cycle++;
56+
for (const r_hash in temp_rules) {
57+
const rule = temp_rules[r_hash];
58+
rules[rule.key()] = [rule, cycle];
59+
}
60+
for (const f_hash in temp_facts) {
61+
const fact = temp_facts[f_hash];
62+
facts[fact.key()] = [fact, cycle];
63+
}
64+
if (!something_new) {
65+
return;
66+
}
67+
68+
yield;
69+
}
70+
}

rollup.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default {
88
ds: "tsds/ds.mjs",
99
tsds: "tsds/tsds.mts",
1010
example: "examples/main.mjs",
11+
search: "examples/search.mjs",
1112
engine: "examples/engine.mjs",
1213
},
1314
output: {

0 commit comments

Comments
 (0)