forked from PuzzleTechHub/nutrimatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplore-index.cpp
73 lines (58 loc) · 1.88 KB
/
explore-index.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Walk the nodes in a Nutrimatic index file from a specified starting point,
// descending recursively to the highest-frequency child nodes first.
// Mostly used for debugging and exploring the index contents.
#include "index.h"
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
bool by_count(IndexReader::Choice const& a, IndexReader::Choice const& b) {
return a.count > b.count;
}
static void walk(IndexReader const& reader, off_t node, int64_t count,
const char *path, int depth, string *sofar) {
if (depth == 0) return;
vector<IndexReader::Choice> children;
if (*path != '\0') {
reader.children(node, count, *path, *path, &children);
++path;
} else {
reader.children(node, count, CHAR_MIN, CHAR_MAX, &children);
}
sort(children.begin(), children.end(), by_count);
for (size_t i = 0; i < children.size(); ++i) {
sofar->push_back(children[i].ch);
printf("%s (%" PRId64 ") @%zd\n", sofar->c_str(),
children[i].count, children[i].next);
walk(reader, children[i].next, children[i].count, path, depth - 1, sofar);
sofar->resize(sofar->size() - 1);
}
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "usage: %s input.index \"path\" [depth]\n", argv[0]);
return 2;
}
FILE *input = fopen(argv[1], "rb");
if (input == NULL) {
fprintf(stderr, "error: can't open \"%s\"\n", argv[1]);
return 1;
}
IndexReader reader(input);
printf("Root (%" PRId64 ") @%zd\n", reader.count(), reader.root());
int depth = strlen(argv[2]);
if (argc > 3) {
depth = atoi(argv[3]);
if (depth == 0) {
fprintf(stderr, "error: invalid depth \"%s\"\n", argv[3]);
exit(2);
}
}
string sofar = "";
walk(reader, reader.root(), reader.count(), argv[2], depth, &sofar);
return 0;
}