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