Skip to content

Commit be84b76

Browse files
committed
add CCRingBuffer.is_full
1 parent f91af32 commit be84b76

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Diff for: src/data/CCRingBuffer.ml

+23-8
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ module type S = sig
8080
val create : int -> t
8181
(** [create size] creates a new bounded buffer with given size.
8282
The underlying array is allocated immediately and no further (large)
83-
allocation will happen from now on. *)
83+
allocation will happen from now on.
84+
@raise Invalid_argument if the arguments is [< 1] *)
8485

8586
val copy : t -> t
8687
(** Make a fresh copy of the buffer. *)
@@ -91,6 +92,10 @@ module type S = sig
9192
val length : t -> int
9293
(** Number of elements currently stored in the buffer. *)
9394

95+
val is_full : t -> bool
96+
(** true if pushing an element would erase another element.
97+
@since NEXT_RELEASE *)
98+
9499
val blit_from : t -> Array.t -> int -> int -> unit
95100
(** [blit_from buf from_buf o len] copies the slice [o, ... o + len - 1] from
96101
a input buffer [from_buf] to the end of the buffer.
@@ -152,25 +157,25 @@ module type S = sig
152157
otherwise the oldest elements are replaced first. *)
153158

154159
val peek_front : t -> Array.elt
155-
(** First value from front of [t].
160+
(** First value from front of [t], without modification.
156161
@raise Empty if buffer is empty. *)
157162

158163
val peek_back : t -> Array.elt
159-
(** Get the last value from back of [t].
164+
(** Get the last value from back of [t], without modification.
160165
@raise Empty if buffer is empty. *)
161166

162167
val take_back : t -> Array.elt option
163-
(** Take the last value from back of [t], if any *)
168+
(** Take and remove the last value from back of [t], if any *)
164169

165170
val take_back_exn : t -> Array.elt
166-
(** Take the last value from back of [t].
171+
(** Take and remove the last value from back of [t].
167172
@raise Empty if buffer is already empty. *)
168173

169174
val take_front : t -> Array.elt option
170-
(** Take the first value from front of [t], if any *)
175+
(** Take and remove the first value from front of [t], if any *)
171176

172177
val take_front_exn : t -> Array.elt
173-
(** Take the first value from front of [t].
178+
(** Take and remove the first value from front of [t].
174179
@raise Empty if buffer is already empty. *)
175180

176181
val of_array : Array.t -> t
@@ -235,6 +240,8 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
235240
then b.stop - b.start
236241
else (A.length b.buf - b.start) + b.stop
237242

243+
let is_full b = length b + 1 = Array.length b.buf
244+
238245
let next_ b i =
239246
let j = i+1 in
240247
if j = A.length b.buf then 0 else j
@@ -629,6 +636,7 @@ type op =
629636
| Junk_back
630637
| Skip of int
631638
| Blit of string * int * int
639+
| Z_if_full
632640
633641
let str_of_op = function
634642
| Push_back c -> Printf.sprintf "push_back(%C)" c
@@ -638,6 +646,7 @@ let str_of_op = function
638646
| Junk_back -> Printf.sprintf "junk_back"
639647
| Skip n -> Printf.sprintf "skip(%d)" n
640648
| Blit (s,i,len) -> Printf.sprintf "blit(%S,%d,%d)" s i len
649+
| Z_if_full -> "zero_if_full"
641650
642651
let push_back c = Push_back c
643652
let skip n = assert (n>=0); Skip n
@@ -651,7 +660,9 @@ let shrink_op =
651660
let open Q.Iter in
652661
function
653662
| Push_back c -> Q.Shrink.char c >|= push_back
654-
| Take_front | Take_back | Junk_back | Junk_front -> empty
663+
| Take_front | Take_back | Junk_back | Junk_front
664+
| Z_if_full
665+
-> empty
655666
| Skip n -> Q.Shrink.int n >|= skip
656667
| Blit (s,i,len) ->
657668
let s_i =
@@ -672,6 +683,7 @@ let rec len_op size acc = function
672683
| Push_back _ -> min size (acc + 1)
673684
| Take_front | Take_back | Junk_front | Junk_back -> max (acc-1) 0
674685
| Skip n -> if acc >= n then acc-n else acc
686+
| Z_if_full -> acc
675687
| Blit (_,_,len) -> min size (acc + len)
676688
677689
let apply_op b = function
@@ -684,6 +696,7 @@ let apply_op b = function
684696
| Blit (s,i,len) ->
685697
assert(i+len <= String.length s);
686698
BS.blit_from b (Bytes.unsafe_of_string s) i len; None
699+
| Z_if_full -> if BS.is_full b then Some '0' else None
687700
688701
let gen_op =
689702
let open Q.Gen in
@@ -702,6 +715,7 @@ let gen_op =
702715
2, g_blit;
703716
1, (0--5 >|= skip);
704717
2, map push_back g_char;
718+
1, return Z_if_full;
705719
]
706720
707721
let arb_op =
@@ -755,6 +769,7 @@ module L_impl = struct
755769
| Junk_front -> junk_front b; None
756770
| Skip n -> skip b n; None
757771
| Blit (s,i,len) -> blit b s i len; None
772+
| Z_if_full -> if b.size = List.length b.l then Some '0' else None
758773
759774
let to_list b = b.l
760775
end

Diff for: src/data/CCRingBuffer.mli

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ module type S = sig
9393
val length : t -> int
9494
(** Number of elements currently stored in the buffer. *)
9595

96+
val is_full : t -> bool
97+
(** true if pushing an element would erase another element.
98+
@since NEXT_RELEASE *)
99+
96100
val blit_from : t -> Array.t -> int -> int -> unit
97101
(** [blit_from buf from_buf o len] copies the slice [o, ... o + len - 1] from
98102
a input buffer [from_buf] to the end of the buffer.

0 commit comments

Comments
 (0)