feat(util): add Vec_sig to express common vector interface

This commit is contained in:
Simon Cruanes 2021-08-20 18:04:54 -04:00
parent 3fbb9af664
commit c8eb1ec29e
No known key found for this signature in database
GPG key ID: 4AC01D0849AA62B6
5 changed files with 35 additions and 53 deletions

View file

@ -6,6 +6,7 @@ module Util = Util
module Vec = Vec module Vec = Vec
module VecI32 = VecI32 module VecI32 = VecI32
module Vec_float = Vec_float module Vec_float = Vec_float
module Vec_sig = Vec_sig
module Bitvec = Bitvec module Bitvec = Bitvec
module IArray = IArray module IArray = IArray

View file

@ -3,37 +3,13 @@
These vectors are more optimized than {!Vec}. *) These vectors are more optimized than {!Vec}. *)
type t include Vec_sig.S with type elt := int
val create : ?cap:int -> unit -> t
val ensure_size : t -> int -> unit val ensure_size : t -> int -> unit
val size : t -> int
val clear : t -> unit
val is_empty : t -> bool
val push : t -> int -> unit
val pop : t -> int
val push_i32 : t -> int32 -> unit val push_i32 : t -> int32 -> unit
val get : t -> int -> int
val get_i32 : t -> int -> int32 val get_i32 : t -> int -> int32
val set : t -> int -> int -> unit
val set_i32 : t -> int -> int32 -> unit val set_i32 : t -> int -> int32 -> unit
val shrink : t -> int -> unit
val iter : f:(int -> unit) -> t -> unit
val iteri : f:(int -> int -> unit) -> t -> unit
val to_iter : t -> int Iter.t
val pp : t CCFormat.printer

View file

@ -10,8 +10,8 @@ type t = {
let mk_arr_ sz : float_arr = A.create Bigarray.float64 Bigarray.c_layout sz let mk_arr_ sz : float_arr = A.create Bigarray.float64 Bigarray.c_layout sz
let create () : t = let create ?(cap=16) () : t =
{ sz=0; data=mk_arr_ 16 } { sz=0; data=mk_arr_ cap }
let[@inline] clear self = self.sz <- 0 let[@inline] clear self = self.sz <- 0
let[@inline] shrink self n = if n < self.sz then self.sz <- n let[@inline] shrink self n = if n < self.sz then self.sz <- n

View file

@ -3,31 +3,6 @@
These vectors are more optimized than {!Vec}. *) These vectors are more optimized than {!Vec}. *)
type t include Vec_sig.S with type elt := float
val create : unit -> t
val ensure_size : t -> int -> unit val ensure_size : t -> int -> unit
val size : t -> int
val clear : t -> unit
val is_empty : t -> bool
val push : t -> float -> unit
val pop : t -> float
val get : t -> int -> float
val set : t -> int -> float -> unit
val shrink : t -> int -> unit
val iter : f:(float -> unit) -> t -> unit
val iteri : f:(int -> float -> unit) -> t -> unit
val to_iter : t -> float Iter.t
val pp : t CCFormat.printer

30
src/util/Vec_sig.ml Normal file
View file

@ -0,0 +1,30 @@
module type S = sig
type elt
type t
val create : ?cap:int -> unit -> t
val size : t -> int
val clear : t -> unit
val is_empty : t -> bool
val push : t -> elt -> unit
val pop : t -> elt
val get : t -> int -> elt
val set : t -> int -> elt -> unit
val shrink : t -> int -> unit
val iter : f:(int -> unit) -> t -> unit
val iteri : f:(int -> elt -> unit) -> t -> unit
val to_iter : t -> elt Iter.t
val pp : t CCFormat.printer
end