Skip to content
This repository
Newer
Older
100644 90 lines (76 sloc) 2.638 kb
fccc6851 » MLstate
2011-06-21 Initial open-source release
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 (**)
19
20 module type SafeOrderedType =
21 sig
22 include Set.OrderedType
23 val to_string : t -> string
24 end
25
26 module StringOrder : (SafeOrderedType with type t = string) =
27 struct type t = string let compare = String.compare let to_string x = x end
28 module StringMap = Map.Make (StringOrder)
29 type 'a stringmap = 'a StringMap.t
30
31 module IntOrder : (SafeOrderedType with type t = int) =
32 struct type t = int let compare = (-) let to_string x = string_of_int x end
33 module IntMap = Map.Make (IntOrder)
34 type 'a intmap = 'a IntMap.t
35
36 module type SafeSet =
37 sig
38 type elt
39 type t
40 val empty : unit -> t
41 val is_empty : t -> bool
42 val mem : elt -> t -> bool
43 val add : elt -> t -> t
44 val singleton : elt -> t
45 val remove : elt -> t -> t
46 val union : t -> t -> t
47 val inter : t -> t -> t
48 val diff : t -> t -> t
49 val compare : t -> t -> int
50 val equal : t -> t -> bool
51 val subset : t -> t -> bool
52 val iter : (elt -> unit) -> t -> unit
53 val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
54 val map : (elt -> elt) -> t -> t
55 val for_all : (elt -> bool) -> t -> bool
56 val exists : (elt -> bool) -> t -> bool
57 val filter : (elt -> bool) -> t -> t
58 val partition : (elt -> bool) -> t -> t * t
59 val cardinal : t -> int
60 val elements : t -> elt list
61 val min_elt : t -> elt
62 val max_elt : t -> elt
63 val choose : t -> elt
64 val split : elt -> t -> t * bool * t
65
66 val to_string : t -> string
67 end
68
69 module SafeSet(Ord : SafeOrderedType) : (SafeSet with type elt = Ord.t) =
70 struct
71 module Set = Set.Make(Ord)
72 include Set
73
74 let empty () = empty
75
76 let map f s =
77 fold (fun x acc -> add (f x) acc) s (empty())
78
79 let to_string set =
80 let separator = ", " in
81 let content = fold
82 (fun elt acc ->
83 Format.sprintf "%s%s%s" acc (Ord.to_string elt) separator) set "" in
84 let newsize = max 0 ((String.length content) - (String.length separator))
85 in
86 Format.sprintf "{%s}" (String.sub content 0 newsize)
87 end
88
89 module StringSet = SafeSet(StringOrder)
90 type stringset = StringSet.t
Something went wrong with that request. Please try again.