Skip to content

Commit

Permalink
refactor(pkg): move swap to [Stdune] (ocaml#11284)
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg authored and ElectreAAS committed Jan 27, 2025
1 parent fb9a5dd commit 9593f58
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
8 changes: 8 additions & 0 deletions otherlibs/stdune/src/array.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module Array = Stdlib.Array

let swap arr i j =
let first, second = arr.(i), arr.(j) in
arr.(i) <- second;
arr.(j) <- first
;;

module T = struct
include ArrayLabels

Expand Down
1 change: 1 addition & 0 deletions otherlibs/stdune/src/array.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val map : 'a t -> f:('a -> 'b) -> 'b t
val exists : 'a t -> f:('a -> bool) -> bool
val fold_right : 'a t -> f:('a -> 'acc -> 'acc) -> init:'acc -> 'acc
val swap : 'a t -> int -> int -> unit

module Immutable : sig
type 'a t
Expand Down
12 changes: 3 additions & 9 deletions src/0install-solver/sat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ let log_debug p =
Pp.to_fmt Format.std_formatter (Pp.vbox (Pp.hovbox (Pp.text "sat: " ++ p)) ++ Pp.cut)
;;

let swap arr i j =
let first, second = arr.(i), arr.(j) in
arr.(i) <- second;
arr.(j) <- first
;;

module Make (User : USER) = struct
type clause =
< (* [lit] is now [True]. Add any new deductions.
Expand Down Expand Up @@ -401,7 +395,7 @@ module Make (User : USER) = struct
- value[lits[0]] = Undecided | True
- value[lits[1]] = False
If it's the other way around, just swap them before we start. *)
if lit_equal lits.(0) (neg lit) then swap lits 0 1;
if lit_equal lits.(0) (neg lit) then Array.swap lits 0 1;
if lit_value lits.(0) = True
then (
(* We're already satisfied. Do nothing. *)
Expand All @@ -422,7 +416,7 @@ module Make (User : USER) = struct
| Undecided | True ->
(* If it's True then we've already done our job,
so this means we don't get notified unless we backtrack, which is fine. *)
swap lits 1 i;
Array.swap lits 1 i;
watch_lit (neg lits.(1)) (self :> clause);
true
| False -> find_not_false (i + 1))
Expand Down Expand Up @@ -572,7 +566,7 @@ module Make (User : USER) = struct
best_level := level;
best_i := i)
done;
swap lits 1 !best_i);
Array.swap lits 1 !best_i);
(* Watch the first two literals in the clause (both must be
undefined at this point). *)
let watch i = watch_lit (neg lits.(i)) clause in
Expand Down

0 comments on commit 9593f58

Please sign in to comment.