mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 19:25:28 -05:00
functorize ring buffer over ARRAY sig
This commit is contained in:
parent
07f0afcd28
commit
c7607f8ce7
2 changed files with 291 additions and 244 deletions
|
|
@ -22,10 +22,35 @@
|
|||
|
||||
(** Polymorphic Circular Buffer for IO *)
|
||||
|
||||
type 'a t = {
|
||||
module type ARRAY = sig
|
||||
type elt
|
||||
type t
|
||||
|
||||
val make: int -> elt -> t
|
||||
val length: t -> int
|
||||
|
||||
val get: t -> int -> elt
|
||||
|
||||
val set: t -> int -> elt -> unit
|
||||
|
||||
val sub: t -> int -> int -> t
|
||||
val max_length: int
|
||||
|
||||
val copy : t -> t
|
||||
val of_list : elt list -> t
|
||||
val to_list : t -> elt list
|
||||
val blit : t -> int -> t -> int -> int -> unit
|
||||
|
||||
val iter : (elt -> unit) -> t -> unit
|
||||
end
|
||||
|
||||
module Make(Array:ARRAY) =
|
||||
struct
|
||||
|
||||
type t = {
|
||||
mutable start : int;
|
||||
mutable stop : int; (* excluded *)
|
||||
mutable buf : 'a array;
|
||||
mutable buf : Array.t;
|
||||
bounded : bool;
|
||||
size : int
|
||||
}
|
||||
|
|
@ -248,8 +273,4 @@ let peek_front b = if is_empty b then
|
|||
let peek_back b = if is_empty b then
|
||||
raise Empty else Array.get b.buf
|
||||
(if b.stop = 0 then capacity b - 1 else b.stop-1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -20,89 +20,115 @@
|
|||
|
||||
(** Circular Polymorphic Buffer for IO *)
|
||||
|
||||
type 'a t = private {
|
||||
module type ARRAY = sig
|
||||
type elt
|
||||
type t
|
||||
|
||||
val make: int -> elt -> t
|
||||
val length: t -> int
|
||||
|
||||
val get: t -> int -> elt
|
||||
|
||||
val set: t -> int -> elt -> unit
|
||||
|
||||
val sub: t -> int -> int -> t
|
||||
val max_length: int
|
||||
|
||||
val copy : t -> t
|
||||
val of_list : elt list -> t
|
||||
val to_list : t -> elt list
|
||||
val blit : t -> int -> t -> int -> int -> unit
|
||||
|
||||
val iter : (elt -> unit) -> t -> unit
|
||||
end
|
||||
|
||||
|
||||
module Make : functor (Array:ARRAY) ->
|
||||
sig
|
||||
|
||||
type t = private {
|
||||
mutable start : int;
|
||||
mutable stop : int; (* excluded *)
|
||||
mutable buf : 'a array;
|
||||
mutable buf : Array.t;
|
||||
bounded: bool;
|
||||
size : int
|
||||
}
|
||||
|
||||
exception Empty
|
||||
|
||||
val create : ?bounded:bool -> int -> 'a t
|
||||
val create : ?bounded:bool -> int -> t
|
||||
(** [create ?bounded size] creates a new buffer with given size.
|
||||
Defaults to [bounded=false]. *)
|
||||
|
||||
val copy : 'a t ->'a t
|
||||
val copy : t -> t
|
||||
(** fresh copy of the buffer *)
|
||||
|
||||
val capacity : 'a t -> int
|
||||
val capacity : t -> int
|
||||
(** length of the inner buffer *)
|
||||
|
||||
val max_capacity : 'a t -> int option
|
||||
val max_capacity : t -> int option
|
||||
(** maximum length of the inner buffer, or [None] if unbounded. *)
|
||||
|
||||
val length : 'a t -> int
|
||||
val length : t -> int
|
||||
(** number of elements currently stored in the buffer *)
|
||||
|
||||
val blit_from : 'a t -> 'a array -> int -> int -> unit
|
||||
val blit_from : t -> Array.t -> int -> int -> unit
|
||||
(** [blit_from buf from_buf o len] copies the slice [o, ... o + len - 1] from
|
||||
a input buffer [from_buf] to the end of the buffer.
|
||||
@raise Invalid_argument if [o,len] is not a valid slice of [s] *)
|
||||
|
||||
val blit_into : 'a t -> 'a array -> int -> int -> int
|
||||
val blit_into : t -> Array.t -> int -> int -> int
|
||||
(** [blit_into buf to_buf o len] copies at most [len] elements from [buf]
|
||||
into [to_buf] starting at offset [o] in [s].
|
||||
@return the number of elements actually copied ([min len (length buf)]).
|
||||
@raise Invalid_argument if [o,len] is not a valid slice of [s] *)
|
||||
|
||||
val to_list : 'a t -> 'a list
|
||||
val to_list : t -> Array.elt list
|
||||
(** extract the current content into a list *)
|
||||
|
||||
val clear : 'a t -> unit
|
||||
val clear : t -> unit
|
||||
(** clear the content of the buffer. Doesn't actually destroy the content. *)
|
||||
|
||||
val reset : 'a t -> unit
|
||||
val reset : t -> unit
|
||||
(** clear the content of the buffer, and also resize it to a default size *)
|
||||
|
||||
val is_empty :'a t -> bool
|
||||
val is_empty :t -> bool
|
||||
(** is the buffer empty (i.e. contains no elements)? *)
|
||||
|
||||
val next : 'a t -> 'a
|
||||
val next : t -> Array.elt
|
||||
(** obtain next element (the first one of the buffer)
|
||||
@raise Empty if the buffer is empty *)
|
||||
|
||||
val junk : 'a t -> unit
|
||||
val junk : t -> unit
|
||||
(** Drop next element.
|
||||
@raise Empty if the buffer is already empty *)
|
||||
|
||||
val skip : 'a t -> int -> unit
|
||||
val skip : t -> int -> unit
|
||||
(** [skip b len] removes [len] elements from [b].
|
||||
@raise Invalid_argument if [len > length b]. *)
|
||||
|
||||
val iteri : 'a t -> (int -> 'a -> unit) -> unit
|
||||
val iteri : t -> (int -> Array.elt -> unit) -> unit
|
||||
(** [iteri b f] calls [f i t] for each element [t] in [buf], with [i]
|
||||
being its relative index within [buf]. *)
|
||||
|
||||
val get : 'a t -> int -> 'a
|
||||
val get : t -> int -> Array.elt
|
||||
(** [get buf i] returns the [i]-th element of [buf], ie the one that
|
||||
is returned by [next buf] after [i-1] calls to [junk buf].
|
||||
@raise Invalid_argument if the index is invalid (> [length buf]) *)
|
||||
|
||||
val push_back : 'a t -> 'a -> unit
|
||||
val push_back : t -> Array.elt -> unit
|
||||
(** Push value at the back *)
|
||||
|
||||
val peek_front : 'a t -> 'a
|
||||
val peek_front : t -> Array.elt
|
||||
(** First value, or Empty *)
|
||||
|
||||
val peek_back : 'a t -> 'a
|
||||
val peek_back : t -> Array.elt
|
||||
(** Last value, or Empty *)
|
||||
|
||||
val take_back : 'a t -> 'a
|
||||
val take_back : t -> Array.elt
|
||||
(** Take last value, or raise Empty *)
|
||||
|
||||
val take_front : 'a t -> 'a
|
||||
val take_front : t -> Array.elt
|
||||
(** Take first value, or raise Empty *)
|
||||
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue