@@ -156,13 +156,21 @@ module type S = sig
156
156
If [t.bounded=false], the buffer will grow as needed,
157
157
otherwise the oldest elements are replaced first. *)
158
158
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
160
163
(* * 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. *)
162
169
163
- val peek_back : t -> Array .elt
170
+ val peek_back_exn : t -> Array .elt
164
171
(* * 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 *)
166
174
167
175
val take_back : t -> Array .elt option
168
176
(* * 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
279
287
let b = Byte.create (max s_len 64) in \
280
288
Byte.blit_from b s 0 s_len; \
281
289
Byte.push_back b 'X'; \
282
- Byte.peek_back b = 'X')
290
+ Byte.peek_back_exn b = 'X')
283
291
*)
284
292
285
293
(* $Q
@@ -544,32 +552,36 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
544
552
let append b ~into =
545
553
iter b ~f: (push_back into)
546
554
547
- let peek_front b =
555
+ let peek_front_exn b =
548
556
if is_empty b then raise Empty
549
557
else A. get b.buf b.start
550
558
559
+ let peek_front b = try Some (peek_front_exn b) with Empty -> None
560
+
551
561
(* $Q
552
562
a_str (fun s -> let s = Bytes.of_string s in \
553
563
let s_len = Bytes.length s in \
554
564
let b = Byte.create (max s_len 64) in \
555
565
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 \
557
567
back = Bytes.get s 0 with Byte.Empty -> s_len = 0)
558
568
*)
559
569
560
- let peek_back b = if is_empty b
570
+ let peek_back_exn b = if is_empty b
561
571
then raise Empty
562
572
else (
563
573
let i = if b.stop = 0 then A. length b.buf - 1 else b.stop-1 in
564
574
A. get b.buf i
565
575
)
566
576
577
+ let peek_back b = try Some (peek_back_exn b) with Empty -> None
578
+
567
579
(* $Q
568
580
a_str (fun s -> let s = Bytes.of_string s in \
569
581
let s_len = Bytes.length s in \
570
582
let b = Byte.create (max s_len 64) in \
571
583
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 \
573
585
back = Bytes.get s (s_len - 1) with Byte.Empty -> s_len = 0)
574
586
*)
575
587
@@ -632,6 +644,8 @@ type op =
632
644
| Push_back of char
633
645
| Take_front
634
646
| Take_back
647
+ | Peek_front
648
+ | Peek_back
635
649
| Junk_front
636
650
| Junk_back
637
651
| Skip of int
@@ -642,6 +656,8 @@ let str_of_op = function
642
656
| Push_back c -> Printf.sprintf "push_back(%C)" c
643
657
| Take_front -> Printf.sprintf "take_front"
644
658
| Take_back -> Printf.sprintf "take_back"
659
+ | Peek_front -> Printf.sprintf "peek_front"
660
+ | Peek_back -> Printf.sprintf "peek_back"
645
661
| Junk_front -> Printf.sprintf "junk_front"
646
662
| Junk_back -> Printf.sprintf "junk_back"
647
663
| Skip n -> Printf.sprintf "skip(%d)" n
@@ -661,7 +677,7 @@ let shrink_op =
661
677
function
662
678
| Push_back c -> Q.Shrink.char c >|= push_back
663
679
| Take_front | Take_back | Junk_back | Junk_front
664
- | Z_if_full
680
+ | Z_if_full | Peek_front | Peek_back
665
681
-> empty
666
682
| Skip n -> Q.Shrink.int n >|= skip
667
683
| Blit (s,i,len) ->
@@ -683,7 +699,7 @@ let rec len_op size acc = function
683
699
| Push_back _ -> min size (acc + 1)
684
700
| Take_front | Take_back | Junk_front | Junk_back -> max (acc-1) 0
685
701
| Skip n -> if acc >= n then acc-n else acc
686
- | Z_if_full -> acc
702
+ | Z_if_full | Peek_front | Peek_back -> acc
687
703
| Blit (_,_,len) -> min size (acc + len)
688
704
689
705
let apply_op b = function
@@ -692,6 +708,8 @@ let apply_op b = function
692
708
| Take_back -> BS.take_back b
693
709
| Junk_front -> (try BS.junk_front b with BS.Empty -> ()); None
694
710
| 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
695
713
| Skip n -> if n <= BS.length b then BS.skip b n; None
696
714
| Blit (s,i,len) ->
697
715
assert(i+len <= String.length s);
@@ -712,6 +730,8 @@ let gen_op =
712
730
3, return Take_front;
713
731
1, return Junk_back;
714
732
1, return Junk_front;
733
+ 1, return Peek_front;
734
+ 1, return Peek_back;
715
735
2, g_blit;
716
736
1, (0--5 >|= skip);
717
737
2, map push_back g_char;
@@ -742,6 +762,7 @@ module L_impl = struct
742
762
let take_front b = match b.l with
743
763
| [] -> None
744
764
| c :: l -> b.l <- l; Some c
765
+ let peek_front b = match b.l with [] -> None | x::_ -> Some x
745
766
let take_back b =
746
767
let n = List.length b.l in
747
768
if n=0 then None
@@ -751,6 +772,7 @@ module L_impl = struct
751
772
b.l <- init;
752
773
Some x
753
774
)
775
+ let peek_back b = match b.l with [] -> None | l -> Some (List.hd (List.rev l))
754
776
let junk_front b = ignore (take_front b)
755
777
let junk_back b = ignore (take_back b)
756
778
let skip b n =
@@ -765,6 +787,8 @@ module L_impl = struct
765
787
| Push_back c -> push_back b c; None
766
788
| Take_front -> take_front b
767
789
| Take_back -> take_back b
790
+ | Peek_front -> peek_front b
791
+ | Peek_back -> peek_back b
768
792
| Junk_back -> junk_back b; None
769
793
| Junk_front -> junk_front b; None
770
794
| Skip n -> skip b n; None
778
802
779
803
(* check that a lot of operations can be applied without failure,
780
804
and that the result has correct length *)
781
- (* $QR & ~count:1_000
805
+ (* $QR & ~count:3_000
782
806
arb_ops (fun ops ->
783
807
let size = 64 in
784
808
let b = BS.create size in
787
811
*)
788
812
789
813
(* check identical behavior with list implem *)
790
- (* $QR & ~count:1_000
814
+ (* $QR & ~count:3_000
791
815
arb_ops (fun ops ->
792
816
let size = 64 in
793
817
let b = BS.create size in
0 commit comments