Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (92 sloc) 3.394 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 (* depends *)
19 let (|>) = InfixOperator.(|>)
20
21 (* -- *)
22
23 exception Found
24 type elt = int
25 type t = unit IntMap.t
26 let empty:t = IntMap.empty
27 let is_empty t = IntMap.is_empty t
28 let mem (int:int) (set:t) = (IntMap.find_opt int set ) <> None
29 let add int set = IntMap.add int () set
30 let add_list int_list set = List.fold_left (fun acc item -> add item acc) set int_list
31 let singleton int = IntMap.singleton int ()
32 let remove int set = IntMap.remove int set
33 let union (set:t) (set':t) = IntMap.merge (fun a _b -> a) set set'
34 let compare (set:t) set' = IntMap.compare (compare) set set'
35 let elements (set:t) = IntMap.keys set
36 let from_list (li: int list) = List.map (fun x -> x, ()) li |> IntMap.from_list
37 let iter (f:int -> unit) (set:t) = IntMap.iter (fun k _v -> f k) set
38 let fold f (set:t) acc = IntMap.fold (fun int () acc -> f int acc) set acc
39 let fold_rev f set acc = IntMap.fold_rev (fun int () acc -> f int acc) set acc
40 let equal set set' = IntMap.equal (=) set set'
41 let min_elt (set:t) = IntMap.min set |> fst
42 let max_elt (set:t) = IntMap.max set |> fst
43 let size (set:t) = IntMap.size set
44 let cardinal = size
45 let filter = IntMap.filter_keys
46
47 let exists f set =
48 try
49 iter (fun x -> if f x then raise Found) set;
50 false
51 with Found -> true
52
53 let map (f:int -> int) (set:t) = fold (fun x acc -> add (f x) acc) set empty
54
55 let for_all f (set:t) =
56 try
57 iter (fun x -> if f x then () else raise Found) set;
58 true
59 with Found -> false
60
61 let partition f set = fold
62 (fun k (ok, no) ->
63 if f k
64 then (add k ok), no
65 else ok, (add k no)
66 ) set (empty, empty)
67
68 let inter set set' = filter (fun x -> mem x set') set
69 let diff set set' = filter (fun x -> not (mem x set')) set
70 let subset (set:t) (set':t) = for_all (fun x -> mem x set') set
71
6f7b0d3 [enhance] Set: choose become quicker
Arthur Milchior authored
72 let choose t = fst (IntMap.choose t)
e74f6b5 [feature] libbase: choose_opt in Maps and Sets
Arthur Milchior authored
73
74 let choose_opt (set:t) =
75 match IntMap.choose_opt set with
76 | None -> None
77 | Some (a, _) -> Some a
78
645f07a [feature] libbase: Taking an element to show the difference between two...
Arthur Milchior authored
79 let example_diff s1 s2 =
80 let diff_ = diff s1 s2 in
81 match choose_opt diff_ with
82 | Some elt -> Some elt
83 | None ->
84 let diff = diff s2 s1 in
85 match choose_opt diff with
86 | Some elt -> Some elt
87 | None -> None
88
fccc685 Initial open-source release
MLstate authored
89 let split int set =
90 fold
91 (fun i (inf, b, sup) ->
92 if i < int then ((add i inf), b, sup)
93 else if i > int then (inf, b, (add i sup))
94 else (inf, true, sup)
95 ) set (empty, false, empty)
96
97 let draw set = let min = min_elt set in min, remove min set
98 let complete (_:(elt -> elt -> bool)) (_:elt) (_s:t) = let _ = assert false in empty
99
100 (* cf doc *)
101 let pp sep ppe fmt t =
102 let fiter elt =
103 ppe fmt elt ;
104 Format.fprintf fmt sep
105 in
106 iter fiter t
107
108 let compare_elt : int -> int -> int = Pervasives.compare
109
110 let safe_union = IntMap.safe_merge
111
112 let from_sorted_array _ = assert false
Something went wrong with that request. Please try again.