2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. This file is distributed under the terms of *)
|
|
|
|
(* the GNU Lesser General Public License version 2.1, with the *)
|
|
|
|
(* special exception on linking described in the file LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
1995-08-09 08:06:35 -07:00
|
|
|
|
2001-10-26 16:33:00 -07:00
|
|
|
(** Last-in first-out stacks.
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-10-25 11:34:07 -07:00
|
|
|
This module implements stacks (LIFOs), with in-place modification.
|
2001-10-26 16:33:00 -07:00
|
|
|
*)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
type 'a t
|
2001-12-03 14:16:03 -08:00
|
|
|
(** The type of stacks containing elements of type ['a]. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
exception Empty
|
2001-12-03 14:16:03 -08:00
|
|
|
(** Raised when {!Stack.pop} or {!Stack.top} is applied to an empty stack. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-10-26 16:33:00 -07:00
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val create : unit -> 'a t
|
2001-10-26 16:33:00 -07:00
|
|
|
(** Return a new stack, initially empty. *)
|
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val push : 'a -> 'a t -> unit
|
2001-10-26 16:33:00 -07:00
|
|
|
(** [push x s] adds the element [x] at the top of stack [s]. *)
|
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val pop : 'a t -> 'a
|
2001-10-26 16:33:00 -07:00
|
|
|
(** [pop s] removes and returns the topmost element in stack [s],
|
|
|
|
or raises [Empty] if the stack is empty. *)
|
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val top : 'a t -> 'a
|
2001-10-26 16:33:00 -07:00
|
|
|
(** [top s] returns the topmost element in stack [s],
|
|
|
|
or raises [Empty] if the stack is empty. *)
|
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val clear : 'a t -> unit
|
2001-10-26 16:33:00 -07:00
|
|
|
(** Discard all elements from a stack. *)
|
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val copy : 'a t -> 'a t
|
2001-10-26 16:33:00 -07:00
|
|
|
(** Return a copy of the given stack. *)
|
|
|
|
|
2002-06-27 01:48:26 -07:00
|
|
|
val is_empty : 'a t -> bool
|
|
|
|
(** Return [true] if the given stack is empty, [false] otherwise. *)
|
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val length : 'a t -> int
|
2015-11-22 12:15:03 -08:00
|
|
|
(** Return the number of elements in a stack. Time complexity O(1) *)
|
2001-10-26 16:33:00 -07:00
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val iter : ('a -> unit) -> 'a t -> unit
|
2001-10-26 16:33:00 -07:00
|
|
|
(** [iter f s] applies [f] in turn to all elements of [s],
|
|
|
|
from the element at the top of the stack to the element at the
|
|
|
|
bottom of the stack. The stack itself is unchanged. *)
|
2015-06-09 05:52:40 -07:00
|
|
|
|
|
|
|
val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
|
2015-06-10 07:37:30 -07:00
|
|
|
(** [fold f accu s] is [(f (... (f (f accu x1) x2) ...) xn)]
|
|
|
|
where [x1] is the top of the stack, [x2] the second element,
|
|
|
|
and [xn] the bottom element. The stack is unchanged.
|
2015-06-09 05:52:40 -07:00
|
|
|
@since 4.03 *)
|