4 | Cons Integer (Set a) a (Set a)
7 cons :: Set a -> a -> Set a -> Set a
8 cons l x r = Cons (size l + size r + 1) l x r
11 rotateSingleL l x (Cons _ m y r) = cons (cons l x m) y r
14 rotateSingleR (Cons _ l x m) y r = cons l x (cons m y r)
17 rotateDoubleL l x (Cons _ (Cons _ m1 y m2) z r) = cons (cons l x m1) y (cons m2 z r)
20 rotateDoubleR (Cons _ l x (Cons _ m1 y m2)) z r = cons (cons l x m1) y (cons m2 z r)
23 balance :: Set a -> a -> Set a -> Set a
27 in if ln + rn < 2 then cons l x r
28 else if rn > 4*ln then
29 let (Cons _ rl _ rr) = r
30 in if size rl < 2 * size rr
31 then rotateSingleL l x r
32 else rotateDoubleL l x r
33 else if ln > 4*rn then
34 let (Cons _ ll _ lr) = l
35 in if size lr < 2 * size ll
36 then rotateSingleR l x r
37 else rotateDoubleR l x r
40 isEmpty :: Set a -> Boolean
44 size :: Set a -> Integer
46 size (Cons s _ _ _) = s
48 member :: Ord a => a -> Set a -> Boolean
49 member _ Empty = False
50 member x (Cons _ l y r) =
51 let cmp = compare x y in
61 singleton :: a -> Set a
62 singleton x = Cons 1 Empty x Empty
64 insert :: Ord a => Set a -> a -> Set a
65 insert Empty x = singleton x
66 insert c@(Cons i l y r) x =
67 let cmp = compare x y in
69 then balance (insert l x) y r
71 then balance l y (insert r x)
74 delete :: Ord a => Set a -> a -> Set a
75 delete Empty _ = Empty
76 delete (Cons _ l y r) x =
77 let cmp = compare x y in
79 then balance (delete l x) y r
81 then balance l y (delete r x)
86 aux l r = let (x, rr) = delmin r
89 delmin :: Ord a => Set a -> (a, Set a)
90 delmin (Cons _ Empty x r) = (x, r)
91 delmin (Cons _ l x r) = let (y, ll) = delmin l
92 in (y, balance ll x r)
93 delmin _ = fail "Empty set given to delmin."
95 union :: Ord a => Set a -> Set a -> Set a
98 union a (Cons _ l x r) =
101 in concat3 (union l' l) x (union r' r)
103 splitL :: Ord a => Set a -> a -> Set a
104 splitL Empty _ = Empty
105 splitL (Cons _ l x r) y =
106 let cmp = compare x y in
108 then concat3 l x (splitL r y)
113 splitR :: Ord a => Set a -> a -> Set a
114 splitR Empty _ = Empty
115 splitR (Cons _ l x r) y =
116 let cmp = compare x y in
120 then concat3 (splitR l y) x r
123 concat3 :: Ord a => Set a -> a -> Set a -> Set a
124 concat3 Empty x r = insert r x
125 concat3 l x Empty = insert l x
126 concat3 l@(Cons n1 l1 v1 r1) x r@(Cons n2 l2 v2 r2) =
128 then balance (concat3 l x l2) v2 r2
130 then balance l1 v1 (concat3 r1 x r)
133 fold :: (a -> b -> a) -> a -> Set b -> a
134 fold f init Empty = init
135 fold f init (Cons _ l x r) = fold f (f (fold f init l) x) r
137 instance (Show a) => Show (Set a) where
138 sb <+ t = p True True t
140 p lm rm Empty = sb <<
142 then (if rm then "empty" else "set [")
143 else (if rm then "]" else ", ")
144 p lm rm (Cons _ l x r) = do
149 set :: Ord a => [a] -> Set a
150 set = foldl insert empty