Skip to content

Commit

Permalink
Create Stamped_hashtable structure
Browse files Browse the repository at this point in the history
  • Loading branch information
let-def committed May 17, 2023
1 parent 8f1d3f1 commit 147f0c3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/utils/stamped_hashtable.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
type ('a, 'b) t = {
table: ('a, 'b) Hashtbl.t;
mutable recent: (int * 'a) list;
mutable sorted: (int * 'a) list;
}

let create n = {
table = Hashtbl.create n;
recent = [];
sorted = [];
}

let add t ~stamp a b =
Hashtbl.add t.table a b;
t.recent <- (stamp, a) :: t.recent

let mem t a =
Hashtbl.mem t.table a

let find t a =
Hashtbl.find t.table a

(* Sort by decreasing stamps *)
let order (i1, _) (i2, _) =
Int.compare i2 i1

let rec filter_prefix pred = function
| x :: xs when not (pred x) ->
filter_prefix pred xs
| xs -> xs

let backtrack t ~stamp =
let process (stamp', path) =
if stamp' > stamp then (
Hashtbl.remove t.table path;
false
) else
true
in
let recent =
t.recent
|> List.filter process
|> List.fast_sort order
in
t.recent <- [];
let sorted =
t.sorted
|> filter_prefix process
|> List.merge order recent
in
t.sorted <- sorted
11 changes: 11 additions & 0 deletions src/utils/stamped_hashtable.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type ('a, 'b) t
type changes

val create : int -> ('a, 'b) t
val add : ('a, 'b) t -> stamp:int -> 'a -> 'b -> unit
val mem : ('a, 'b) t -> 'a -> bool
val find : ('a, 'b) t -> 'a -> 'b

(* [backtrack table ~stamp] remove all items of [table] with a stamp strictly
greater than [stamp] *)
val backtrack : ('a, 'b) t -> stamp:int -> unit

0 comments on commit 147f0c3

Please sign in to comment.