move 2405 to use a hash table.

This commit is contained in:
2024-12-05 11:26:38 +00:00
parent cbc5c808f5
commit 137ef06d9d

View File

@@ -1,19 +1,21 @@
module IntSet = Set.Make (Int)
module IntMap = Map.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]. *)
let add_rule a b =
let update_rule = function
| None -> Some (IntSet.singleton b)
| Some s -> Some (IntSet.add b s)
in
IntMap.update a update_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
(** [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 IntMap.find_opt a m with
match IntHashtbl.find_opt m a with
| Some s -> ( match IntSet.find_opt b s with Some _ -> true | None -> false)
| None -> false
@@ -51,7 +53,7 @@ let parse_rules =
| [ a; b ] -> impl (add_rule a b acc) t
| _ -> failwith "parse_rules.impl")
in
impl IntMap.empty
impl (IntHashtbl.create 17)
(** [parse_page_orders lst] parses a list of page orders. *)
let parse_page_orders =