Skip to content

Commit

Permalink
refactor(pkg): stop using integers for ordering (ocaml#11313)
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 36dee76 commit f03bf33
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
8 changes: 6 additions & 2 deletions src/0install-solver/cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ open Fiber.O
module Make (CacheEntry : sig
type t

val compare : t -> t -> int
val compare : t -> t -> Ordering.t
end) =
struct
module M = Map.Make (CacheEntry)
module M = Map.Make (struct
include CacheEntry

let compare x y = Ordering.to_int (CacheEntry.compare x y)
end)

type 'a snapshot = 'a M.t
type 'a t = 'a snapshot ref
Expand Down
2 changes: 1 addition & 1 deletion src/0install-solver/cache.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Make (CacheEntry : sig
type t

val compare : t -> t -> int
val compare : t -> t -> Ordering.t
end) : sig
(** The cache is used in [build_problem], while the clauses are still being added. *)
type 'a t
Expand Down
9 changes: 5 additions & 4 deletions src/0install-solver/diagnostics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ module Make (Results : S.SOLVER_RESULT) = struct
let deps = Model.requires t.role impl in
List.find_map deps ~f:(fun dep ->
let { Model.dep_role; _ } = Model.dep_info dep in
if Model.Role.compare dep_role t.role <> 0
then None
else
match Model.Role.compare dep_role t.role with
| Lt | Gt -> None
| Eq ->
(* It depends on itself. *)
Model.restrictions dep
|> List.find_map ~f:(fun r ->
Expand Down Expand Up @@ -381,7 +381,8 @@ module Make (Results : S.SOLVER_RESULT) = struct
| [] -> None
| cl :: cls ->
(match Classes.find classes cl with
| Some other_role when Model.Role.compare role other_role <> 0 ->
| Some other_role
when not (Ordering.is_eq (Model.Role.compare role other_role)) ->
Some (`ClassConflict (other_role, cl))
| _ -> aux cls)
in
Expand Down
2 changes: 1 addition & 1 deletion src/0install-solver/s.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module type CORE_MODEL = sig
type t

val pp : t -> 'tag Pp.t
val compare : t -> t -> int
val compare : t -> t -> Ordering.t
end

(** An [impl] is something that can fill a [Role.t] (e.g. a particular version of
Expand Down
2 changes: 1 addition & 1 deletion src/0install-solver/solver_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ module Make (Model : S.SOLVER_INPUT) = struct
let module Requirements = struct
type t = Output.requirements

let equal x y = Int.equal 0 (Output.Role.compare x y)
let equal x y = Ordering.is_eq (Output.Role.compare x y)
let hash = Poly.hash
let to_dyn = Dyn.opaque
end
Expand Down
8 changes: 4 additions & 4 deletions src/dune_pkg/opam_solver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ module Solver = struct

let compare a b =
match a, b with
| Real a, Real b -> OpamPackage.Name.compare a.name b.name
| Virtual (a, _), Virtual (b, _) -> Ordering.to_int (Poly.compare a b)
| Real _, Virtual _ -> -1
| Virtual _, Real _ -> 1
| Real a, Real b -> Ordering.of_int (OpamPackage.Name.compare a.name b.name)
| Virtual (a, _), Virtual (b, _) -> Poly.compare a b
| Real _, Virtual _ -> Lt
| Virtual _, Real _ -> Gt
;;
end

Expand Down

0 comments on commit f03bf33

Please sign in to comment.