-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_ancient_dict_read.ml
70 lines (58 loc) · 1.52 KB
/
test_ancient_dict_read.ml
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
(* Read shared dictionary. *)
open Test_ancient_dict
open Printf
open Unix
let argv = Array.to_list Sys.argv
let datafile =
match argv with
| [_; datafile] ->
datafile
| _ ->
failwith (sprintf "usage: %s datafile"
Sys.executable_name)
let md =
let fd = openfile datafile [O_RDWR] 0o644 in
Ancient.attach fd 0n
let arraysize = 256 (* one element for each character *)
let tree : tree array Ancient.ancient = Ancient.get md 0
let tree = Ancient.follow tree
let word_exists word =
try
let tree = ref tree in
let len = String.length word in
for i = 0 to len-2; do
let c = word.[i] in
let c = Char.code c in
match (!tree).(c) with
| Not_Found -> raise Not_found
| Exists (witness,tree') ->
assert (Array.length witness = witness_size);
tree := tree'
| Not_Exists tree' ->
tree := tree'
done;
(* Final character. *)
let c = word.[len-1] in
let c = Char.code c in
match (!tree).(c) with
| Not_Found
| Not_Exists _ -> false
| Exists (witness, _) ->
assert (Array.length witness = witness_size);
true
with
Not_found -> false
let () =
let rec loop () =
printf "Enter a word to check (q = quit program): ";
let word = read_line () in
if word <> "q" then (
printf "'%s' exists? %B\n%!" word (word_exists word);
loop ()
)
in
loop ();
Ancient.detach md;
(* Garbage collect - good way to check we haven't broken anything. *)
Gc.compact ();
printf "Program finished.\n"