Skip to content

Commit 147f0c3

Browse files
committed
Create Stamped_hashtable structure
1 parent 8f1d3f1 commit 147f0c3

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/utils/stamped_hashtable.ml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
type ('a, 'b) t = {
2+
table: ('a, 'b) Hashtbl.t;
3+
mutable recent: (int * 'a) list;
4+
mutable sorted: (int * 'a) list;
5+
}
6+
7+
let create n = {
8+
table = Hashtbl.create n;
9+
recent = [];
10+
sorted = [];
11+
}
12+
13+
let add t ~stamp a b =
14+
Hashtbl.add t.table a b;
15+
t.recent <- (stamp, a) :: t.recent
16+
17+
let mem t a =
18+
Hashtbl.mem t.table a
19+
20+
let find t a =
21+
Hashtbl.find t.table a
22+
23+
(* Sort by decreasing stamps *)
24+
let order (i1, _) (i2, _) =
25+
Int.compare i2 i1
26+
27+
let rec filter_prefix pred = function
28+
| x :: xs when not (pred x) ->
29+
filter_prefix pred xs
30+
| xs -> xs
31+
32+
let backtrack t ~stamp =
33+
let process (stamp', path) =
34+
if stamp' > stamp then (
35+
Hashtbl.remove t.table path;
36+
false
37+
) else
38+
true
39+
in
40+
let recent =
41+
t.recent
42+
|> List.filter process
43+
|> List.fast_sort order
44+
in
45+
t.recent <- [];
46+
let sorted =
47+
t.sorted
48+
|> filter_prefix process
49+
|> List.merge order recent
50+
in
51+
t.sorted <- sorted

src/utils/stamped_hashtable.mli

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type ('a, 'b) t
2+
type changes
3+
4+
val create : int -> ('a, 'b) t
5+
val add : ('a, 'b) t -> stamp:int -> 'a -> 'b -> unit
6+
val mem : ('a, 'b) t -> 'a -> bool
7+
val find : ('a, 'b) t -> 'a -> 'b
8+
9+
(* [backtrack table ~stamp] remove all items of [table] with a stamp strictly
10+
greater than [stamp] *)
11+
val backtrack : ('a, 'b) t -> stamp:int -> unit

0 commit comments

Comments
 (0)