1995-05-04 03:15:53 -07:00
|
|
|
open Lambda
|
|
|
|
|
|
|
|
|
|
|
|
(* Input: a list of (key, action) pairs, where keys are integers. *)
|
|
|
|
(* Output: a table of (low, high, offset) triples for Ktranslate
|
|
|
|
an array of actions for Kswitch *)
|
|
|
|
|
|
|
|
let make_decision_tree casei =
|
|
|
|
(* Sort the cases by increasing keys *)
|
|
|
|
let cases =
|
|
|
|
Sort.list (fun (key1,act1) (key2,act2) -> key1 <= key2) casei in
|
|
|
|
(* Extract the keys and the actions *)
|
|
|
|
let keyv = Array.of_list (List.map fst cases)
|
|
|
|
and actv = Array.of_list (List.map snd cases) in
|
|
|
|
let n = Array.length keyv in
|
|
|
|
(* Partition the set of keys keyv into maximal dense enough segments.
|
|
|
|
A segment is dense enough if its span (max point - min point) is
|
|
|
|
less than four times its size (number of points). *)
|
|
|
|
let rec partition start =
|
|
|
|
if start >= n then [] else
|
|
|
|
let stop = ref (n-1) in
|
1995-05-22 08:43:44 -07:00
|
|
|
while let span = keyv.(!stop) - keyv.(start) in
|
|
|
|
span >= 256 or span > 4 * (!stop - start) do
|
1995-05-04 03:15:53 -07:00
|
|
|
decr stop
|
|
|
|
done;
|
|
|
|
(* We've found a dense enough segment.
|
|
|
|
In the worst case, !stop = start and the segment is a single point *)
|
|
|
|
(* Record the segment and continue *)
|
|
|
|
(start, !stop) :: partition (!stop + 1) in
|
|
|
|
let part = partition 0 in
|
|
|
|
(* Build the two tables *)
|
|
|
|
let transl = Array.new (List.length part) (0, 0, 0)
|
1995-06-18 07:44:56 -07:00
|
|
|
and switch = ref [] in
|
1995-05-04 03:15:53 -07:00
|
|
|
let tr_pos = ref 0
|
1995-06-18 07:44:56 -07:00
|
|
|
and sw_ind = ref 1 in (* Slot 0 in switch is reserved for Lstaticfail *)
|
1995-05-04 03:15:53 -07:00
|
|
|
List.iter
|
|
|
|
(fun (start, stop) ->
|
|
|
|
transl.(!tr_pos) <- (keyv.(start), keyv.(stop), !sw_ind);
|
|
|
|
for i = start to stop do
|
1995-06-18 07:44:56 -07:00
|
|
|
switch := (!sw_ind + keyv.(i) - keyv.(start), actv.(i)) :: !switch
|
1995-05-04 03:15:53 -07:00
|
|
|
done;
|
|
|
|
incr tr_pos;
|
|
|
|
sw_ind := !sw_ind + keyv.(stop) - keyv.(start) + 1)
|
|
|
|
part;
|
1995-06-18 07:44:56 -07:00
|
|
|
(transl, !switch, !sw_ind)
|