-
Notifications
You must be signed in to change notification settings - Fork 1
/
StdSet.fs
33 lines (21 loc) · 999 Bytes
/
StdSet.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/// Tree-based set data structure. See StdMap for details.
module rec Std.StdSet
open Std.StdMap
type TreeSet<'T> = TreeMap<'T, unit>
module TSet =
let empty (compare: 'T -> 'T -> int) : TreeSet<'T> = TMap.empty compare
let isEmpty (set: TreeSet<_>) : bool = TMap.isEmpty set
let contains (item: 'T) (set: TreeSet<'T>) : bool = TMap.containsKey item set
let add (item: 'T) (set: TreeSet<'T>) : TreeSet<'T> = TMap.add item () set
let remove (item: 'T) (set: TreeSet<'T>) : bool * TreeSet<'T> =
let unitOpt, set = TMap.remove item set
let isRemoved =
match unitOpt with
| Some () -> true
| None -> false
isRemoved, set
let fold (folder: 'S -> 'T -> 'S) (state: 'S) (set: TreeSet<'T>) : 'S =
TMap.fold (fun state item () -> folder state item) state set
let ofList (compare: 'T -> 'T -> int) (xs: 'T list) : TreeSet<'T> =
TMap.ofList compare (List.map (fun item -> item, ()) xs)
let toList (set: TreeSet<'T>) : 'T list = TMap.toKeys set