Commit | Line | Data |
---|---|---|
feec80c3 C |
1 | (* |
2 | * RefList - List reference | |
3 | * Copyright (C) 2003 Nicolas Cannasse | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version, | |
9 | * with the special exception on linking described in file LICENSE. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | *) | |
20 | ||
21 | (** Reference on lists. | |
22 | ||
23 | RefList is a extended set of functions that manipulate list | |
24 | references. | |
25 | *) | |
26 | ||
27 | exception Empty_list | |
28 | exception Invalid_index of int | |
29 | ||
30 | type 'a t | |
31 | ||
32 | val empty : unit -> 'a t | |
33 | (** Returns a new empty ref list *) | |
34 | ||
35 | val is_empty : 'a t -> bool | |
36 | (** Return [true] if a ref list is empty *) | |
37 | ||
38 | val clear : 'a t -> unit | |
39 | (** Removes all elements *) | |
40 | ||
41 | val length : 'a t -> int | |
42 | (** Returns the number of elements - O(n) *) | |
43 | ||
44 | val copy : dst:'a t -> src:'a t -> unit | |
45 | (** Makes a copy of a ref list - O(1) *) | |
46 | ||
47 | val copy_list : dst:'a t -> src:'a list -> unit | |
48 | (** Makes a copy of a list - O(1) *) | |
49 | ||
50 | val copy_enum : dst:'a t -> src:'a Enum.t -> unit | |
51 | (** Makes a copy of a enum *) | |
52 | ||
53 | val of_list : 'a list -> 'a t | |
54 | (** Creates a ref list from a list - O(1) *) | |
55 | ||
56 | val to_list : 'a t -> 'a list | |
57 | (** Returns the current elements as a list - O(1) *) | |
58 | ||
59 | val of_enum : 'a Enum.t -> 'a t | |
60 | (** Creates a ref list from an enumeration *) | |
61 | ||
62 | val enum : 'a t -> 'a Enum.t | |
63 | (** Returns an enumeration of current elements in the ref list *) | |
64 | ||
65 | val add : 'a t -> 'a -> unit | |
66 | (** Adds an element at the end - O(n) *) | |
67 | ||
68 | val push : 'a t -> 'a -> unit | |
69 | (** Adds an element at the head - O(1) *) | |
70 | ||
71 | val add_sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a -> unit | |
72 | (** Adds an element in a sorted list, using optional comparator | |
73 | or 'compare' as default. *) | |
74 | ||
75 | val first : 'a t -> 'a | |
76 | (** Returns the first element or | |
77 | raises [Empty_list] if the ref list is empty *) | |
78 | ||
79 | val last : 'a t -> 'a | |
80 | (** Returns the last element - O(n) or | |
81 | raises Empty_list if the ref list is empty *) | |
82 | ||
83 | val pop : 'a t -> 'a | |
84 | (** Removes and returns the first element or | |
85 | raises [Empty_list] if the ref list is empty *) | |
86 | ||
87 | val npop : 'a t -> int -> 'a list | |
88 | (** Removes and returns the n first elements or | |
89 | raises [Empty_list] if the ref list does not | |
90 | contain enough elements *) | |
91 | ||
92 | val hd : 'a t -> 'a | |
93 | (** same as [first] *) | |
94 | ||
95 | val tl : 'a t -> 'a t | |
96 | (** Returns a ref list containing the same elements | |
97 | but without the first one or | |
98 | raises [Empty_list] if the ref list is empty *) | |
99 | ||
100 | val rev : 'a t -> unit | |
101 | (** Reverses the ref list - O(n) *) | |
102 | ||
103 | (** {6 Functional Operations} *) | |
104 | ||
105 | val iter : ('a -> unit) -> 'a t -> unit | |
106 | (** Apply the given function to all elements of the | |
107 | ref list, in respect with the order of the list *) | |
108 | ||
109 | val find : ('a -> bool) -> 'a t -> 'a | |
110 | (** Find the first element matching | |
111 | the specified predicate | |
112 | raise [Not_found] if no element is found *) | |
113 | ||
114 | val rfind : ('a -> bool) -> 'a t -> 'a | |
115 | (** Find the first element in the reversed ref list matching | |
116 | the specified predicate | |
117 | raise [Not_found] if no element is found *) | |
118 | ||
119 | val find_exc : ('a -> bool) -> exn -> 'a t -> 'a | |
120 | (** Same as find but takes an exception to be raised when | |
121 | no element is found as additional parameter *) | |
122 | ||
123 | val exists : ('a -> bool) -> 'a t -> bool | |
124 | (** Return [true] if an element matches the specified | |
125 | predicate *) | |
126 | ||
127 | val for_all : ('a -> bool) -> 'a t -> bool | |
128 | (** Return [true] if all elements match the specified | |
129 | predicate *) | |
130 | ||
131 | val map : ('a -> 'b) -> 'a t -> 'b t | |
132 | (** Apply a function to all elements | |
133 | and return the ref list constructed with | |
134 | the function returned values *) | |
135 | ||
136 | val transform : ('a -> 'a) -> 'a t -> unit | |
137 | (** transform all elements in the ref list | |
138 | using a function. *) | |
139 | ||
140 | val map_list : ('a -> 'b) -> 'a t -> 'b list | |
141 | (** Apply a function to all elements | |
142 | and return the list constructed with | |
143 | the function returned values *) | |
144 | ||
145 | val sort : ?cmp:('a -> 'a -> int) -> 'a t -> unit | |
146 | (** Sort elements using the specified comparator | |
147 | or compare as default comparator *) | |
148 | ||
149 | val filter : ('a -> bool) -> 'a t -> unit | |
150 | (** Remove all elements that do not match the | |
151 | specified predicate *) | |
152 | ||
153 | val remove : 'a t -> 'a -> unit | |
154 | (** Remove an element from the ref list | |
155 | raise [Not_found] if the element is not found *) | |
156 | ||
157 | val remove_if : ('a -> bool) -> 'a t -> unit | |
158 | (** Remove the first element matching the | |
159 | specified predicate | |
160 | raise [Not_found] if no element has been removed *) | |
161 | ||
162 | val remove_all : 'a t -> 'a -> unit | |
163 | (** Remove all elements equal to the specified | |
164 | element from the ref list *) | |
165 | ||
166 | ||
167 | ||
168 | (** Functions that operate on the [i]th element of a list. | |
169 | ||
170 | While it is sometimes necessary to perform these | |
171 | operations on lists (hence their inclusion here), the | |
172 | functions were moved to an inner module to prevent | |
173 | their overuse: all functions work in O(n) time. You | |
174 | might prefer to use [Array] or [DynArray] for constant | |
175 | time indexed element access. | |
176 | *) | |
177 | module Index : sig | |
178 | ||
179 | val index_of : 'a t -> 'a -> int | |
180 | (** Return the index (position : 0 starting) of an element in | |
181 | a ref list, using ( = ) for testing element equality | |
182 | raise [Not_found] if no element was found *) | |
183 | ||
184 | val index : ('a -> bool) -> 'a t -> int | |
185 | (** Return the index (position : 0 starting) of an element in | |
186 | a ref list, using the specified comparator | |
187 | raise [Not_found] if no element was found *) | |
188 | ||
189 | val at_index : 'a t -> int -> 'a | |
190 | (** Return the element of ref list at the specified index | |
191 | raise [Invalid_index] if the index is outside [0 ; length-1] *) | |
192 | ||
193 | val set : 'a t -> int -> 'a -> unit | |
194 | (** Change the element at the specified index | |
195 | raise [Invalid_index] if the index is outside [0 ; length-1] *) | |
196 | ||
197 | val remove_at : 'a t -> int -> unit | |
198 | (** Remove the element at the specified index | |
199 | raise [Invalid_index] if the index is outside [0 ; length-1] *) | |
200 | ||
201 | end |