diff --git a/src/data/CCRingBuffer.ml b/src/data/CCRingBuffer.ml index d860de1b..d571d6f7 100644 --- a/src/data/CCRingBuffer.ml +++ b/src/data/CCRingBuffer.ml @@ -115,9 +115,101 @@ module Array = struct end -module Make(Array:Array.S) = +module type S = +sig + + module Array : Array.S + + type t = private { + mutable start : int; + mutable stop : int; (* excluded *) + mutable buf : Array.t; + bounded: bool; + size : int + } + exception Empty + + val create : ?bounded:bool -> int -> t + (** [create ?bounded size] creates a new buffer with given size. + Defaults to [bounded=false]. *) + + val copy : t -> t + (** fresh copy of the buffer *) + + val capacity : t -> int + (** length of the inner buffer *) + + val max_capacity : t -> int option + (** maximum length of the inner buffer, or [None] if unbounded. *) + + val length : t -> int + (** number of elements currently stored in the buffer *) + + 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 : 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 : t -> Array.elt list + (** extract the current content into a list *) + + val clear : t -> unit + (** clear the content of the buffer. Doesn't actually destroy the content. *) + + val reset : t -> unit + (** clear the content of the buffer, and also resize it to a default size *) + + val is_empty :t -> bool + (** is the buffer empty (i.e. contains no elements)? *) + + val next : t -> Array.elt + (** obtain next element (the first one of the buffer) + @raise Empty if the buffer is empty *) + + val junk : t -> unit + (** Drop next element. + @raise Empty if the buffer is already empty *) + + val skip : t -> int -> unit + (** [skip b len] removes [len] elements from [b]. + @raise Invalid_argument if [len > length b]. *) + + 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 : 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 : t -> Array.elt -> unit + (** Push value at the back *) + + val peek_front : t -> Array.elt + (** First value, or Empty *) + + val peek_back : t -> Array.elt + (** Last value, or Empty *) + + val take_back : t -> Array.elt + (** Take last value, or raise Empty *) + + val take_front : t -> Array.elt + (** Take first value, or raise Empty *) + +end + +module Make_array(Array:Array.S) = struct + module Array = Array type t = { mutable start : int; mutable stop : int; (* excluded *) @@ -346,3 +438,11 @@ struct (if b.stop = 0 then capacity b - 1 else b.stop-1) end +module Bytes = Make_array(Array.ByteArray) +module Floats = Make_array(Array.FloatArray) +module Ints = Make_array(Array.IntArray) +module Bools = Make_array(Array.BoolArray) + +module Make(Elt:sig type t end) = Make_array(Array.Make(Elt)) + + diff --git a/src/data/CCRingBuffer.mli b/src/data/CCRingBuffer.mli index 1affaaf9..6a10322f 100644 --- a/src/data/CCRingBuffer.mli +++ b/src/data/CCRingBuffer.mli @@ -61,10 +61,11 @@ module Array : sig end - -module Make : functor (Array:Array.S) -> +module type S = sig + module Array : Array.S + type t = private { mutable start : int; mutable stop : int; (* excluded *) @@ -72,7 +73,6 @@ sig bounded: bool; size : int } - exception Empty val create : ?bounded:bool -> int -> t @@ -151,3 +151,12 @@ sig (** Take first value, or raise Empty *) end + +module Make_array : functor (Array:Array.S) -> S with module Array = Array + +module Bytes : S with module Array = Array.ByteArray +module Floats : S with module Array = Array.FloatArray +module Ints : S with module Array = Array.IntArray +module Bools : S with module Array = Array.BoolArray + +module Make: functor(Elt:sig type t end) -> S with module Array = Array.Make(Elt)