From 56928a1a156a84fd3c2221362aca62499a0248bb Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 10 Sep 2017 19:56:01 +0200 Subject: [PATCH] specify behavior of `CCFQueue.take_{front,back}_l` in some corner cases --- src/data/CCFQueue.ml | 6 ++++++ src/data/CCFQueue.mli | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/data/CCFQueue.ml b/src/data/CCFQueue.ml index e1cb7736..dfab9c50 100644 --- a/src/data/CCFQueue.ml +++ b/src/data/CCFQueue.ml @@ -132,6 +132,9 @@ let take_front q = with Empty -> None let take_front_l n q = + if n<0 then ( + invalid_arg "take_back_l: cannot take negative number of arguments" + ); let rec aux acc q n = if n=0 || is_empty q then List.rev acc, q else @@ -183,6 +186,9 @@ let take_back q = with Empty -> None let take_back_l n q = + if n<0 then ( + invalid_arg "take_back_l: cannot take negative number of arguments" + ); let rec aux acc q n = if n=0 || is_empty q then q, acc else diff --git a/src/data/CCFQueue.mli b/src/data/CCFQueue.mli index fe159c4e..6bb56af5 100644 --- a/src/data/CCFQueue.mli +++ b/src/data/CCFQueue.mli @@ -38,7 +38,8 @@ val take_front_exn : 'a t -> ('a * 'a t) val take_front_l : int -> 'a t -> 'a list * 'a t (** [take_front_l n q] takes at most [n] elements from the front - of [q], and returns them wrapped in a list *) + of [q], and returns them wrapped in a list + @raise Invalid_argument if n<0 *) val take_front_while : ('a -> bool) -> 'a t -> 'a list * 'a t @@ -51,7 +52,8 @@ val take_back_l : int -> 'a t -> 'a t * 'a list (** [take_back_l n q] removes and returns the last [n] elements of [q]. The elements are in the order of the queue, that is, the head of the returned list is the first element to appear via {!take_front}. - [take_back_l 2 (of_list [1;2;3;4]) = of_list [1;2], [3;4]] *) + [take_back_l 2 (of_list [1;2;3;4]) = of_list [1;2], [3;4]] + @raise Invalid_argument if n<0 *) val take_back_while : ('a -> bool) -> 'a t -> 'a t * 'a list