functorize ring buffer over ARRAY sig

This commit is contained in:
carm 2015-02-16 00:19:17 -05:00
parent 07f0afcd28
commit c7607f8ce7
2 changed files with 291 additions and 244 deletions

View file

@ -22,234 +22,255 @@
(** Polymorphic Circular Buffer for IO *) (** Polymorphic Circular Buffer for IO *)
type 'a t = { module type ARRAY = sig
mutable start : int; type elt
mutable stop : int; (* excluded *) type t
mutable buf : 'a array;
bounded : bool;
size : int
}
exception Empty val make: int -> elt -> t
val length: t -> int
let create ?(bounded=false) size = val get: t -> int -> elt
{ start=0;
stop=0; val set: t -> int -> elt -> unit
bounded;
size; val sub: t -> int -> int -> t
buf = Array.of_list [] 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 : Array.t;
bounded : bool;
size : int
} }
let copy b = exception Empty
{ b with buf=Array.copy b.buf; }
let create ?(bounded=false) size =
{ start=0;
stop=0;
bounded;
size;
buf = Array.of_list []
}
let copy b =
{ b with buf=Array.copy b.buf; }
let capacity b = Array.length b.buf let capacity b = Array.length b.buf
let max_capacity b = if b.bounded then Some b.size else None let max_capacity b = if b.bounded then Some b.size else None
let length b = let length b =
if b.stop >= b.start
then b.stop - b.start
else (Array.length b.buf - b.start) + b.stop
(* resize [b] so that inner capacity is [cap] *)
let resize b cap elem =
assert (cap >= Array.length b.buf);
let buf' = Array.make cap elem in
(* copy into buf' *)
let _:int =
if b.stop >= b.start if b.stop >= b.start
then begin then b.stop - b.start
Array.blit b.buf b.start buf' 0 (b.stop - b.start); else (Array.length b.buf - b.start) + b.stop
b.stop - b.start
end else begin
let len_end = Array.length b.buf - b.start in
Array.blit b.buf b.start buf' 0 len_end;
Array.blit b.buf 0 buf' len_end b.stop;
len_end + b.stop
end
in
b.buf <- buf'
let blit_from_bounded b from_buf o len = (* resize [b] so that inner capacity is [cap] *)
let cap = capacity b - len in let resize b cap elem =
(* resize if needed, with a constant to amortize *) assert (cap >= Array.length b.buf);
if cap < len then begin let buf' = Array.make cap elem in
let new_size = (* copy into buf' *)
let desired = Array.length b.buf + len + 24 in let _:int =
min (b.size+1) desired in if b.stop >= b.start
resize b new_size from_buf.(0) then begin
end; Array.blit b.buf b.start buf' 0 (b.stop - b.start);
let sub = Array.sub from_buf o len in b.stop - b.start
let iter x = end else begin
let capacity = capacity b in let len_end = Array.length b.buf - b.start in
Array.set b.buf b.stop x; Array.blit b.buf b.start buf' 0 len_end;
if b.stop = capacity-1 then b.stop <- 0 else b.stop <- b.stop + 1; Array.blit b.buf 0 buf' len_end b.stop;
if b.start = b.stop then len_end + b.stop
begin
if b.start = capacity-1 then b.start <- 0 else b.start <- b.start + 1
end end
in in
Array.iter iter sub b.buf <- buf'
let blit_from_unbounded b from_buf o len = let blit_from_bounded b from_buf o len =
let cap = capacity b - len in let cap = capacity b - len in
(* resize if needed, with a constant to amortize *) (* resize if needed, with a constant to amortize *)
if cap < len then resize b (max b.size (Array.length b.buf + len + 24)) from_buf.(0); if cap < len then begin
assert (capacity b - length b >= len); let new_size =
if b.stop >= b.start let desired = Array.length b.buf + len + 24 in
then (* [_______ start xxxxxxxxx stop ______] *) min (b.size+1) desired in
let len_end = Array.length b.buf - b.stop in resize b new_size from_buf.(0)
if len_end >= len end;
then (Array.blit from_buf o b.buf b.stop len; let sub = Array.sub from_buf o len in
b.stop <- b.stop + len) let iter x =
else (Array.blit from_buf o b.buf b.stop len_end; let capacity = capacity b in
Array.blit from_buf (o+len_end) b.buf 0 (len-len_end); Array.set b.buf b.stop x;
b.stop <- len-len_end) if b.stop = capacity-1 then b.stop <- 0 else b.stop <- b.stop + 1;
else begin (* [xxxxx stop ____________ start xxxxxx] *) if b.start = b.stop then
let len_middle = b.start - b.stop in begin
assert (len_middle >= len); if b.start = capacity-1 then b.start <- 0 else b.start <- b.start + 1
Array.blit from_buf o b.buf b.stop len; end
b.stop <- b.stop + len in
end; Array.iter iter sub
()
let blit_from b from_buf o len =
if (Array.length from_buf) = 0 then () else
if b.bounded then
blit_from_bounded b from_buf o len
else
blit_from_unbounded b from_buf o len
let blit_into b to_buf o len = let blit_from_unbounded b from_buf o len =
if o+len > Array.length to_buf let cap = capacity b - len in
(* resize if needed, with a constant to amortize *)
if cap < len then resize b (max b.size (Array.length b.buf + len + 24)) from_buf.(0);
assert (capacity b - length b >= len);
if b.stop >= b.start
then (* [_______ start xxxxxxxxx stop ______] *)
let len_end = Array.length b.buf - b.stop in
if len_end >= len
then (Array.blit from_buf o b.buf b.stop len;
b.stop <- b.stop + len)
else (Array.blit from_buf o b.buf b.stop len_end;
Array.blit from_buf (o+len_end) b.buf 0 (len-len_end);
b.stop <- len-len_end)
else begin (* [xxxxx stop ____________ start xxxxxx] *)
let len_middle = b.start - b.stop in
assert (len_middle >= len);
Array.blit from_buf o b.buf b.stop len;
b.stop <- b.stop + len
end;
()
let blit_from b from_buf o len =
if (Array.length from_buf) = 0 then () else
if b.bounded then
blit_from_bounded b from_buf o len
else
blit_from_unbounded b from_buf o len
let blit_into b to_buf o len =
if o+len > Array.length to_buf
then raise (Invalid_argument "BufferIO.blit_into"); then raise (Invalid_argument "BufferIO.blit_into");
if b.stop >= b.start if b.stop >= b.start
then then
let n = min (b.stop - b.start) len in let n = min (b.stop - b.start) len in
let _ = Array.blit b.buf b.start to_buf o n in let _ = Array.blit b.buf b.start to_buf o n in
n n
else begin
let len_end = Array.length b.buf - b.start in
Array.blit b.buf b.start to_buf o (min len_end len);
if len_end >= len
then len (* done *)
else begin else begin
let n = min b.stop (len - len_end) in let len_end = Array.length b.buf - b.start in
Array.blit b.buf 0 to_buf (o+len_end) n; Array.blit b.buf b.start to_buf o (min len_end len);
n + len_end if len_end >= len
then len (* done *)
else begin
let n = min b.stop (len - len_end) in
Array.blit b.buf 0 to_buf (o+len_end) n;
n + len_end
end
end end
end
let add b s = blit_from b s 0 (Array.length s) let add b s = blit_from b s 0 (Array.length s)
(*$Q (*$Q
(Q.pair Q.printable_string Q.printable_string) (fun (s,s') -> \ (Q.pair Q.printable_string Q.printable_string) (fun (s,s') -> \
let b = create 24 in add b s; add_string b s'; \ let b = create 24 in add b s; add_string b s'; \
Array.length s + String.length s' = length b) Array.length s + String.length s' = length b)
*) *)
let clear b = let clear b =
b.stop <- 0; b.stop <- 0;
b.start <- 0; b.start <- 0;
() ()
let reset b = let reset b =
clear b; clear b;
b.buf <- Array.of_list [] b.buf <- Array.of_list []
let is_empty b = b.start = b.stop let is_empty b = b.start = b.stop
let next b = let next b =
if b.start = b.stop then raise Empty; if b.start = b.stop then raise Empty;
b.buf.(b.start) b.buf.(b.start)
let take_front b = let take_front b =
if b.start = b.stop then raise Empty; if b.start = b.stop then raise Empty;
let c = b.buf.(b.start) in let c = b.buf.(b.start) in
if b.start + 1 = Array.length b.buf if b.start + 1 = Array.length b.buf
then b.start <- 0 then b.start <- 0
else b.start <- b.start + 1; else b.start <- b.start + 1;
c c
let take_back b = let take_back b =
if b.start = b.stop then raise Empty; if b.start = b.stop then raise Empty;
if b.stop - 1 = 0 if b.stop - 1 = 0
then b.stop <- Array.length b.buf - 1 then b.stop <- Array.length b.buf - 1
else b.stop <- b.stop - 1; else b.stop <- b.stop - 1;
b.buf.(b.stop) b.buf.(b.stop)
let junk b = let junk b =
if b.start = b.stop then raise Empty; if b.start = b.stop then raise Empty;
if b.start + 1 = Array.length b.buf if b.start + 1 = Array.length b.buf
then b.start <- 0 then b.start <- 0
else b.start <- b.start + 1 else b.start <- b.start + 1
let skip b len = let skip b len =
if len > length b then raise (Invalid_argument "BufferIO.skip"); if len > length b then raise (Invalid_argument "BufferIO.skip");
if b.stop >= b.start if b.stop >= b.start
then b.start <- b.start + len then b.start <- b.start + len
else else
let len_end = Array.length b.buf - b.start in let len_end = Array.length b.buf - b.start in
if len > len_end if len > len_end
then b.start <- len-len_end (* wrap to the beginning *) then b.start <- len-len_end (* wrap to the beginning *)
else b.start <- b.start + len else b.start <- b.start + len
(*$Q (*$Q
(Q.pair Q.printable_string Q.printable_string) (fun (s,s') -> \ (Q.pair Q.printable_string Q.printable_string) (fun (s,s') -> \
let b = create 24 in add_string b s; add_string b s'; \ let b = create 24 in add_string b s; add_string b s'; \
add_string b "hello world"; (* big enough *) \ add_string b "hello world"; (* big enough *) \
let l = length b in let l' = l/2 in skip b l'; \ let l = length b in let l' = l/2 in skip b l'; \
length b + l' = l) length b + l' = l)
*) *)
let iteri b f = let iteri b f =
if b.stop >= b.start if b.stop >= b.start
then for i = b.start to b.stop - 1 do f i b.buf.(i) done then for i = b.start to b.stop - 1 do f i b.buf.(i) done
else ( else (
for i = b.start to Array.length b.buf -1 do f i b.buf.(i) done; for i = b.start to Array.length b.buf -1 do f i b.buf.(i) done;
for i = 0 to b.stop - 1 do f i b.buf.(i) done; for i = 0 to b.stop - 1 do f i b.buf.(i) done;
) )
(*$T (*$T
let s = "hello world" in \ let s = "hello world" in \
let b = of_string s in \ let b = of_string s in \
try iteri b (fun i c -> if s.[i] <> c then raise Exit); true with Exit -> false try iteri b (fun i c -> if s.[i] <> c then raise Exit); true with Exit -> false
*) *)
let get b i = let get b i =
if b.stop >= b.start if b.stop >= b.start
then then
if i >= b.stop - b.start if i >= b.stop - b.start
then raise (Invalid_argument "BufferIO.get") then raise (Invalid_argument "BufferIO.get")
else b.buf.(b.start + i) else b.buf.(b.start + i)
else else
let len_end = Array.length b.buf - b.start in let len_end = Array.length b.buf - b.start in
if i < len_end if i < len_end
then b.buf.(b.start + i) then b.buf.(b.start + i)
else if i - len_end > b.stop else if i - len_end > b.stop
then raise (Invalid_argument "BufferIO.get") then raise (Invalid_argument "BufferIO.get")
else b.buf.(i - len_end) else b.buf.(i - len_end)
let to_list b = let to_list b =
if (b.stop >= b.start) if (b.stop >= b.start)
then Array.to_list (Array.sub b.buf b.start (b.stop-b.start)) then Array.to_list (Array.sub b.buf b.start (b.stop-b.start))
else List.append else List.append
(Array.to_list (Array.sub b.buf b.start (Array.length b.buf - b.start))) (Array.to_list (Array.sub b.buf b.start (Array.length b.buf - b.start)))
(Array.to_list (Array.sub b.buf 0 b.stop)) (Array.to_list (Array.sub b.buf 0 b.stop))
let push_back b e = add b (Array.of_list [e])
let peek_front b = if is_empty b then
raise Empty else Array.get b.buf b.start
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)
let push_back b e = add b (Array.of_list [e])
let peek_front b = if is_empty b then
raise Empty else Array.get b.buf b.start
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

View file

@ -20,89 +20,115 @@
(** Circular Polymorphic Buffer for IO *) (** Circular Polymorphic Buffer for IO *)
type 'a t = private { module type ARRAY = sig
mutable start : int; type elt
mutable stop : int; (* excluded *) type t
mutable buf : 'a array;
bounded: bool;
size : int
}
exception Empty val make: int -> elt -> t
val length: t -> int
val create : ?bounded:bool -> int -> 'a t val get: t -> int -> elt
(** [create ?bounded size] creates a new buffer with given size.
Defaults to [bounded=false]. *)
val copy : 'a t ->'a t val set: t -> int -> elt -> unit
(** fresh copy of the buffer *)
val capacity : 'a t -> int val sub: t -> int -> int -> t
(** length of the inner buffer *) val max_length: int
val max_capacity : 'a t -> int option val copy : t -> t
(** maximum length of the inner buffer, or [None] if unbounded. *) val of_list : elt list -> t
val to_list : t -> elt list
val blit : t -> int -> t -> int -> int -> unit
val length : 'a t -> int val iter : (elt -> unit) -> t -> unit
(** number of elements currently stored in the buffer *) end
val blit_from : 'a t -> 'a array -> 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 module Make : functor (Array:ARRAY) ->
(** [blit_into buf to_buf o len] copies at most [len] elements from [buf] sig
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 type t = private {
(** extract the current content into a list *) mutable start : int;
mutable stop : int; (* excluded *)
mutable buf : Array.t;
bounded: bool;
size : int
}
val clear : 'a t -> unit exception Empty
(** clear the content of the buffer. Doesn't actually destroy the content. *)
val reset : 'a t -> unit val create : ?bounded:bool -> int -> t
(** clear the content of the buffer, and also resize it to a default size *) (** [create ?bounded size] creates a new buffer with given size.
Defaults to [bounded=false]. *)
val is_empty :'a t -> bool val copy : t -> t
(** is the buffer empty (i.e. contains no elements)? *) (** fresh copy of the buffer *)
val next : 'a t -> 'a val capacity : t -> int
(** obtain next element (the first one of the buffer) (** length of the inner buffer *)
@raise Empty if the buffer is empty *)
val junk : 'a t -> unit val max_capacity : t -> int option
(** Drop next element. (** maximum length of the inner buffer, or [None] if unbounded. *)
@raise Empty if the buffer is already empty *)
val skip : 'a t -> int -> unit val length : t -> int
(** [skip b len] removes [len] elements from [b]. (** number of elements currently stored in the buffer *)
@raise Invalid_argument if [len > length b]. *)
val iteri : 'a t -> (int -> 'a -> unit) -> unit val blit_from : t -> Array.t -> int -> int -> unit
(** [iteri b f] calls [f i t] for each element [t] in [buf], with [i] (** [blit_from buf from_buf o len] copies the slice [o, ... o + len - 1] from
being its relative index within [buf]. *) 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 get : 'a t -> int -> 'a val blit_into : t -> Array.t -> int -> int -> int
(** [get buf i] returns the [i]-th element of [buf], ie the one that (** [blit_into buf to_buf o len] copies at most [len] elements from [buf]
is returned by [next buf] after [i-1] calls to [junk buf]. into [to_buf] starting at offset [o] in [s].
@raise Invalid_argument if the index is invalid (> [length buf]) *) @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 push_back : 'a t -> 'a -> unit 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 *) (** Push value at the back *)
val peek_front : 'a t -> 'a val peek_front : t -> Array.elt
(** First value, or Empty *) (** First value, or Empty *)
val peek_back : 'a t -> 'a val peek_back : t -> Array.elt
(** Last value, or Empty *) (** Last value, or Empty *)
val take_back : 'a t -> 'a val take_back : t -> Array.elt
(** Take last value, or raise Empty *) (** Take last value, or raise Empty *)
val take_front : 'a t -> 'a val take_front : t -> Array.elt
(** Take first value, or raise Empty *) (** Take first value, or raise Empty *)
end