Skip to content

Commit 9812871

Browse files
committed
refactor: Rrplace Veci and Vec_float with polymorphic Vec
1 parent fcbaaae commit 9812871

10 files changed

Lines changed: 53 additions & 274 deletions

File tree

src/checker/drup_check.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ end
5353
5454
Each event is checked by reverse-unit propagation on previous events. *)
5555
module Fwd_check : sig
56-
type error = [ `Bad_steps of Veci.t | `No_empty_clause ]
56+
type error = [ `Bad_steps of int Vec.t | `No_empty_clause ]
5757

5858
val pp_error : Trace.t -> error Fmt.printer
5959

@@ -62,10 +62,10 @@ module Fwd_check : sig
6262
success. In case of error it returns [Error idxs] where [idxs] are the
6363
indexes in the trace of the steps that failed. *)
6464
end = struct
65-
type t = { checker: Checker.t; errors: Veci.t }
65+
type t = { checker: Checker.t; errors: int Vec.t }
6666

6767
let create cstore : t =
68-
{ checker = Checker.create cstore; errors = Veci.create () }
68+
{ checker = Checker.create cstore; errors = Vec.create () }
6969

7070
(* check event, return [true] if it's valid *)
7171
let check_op (self : t) i (op : Trace.op) : bool =
@@ -85,15 +85,15 @@ end = struct
8585
Checker.del_clause self.checker c;
8686
true
8787

88-
type error = [ `Bad_steps of Veci.t | `No_empty_clause ]
88+
type error = [ `Bad_steps of int Vec.t | `No_empty_clause ]
8989

9090
let pp_error trace out = function
9191
| `No_empty_clause -> Fmt.string out "no empty clause found"
9292
| `Bad_steps bad ->
93-
let n0 = Veci.get bad 0 in
93+
let n0 = Vec.get bad 0 in
9494
Fmt.fprintf out
9595
"@[<v>checking failed on %d ops.@ @[<2>First failure is op[%d]:@ %a@]@]"
96-
(Veci.size bad) n0 Trace.pp_op (Trace.get trace n0)
96+
(Vec.size bad) n0 Trace.pp_op (Trace.get trace n0)
9797

9898
let check trace : _ result =
9999
let self = create (Trace.cstore trace) in
@@ -114,13 +114,13 @@ end = struct
114114
) else (
115115
Log.debugf 10 (fun k ->
116116
k "(@[check.proof_rule.fail@ :idx %d@ :op %a@])" i Trace.pp_op op);
117-
Veci.push self.errors i
117+
Vec.push self.errors i
118118
));
119119

120-
Log.debugf 10 (fun k -> k "found %d errors" (Veci.size self.errors));
120+
Log.debugf 10 (fun k -> k "found %d errors" (Vec.size self.errors));
121121
if not !has_false then
122122
Error `No_empty_clause
123-
else if Veci.size self.errors > 0 then
123+
else if Vec.size self.errors > 0 then
124124
Error (`Bad_steps self.errors)
125125
else
126126
Ok ()

src/main/show_trace.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type state = {
1010
p_reader: Proof.Trace_reader.t;
1111
}
1212

13-
let show_sat (self : state) ~tag v : unit =
13+
let show_sat (_self : state) ~tag v : unit =
1414
match tag with
1515
| "AssCSat" ->
1616
(match

src/sat/heap.ml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module type S = Heap_intf.S
44
module Make (Elt : RANKED) = struct
55
type elt_store = Elt.store
66
type elt = Elt.t
7-
type t = { store: elt_store; heap: Veci.t (* vec of elements *) }
7+
type t = { store: elt_store; heap: int Vec.t (* vec of elements *) }
88

99
let _absent_index = -1
10-
let create store : t = { store; heap = Veci.create () }
10+
let create store : t = { store; heap = Vec.create () }
1111
let[@inline] left i = (i lsl 1) + 1 (* i*2 + 1 *)
1212

1313
let[@inline] right i = (i + 1) lsl 1 (* (i+1)*2 *)
@@ -16,17 +16,17 @@ module Make (Elt : RANKED) = struct
1616

1717
(*
1818
let rec heap_property cmp ({heap=heap} as s) i =
19-
i >= (Veci.size heap) ||
19+
i >= (Vec.size heap) ||
2020
((i = 0 || not(cmp (Vec. get heap i) (Vec.get heap (parent i))))
2121
&& heap_property cmp s (left i) && heap_property cmp s (right i))
2222
2323
let heap_property cmp s = heap_property cmp s 1
2424
*)
2525

26-
let[@inline] get_elt_ self i = Elt.of_int_unsafe (Veci.get self.heap i)
26+
let[@inline] get_elt_ self i = Elt.of_int_unsafe (Vec.get self.heap i)
2727

2828
let[@inline] set_elt_ self i elt =
29-
Veci.set self.heap i (elt : Elt.t :> int)
29+
Vec.set self.heap i (elt : Elt.t :> int)
3030

3131
(* [elt] is above or at its expected position. Move it up the heap
3232
(towards high indices) to restore the heap property *)
@@ -43,7 +43,7 @@ module Make (Elt : RANKED) = struct
4343
Elt.set_heap_idx self.store elt !i
4444

4545
let percolate_down (self : t) (elt : Elt.t) : unit =
46-
let sz = Veci.size self.heap in
46+
let sz = Vec.size self.heap in
4747
let li = ref (left (Elt.heap_idx self.store elt)) in
4848
let ri = ref (right (Elt.heap_idx self.store elt)) in
4949
let i = ref (Elt.heap_idx self.store elt) in
@@ -83,7 +83,7 @@ module Make (Elt : RANKED) = struct
8383

8484
let filter (self : t) filt : unit =
8585
let j = ref 0 in
86-
let lim = Veci.size self.heap in
86+
let lim = Vec.size self.heap in
8787
for i = 0 to lim - 1 do
8888
if filt (get_elt_ self i) then (
8989
set_elt_ self !j (get_elt_ self i);
@@ -92,24 +92,24 @@ module Make (Elt : RANKED) = struct
9292
) else
9393
Elt.set_heap_idx self.store (get_elt_ self i) _absent_index
9494
done;
95-
Veci.shrink self.heap (lim - !j);
95+
Vec.shrink self.heap (lim - !j);
9696
for i = (lim / 2) - 1 downto 0 do
9797
percolate_down self (get_elt_ self i)
9898
done
9999

100-
let[@inline] size s = Veci.size s.heap
101-
let[@inline] is_empty s = Veci.is_empty s.heap
100+
let[@inline] size s = Vec.size s.heap
101+
let[@inline] is_empty s = Vec.is_empty s.heap
102102

103103
let clear self : unit =
104-
Veci.iter self.heap ~f:(fun e ->
104+
Vec.iter self.heap ~f:(fun e ->
105105
Elt.set_heap_idx self.store (Elt.of_int_unsafe e) _absent_index);
106-
Veci.clear self.heap;
106+
Vec.clear self.heap;
107107
()
108108

109109
let insert self elt =
110110
if not (in_heap self elt) then (
111-
Elt.set_heap_idx self.store elt (Veci.size self.heap);
112-
Veci.push self.heap (elt : Elt.t :> int);
111+
Elt.set_heap_idx self.store elt (Vec.size self.heap);
112+
Vec.push self.heap (elt : Elt.t :> int);
113113
percolate_up self elt
114114
)
115115

@@ -128,21 +128,21 @@ module Make (Elt : RANKED) = struct
128128
*)
129129

130130
let remove_min self =
131-
match Veci.size self.heap with
131+
match Vec.size self.heap with
132132
| 0 -> raise Not_found
133133
| 1 ->
134-
let x = Elt.of_int_unsafe (Veci.pop self.heap) in
134+
let x = Elt.of_int_unsafe (Vec.pop_exn self.heap) in
135135
Elt.set_heap_idx self.store x _absent_index;
136136
x
137137
| _ ->
138138
let x = get_elt_ self 0 in
139-
let new_hd = Elt.of_int_unsafe (Veci.pop self.heap) in
139+
let new_hd = Elt.of_int_unsafe (Vec.pop_exn self.heap) in
140140
(* heap.last() *)
141141
set_elt_ self 0 new_hd;
142142
Elt.set_heap_idx self.store x _absent_index;
143143
Elt.set_heap_idx self.store new_hd 0;
144144
(* enforce heap property again *)
145-
if Veci.size self.heap > 1 then percolate_down self new_hd;
145+
if Vec.size self.heap > 1 then percolate_down self new_hd;
146146
x
147147
end
148148
[@@inline]

src/sat/solver.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Atom = Store.Atom
1313
module Var = Store.Var
1414
module Clause = Store.Clause
1515

16-
module H = Heap.Make [@specialise] (struct
16+
module H = Heap.Make (struct
1717
type store = Store.t
1818
type t = var
1919

@@ -262,7 +262,7 @@ type t = {
262262
store them here. *)
263263
trail: AVec.t;
264264
(* decision stack + propagated elements (atoms or assignments). *)
265-
var_levels: Veci.t; (* decision levels in [trail] *)
265+
var_levels: int Vec.t; (* decision levels in [trail] *)
266266
assumptions: AVec.t; (* current assumptions *)
267267
mutable th_head: int;
268268
(* Start offset in the queue {!trail} of
@@ -328,7 +328,7 @@ let create_ ~store ~tracer ~stat ~max_clauses_learnt (plugin : plugin) : t =
328328
th_head = 0;
329329
elt_head = 0;
330330
trail = AVec.create ();
331-
var_levels = Veci.create ();
331+
var_levels = Vec.create ();
332332
assumptions = AVec.create ();
333333
order = H.create store;
334334
var_incr = 1.;
@@ -357,7 +357,7 @@ let iter_clauses_learnt_ (self : t) ~f : unit =
357357
Vec.iter ~f:iter_pool self.clause_pools;
358358
()
359359

360-
let[@inline] decision_level st = Veci.size st.var_levels
360+
let[@inline] decision_level st = Vec.size st.var_levels
361361
let[@inline] nb_clauses st = CVec.size st.clauses_hyps
362362
let stat self = self.stat
363363

@@ -578,7 +578,7 @@ let preprocess_clause_ (self : t) (c : Clause.t) : Clause.t =
578578
let new_decision_level (self : t) =
579579
assert (self.th_head = AVec.size self.trail);
580580
assert (self.elt_head = AVec.size self.trail);
581-
Veci.push self.var_levels (AVec.size self.trail);
581+
Vec.push self.var_levels (AVec.size self.trail);
582582
let (module P) = self.plugin in
583583
P.push_level ();
584584
()
@@ -613,7 +613,7 @@ let cancel_until (self : t) lvl =
613613
else (
614614
Log.debugf 5 (fun k -> k "(@[sat.cancel-until %d@])" lvl);
615615
(* We set the head of the solver and theory queue to what it was. *)
616-
let head = ref (Veci.get self.var_levels lvl) in
616+
let head = ref (Vec.get self.var_levels lvl) in
617617
self.elt_head <- !head;
618618
self.th_head <- !head;
619619
(* Now we need to cleanup the vars that are not valid anymore
@@ -646,7 +646,7 @@ let cancel_until (self : t) lvl =
646646
assert (n > 0);
647647
(* Resize the vectors according to their new size. *)
648648
AVec.shrink self.trail !head;
649-
Veci.shrink self.var_levels lvl;
649+
Vec.shrink self.var_levels lvl;
650650
let (module P) = self.plugin in
651651
P.pop_levels n;
652652
Delayed_actions.clear_on_backtrack self.delayed_actions;

src/sat/store.ml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module Lit_tbl = Hashtbl.Make (Lit)
55

66
type cstore = {
77
c_lits: atom array Vec.t; (* storage for clause content *)
8-
c_activity: Vec_float.t;
9-
c_recycle_idx: Veci.t; (* recycle clause numbers that were GC'd *)
8+
c_activity: float Vec.t;
9+
c_recycle_idx: int Vec.t; (* recycle clause numbers that were GC'd *)
1010
c_proof: Step_vec.t; (* clause -> proof_rule for its proof *)
1111
c_attached: Bitvec.t;
1212
c_marked: Bitvec.t;
@@ -19,7 +19,7 @@ type t = {
1919
v_of_lit: var Lit_tbl.t; (* lit -> var *)
2020
v_level: int Vec.t; (* decision/assignment level, or -1 *)
2121
v_heap_idx: int Vec.t; (* index in priority heap *)
22-
v_weight: Vec_float.t; (* heuristic activity *)
22+
v_weight: float Vec.t; (* heuristic activity *)
2323
v_reason: var_reason option Vec.t; (* reason for assignment *)
2424
v_seen: Bitvec.t; (* generic temporary marker *)
2525
v_default_polarity: Bitvec.t; (* default polarity in decisions *)
@@ -52,7 +52,7 @@ let create ?(size = `Big) ~stat () : t =
5252
v_of_lit = Lit_tbl.create size_map;
5353
v_level = Vec.create ();
5454
v_heap_idx = Vec.create ();
55-
v_weight = Vec_float.create ();
55+
v_weight = Vec.create ();
5656
v_reason = Vec.create ();
5757
v_seen = Bitvec.create ();
5858
v_default_polarity = Bitvec.create ();
@@ -67,8 +67,8 @@ let create ?(size = `Big) ~stat () : t =
6767
c_store =
6868
{
6969
c_lits = Vec.create ();
70-
c_activity = Vec_float.create ();
71-
c_recycle_idx = Veci.create ~cap:0 ();
70+
c_activity = Vec.create ();
71+
c_recycle_idx = Vec.create ();
7272
c_proof = Step_vec.create ~cap:0 ();
7373
c_dead = Bitvec.create ();
7474
c_attached = Bitvec.create ();
@@ -88,10 +88,10 @@ module Var = struct
8888
let[@inline] set_level self v l = Vec.set self.v_level (v : var :> int) l
8989
let[@inline] reason self v = Vec.get self.v_reason (v : var :> int)
9090
let[@inline] set_reason self v r = Vec.set self.v_reason (v : var :> int) r
91-
let[@inline] weight self v = Vec_float.get self.v_weight (v : var :> int)
91+
let[@inline] weight self v = Vec.get self.v_weight (v : var :> int)
9292

9393
let[@inline] set_weight self v w =
94-
Vec_float.set self.v_weight (v : var :> int) w
94+
Vec.set self.v_weight (v : var :> int) w
9595

9696
let[@inline] mark self v = Bitvec.set self.v_seen (v : var :> int) true
9797
let[@inline] unmark self v = Bitvec.set self.v_seen (v : var :> int) false
@@ -208,16 +208,16 @@ module Clause = struct
208208
in
209209
(* allocate new ID *)
210210
let cid =
211-
if Veci.is_empty c_recycle_idx then
211+
if Vec.is_empty c_recycle_idx then
212212
Vec.size c_lits
213213
else
214-
Veci.pop c_recycle_idx
214+
Vec.pop_exn c_recycle_idx
215215
in
216216

217217
(* allocate space *)
218218
(let new_len = cid + 1 in
219219
Vec.ensure_size c_lits ~elt:[||] new_len;
220-
Vec_float.ensure_size c_activity new_len;
220+
Vec.ensure_size c_activity ~elt:0. new_len;
221221
Step_vec.ensure_size c_proof new_len;
222222
Bitvec.ensure_size c_attached new_len;
223223
Bitvec.ensure_size c_dead new_len;
@@ -298,17 +298,17 @@ module Clause = struct
298298
Bitvec.set c_removable cid false;
299299
Bitvec.set c_marked cid false;
300300
Vec.set c_lits cid [||];
301-
Vec_float.set c_activity cid 0.;
301+
Vec.set c_activity cid 0.;
302302

303-
Veci.push c_recycle_idx cid;
303+
Vec.push c_recycle_idx cid;
304304
(* recycle idx *)
305305
()
306306

307307
let[@inline] activity store c =
308-
Vec_float.get store.c_store.c_activity (c : t :> int)
308+
Vec.get store.c_store.c_activity (c : t :> int)
309309

310310
let[@inline] set_activity store c f =
311-
Vec_float.set store.c_store.c_activity (c : t :> int) f
311+
Vec.set store.c_store.c_activity (c : t :> int) f
312312

313313
let[@inline] atoms_a store c : atom array =
314314
Vec.get store.c_store.c_lits (c : t :> int)
@@ -372,7 +372,7 @@ let alloc_var_uncached_ ?default_pol:(pol = true) self (form : Lit.t) : var =
372372
Vec.push v_level (-1);
373373
Vec.push v_heap_idx (-1);
374374
Vec.push v_reason None;
375-
Vec_float.push v_weight 0.;
375+
Vec.push v_weight 0.;
376376
Bitvec.ensure_size v_seen v_idx;
377377
Bitvec.ensure_size v_default_polarity v_idx;
378378
Bitvec.set v_default_polarity v_idx pol;

src/util/Vec.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
type 'a t = { mutable data: 'a array; mutable sz: int }
22

33
let make n x = { data = Array.make n x; sz = 0 }
4+
45
let[@inline] create () = { data = [||]; sz = 0 }
6+
57
let[@inline] clear s = s.sz <- 0
68

79
let[@inline] shrink t i =

0 commit comments

Comments
 (0)