2016-09-08 21:13:10 +04:00
|
|
|
(* Lightweight thread library for OCaml
|
|
|
|
* http://www.ocsigen.org/lwt
|
|
|
|
* Interface Lwt_sequence
|
|
|
|
* Copyright (C) 2009 Jérémie Dimino
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation, with linking exceptions;
|
|
|
|
* either version 2.1 of the License, or (at your option) any later
|
|
|
|
* version. See COPYING file for details.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
2017-11-13 19:34:00 +04:00
|
|
|
*)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(** Mutable sequence of elements *)
|
|
|
|
|
|
|
|
(** A sequence is an object holding a list of elements which support
|
|
|
|
the following operations:
|
|
|
|
|
|
|
|
- adding an element to the left or the right in time and space O(1)
|
|
|
|
- taking an element from the left or the right in time and space O(1)
|
|
|
|
- removing a previously added element from a sequence in time and space O(1)
|
|
|
|
- removing an element while the sequence is being transversed.
|
|
|
|
*)
|
|
|
|
|
|
|
|
type 'a t
|
2017-11-13 19:34:00 +04:00
|
|
|
(** Type of a sequence holding values of type ['a] *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
type 'a node
|
2017-11-13 19:34:00 +04:00
|
|
|
(** Type of a node holding one value of type ['a] in a sequence *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(** {2 Operation on nodes} *)
|
|
|
|
|
|
|
|
val get : 'a node -> 'a
|
2017-11-13 19:34:00 +04:00
|
|
|
(** Returns the contents of a node *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val set : 'a node -> 'a -> unit
|
2017-11-13 19:34:00 +04:00
|
|
|
(** Change the contents of a node *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val remove : 'a node -> unit
|
2017-11-13 19:34:00 +04:00
|
|
|
(** Removes a node from the sequence it is part of. It does nothing
|
|
|
|
if the node has already been removed. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(** {2 Operations on sequence} *)
|
|
|
|
|
|
|
|
val create : unit -> 'a t
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [create ()] creates a new empty sequence *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val is_empty : 'a t -> bool
|
2017-11-13 19:34:00 +04:00
|
|
|
(** Returns [true] iff the given sequence is empty *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val length : 'a t -> int
|
2017-11-13 19:34:00 +04:00
|
|
|
(** Returns the number of elemenets in the given sequence. This is a
|
|
|
|
O(n) operation where [n] is the number of elements in the
|
|
|
|
sequence. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val add_l : 'a -> 'a t -> 'a node
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [add_l x s] adds [x] to the left of the sequence [s] *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val add_r : 'a -> 'a t -> 'a node
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [add_l x s] adds [x] to the right of the sequence [s] *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
exception Empty
|
2017-11-13 19:34:00 +04:00
|
|
|
(** Exception raised by [take_l] and [tale_s] and when the sequence
|
|
|
|
is empty *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val take_l : 'a t -> 'a
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [take_l x s] remove and returns the leftmost element of [s]
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-11-13 19:34:00 +04:00
|
|
|
@raise Empty if the sequence is empty *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val take_r : 'a t -> 'a
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [take_l x s] remove and returns the rightmost element of [s]
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-11-13 19:34:00 +04:00
|
|
|
@raise Empty if the sequence is empty *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val take_opt_l : 'a t -> 'a option
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [take_opt_l x s] remove and returns [Some x] where [x] is the
|
|
|
|
leftmost element of [s] or [None] if [s] is empty *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val take_opt_r : 'a t -> 'a option
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [take_opt_l x s] remove and returns [Some x] where [x] is the
|
|
|
|
rightmost element of [s] or [None] if [s] is empty *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val transfer_l : 'a t -> 'a t -> unit
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [transfer_l s1 s2] removes all elements of [s1] and add them at
|
|
|
|
the left of [s2]. This operation runs in constant time and
|
|
|
|
space. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val transfer_r : 'a t -> 'a t -> unit
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [transfer_r s1 s2] removes all elements of [s1] and add them at
|
|
|
|
the right of [s2]. This operation runs in constant time and
|
|
|
|
space. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
(** {2 Sequence iterators} *)
|
|
|
|
|
|
|
|
(** Note: it is OK to remove a node while traversing a sequence *)
|
|
|
|
|
|
|
|
val iter_l : ('a -> unit) -> 'a t -> unit
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [iter_l f s] applies [f] on all elements of [s] starting from
|
|
|
|
the left *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val iter_r : ('a -> unit) -> 'a t -> unit
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [iter_l f s] applies [f] on all elements of [s] starting from
|
|
|
|
the right *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val iter_node_l : ('a node -> unit) -> 'a t -> unit
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [iter_l f s] applies [f] on all nodes of [s] starting from
|
|
|
|
the left *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val iter_node_r : ('a node -> unit) -> 'a t -> unit
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [iter_l f s] applies [f] on all nodes of [s] starting from
|
|
|
|
the right *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val fold_l : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [fold_l f s] is:
|
|
|
|
{[
|
|
|
|
fold_l f s x = f en (... (f e2 (f e1 x)))
|
|
|
|
]}
|
|
|
|
where [e1], [e2], ..., [en] are the elements of [s]
|
|
|
|
*)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val fold_r : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [fold_r f s] is:
|
|
|
|
{[
|
|
|
|
fold_r f s x = f e1 (f e2 (... (f en x)))
|
|
|
|
]}
|
|
|
|
where [e1], [e2], ..., [en] are the elements of [s]
|
|
|
|
*)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val find_node_opt_l : ('a -> bool) -> 'a t -> 'a node option
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [find_node_opt_l f s] returns [Some x], where [x] is the first node of
|
|
|
|
[s] starting from the left that satisfies [f] or [None] if none
|
|
|
|
exists. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val find_node_opt_r : ('a -> bool) -> 'a t -> 'a node option
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [find_node_opt_r f s] returns [Some x], where [x] is the first node of
|
|
|
|
[s] starting from the right that satisfies [f] or [None] if none
|
|
|
|
exists. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val find_node_l : ('a -> bool) -> 'a t -> 'a node
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [find_node_l f s] returns the first node of [s] starting from the left
|
|
|
|
that satisfies [f] or raises [Not_found] if none exists. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
val find_node_r : ('a -> bool) -> 'a t -> 'a node
|
2017-11-13 19:34:00 +04:00
|
|
|
(** [find_node_r f s] returns the first node of [s] starting from the right
|
|
|
|
that satisfies [f] or raises [Not_found] if none exists. *)
|