-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlookup.dart
More file actions
79 lines (68 loc) · 1.9 KB
/
lookup.dart
File metadata and controls
79 lines (68 loc) · 1.9 KB
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
74
75
76
77
78
79
import 'dart:io';
import 'dart:typed_data';
import 'dart:convert';
const int FIBONACCI_CONSTANT = 2654435769;
int h24(String s) {
const int FNV_OFFSET_BASIS = 2166136261;
const int FNV_PRIME = 16777619;
int h = FNV_OFFSET_BASIS;
for (final b in utf8.encode(s)) {
h ^= b;
h = (h * FNV_PRIME) & 0xFFFFFFFF;
}
final v = h >> 8;
return v == 0 ? 1 : v;
}
int slotIndex(int key, int capacity) => (key * FIBONACCI_CONSTANT) & (capacity - 1);
void main(List<String> args) {
if (args.isEmpty) {
stdout.writeln('Usage: dart run <script.dart> <name>');
exit(1);
}
final name = args[0];
final path = 'output/py_call_freq.7.bin';
final file = File(path);
if (!file.existsSync()) {
stderr.writeln('file not found: $path');
exit(2);
}
final bytes = file.readAsBytesSync();
if (bytes.length < 16) {
stderr.writeln('corrupt or truncated file');
exit(3);
}
final bd = ByteData.sublistView(bytes);
var offset = 0;
final magic = bd.getUint32(offset, Endian.little);
offset += 4;
if (magic != 0x48534354) {
stderr.writeln('bad magic number: 0x${magic.toRadixString(16)}');
exit(4);
}
offset += 8;
final capacity = bd.getUint32(offset, Endian.little);
offset += 4;
if (bytes.length < offset + capacity * 4) {
stderr.writeln('corrupt or truncated slots section');
exit(5);
}
final slots = List<int>.generate(capacity, (i) => bd.getUint32(offset + i * 4, Endian.little));
final key = h24(name);
var i = slotIndex(key, capacity);
final mask = capacity - 1;
for (var probes = 0; probes < capacity; probes++) {
final slot = slots[i];
if (slot == 0) {
stdout.writeln('$name: not found');
exit(0);
}
final slotKey = slot >> 8;
final score = slot & 0xFF;
if (slotKey == key) {
stdout.writeln('$name: score=$score');
exit(0);
}
i = (i + 1) & mask;
}
stdout.writeln('$name: not found');
}