mirror of
https://github.com/c-cube/sidekick.git
synced 2025-12-06 03:05:31 -05:00
feat(vec): factor a bit of code for auxiliary functions in vectors
This commit is contained in:
parent
acc682c5af
commit
74326b39c0
3 changed files with 65 additions and 22 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue