Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file CCRingBuffer.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856(* This file is free software, part of containers. See file "license" for more details. *)(* Copyright (C) 2015 Simon Cruanes, Carmelo Piccione *)(** Generic Circular Buffer for IO, with bulk operations.
The bulk operations (e.g. based on {!Array.blit} or {!Bytes.blit})
are more efficient than item-by-item copy.
See https://en.wikipedia.org/wiki/Circular_buffer for an overview. *)moduleArray=struct(** The abstract type for arrays *)moduletypeS=sig(** The element type *)typeelt(** The type of an array instance *)typetvaldummy:elt(** A dummy element used for empty slots in the array
@since 2.4 *)valcreate:int->t(** Make an array of the given size, filled with dummy elements *)vallength:t->int(** [length t] gets the total number of elements currently in [t] *)valget:t->int->elt(** [get t i] gets the element at position [i] *)valset:t->int->elt->unit(** [set t i e] sets the element at position [i] to [e] *)valsub:t->int->int->t(** [sub t i len] gets the subarray of [t] from
position [i] to [i + len] *)valcopy:t->t(** [copy t] makes a fresh copy of the array [t] *)valblit:t->int->t->int->int->unit(** [blit t s arr i len] copies [len] elements from [arr] starting at [i]
to position [s] from [t] *)valiter:(elt->unit)->t->unit(** [iter f t] iterates over the array [t] invoking [f] with
the current element, in array order *)endmoduleByte:Swithtypeelt=charandtypet=Bytes.t=structtypeelt=charletdummy='\x00'includeBytesendmoduleMake(Elt:sigtypetvaldummy:tend):Swithtypeelt=Elt.tandtypet=Elt.tarray=structtypeelt=Elt.ttypet=Elt.tarrayletdummy=Elt.dummyletcreatesize=Array.makesizeElt.dummyletlength=Array.lengthletget=Array.getletset=Array.setletcopy=Array.copyletblit=Array.blitletiter=Array.iterletsub=Array.subendendmoduletypeS=sig(** The module type of Array for this ring buffer *)moduleArray:Array.S(** Defines the bounded ring buffer type *)typet(** Raised in querying functions when the buffer is empty *)exceptionEmptyvalcreate:int->t(** [create size] creates a new bounded buffer with given size.
The underlying array is allocated immediately and no further (large)
allocation will happen from now on.
@raise Invalid_argument if the arguments is [< 1] *)valcopy:t->t(** Make a fresh copy of the buffer. *)valcapacity:t->int(** Length of the inner buffer. *)vallength:t->int(** Number of elements currently stored in the buffer. *)valis_full:t->bool(** true if pushing an element would erase another element.
@since 1.3 *)valblit_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.
If the slice is too large for the buffer, only the last part of the array
will be copied.
@raise Invalid_argument if [o,len] is not a valid slice of [s] *)valblit_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]. *)valappend:t->into:t->unit(** [append b ~into] copies all data from [b] and adds it at the
end of [into]. Erases data of [into] if there is not enough room. *)valto_list:t->Array.eltlist(** Extract the current content into a list *)valclear:t->unit(** Clear the content of the buffer. Doesn't actually destroy the content. *)valis_empty:t->bool(** Is the buffer empty (i.e. contains no elements)? *)valjunk_front:t->unit(** Drop the front element from [t].
@raise Empty if the buffer is already empty. *)valjunk_back:t->unit(** Drop the back element from [t].
@raise Empty if the buffer is already empty. *)valskip:t->int->unit(** [skip b len] removes [len] elements from the front of [b].
@raise Invalid_argument if [len > length b]. *)valiter:t->f:(Array.elt->unit)->unit(** [iter b ~f] calls [f i t] for each element [t] in [buf] *)valiteri:t->f:(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]. *)valget_front:t->int->Array.elt(** [get_front buf i] returns the [i]-th element of [buf] from the front, ie
the one returned by [take_front buf] after [i-1] calls to [junk_front buf].
@raise Invalid_argument if the index is invalid (> [length buf]) *)valget_back:t->int->Array.elt(** [get_back buf i] returns the [i]-th element of [buf] from the back, ie
the one returned by [take_back buf] after [i-1] calls to [junk_back buf].
@raise Invalid_argument if the index is invalid (> [length buf]) *)valpush_back:t->Array.elt->unit(** Push value at the back of [t].
If [t.bounded=false], the buffer will grow as needed,
otherwise the oldest elements are replaced first. *)valpeek_front:t->Array.eltoption(** First value from front of [t], without modification. *)valpeek_front_exn:t->Array.elt(** First value from front of [t], without modification.
@raise Empty if buffer is empty.
@since 1.3 *)valpeek_back:t->Array.eltoption(** Get the last value from back of [t], without modification. *)valpeek_back_exn:t->Array.elt(** Get the last value from back of [t], without modification.
@raise Empty if buffer is empty.
@since 1.3 *)valtake_back:t->Array.eltoption(** Take and remove the last value from back of [t], if any *)valtake_back_exn:t->Array.elt(** Take and remove the last value from back of [t].
@raise Empty if buffer is already empty. *)valtake_front:t->Array.eltoption(** Take and remove the first value from front of [t], if any *)valtake_front_exn:t->Array.elt(** Take and remove the first value from front of [t].
@raise Empty if buffer is already empty. *)valof_array:Array.t->t(** Create a buffer from an initial array, but doesn't take ownership
of it (stills allocates a new internal array)
@since 0.11 *)valto_array:t->Array.t(** Create an array from the elements, in order.
@since 0.11 *)end(*$inject
open Q.Gen
let g_char = map Char.chr (Char.code 'A' -- Char.code 'z')
let g_str = string_size ~gen:g_char (0--10)
let a_str = Q.set_gen g_str Q.string
*)moduleMakeFromArray(A:Array.S):SwithmoduleArray=A=structmoduleArray=Atypet={mutablestart:int;mutablestop:int;(* excluded *)buf:Array.t;}exceptionEmptyletcreatesize=ifsize<1theninvalid_arg"CCRingBuffer.create";{start=0;stop=0;buf=A.create(size+1);(* keep room for extra slot *)}letcopyb={bwithbuf=A.copyb.buf;}(*$T
let b = Byte.of_array (Bytes.of_string "abc") in \
let b' = Byte.copy b in \
Byte.clear b; \
Byte.to_array b' = (Bytes.of_string "abc") && Byte.to_array b = Bytes.empty
*)letcapacityb=letlen=A.lengthb.bufinmatchlenwith0->0|l->l-1(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
Byte.capacity b >= s_len)
*)letlengthb=ifb.stop>=b.startthenb.stop-b.startelse(A.lengthb.buf-b.start)+b.stopletis_fullb=lengthb+1=Array.lengthb.bufletnext_bi=letj=i+1inifj=A.lengthb.bufthen0elsejletincr_start_b=b.start<-next_bb.startletincr_stop_b=b.stop<-next_bb.stopletpush_backbe=A.setb.bufb.stope;incr_stop_b;ifb.start=b.stopthenincr_start_b;(* overwritten one element *)()letblit_frombfrom_bufolen=iflen=0then()elseifo+len>A.lengthfrom_buftheninvalid_arg"CCRingBuffer.blit_from"else(fori=otoo+len-1dopush_backb(A.getfrom_bufi)done)(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
let b' = Byte.copy b in \
try Byte.iteri b ~f:(fun i c -> if Byte.get_front b' i <> c then raise Exit); true with Exit -> false)
*)(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
Byte.push_back b 'X'; \
Byte.peek_back_exn b = 'X')
*)(*$Q
(Q.pair a_str a_str) (fun (s,s') -> \
let b = Byte.create (max (String.length s+String.length s') 64) in \
let s = Bytes.of_string s in let s' = Bytes.of_string s' in \
Byte.blit_from b s 0 (Bytes.length s); \
Byte.blit_from b s' 0 (Bytes.length s'); \
Byte.length b = Bytes.length s + Bytes.length s')
*)(*$Q
(Q.pair a_str a_str) (fun (s,s') -> \
let s = Bytes.of_string s in let s' = Bytes.of_string s' in \
let b = Byte.create (max (Bytes.length s + Bytes.length s') 64) in \
Byte.blit_from b s 0 (Bytes.length s); \
Byte.blit_from b s' 0 (Bytes.length s'); \
Byte.length b = Bytes.length s + Bytes.length s')
*)letblit_intobto_bufolen=ifo+len>A.lengthto_bufthen(invalid_arg"CCRingBuffer.blit_into";);ifb.stop>=b.startthen(letn=min(b.stop-b.start)leninA.blitb.bufb.startto_bufon;n)else(letlen_end=A.lengthb.buf-b.startinA.blitb.bufb.startto_bufo(minlen_endlen);iflen_end>=lenthenlen(* done *)else(letn=minb.stop(len-len_end)inA.blitb.buf0to_buf(o+len_end)n;n+len_end))(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let b = Byte.create (max 64 (Bytes.length s)) in \
Byte.blit_from b s 0 (Bytes.length s); \
let to_buf = Bytes.create (Bytes.length s) in \
let len = Byte.blit_into b to_buf 0 (Bytes.length s) in \
to_buf = s && len = Bytes.length s)
*)letis_emptyb=b.start=b.stop(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
Byte.skip b s_len; \
Byte.is_empty b)
*)lettake_front_exnb=ifb.start=b.stopthenraiseEmpty;letc=A.getb.bufb.startinA.setb.bufb.startA.dummy;b.start<-next_bb.start;clettake_frontb=trySome(take_front_exnb)withEmpty->None(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
try let front = Byte.take_front_exn b in \
front = Bytes.get s 0 with Byte.Empty -> s_len = 0)
*)lettake_back_exnb=ifb.start=b.stopthenraiseEmpty;ifb.stop=0thenb.stop<-A.lengthb.buf-1elseb.stop<-b.stop-1;letc=A.getb.bufb.stopinA.setb.bufb.stopA.dummy;clettake_backb=trySome(take_back_exnb)withEmpty->None(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
try let back = Byte.take_back_exn b in \
back = Bytes.get s (Bytes.length s - 1) \
with Byte.Empty -> s_len = 0)
*)letjunk_frontb=ifb.start=b.stopthenraiseEmpty;A.setb.bufb.startA.dummy;ifb.start+1=A.lengthb.bufthenb.start<-0elseb.start<-b.start+1(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
try let () = Byte.junk_front b in \
s_len - 1 = Byte.length b with Byte.Empty -> s_len = 0)
*)letjunk_backb=ifb.start=b.stopthenraiseEmpty;ifb.stop=0thenb.stop<-A.lengthb.buf-1elseb.stop<-b.stop-1;A.setb.bufb.stopA.dummy(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
try let () = Byte.junk_back b in \
s_len - 1 = Byte.length b with Byte.Empty -> s_len = 0)
*)letskipblen=iflen>lengthbthen(invalid_arg"CCRingBuffer.skip";);for_=1tolendojunk_frontbdone(*$Q
(Q.pair a_str a_str) (fun (s,s') -> \
let s = Bytes.of_string s in let s' = Bytes.of_string s' in \
let b = Byte.create (max (Bytes.length s+Bytes.length s') 64) in \
Byte.blit_from b s 0 (Bytes.length s); \
Byte.blit_from b s' 0 (Bytes.length s'); \
let h = Bytes.of_string "hello world" in \
Byte.blit_from b h 0 (Bytes.length h); (* big enough *) \
let l = Byte.length b in let l' = l/2 in Byte.skip b l'; \
Byte.length b + l' = l)
*)letclearb=skipb(lengthb)(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
Byte.clear b; \
Byte.length b = 0)
*)letiterb~f=ifb.stop>=b.startthenfori=b.starttob.stop-1dof(A.getb.bufi)doneelse(fori=b.starttoA.lengthb.buf-1dof(A.getb.bufi)done;fori=0tob.stop-1dof(A.getb.bufi)done;)letiterib~f=ifb.stop>=b.startthenfori=b.starttob.stop-1dofi(A.getb.bufi)doneelse(fori=b.starttoA.lengthb.buf-1dofi(A.getb.bufi)done;fori=0tob.stop-1dofi(A.getb.bufi)done;)(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
try Byte.iteri b ~f:(fun i c -> if Byte.get_front b i <> c then raise Exit); \
true with Exit -> false)
*)letgetbi=ifb.stop>=b.startthen(ifi>=b.stop-b.startthen(invalid_arg"CCRingBuffer.get")elseA.getb.buf(b.start+i))else(letlen_end=A.lengthb.buf-b.startinifi<len_endthenA.getb.buf(b.start+i)elseifi-len_end>b.stopthen(invalid_arg"CCRingBuffer.get")elseA.getb.buf(i-len_end))letget_frontbi=ifis_emptybthen(invalid_arg"CCRingBuffer.get_front")elsegetbi(*$Q
(Q.pair Q.small_int a_str) (fun (i, s) -> \
let s = Bytes.of_string (s ^ " ") in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
let index = abs (i mod Byte.length b) in \
let front = Byte.get_front b index in \
front = Bytes.get s index)
*)letget_backbi=letoffset=((lengthb)-i-1)inifoffset<0then(invalid_arg"CCRingBuffer.get_back")elsegetboffset(*$Q
(Q.pair Q.small_int a_str) (fun (i, s) -> \
let s = Bytes.of_string (s ^ " ") in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
let index = abs (i mod Byte.length b) in \
let back = Byte.get_back b index in \
back = Bytes.get s (s_len - index - 1))
*)letto_listb=letlen=lengthbinletrecbuildli=ifi<0thenlelsebuild((get_frontbi)::l)(i-1)inbuild[](len-1)(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
let l = Byte.to_list b in \
let explode s = let rec exp i l = \
if i < 0 then l else exp (i - 1) (Bytes.get s i :: l) in \
exp (Bytes.length s - 1) [] in \
explode s = l)
*)(* TODO: more efficient version, with one or two blit *)letappendb~into=iterb~f:(push_backinto)letpeek_front_exnb=ifis_emptybthenraiseEmptyelseA.getb.bufb.startletpeek_frontb=trySome(peek_front_exnb)withEmpty->None(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
try let back = Byte.peek_front_exn b in \
back = Bytes.get s 0 with Byte.Empty -> s_len = 0)
*)letpeek_back_exnb=ifis_emptybthenraiseEmptyelse(leti=ifb.stop=0thenA.lengthb.buf-1elseb.stop-1inA.getb.bufi)letpeek_backb=trySome(peek_back_exnb)withEmpty->None(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
try let back = Byte.peek_back_exn b in \
back = Bytes.get s (s_len - 1) with Byte.Empty -> s_len = 0)
*)letof_arraya=letb=create(max(A.lengtha)16)inblit_fromba0(A.lengtha);bletto_arrayb=leta=A.create(lengthb)inletn=blit_intoba0(lengthb)inassert(n=lengthb);a(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let b = Byte.of_array s in let s' = Byte.to_array b in \
s = s')
*)endmoduleByte=MakeFromArray(Array.Byte)moduleMake(Elt:sigtypetvaldummy:tend)=MakeFromArray(Array.Make(Elt))(*$inject
module BI = CCRingBuffer.Make(struct type t = int let dummy=0 end)
*)(* try to trigger an error on resize
see issue #126 *)(*$R
let b = BI.create 50 in
let st = Random.State.make [| 0 |] in
for _i = 1 to 100_000 do
if Random.State.float st 1.0 < 0.5 then
BI.push_back b 0
else
let _ = BI.take_front b in ()
done
*)(* Test against reference implementation (lists) on a succession of
operations.
Remarks on semantics:
JUNK_FRONT/JUNK_BACK: try to remove if not empty
SKIP: if at least n elements, skip; else nop
*)(*$inject
module BS = CCRingBuffer.Byte
type op =
| Push_back of char
| Take_front
| Take_back
| Peek_front
| Peek_back
| Junk_front
| Junk_back
| Skip of int
| Blit of string * int * int
| Z_if_full
let str_of_op = function
| Push_back c -> Printf.sprintf "push_back(%C)" c
| Take_front -> Printf.sprintf "take_front"
| Take_back -> Printf.sprintf "take_back"
| Peek_front -> Printf.sprintf "peek_front"
| Peek_back -> Printf.sprintf "peek_back"
| Junk_front -> Printf.sprintf "junk_front"
| Junk_back -> Printf.sprintf "junk_back"
| Skip n -> Printf.sprintf "skip(%d)" n
| Blit (s,i,len) -> Printf.sprintf "blit(%S,%d,%d)" s i len
| Z_if_full -> "zero_if_full"
let push_back c = Push_back c
let skip n = assert (n>=0); Skip n
let blit s i len =
if i<0 || len<0 || i+len > String.length s then (
failwith ("wrong blit: " ^ str_of_op (Blit (s,i,len)));
);
Blit (s,i,len)
let shrink_op =
let open Q.Iter in
function
| Push_back c -> Q.Shrink.char c >|= push_back
| Take_front | Take_back | Junk_back | Junk_front
| Z_if_full | Peek_front | Peek_back
-> empty
| Skip n -> Q.Shrink.int n >|= skip
| Blit (s,i,len) ->
let s_i =
Q.Shrink.int i >>= fun i' ->
assert (i' <= i && i' + len <= String.length s);
if i' <= 0 then empty else return (blit s i' len)
and s_len =
Q.Shrink.int len >>= fun len'->
assert (len' <= len && i + len' <= String.length s);
if len' <= 0 then empty else return (blit s i len')
and s_s =
Q.Shrink.string s >>= fun s' ->
if i+len > String.length s' then empty else return (blit s' i len)
in
append s_i (append s_len s_s)
let rec len_op size acc = function
| Push_back _ -> min size (acc + 1)
| Take_front | Take_back | Junk_front | Junk_back -> max (acc-1) 0
| Skip n -> if acc >= n then acc-n else acc
| Z_if_full | Peek_front | Peek_back -> acc
| Blit (_,_,len) -> min size (acc + len)
let apply_op b = function
| Push_back c -> BS.push_back b c; None
| Take_front -> BS.take_front b
| Take_back -> BS.take_back b
| Junk_front -> (try BS.junk_front b with BS.Empty -> ()); None
| Junk_back -> (try BS.junk_back b with BS.Empty -> ()); None
| Peek_front -> BS.peek_front b
| Peek_back -> BS.peek_back b
| Skip n -> if n <= BS.length b then BS.skip b n; None
| Blit (s,i,len) ->
assert(i+len <= String.length s);
BS.blit_from b (Bytes.unsafe_of_string s) i len; None
| Z_if_full -> if BS.is_full b then Some '0' else None
let gen_op =
let open Q.Gen in
let g_blit =
string_size ~gen:g_char (5--20) >>= fun s ->
(0 -- String.length s) >>= fun len ->
assert (len >= 0 && len <= String.length s);
(0--(String.length s-len)) >|= fun i ->
blit s i len
in
frequency
[ 3, return Take_back;
3, return Take_front;
1, return Junk_back;
1, return Junk_front;
1, return Peek_front;
1, return Peek_back;
2, g_blit;
1, (0--5 >|= skip);
2, map push_back g_char;
1, return Z_if_full;
]
let arb_op =
Q.make
~shrink:shrink_op
~print:str_of_op
gen_op
let arb_ops = Q.list_of_size Q.Gen.(0 -- 20) arb_op
module L_impl = struct
type t = {
size: int;
mutable l: char list;
}
let create size = {size; l=[]}
let normalize_ b =
let n = List.length b.l in
if n>b.size then b.l <- CCList.drop (n-b.size) b.l
let push_back b c = b.l <- b.l @ [c]; normalize_ b
let take_front b = match b.l with
| [] -> None
| c :: l -> b.l <- l; Some c
let peek_front b = match b.l with [] -> None | x::_ -> Some x
let take_back b =
let n = List.length b.l in
if n=0 then None
else (
let init, last = CCList.take_drop (n-1) b.l in
let x = List.hd last in
b.l <- init;
Some x
)
let peek_back b = match b.l with [] -> None | l -> Some (List.hd (List.rev l))
let junk_front b = ignore (take_front b)
let junk_back b = ignore (take_back b)
let skip b n =
if n <= List.length b.l then (
CCInt.range' 0 n (fun _ -> junk_front b)
)
let blit b s i len =
for j=i to i+len-1 do push_back b (String.get s j) done
let apply_op b = function
| Push_back c -> push_back b c; None
| Take_front -> take_front b
| Take_back -> take_back b
| Peek_front -> peek_front b
| Peek_back -> peek_back b
| Junk_back -> junk_back b; None
| Junk_front -> junk_front b; None
| Skip n -> skip b n; None
| Blit (s,i,len) -> blit b s i len; None
| Z_if_full -> if b.size = List.length b.l then Some '0' else None
let to_list b = b.l
end
*)(* check that a lot of operations can be applied without failure,
and that the result has correct length *)(*$QR & ~count:3_000
arb_ops (fun ops ->
let size = 64 in
let b = BS.create size in
List.iter (fun o-> ignore (apply_op b o)) ops;
BS.length b = List.fold_left (len_op size) 0 ops)
*)(* check identical behavior with list implem *)(*$QR & ~count:3_000
arb_ops (fun ops ->
let size = 64 in
let b = BS.create size in
let l = L_impl.create size in
let l1 = CCList.filter_map (apply_op b) ops in
let l2 = CCList.filter_map (L_impl.apply_op l) ops in
l1=l2 && BS.to_list b = L_impl.to_list l)
*)(* check that deleted elements can be GCed *)(*$inject
module BO = CCRingBuffer.Make(struct type t = int option let dummy=None end)
let make_bo () =
let b = BO.create 1000 in
for i = 1 to BO.capacity b do
BO.push_back b (Some i)
done;
b
let test_no_major_blocks clear =
Gc.full_major ();
let live_blocks_before = (Gc.stat ()).live_blocks in
let b = make_bo () in
clear b;
Gc.full_major ();
let live_blocks_after = (Gc.stat ()).live_blocks in
assert (BO.length b = 0);
let diff = live_blocks_after - live_blocks_before in
diff < BO.capacity b / 2
*)(*$T
test_no_major_blocks (fun b -> for _ = 1 to BO.length b do BO.junk_front b; done)
test_no_major_blocks (fun b -> for _ = 1 to BO.length b do BO.junk_back b; done)
test_no_major_blocks (fun b -> for _ = 1 to BO.length b do ignore (BO.take_front b); done)
test_no_major_blocks (fun b -> for _ = 1 to BO.length b do ignore (BO.take_back b); done)
test_no_major_blocks (fun b -> BO.skip b (BO.length b))
test_no_major_blocks (fun b -> BO.clear b)
*)