first cut of a polymorphic buffer

This commit is contained in:
carm 2015-02-10 23:00:09 -05:00
parent 0d3158d1bd
commit 472aaa83a2
2 changed files with 85 additions and 100 deletions

View file

@ -20,10 +20,11 @@
(** Circular Byte Buffer for IO *) (** Circular Byte Buffer for IO *)
type t = { type 'a t = {
mutable start : int; mutable start : int;
mutable stop : int; (* excluded *) mutable stop : int; (* excluded *)
mutable buf : string; mutable buf : 'a array;
size: int
} }
exception Empty exception Empty
@ -31,39 +32,35 @@ exception Empty
let create size = let create size =
{ start=0; { start=0;
stop=0; stop=0;
buf =String.make size ' '; size;
buf = Array.of_list [];
} }
let copy b = let copy b =
{ b with buf=String.copy b.buf; } { b with buf=Array.copy b.buf; }
let of_string s =
{ start=0;
stop=String.length s;
buf=String.copy s;
}
let capacity b = String.length b.buf let capacity b = b.size
let length b = let length b =
if b.stop >= b.start if b.stop >= b.start
then b.stop - b.start then b.stop - b.start
else (String.length b.buf - b.start) + b.stop else (Array.length b.buf - b.start) + b.stop
(* resize [b] so that inner capacity is [cap] *) (* resize [b] so that inner capacity is [cap] *)
let resize b cap = let resize b cap elem =
assert (cap >= String.length b.buf); assert (cap >= Array.length b.buf);
let buf' = String.make cap ' ' in let buf' = Array.make cap elem in
(* copy into buf' *) (* copy into buf' *)
let len = let len =
if b.stop >= b.start if b.stop >= b.start
then begin then begin
String.blit b.buf b.start buf' 0 (b.stop - b.start); Array.blit b.buf b.start buf' 0 (b.stop - b.start);
b.stop - b.start b.stop - b.start
end else begin end else begin
let len_end = String.length b.buf - b.start in let len_end = Array.length b.buf - b.start in
String.blit b.buf b.start buf' 0 len_end; Array.blit b.buf b.start buf' 0 len_end;
String.blit b.buf 0 buf' len_end b.stop; Array.blit b.buf 0 buf' len_end b.stop;
len_end + b.stop len_end + b.stop
end end
in in
@ -72,66 +69,56 @@ let resize b cap =
b.stop <- len; b.stop <- len;
() ()
let blit_from b s o len = let blit_from b from_buf o len =
let cap = capacity b - length b in let cap = capacity b - length b in
(* resize if needed, with a constant to amortize *) (* resize if needed, with a constant to amortize *)
if cap < len then resize b (String.length b.buf + len + 24); if (Array.length from_buf) = 0 then () else
if cap < len then
resize b (Array.length b.buf + len + 24) from_buf.(0);
assert (capacity b - length b >= len); assert (capacity b - length b >= len);
if b.stop >= b.start if b.stop >= b.start
then (* [_______ start xxxxxxxxx stop ______] *) then (* [_______ start xxxxxxxxx stop ______] *)
let len_end = String.length b.buf - b.stop in let len_end = Array.length b.buf - b.stop in
if len_end >= len if len_end >= len
then (String.blit s o b.buf b.stop len; then (Array.blit from_buf o b.buf b.stop len;
b.stop <- b.stop + len) b.stop <- b.stop + len)
else (String.blit s o b.buf b.stop len_end; else (Array.blit from_buf o b.buf b.stop len_end;
String.blit s (o+len_end) b.buf 0 (len-len_end); Array.blit from_buf (o+len_end) b.buf 0 (len-len_end);
b.stop <- len-len_end) b.stop <- len-len_end)
else begin (* [xxxxx stop ____________ start xxxxxx] *) else begin (* [xxxxx stop ____________ start xxxxxx] *)
let len_middle = b.start - b.stop in let len_middle = b.start - b.stop in
assert (len_middle >= len); assert (len_middle >= len);
String.blit s o b.buf b.stop len; Array.blit from_buf 0 b.buf b.stop len;
b.stop <- b.stop + len b.stop <- b.stop + len
end; end;
() ()
let blit_into b s o len = let blit_into b to_buf o len =
if o+len > String.length s 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 _ = String.blit b.buf b.start s o n in let _ = Array.blit b.buf b.start to_buf o n in
n n
else begin else begin
let len_end = String.length b.buf - b.start in let len_end = Array.length b.buf - b.start in
String.blit b.buf b.start s o (min len_end len); Array.blit b.buf b.start to_buf o (min len_end len);
if len_end >= len if len_end >= len
then len (* done *) then len (* done *)
else begin else begin
let n = min b.stop (len - len_end) in let n = min b.stop (len - len_end) in
String.blit b.buf 0 s (o+len_end) n; Array.blit b.buf 0 to_buf (o+len_end) n;
n + len_end n + len_end
end end
end end
let add_string b s = blit_from b s 0 (String.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_string b s; add_string b s'; \ let b = create 24 in add b s; add_string b s'; \
String.length s + String.length s' = length b) Array.length s + String.length s' = length b)
*)
let to_string b =
let s = String.make (length b) ' ' in
let n = blit_into b s 0 (String.length s) in
assert (n = String.length s);
s
(*$Q
(Q.pair Q.printable_string Q.printable_string) (fun (s,s') -> \
let b = create 24 in add_string b s; add_string b s'; \
to_string b = s ^ s')
*) *)
let clear b = let clear b =
@ -142,26 +129,26 @@ let clear b =
let reset b = let reset b =
clear b; clear b;
if capacity b > 64 if capacity b > 64
then b.buf <- String.make 64 ' '; (* reset *) then b.buf <- Array.sub b.buf 0 64;
() ()
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 pop b = let pop 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 = String.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 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 = String.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
@ -170,7 +157,7 @@ let skip b len =
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 = String.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
@ -185,10 +172,10 @@ let skip b len =
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 String.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
@ -202,13 +189,14 @@ let get b i =
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 = String.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 =
Array.to_list (Array.sub b.buf b.start b.stop)

View file

@ -1,5 +1,5 @@
(* (**
* BatBufferIO - Circular byte buffer * CCBufferIO - Polymorphic Circular Buffer
* Copyright (C) 2014 Simon Cruanes * Copyright (C) 2014 Simon Cruanes
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -18,79 +18,76 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*) *)
(** Circular Byte Buffer for IO *) (** Circular Polymorphic Buffer for IO *)
type t = private { type 'a t = private {
mutable start : int; mutable start : int;
mutable stop : int; (* excluded *) mutable stop : int; (* excluded *)
mutable buf : string; mutable buf : 'a array;
size : int
} }
exception Empty exception Empty
val create : int -> t val create : int -> 'a t
(** [create size] creates a new buffer with given size *) (** [create size] creates a new buffer with given size *)
val copy : t -> t val copy : 'a t ->'a t
(** fresh copy of the buffer *) (** fresh copy of the buffer *)
val of_string : string -> t val capacity : 'a t -> int
(** build a buffer from an initial string. The string is copied. (** length of the inner buffer *)
Use {!String.blit_from} if you want more control. *)
val capacity : t -> int val length : 'a t -> int
(** length of the inner string buffer *) (** number of elements currently stored in the buffer *)
val length : t -> int val blit_from : 'a t -> 'a array -> int -> int -> unit
(** number of bytes currently stored in the buffer *) (** [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.
val blit_from : t -> string -> int -> int -> unit
(** [blit_from buf s o len] copies the slice [o, ... o + len - 1] from
the string [s] to the end of the buffer.
@raise Invalid_argument if [o,len] is not a valid slice of [s] *) @raise Invalid_argument if [o,len] is not a valid slice of [s] *)
val blit_into : t -> string -> int -> int -> int val blit_into : 'a t -> 'a array -> int -> int -> int
(** [blit_into buf s o len] copies at most [len] bytes from [buf] (** [blit_into buf to_buf o len] copies at most [len] elements from [buf]
into [s], starting at offset [o] in [s]. into [to_buf] starting at offset [o] in [s].
@return the number of bytes actually copied ([min len (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] *) @raise Invalid_argument if [o,len] is not a valid slice of [s] *)
val add_string : t -> string -> unit val add : 'a t -> 'a array -> unit
(** [add_string buf s] adds [s] at the end of [buf]. *) (** [add buf t] adds elements [t] at the end of [buf]. *)
val to_string : t -> string val to_list : 'a t -> 'a list
(** extract the current content into a string *) (** extract the current content into a list *)
val clear : t -> unit val clear : 'a t -> unit
(** clear the content of the buffer. Doesn't actually destroy the content. *) (** clear the content of the buffer. Doesn't actually destroy the content. *)
val reset : t -> unit val reset : 'a t -> unit
(** clear the content of the buffer, and also resize it to a default size *) (** clear the content of the buffer, and also resize it to a default size *)
val is_empty : t -> bool val is_empty :'a t -> bool
(** is the buffer empty (i.e. contains no byte)? *) (** is the buffer empty (i.e. contains no elements)? *)
val next : t -> char val next : 'a t -> 'a
(** obtain next char (the first one of the buffer) (** obtain next element (the first one of the buffer)
@raise Empty if the buffer is empty *) @raise Empty if the buffer is empty *)
val pop : t -> char val pop : 'a t -> 'a
(** obtain and remove next char (the first one) (** obtain and remove next element (the first one)
@raise Empty if the buffer is empty *) @raise Empty if the buffer is empty *)
val junk : t -> unit val junk : 'a t -> unit
(** Drop next element. (** Drop next element.
@raise Empty if the buffer is already empty *) @raise Empty if the buffer is already empty *)
val skip : t -> int -> unit val skip : 'a t -> int -> unit
(** [skip b len] removes [len] elements from [b]. (** [skip b len] removes [len] elements from [b].
@raise Invalid_argument if [len > length b]. *) @raise Invalid_argument if [len > length b]. *)
val iteri : t -> (int -> char -> unit) -> unit val iteri : 'a t -> (int -> 'a -> unit) -> unit
(** [iteri b f] calls [f i c] for each char [c] in [buf], with [i] (** [iteri b f] calls [f i t] for each element [t] in [buf], with [i]
being its relative index within [buf]. *) being its relative index within [buf]. *)
val get : t -> int -> char val get : 'a t -> int -> 'a
(** [get buf i] returns the [i]-th character of [buf], ie the one that (** [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]. is returned by [next buf] after [i-1] calls to [junk buf].
@raise Invalid_argument if the index is invalid (> [length buf]) *) @raise Invalid_argument if the index is invalid (> [length buf]) *)