From 74326b39c01384c8e083463b4d8af4e9fb2f036d Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 26 Sep 2021 21:01:04 -0400 Subject: [PATCH] feat(vec): factor a bit of code for auxiliary functions in vectors --- src/util/VecI32.ml | 15 ++++++------ src/util/Vec_float.ml | 16 ++++++------- src/util/Vec_sig.ml | 56 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/util/VecI32.ml b/src/util/VecI32.ml index 33f5138c..207946c6 100644 --- a/src/util/VecI32.ml +++ b/src/util/VecI32.ml @@ -91,10 +91,11 @@ let[@inline] iteri ~f self = f i (Int32.to_int self.data.{i}) done -let[@inline] to_iter self k = iter ~f:k self - -let pp ppx out self = - Format.fprintf out "[@["; - iteri self ~f:(fun i x -> if i>0 then Format.fprintf out ",@ "; ppx out x); - Format.fprintf out "@]]" - +include Vec_sig.Make_extensions(struct + type nonrec elt = int + type nonrec t = t + let get = get + let size = size + let iter = iter + let iteri = iteri + end) diff --git a/src/util/Vec_float.ml b/src/util/Vec_float.ml index 1ddf9f05..55757b90 100644 --- a/src/util/Vec_float.ml +++ b/src/util/Vec_float.ml @@ -78,11 +78,11 @@ let[@inline] iteri ~f self = f i self.data.{i} done -let[@inline] to_iter self k = iter ~f:k self - -let pp ppx out self = - Format.fprintf out "[@["; - iteri self - ~f:(fun i x -> if i>0 then Format.fprintf out ",@ "; ppx out x); - Format.fprintf out "@]]" - +include Vec_sig.Make_extensions(struct + type nonrec elt = float + type nonrec t = t + let get = get + let size = size + let iter = iter + let iteri = iteri + end) diff --git a/src/util/Vec_sig.ml b/src/util/Vec_sig.ml index 52635bdc..e1e21f63 100644 --- a/src/util/Vec_sig.ml +++ b/src/util/Vec_sig.ml @@ -1,12 +1,22 @@ -module type S = sig +(** Basics *) +module type BASE_RO = sig type elt type t - val create : ?cap:int -> unit -> t - val size : t -> int + val get : t -> int -> elt + + val iter : f:(elt -> unit) -> t -> unit + val iteri : f:(int -> elt -> unit) -> t -> unit +end + +module type BASE = sig + include BASE_RO + + val create : ?cap:int -> unit -> t + val clear : t -> unit val is_empty : t -> bool @@ -21,16 +31,48 @@ module type S = sig val pop : t -> elt - val get : t -> int -> elt - val set : t -> int -> elt -> unit val shrink : t -> int -> unit +end - val iter : f:(elt -> unit) -> t -> unit - val iteri : f:(int -> elt -> unit) -> t -> unit +module type EXTENSIONS = sig + type elt + type t val to_iter : t -> elt Iter.t + val to_array : t -> elt array + val pp : elt CCFormat.printer -> t CCFormat.printer end + +module type S = sig + include BASE + include EXTENSIONS with type t := t and type elt := elt +end + +module Make_extensions(B: BASE_RO) + : EXTENSIONS + with type t := B.t + and type elt := B.elt += struct + include B + + let[@inline] to_iter self k = iter ~f:k self + + let to_array self = + if size self = 0 then [||] + else ( + let a = Array.make (size self) (get self 0) in + iteri self ~f:(Array.set a); + a + ) + + let pp ppx out self = + Format.fprintf out "[@["; + iteri self + ~f:(fun i x -> if i>0 then Format.fprintf out ",@ "; ppx out x); + Format.fprintf out "@]]" + +end