breaking: change the exceptions in CCVector

This commit is contained in:
Simon Cruanes 2015-08-31 19:29:20 +02:00
parent e755065fc7
commit 1feea19383
3 changed files with 16 additions and 12 deletions

View file

@ -102,7 +102,7 @@ module type S = sig
val to_tree : t -> elt ktree val to_tree : t -> elt ktree
end end
module Make(E : PARTIAL_ORD) = struct module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct
type elt = E.t type elt = E.t
type t = type t =

View file

@ -157,15 +157,15 @@ let append a b =
*) *)
let get v i = 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 Array.unsafe_get v.vec i
let set v i x = 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 Array.unsafe_set v.vec i x
let remove v i = 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 v.(i) not the last element, then put last element at index i *)
if i < v.size - 1 if i < v.size - 1
then v.vec.(i) <- v.vec.(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 if c = 0 then check (i+1) else c
in check 0 in check 0
exception Empty
let pop_exn v = let pop_exn v =
if v.size = 0 if v.size = 0 then raise Empty;
then failwith "Vector.pop on empty vector";
v.size <- v.size - 1; v.size <- v.size - 1;
let x = v.vec.(v.size) in let x = v.vec.(v.size) in
x x
let pop v = let pop v =
try Some (pop_exn v) try Some (pop_exn v)
with Failure _ -> None with Empty -> None
let top v = let top v =
if v.size = 0 then None else Some v.vec.(v.size-1) if v.size = 0 then None else Some v.vec.(v.size-1)
let top_exn v = let top_exn v =
if v.size = 0 then failwith "Vector.top"; if v.size = 0 then raise Empty;
v.vec.(v.size-1) v.vec.(v.size-1)
(*$T (*$T

View file

@ -92,12 +92,15 @@ val equal : 'a equal -> ('a,_) t equal
val compare : 'a ord -> ('a,_) t ord val compare : 'a ord -> ('a,_) t ord
(** Total ordering on vectors: Lexicographic comparison. *) (** Total ordering on vectors: Lexicographic comparison. *)
exception Empty
(** Raised on empty stack *)
val pop : ('a, rw) t -> 'a option val pop : ('a, rw) t -> 'a option
(** Remove last element, or [None] *) (** Remove last element, or [None] *)
val pop_exn : ('a, rw) t -> 'a val pop_exn : ('a, rw) t -> 'a
(** remove last element, or raise a Failure if empty (** 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 val top : ('a, _) t -> 'a option
(** Top element, if present (** Top element, if present
@ -105,7 +108,7 @@ val top : ('a, _) t -> 'a option
val top_exn : ('a, _) t -> 'a val top_exn : ('a, _) t -> 'a
(** Top element, if present (** Top element, if present
@raise Failure on an empty vector @raise Empty on an empty vector
@since 0.6 *) @since 0.6 *)
val copy : ('a,_) t -> ('a,'mut) t 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 val get : ('a,_) t -> int -> 'a
(** access element by its index, or (** 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 val set : ('a, rw) t -> int -> 'a -> unit
(** modify element at given index, or (** modify element at given index, or
@raise Failure if bad index *) @raise Invalid_argument if bad index *)
val remove : ('a, rw) t -> int -> unit val remove : ('a, rw) t -> int -> unit
(** Remove the [n-th] element of the vector. Does {b NOT} preserve the order (** Remove the [n-th] element of the vector. Does {b NOT} preserve the order