Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 236 lines (198 sloc) 7.889 kb
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 module type S =
19 sig
20 type key
21 type +'a t
22 val empty : 'a t
23 val is_empty : 'a t -> bool
24 val singleton : key -> 'a -> 'a t
25
26 (**
27 Decons un non empty map
28 *)
29 val decons : 'a t -> 'a t * key * 'a * 'a t
30
31 (**
32 Overwrites the old value at that key.
33 *)
34 val add : key -> 'a -> 'a t -> 'a t
35
36 (**
37 Fails raising [Invalid_argument "Base.Map.safe_add"]
38 @raise Invalid_argument if the key is already present
39 *)
40 val safe_add : key -> 'a -> 'a t -> 'a t
41
42 (**
43 [replace key maping t]
44 Call the maping function from the previous value contained at that key,
45 and add the resulting value into the map.
46 *)
47 val replace : key -> ('a option -> 'a) -> 'a t -> 'a t
48
49 (**
50 @raise Not_found if the key is not present
51 *)
52 val find : key -> 'a t -> 'a
53
54 val findi : key -> 'a t -> key * 'a
55
56 (**
57 No exception if the key is not present.
58 *)
59 val remove : key -> 'a t -> 'a t
60
61 (**
62 @raise Not_found if the binding to update is not present
63 *)
5b18026 [enhance] Stdlib: Comments in Standard Library
Arthur Milchior authored
64 val update : key -> ('a -> 'a) -> 'a t -> 'a t
fccc685 Initial open-source release
MLstate authored
65
66 (**
67 [update_default key updater default map]
68 Looks for the value of [key] in [map]
69 - if it is present then, replace it with [updater value]
70 - or else add the binding [key] -> [default] to the map
71 *)
5b18026 [enhance] Stdlib: Comments in Standard Library
Arthur Milchior authored
72 val update_default : key -> ('a -> 'a) -> 'a -> 'a t -> 'a t
fccc685 Initial open-source release
MLstate authored
73
74 val mem : key -> 'a t -> bool
75 val iter : (key -> 'a -> unit) -> 'a t -> unit
76 val rev_iter : (key -> 'a -> unit) -> 'a t -> unit
708e865 [feature] Map: Adding the function choose
Arthur Milchior authored
77
78 (**
79 Returns an arbitrary element of the map.
80 @raise Not_found on the empty list
81 *)
82 val choose : 'a t -> (key * 'a)
e74f6b5 [feature] libbase: choose_opt in Maps and Sets
Arthur Milchior authored
83
84 (**
85 Returns an arbitrary element of the set if it is not_empty, or null
86 *)
87 val choose_opt : 'a t -> (key * 'a) option
fccc685 Initial open-source release
MLstate authored
88 val map : ('a -> 'b) -> 'a t -> 'b t
89 val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
90 val fold_map : (key -> 'a -> 'c -> 'c * 'b) -> 'a t -> 'c -> 'c * 'b t
91 val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
2258415 [feature] Map: adding fold_map_2
Arthur Milchior authored
92 (** Takes two sets with the same key and apply the function to the key
93 and the two values associated.
94 The key are seen in increasing order
95 @raise Invalid_argument if the 2 map do not have the same keys
96 *)
97 val fold_map2: (key -> 'a -> 'b -> 'acc -> ('acc * 'result)) -> 'a t -> 'b t -> 'acc -> ('acc * 'result t)
fccc685 Initial open-source release
MLstate authored
98
99 (** [fold_range_compare f map kmin kmax acc] Like fold range but
100 [kmin] and [kmax] are not key type and the range selection is
101 computed with given [compare] function.
102
103 Note : in major cases use [fold_range] ([fold_range_compare
104 Ord.compare]).
105
106 Beware : forall key. [compare kmin key <= compare kmax
107 key]. *)
108 val fold_range_compare : ('c -> key -> int) ->
109 (key -> 'a -> 'b -> 'b) -> 'a t -> 'c -> 'c -> 'b -> 'b
110
111 (** [fold_range f map kmin kmax acc] Fold with [f] on [map]
112 beetween [kmin] and [kmax] includes. [acc |> f kmin vmin |>
113 ... |> f kmax vmax]. *)
114 val fold_range: (key -> 'a -> 'b -> 'b) -> 'a t -> key -> key -> 'b -> 'b
115 val fold_length :
116 start:key -> length:int
117 -> (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
118 (** If [length] is negative, [start] is the end.
119 If [length] is zero the fold is void (accumulator returned).
120 Time cost is O(log n + length) and memory cost O(log n),
121 both for balanced trees. Constants are the same as in
122 DFS restricted to the parts of the tree with the sought keys,
123 plus all their ancestors, plus a thin margin. *)
124
125 val fold_rev : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
126 val filter_keys : (key -> bool) -> 'a t -> 'a t
127 val filter_val : ('a -> bool) ->'a t -> 'a t
128 val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
5b18026 [enhance] Stdlib: Comments in Standard Library
Arthur Milchior authored
129
130 (**
131 Check that both map contains the same keys,
132 and that the binded value is the same,
133 using the equality function for values
134 *)
fccc685 Initial open-source release
MLstate authored
135 val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
136 module Iter :
137 (IterSig.S with type +'a element = key * 'a and type +'a structure = 'a t)
138 module RevIter :
139 (IterSig.S with type +'a element = key * 'a and type +'a structure = 'a t)
140 val nearest : key ->' a t -> key * 'a
141
142 (**
143 Find the naearest smaller entry in a map.
5b18026 [enhance] Stdlib: Comments in Standard Library
Arthur Milchior authored
144 @raise Not_found if there is no smaller key in the map
145 *)
fccc685 Initial open-source release
MLstate authored
146 val find_inf : key -> 'a t -> key * 'a
147
148 (**
149 Find the naearest greater entry in a map.
5b18026 [enhance] Stdlib: Comments in Standard Library
Arthur Milchior authored
150 @raise Not_found if there is no smaller key in the map
151 *)
fccc685 Initial open-source release
MLstate authored
152 val find_sup : key -> 'a t -> key * 'a
153
154 val from_list : (key * 'a) list -> 'a t
155
156 (**
157 Return an assoc list consitued of all bindings contained
158 in the map.
159 <!> The order of elements in the returned list is unspecified.
160 *)
161 val to_list : 'a t -> (key * 'a) list
162
163 (**
164 Same than [to_list] but with the guaranty that the assoc list is
165 sorted by keys, using the order used in the map.
166 The returned list is in increasing order of keys.
167 *)
168 val ordered_list : 'a t -> (key * 'a) list
169
170 (**
171 Same than [List.rev (ordered_list map)] but more efficient.
172 The returned list is in decreasing order of keys.
173 *)
174 val rev_ordered_list : 'a t -> (key * 'a) list
175
176 val keys : 'a t -> key list
177 val elts : 'a t -> 'a list
178 val random : 'a t -> key * 'a
179 val size : 'a t -> int
180 val height : 'a t -> int
181 val merge_i : (key -> 'a -> 'a -> 'a) -> 'a t -> 'a t -> 'a t
182 val merge : ('a -> 'a -> 'a) -> 'a t -> 'a t -> 'a t
183
184 (**
185 Fails raising [Invalid_argument "Base.Map.safe_add"].
186 @raise Invalid_argument is case of conflict
187 *)
188 val safe_merge : 'a t -> 'a t -> 'a t
189 val findi_opt : key -> 'a t -> (key * 'a) option
190 val find_opt : key -> 'a t -> 'a option
191 val min : 'a t -> key * 'a
192 val max : 'a t -> key * 'a
193 val rename : (key -> key) -> 'a t -> 'a t
194
195 (** Binding with Format for printing.
196 The first argument is a separator which will be
197 printed after each elt of the set.
198 If you does not want to see the separator after the last element, use an intermediate
199 list traduction. *)
200 val pp : (unit, Format.formatter, unit) format -> (Format.formatter -> key -> 'a -> unit) -> Format.formatter -> 'a t -> unit
201
202 (** exporting compare on key from Arg.compare *)
203 val compare_key : key -> key -> int
204
205 (** [diff m1 m2] returns the submap of [m1] containing all the keys that
206 are in [m1] but not in [m2]
207 ie it computes [m1 \ m2]
208 *)
209 val diff : 'a t -> _ t -> 'a t
210
211 (**
212 [diff2 m1 m2 m3] computes [m1 \ (m2 \ m3)], ie the submap of [m1]
213 containing all the keys that are in [m3] or not in [m2]
214 *)
215 val diff2 : 'a t -> _ t -> _ t -> 'a t
216
217 (**
645f07a [feature] libbase: Taking an element to show the difference between two...
Arthur Milchior authored
218 If the two set have different key, give back an element in the left set not
219 in the right one, or in the right one not in the left one, or None.
220 See also [BaseSetSig.S.example_diff]
221 *)
222 val example_diff : 'a t -> 'a t -> (key * 'a) option
223
224 (**
fccc685 Initial open-source release
MLstate authored
225 Optimized initialization for huge maps, to avoid temporary unused balancing.
226 The key array should be sorted in increasing order, using the same order than the set.
227 The value array should have the same length than the key array.
228 {[
229 [| key0 ; key1 ; key2 |] [| val0 ; val1 ; val2 |]
230 ]}
231
232 <!> Unoptimized on IntMap
233 *)
234 val from_sorted_array : key array -> 'a array -> 'a t
235 end
Something went wrong with that request. Please try again.