1 (* Create shared dictionary. *)
6 let argv = Array.to_list Sys.argv
8 let wordsfile, datafile, baseaddr =
10 | [_; wordsfile; datafile; baseaddr] ->
11 let baseaddr = Nativeint.of_string baseaddr in
12 wordsfile, datafile, baseaddr
14 failwith (sprintf "usage: %s wordsfile datafile baseaddr"
18 let fd = openfile datafile [O_RDWR; O_TRUNC; O_CREAT] 0o644 in
19 Ancient.attach fd baseaddr
21 (* Tree used to store the words. This is stupid and inefficient
22 * but it is here to demonstrate the 'Ancient' module, not good use
26 let arraysize = 256 (* one element for each character *)
28 type t = Not_Found | Exists of t array | Not_Exists of t array;;
29 let tree : t array = Array.make arraysize Not_Found
31 let add_to_tree word =
32 let len = String.length word in
34 let tree = ref tree in
35 for i = 0 to len-2; do
37 let c = Char.code c in
38 match (!tree).(c) with
40 (* Allocate more tree. *)
41 let tree' = Array.make arraysize Not_Found in
42 (!tree).(c) <- Not_Exists tree';
49 (* Final character. *)
50 let c = word.[len-1] in
51 let c = Char.code c in
52 match (!tree).(c) with
54 (!tree).(c) <- Exists (Array.make arraysize Not_Found)
55 | Exists _ -> () (* same word added twice *)
57 (!tree).(c) <- Exists tree'
61 (* Read in the words and put them in the tree. *)
62 let chan = open_in wordsfile in
65 let word = input_line chan in
70 (try loop () with End_of_file -> ());
73 printf "Added %d words to the tree.\n" !count;
75 printf "Sharing tree in data file ...\n%!";
76 ignore (Ancient.share md 0 tree);
78 (* Perform a full GC and compact, which is a good way to see
79 * if we've trashed the OCaml heap in some way.
81 Array.fill tree 0 arraysize Not_Found;
82 printf "Garbage collecting ...\n%!";
85 printf "Detaching file and finishing.\n%!";