diff --git a/src/core/CCHeap.ml b/src/core/CCHeap.ml index 25f9d6f5..db84107a 100644 --- a/src/core/CCHeap.ml +++ b/src/core/CCHeap.ml @@ -102,7 +102,7 @@ module type S = sig val to_tree : t -> elt ktree end -module Make(E : PARTIAL_ORD) = struct +module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct type elt = E.t type t = diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index e8f0d741..de490786 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -157,15 +157,15 @@ let append a b = *) let get v i = - if i < 0 || i >= v.size then failwith "Vector.get"; + if i < 0 || i >= v.size then invalid_arg "Vector.get"; Array.unsafe_get v.vec i let set v i x = - if i < 0 || i >= v.size then failwith "Vector.set"; + if i < 0 || i >= v.size then invalid_arg "Vector.set"; Array.unsafe_set v.vec i x let remove v i = - if i < 0 || i >= v.size then failwith "Vector.remove"; + if i < 0 || i >= v.size then invalid_arg "Vector.remove"; (* if v.(i) not the last element, then put last element at index i *) if i < v.size - 1 then v.vec.(i) <- v.vec.(v.size - 1); @@ -204,22 +204,23 @@ let compare cmp v1 v2 = if c = 0 then check (i+1) else c in check 0 +exception Empty + let pop_exn v = - if v.size = 0 - then failwith "Vector.pop on empty vector"; + if v.size = 0 then raise Empty; v.size <- v.size - 1; let x = v.vec.(v.size) in x let pop v = try Some (pop_exn v) - with Failure _ -> None + with Empty -> None let top v = if v.size = 0 then None else Some v.vec.(v.size-1) let top_exn v = - if v.size = 0 then failwith "Vector.top"; + if v.size = 0 then raise Empty; v.vec.(v.size-1) (*$T diff --git a/src/core/CCVector.mli b/src/core/CCVector.mli index e9362f75..79cc9798 100644 --- a/src/core/CCVector.mli +++ b/src/core/CCVector.mli @@ -92,12 +92,15 @@ val equal : 'a equal -> ('a,_) t equal val compare : 'a ord -> ('a,_) t ord (** Total ordering on vectors: Lexicographic comparison. *) +exception Empty +(** Raised on empty stack *) + val pop : ('a, rw) t -> 'a option (** Remove last element, or [None] *) val pop_exn : ('a, rw) t -> 'a (** remove last element, or raise a Failure if empty - @raise Failure on an empty vector *) + @raise Empty on an empty vector *) val top : ('a, _) t -> 'a option (** Top element, if present @@ -105,7 +108,7 @@ val top : ('a, _) t -> 'a option val top_exn : ('a, _) t -> 'a (** Top element, if present - @raise Failure on an empty vector + @raise Empty on an empty vector @since 0.6 *) val copy : ('a,_) t -> ('a,'mut) t @@ -178,11 +181,11 @@ val (>|=) : ('a,_) t -> ('a -> 'b) -> ('b, 'mut) t val get : ('a,_) t -> int -> 'a (** access element by its index, or - @raise Failure if bad index *) + @raise Invalid_argument if bad index *) val set : ('a, rw) t -> int -> 'a -> unit (** modify element at given index, or - @raise Failure if bad index *) + @raise Invalid_argument if bad index *) val remove : ('a, rw) t -> int -> unit (** Remove the [n-th] element of the vector. Does {b NOT} preserve the order