mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
support for unbounded ring buffer
This commit is contained in:
parent
4a994cff38
commit
2cf485ebee
2 changed files with 14 additions and 8 deletions
|
|
@ -26,16 +26,16 @@ type 'a t = {
|
|||
mutable start : int;
|
||||
mutable stop : int; (* excluded *)
|
||||
mutable buf : 'a array;
|
||||
size: int
|
||||
max_capacity: int
|
||||
}
|
||||
|
||||
exception Empty
|
||||
|
||||
let create size =
|
||||
let create ?(max_capacity=max_int) () =
|
||||
{ start=0;
|
||||
stop=0;
|
||||
size;
|
||||
buf = Array.of_list [];
|
||||
max_capacity;
|
||||
buf = Array.of_list []
|
||||
}
|
||||
|
||||
let copy b =
|
||||
|
|
@ -44,6 +44,8 @@ let copy b =
|
|||
|
||||
let capacity b = Array.length b.buf
|
||||
|
||||
let max_capacity b = b.max_capacity
|
||||
|
||||
let length b =
|
||||
if b.stop >= b.start
|
||||
then b.stop - b.start
|
||||
|
|
@ -73,7 +75,7 @@ let blit_from b from_buf o len =
|
|||
let cap = capacity b - length b in
|
||||
(* resize if needed, with a constant to amortize *)
|
||||
if cap < len then
|
||||
resize b (min (b.size+1) (Array.length b.buf + len + 24)) from_buf.(0);
|
||||
resize b (min (b.max_capacity+1) (Array.length b.buf + len + 24)) from_buf.(0);
|
||||
let sub = Array.sub from_buf o len in
|
||||
let iter x =
|
||||
if b.start = 0 then b.start <- capacity b - 1 else b.start <- b.start - 1;
|
||||
|
|
|
|||
|
|
@ -24,13 +24,14 @@ type 'a t = private {
|
|||
mutable start : int;
|
||||
mutable stop : int; (* excluded *)
|
||||
mutable buf : 'a array;
|
||||
size : int
|
||||
max_capacity : int
|
||||
}
|
||||
|
||||
exception Empty
|
||||
|
||||
val create : int -> 'a t
|
||||
(** [create size] creates a new buffer with given size *)
|
||||
val create : ?max_capacity:int -> unit -> 'a t
|
||||
(** [create ~max_capacity ()] creates a new buffer with given maximum capacity.
|
||||
Defaults to unbounded. *)
|
||||
|
||||
val copy : 'a t ->'a t
|
||||
(** fresh copy of the buffer *)
|
||||
|
|
@ -38,6 +39,9 @@ val copy : 'a t ->'a t
|
|||
val capacity : 'a t -> int
|
||||
(** length of the inner buffer *)
|
||||
|
||||
val max_capacity : 'a t -> int
|
||||
(** maximum length of the inner buffer *)
|
||||
|
||||
val length : 'a t -> int
|
||||
(** number of elements currently stored in the buffer *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue