mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 03:35:30 -05:00
wip fixes
This commit is contained in:
parent
472aaa83a2
commit
8fec7b005e
1 changed files with 18 additions and 28 deletions
|
|
@ -40,7 +40,7 @@ let copy b =
|
||||||
{ b with buf=Array.copy b.buf; }
|
{ b with buf=Array.copy b.buf; }
|
||||||
|
|
||||||
|
|
||||||
let capacity b = b.size
|
let capacity b = Array.length b.buf
|
||||||
|
|
||||||
let length b =
|
let length b =
|
||||||
if b.stop >= b.start
|
if b.stop >= b.start
|
||||||
|
|
@ -65,34 +65,20 @@ let resize b cap elem =
|
||||||
end
|
end
|
||||||
in
|
in
|
||||||
b.buf <- buf';
|
b.buf <- buf';
|
||||||
b.start <- 0;
|
|
||||||
b.stop <- len;
|
|
||||||
()
|
()
|
||||||
|
|
||||||
let blit_from b from_buf o len =
|
let blit_from b from_buf o len =
|
||||||
|
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 (Array.length from_buf) = 0 then () else
|
if capacity b < b.size then
|
||||||
if cap < len then
|
resize b b.size from_buf.(0);
|
||||||
resize b (Array.length b.buf + len + 24) from_buf.(0);
|
let sub = Array.sub from_buf o len in
|
||||||
assert (capacity b - length b >= len);
|
let iter i x =
|
||||||
if b.stop >= b.start
|
b.start <- i mod capacity b;
|
||||||
then (* [_______ start xxxxxxxxx stop ______] *)
|
Array.set b.buf x b.start in
|
||||||
let len_end = Array.length b.buf - b.stop in
|
Array.iteri iter sub
|
||||||
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 0 b.buf b.stop len;
|
|
||||||
b.stop <- b.stop + len
|
|
||||||
end;
|
|
||||||
()
|
|
||||||
|
|
||||||
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
|
||||||
then raise (Invalid_argument "BufferIO.blit_into");
|
then raise (Invalid_argument "BufferIO.blit_into");
|
||||||
|
|
@ -128,9 +114,7 @@ let clear b =
|
||||||
|
|
||||||
let reset b =
|
let reset b =
|
||||||
clear b;
|
clear b;
|
||||||
if capacity b > 64
|
b.buf <- Array.of_list []
|
||||||
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
|
||||||
|
|
||||||
|
|
@ -199,4 +183,10 @@ let get b i =
|
||||||
else b.buf.(i - len_end)
|
else b.buf.(i - len_end)
|
||||||
|
|
||||||
let to_list b =
|
let to_list b =
|
||||||
Array.to_list (Array.sub b.buf b.start b.stop)
|
if (b.stop >= b.start)
|
||||||
|
then Array.to_list (Array.sub b.buf b.start b.stop)
|
||||||
|
else List.append
|
||||||
|
(Array.to_list (Array.sub b.buf b.start (Array.length b.buf)))
|
||||||
|
(Array.to_list (Array.sub b.buf 0 b.stop))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue