Skip to content

Commit a3ff9db

Browse files
committed
change CCRingBuffer.peek_{front,back} to return options (closes #127)
1 parent be84b76 commit a3ff9db

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

src/data/CCRingBuffer.ml

+37-13
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,21 @@ module type S = sig
156156
If [t.bounded=false], the buffer will grow as needed,
157157
otherwise the oldest elements are replaced first. *)
158158

159-
val peek_front : t -> Array.elt
159+
val peek_front : t -> Array.elt option
160+
(** First value from front of [t], without modification. *)
161+
162+
val peek_front_exn : t -> Array.elt
160163
(** First value from front of [t], without modification.
161-
@raise Empty if buffer is empty. *)
164+
@raise Empty if buffer is empty.
165+
@since NEXT_RELEASE *)
166+
167+
val peek_back : t -> Array.elt option
168+
(** Get the last value from back of [t], without modification. *)
162169

163-
val peek_back : t -> Array.elt
170+
val peek_back_exn : t -> Array.elt
164171
(** Get the last value from back of [t], without modification.
165-
@raise Empty if buffer is empty. *)
172+
@raise Empty if buffer is empty.
173+
@since NEXT_RELEASE *)
166174

167175
val take_back : t -> Array.elt option
168176
(** Take and remove the last value from back of [t], if any *)
@@ -279,7 +287,7 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
279287
let b = Byte.create (max s_len 64) in \
280288
Byte.blit_from b s 0 s_len; \
281289
Byte.push_back b 'X'; \
282-
Byte.peek_back b = 'X')
290+
Byte.peek_back_exn b = 'X')
283291
*)
284292

285293
(*$Q
@@ -544,32 +552,36 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
544552
let append b ~into =
545553
iter b ~f:(push_back into)
546554

547-
let peek_front b =
555+
let peek_front_exn b =
548556
if is_empty b then raise Empty
549557
else A.get b.buf b.start
550558

559+
let peek_front b = try Some (peek_front_exn b) with Empty -> None
560+
551561
(*$Q
552562
a_str (fun s -> let s = Bytes.of_string s in \
553563
let s_len = Bytes.length s in \
554564
let b = Byte.create (max s_len 64) in \
555565
Byte.blit_from b s 0 s_len; \
556-
try let back = Byte.peek_front b in \
566+
try let back = Byte.peek_front_exn b in \
557567
back = Bytes.get s 0 with Byte.Empty -> s_len = 0)
558568
*)
559569

560-
let peek_back b = if is_empty b
570+
let peek_back_exn b = if is_empty b
561571
then raise Empty
562572
else (
563573
let i = if b.stop = 0 then A.length b.buf - 1 else b.stop-1 in
564574
A.get b.buf i
565575
)
566576

577+
let peek_back b = try Some (peek_back_exn b) with Empty -> None
578+
567579
(*$Q
568580
a_str (fun s -> let s = Bytes.of_string s in \
569581
let s_len = Bytes.length s in \
570582
let b = Byte.create (max s_len 64) in \
571583
Byte.blit_from b s 0 s_len; \
572-
try let back = Byte.peek_back b in \
584+
try let back = Byte.peek_back_exn b in \
573585
back = Bytes.get s (s_len - 1) with Byte.Empty -> s_len = 0)
574586
*)
575587

@@ -632,6 +644,8 @@ type op =
632644
| Push_back of char
633645
| Take_front
634646
| Take_back
647+
| Peek_front
648+
| Peek_back
635649
| Junk_front
636650
| Junk_back
637651
| Skip of int
@@ -642,6 +656,8 @@ let str_of_op = function
642656
| Push_back c -> Printf.sprintf "push_back(%C)" c
643657
| Take_front -> Printf.sprintf "take_front"
644658
| Take_back -> Printf.sprintf "take_back"
659+
| Peek_front -> Printf.sprintf "peek_front"
660+
| Peek_back -> Printf.sprintf "peek_back"
645661
| Junk_front -> Printf.sprintf "junk_front"
646662
| Junk_back -> Printf.sprintf "junk_back"
647663
| Skip n -> Printf.sprintf "skip(%d)" n
@@ -661,7 +677,7 @@ let shrink_op =
661677
function
662678
| Push_back c -> Q.Shrink.char c >|= push_back
663679
| Take_front | Take_back | Junk_back | Junk_front
664-
| Z_if_full
680+
| Z_if_full | Peek_front | Peek_back
665681
-> empty
666682
| Skip n -> Q.Shrink.int n >|= skip
667683
| Blit (s,i,len) ->
@@ -683,7 +699,7 @@ let rec len_op size acc = function
683699
| Push_back _ -> min size (acc + 1)
684700
| Take_front | Take_back | Junk_front | Junk_back -> max (acc-1) 0
685701
| Skip n -> if acc >= n then acc-n else acc
686-
| Z_if_full -> acc
702+
| Z_if_full | Peek_front | Peek_back -> acc
687703
| Blit (_,_,len) -> min size (acc + len)
688704
689705
let apply_op b = function
@@ -692,6 +708,8 @@ let apply_op b = function
692708
| Take_back -> BS.take_back b
693709
| Junk_front -> (try BS.junk_front b with BS.Empty -> ()); None
694710
| Junk_back -> (try BS.junk_back b with BS.Empty -> ()); None
711+
| Peek_front -> BS.peek_front b
712+
| Peek_back -> BS.peek_back b
695713
| Skip n -> if n <= BS.length b then BS.skip b n; None
696714
| Blit (s,i,len) ->
697715
assert(i+len <= String.length s);
@@ -712,6 +730,8 @@ let gen_op =
712730
3, return Take_front;
713731
1, return Junk_back;
714732
1, return Junk_front;
733+
1, return Peek_front;
734+
1, return Peek_back;
715735
2, g_blit;
716736
1, (0--5 >|= skip);
717737
2, map push_back g_char;
@@ -742,6 +762,7 @@ module L_impl = struct
742762
let take_front b = match b.l with
743763
| [] -> None
744764
| c :: l -> b.l <- l; Some c
765+
let peek_front b = match b.l with [] -> None | x::_ -> Some x
745766
let take_back b =
746767
let n = List.length b.l in
747768
if n=0 then None
@@ -751,6 +772,7 @@ module L_impl = struct
751772
b.l <- init;
752773
Some x
753774
)
775+
let peek_back b = match b.l with [] -> None | l -> Some (List.hd (List.rev l))
754776
let junk_front b = ignore (take_front b)
755777
let junk_back b = ignore (take_back b)
756778
let skip b n =
@@ -765,6 +787,8 @@ module L_impl = struct
765787
| Push_back c -> push_back b c; None
766788
| Take_front -> take_front b
767789
| Take_back -> take_back b
790+
| Peek_front -> peek_front b
791+
| Peek_back -> peek_back b
768792
| Junk_back -> junk_back b; None
769793
| Junk_front -> junk_front b; None
770794
| Skip n -> skip b n; None
@@ -778,7 +802,7 @@ end
778802

779803
(* check that a lot of operations can be applied without failure,
780804
and that the result has correct length *)
781-
(*$QR & ~count:1_000
805+
(*$QR & ~count:3_000
782806
arb_ops (fun ops ->
783807
let size = 64 in
784808
let b = BS.create size in
@@ -787,7 +811,7 @@ end
787811
*)
788812

789813
(* check identical behavior with list implem *)
790-
(*$QR & ~count:1_000
814+
(*$QR & ~count:3_000
791815
arb_ops (fun ops ->
792816
let size = 64 in
793817
let b = BS.create size in

src/data/CCRingBuffer.mli

+12-4
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,21 @@ module type S = sig
157157
If [t.bounded=false], the buffer will grow as needed,
158158
otherwise the oldest elements are replaced first. *)
159159

160-
val peek_front : t -> Array.elt
160+
val peek_front : t -> Array.elt option
161+
(** First value from front of [t], without modification. *)
162+
163+
val peek_front_exn : t -> Array.elt
161164
(** First value from front of [t], without modification.
162-
@raise Empty if buffer is empty. *)
165+
@raise Empty if buffer is empty.
166+
@since NEXT_RELEASE *)
163167

164-
val peek_back : t -> Array.elt
168+
val peek_back : t -> Array.elt option
169+
(** Get the last value from back of [t], without modification. *)
170+
171+
val peek_back_exn : t -> Array.elt
165172
(** Get the last value from back of [t], without modification.
166-
@raise Empty if buffer is empty. *)
173+
@raise Empty if buffer is empty.
174+
@since NEXT_RELEASE *)
167175

168176
val take_back : t -> Array.elt option
169177
(** Take and remove the last value from back of [t], if any *)

0 commit comments

Comments
 (0)