mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
working polymorphic ring buffer
This commit is contained in:
parent
acd1b6e97e
commit
7d92950a4e
1 changed files with 13 additions and 10 deletions
|
|
@ -52,7 +52,7 @@ let resize b cap elem =
|
||||||
assert (cap >= Array.length b.buf);
|
assert (cap >= Array.length b.buf);
|
||||||
let buf' = Array.make cap elem in
|
let buf' = Array.make cap elem in
|
||||||
(* copy into buf' *)
|
(* copy into buf' *)
|
||||||
let len =
|
let _:int =
|
||||||
if b.stop >= b.start
|
if b.stop >= b.start
|
||||||
then begin
|
then begin
|
||||||
Array.blit b.buf b.start buf' 0 (b.stop - b.start);
|
Array.blit b.buf b.start buf' 0 (b.stop - b.start);
|
||||||
|
|
@ -64,20 +64,23 @@ let resize b cap elem =
|
||||||
len_end + b.stop
|
len_end + b.stop
|
||||||
end
|
end
|
||||||
in
|
in
|
||||||
b.buf <- buf';
|
b.buf <- buf'
|
||||||
()
|
|
||||||
|
|
||||||
let blit_from b from_buf o len =
|
let blit_from b from_buf o len =
|
||||||
if (Array.length from_buf) = 0 then () else
|
if (Array.length from_buf) = 0 then () else
|
||||||
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
|
if cap < len then
|
||||||
resize b (min b.size (Array.length b.buf + len + 24)) from_buf.(0);
|
resize b (min (b.size+1) (Array.length b.buf + len + 24)) from_buf.(0);
|
||||||
let sub = Array.sub from_buf o len in
|
let sub = Array.sub from_buf o len in
|
||||||
let iter i x =
|
let iter x =
|
||||||
b.start <- i mod capacity b;
|
if b.start = 0 then b.start <- capacity b - 1 else b.start <- b.start - 1;
|
||||||
Array.set b.buf x b.start in
|
if b.start = b.stop then
|
||||||
Array.iteri iter sub
|
begin
|
||||||
|
if b.stop = 0 then b.stop <- capacity b - 1 else b.stop <- b.stop - 1
|
||||||
|
end;
|
||||||
|
Array.set b.buf b.start x in
|
||||||
|
Array.iter iter sub
|
||||||
|
|
||||||
let blit_into b to_buf o len =
|
let blit_into b to_buf o len =
|
||||||
if o+len > Array.length to_buf
|
if o+len > Array.length to_buf
|
||||||
|
|
@ -184,9 +187,9 @@ let get b i =
|
||||||
|
|
||||||
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)
|
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)))
|
(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))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue