Use Hashtbl generic interface.
This commit is contained in:
@@ -1,21 +1,16 @@
|
||||
module IntSet = Set.Make (Int)
|
||||
module IntHashtbl = Hashtbl.Make (Int)
|
||||
|
||||
(** [add_rule a b m] adds the rule that [a] must appear before [b] to the rule
|
||||
map [m]. Returns the updated map [m] *)
|
||||
let add_rule a b m =
|
||||
match IntHashtbl.find_opt m a with
|
||||
| None ->
|
||||
IntHashtbl.add m a (IntSet.singleton b);
|
||||
m
|
||||
| Some s ->
|
||||
IntHashtbl.replace m a (IntSet.add b s);
|
||||
m
|
||||
match Hashtbl.find_opt m a with
|
||||
| None -> Hashtbl.add m a (IntSet.singleton b)
|
||||
| Some s -> Hashtbl.replace m a (IntSet.add b s)
|
||||
|
||||
(** [find_rule a b m] returns [true] if the rule map [m] says that [a] should
|
||||
appear before [b]. *)
|
||||
let find_rule a b m =
|
||||
match IntHashtbl.find_opt m a with
|
||||
match Hashtbl.find_opt m a with
|
||||
| Some s -> ( match IntSet.find_opt b s with Some _ -> true | None -> false)
|
||||
| None -> false
|
||||
|
||||
@@ -45,15 +40,18 @@ let rec is_page_order_valid m pages =
|
||||
[tail] starts the line after the empty line. *)
|
||||
let parse_rules =
|
||||
let re = Str.regexp_string "|" in
|
||||
let rec impl acc = function
|
||||
| "" :: t -> (acc, t)
|
||||
let m = Hashtbl.create 17 in
|
||||
let rec impl = function
|
||||
| "" :: t -> (m, t)
|
||||
| [] -> failwith "parse_rules.impl"
|
||||
| h :: t -> (
|
||||
match List.map int_of_string (Str.split re h) with
|
||||
| [ a; b ] -> impl (add_rule a b acc) t
|
||||
| [ a; b ] ->
|
||||
add_rule a b m;
|
||||
impl t
|
||||
| _ -> failwith "parse_rules.impl")
|
||||
in
|
||||
impl (IntHashtbl.create 17)
|
||||
impl
|
||||
|
||||
(** [parse_page_orders lst] parses a list of page orders. *)
|
||||
let parse_page_orders =
|
||||
|
Reference in New Issue
Block a user