on master help sign in
Remove unused code in Dict library.
f196eacMax New
libraries/Dict.elm+28−162
@@ -7,10 +7,6 @@ module Dict (empty,singleton,insert
7 ,toList,fromList
7 ,toList,fromList
8 ) where
8 ) where
9
9
10−-- Cleanup: Change RBEmpty to 2 constructors: Leaf and BBLeaf
11−-- Consider making the types safer or moving all the remove methods to
12−-- a let
13−
14{-| A dictionary mapping unique keys to values. The keys can be any comparable
10{-| A dictionary mapping unique keys to values. The keys can be any comparable
15type. This includes `Int`, `Float`, `Time`, `Char`, `String`, and tuples or
11type. This includes `Int`, `Float`, `Time`, `Char`, `String`, and tuples or
16lists of comparable types.
12lists of comparable types.
@@ -55,84 +51,6 @@ data Dict k v = RBNode NColor k v (Dict k v) (Dict k v)
55empty : Dict comparable v
51empty : Dict comparable v
56empty = RBEmpty LBlack
52empty = RBEmpty LBlack
57
53
58−{-- Helpers for checking invariants
59−
60−-- Check that the tree has an equal number of black nodes on each path
61−equal_pathLen t =
62− let path_numBlacks t =
63− case t of
64− RBEmpty -> 1
65− RBNode col _ _ l r ->
66− let { bl = path_numBlacks l ; br = path_numBlacks r } in
67− if bl /= br || bl == 0-1 || br == 0-1
68− then 0-1
69− else bl + (if col == Red then 0 else 1)
70− in 0-1 /= path_numBlacks t
71−
72−rootBlack t =
73− case t of
74− RBEmpty -> True
75− RBNode Black _ _ _ _ -> True
76− _ -> False
77−
78−redBlack_children t =
79− case t of
80− { RBNode Red _ _ (RBNode Red _ _ _ _) _ -> False
81− ; RBNode Red _ _ _ (RBNode Red _ _ _ _) -> False
82− ; RBEmpty -> True
83− ; RBNode _ _ _ l r -> redBlack_children l && redBlack_children r
84− }
85−
86−findExtreme f t =
87− case t of
88− { RBEmpty -> Nothing
89− ; RBNode c k _ l r ->
90− case findExtreme f (f (l,r)) of
91− { Nothing -> Just k
92− ; Just k' -> Just k' }
93− }
94−
95−findminRbt t = findExtreme fst t
96−findmaxRbt t = findExtreme snd t
97−
98−-- "Option LT than"
99−-- Returns True if either xo or yo is Nothing
100−-- Otherwise returns the result of comparing the values using f
101−
102−optionRelation f u xo yo =
103− case (xo,yo) of
104− { (Nothing,_) -> u
105− ; (_,Nothing) -> u
106− ; (Just x, Just y) -> f x y }
107−
108−olt xo yo = optionRelation (< ) True xo yo
109−olte xo yo = optionRelation (<=) True xo yo
110−
111−ordered t =
112− case t of
113− { RBEmpty -> True
114− ; RBNode c k v l r ->
115− let (lmax,rmin) = (findmaxRbt l, findminRbt r) in
116− olte lmax (Just k) && olte (Just k) rmin && ordered l && ordered r
117− }
118−
119−-- Check that there aren't any right red nodes in the tree *)
120−leftLeaning t =
121− case t of
122− { RBEmpty -> True
123− ; RBNode _ _ _ (RBNode Black _ _ _ _) (RBNode Red _ _ _ _) -> False
124− ; RBNode _ _ _ RBEmpty (RBNode Red _ _ _ _) -> False
125− ; RBNode _ _ _ l r -> (leftLeaning l) && (leftLeaning r)
126− }
127−
128−invariants_hold t =
129− ordered t && rootBlack t && redBlack_children t &&
130− equal_pathLen t && leftLeaning t
131−
132−--** End invariant helpers *****
133−--}
134−
135−
136min : Dict k v -> (k,v)
54min : Dict k v -> (k,v)
137min t =
55min t =
138 case t of
56 case t of
@@ -170,19 +88,6 @@ findWithDefault base k t =
170 EQ -> v
88 EQ -> v
171 GT -> findWithDefault base k r
89 GT -> findWithDefault base k r
172
90
173−{--
174−-- Find the value associated with a key. If the key is not found, there will be a runtime error.
175−find k t =
176− case t of
177− { RBEmpty -> Native.Error.raise "Key was not found in dictionary!"
178− ; RBNode _ k' v l r ->
179− case Native.Utils.compare k k' of
180− { LT -> find k l
181− ; EQ -> v
182− ; GT -> find k r }
183− }
184−--}
185−
186{-| Determine if a key is in a dictionary. -}
91{-| Determine if a key is in a dictionary. -}
187member : comparable -> Dict comparable v -> Bool
92member : comparable -> Dict comparable v -> Bool
188-- Does t contain k?
93-- Does t contain k?
@@ -254,67 +159,14 @@ insert k v t = -- Invariant: t is a valid left-leaning rb tree
254 GT -> RBNode c k' v' l (ins r)
159 GT -> RBNode c k' v' l (ins r)
255 in fixUp h
160 in fixUp h
256 in ensureBlackRoot (ins t)
161 in ensureBlackRoot (ins t)
257−{--
258− if not (invariants_hold t) then
259− Native.Error.raise "invariants broken before insert"
260− else (let new_t = ensureBlackRoot (ins t) in
261− if not (invariants_hold new_t) then
262− Native.Error.raise "invariants broken after insert"
263− else new_t)
264−--}
265
162
266{-| Create a dictionary with one key-value pair. -}
163{-| Create a dictionary with one key-value pair. -}
267singleton : comparable -> v -> Dict comparable v
164singleton : comparable -> v -> Dict comparable v
268singleton k v = insert k v (RBEmpty LBlack)
165singleton k v = insert k v (RBEmpty LBlack)
269−
270−-- deleteMin : Dict k v -> Dict k v
271−-- deleteMin t =
272−-- let del t =
273−-- case t of
274−-- RBNode _ _ _ RBEmpty _ -> RBEmpty
275−-- _ -> case moveRedLeftIfNeeded t of
276−-- RBNode c k v l r -> fixUp (RBNode c k v (del l) r)
277−-- RBEmpty -> RBEmpty
278−-- in ensureBlackRoot (del t)
279−
280−{--
281−deleteMax t =
282− let del t =
283− let t' = if isRedLeft t then rotateRight t else t in
284− case t' of
285− { RBNode _ _ _ _ RBEmpty -> RBEmpty
286− ; _ -> let t'' = moveRedRightIfNeeded t' in
287− case t'' of
288− { RBNode c k v l r -> fixUp (RBNode c k v l (del r))
289− ; RBEmpty -> RBEmpty } }
290− in ensureBlackRoot (del t)
291−--}
292−
293−-- Finds and deletes k in t
294−del : comparable -> Dict comparable v -> Dict comparable v
295−del k t = case t of
296− RBEmpty _ -> t
297− RBNode c k' v l r -> case Native.Utils.compare k k' of
298− LT -> bubble c k' v (del k l) r
299− EQ -> rem t
300− GT -> bubble c k' v l (del k r)
301−
302−-- Remove the top node from the tree, may leave behind BBlacks
303−rem : Dict k v -> Dict k v
304−rem t = case t of
305− RBNode c k v (RBEmpty _) (RBEmpty _) -> case c of
306− Red -> RBEmpty LBlack
307− Black -> RBEmpty LBBlack
308− RBNode Black _ _ (RBEmpty _) (RBNode _ k v l r) ->
309− RBNode Black k v l r
310− RBNode Black _ _ (RBNode _ k v l r) (RBEmpty _) ->
311− RBNode Black k v l r
312− -- l and r are both RBNodes
313− RBNode c _ _ l r ->
314− let (k, v) = max l
315− l' = remove_max l
316− in bubble c k v l' r
317
166
167+{- Remove helpers: everything from here to remove should only be used
168+ internally by remove as they would otherwise break rb-invariants -}
169+
318isBBlack : Dict k v -> Bool
170isBBlack : Dict k v -> Bool
319isBBlack t = case t of
171isBBlack t = case t of
320 RBNode c _ _ _ _ -> case c of
172 RBNode c _ _ _ _ -> case c of
@@ -346,6 +198,31 @@ lessBlackTree t = case t of
346 RBNode c k v l r -> RBNode (lessBlack c) k v l r
198 RBNode c k v l r -> RBNode (lessBlack c) k v l r
347 RBEmpty _ -> RBEmpty LBlack
199 RBEmpty _ -> RBEmpty LBlack
348
200
201+-- Finds and deletes k in t
202+del : comparable -> Dict comparable v -> Dict comparable v
203+del k t = case t of
204+ RBEmpty _ -> t
205+ RBNode c k' v l r -> case Native.Utils.compare k k' of
206+ LT -> bubble c k' v (del k l) r
207+ EQ -> rem t
208+ GT -> bubble c k' v l (del k r)
209+
210+-- Remove the top node from the tree, may leave behind BBlacks
211+rem : Dict k v -> Dict k v
212+rem t = case t of
213+ RBNode c k v (RBEmpty _) (RBEmpty _) -> case c of
214+ Red -> RBEmpty LBlack
215+ Black -> RBEmpty LBBlack
216+ RBNode Black _ _ (RBEmpty _) (RBNode _ k v l r) ->
217+ RBNode Black k v l r
218+ RBNode Black _ _ (RBNode _ k v l r) (RBEmpty _) ->
219+ RBNode Black k v l r
220+ -- l and r are both RBNodes
221+ RBNode c _ _ l r ->
222+ let (k, v) = max l
223+ l' = remove_max l
224+ in bubble c k v l' r
225+
349-- Kills a BBlack or moves it upward, may leave behind NBlack
226-- Kills a BBlack or moves it upward, may leave behind NBlack
350bubble : NColor -> k -> v -> Dict k v -> Dict k v -> Dict k v
227bubble : NColor -> k -> v -> Dict k v -> Dict k v -> Dict k v
351bubble c k v l r = if isBBlack l || isBBlack r
228bubble c k v l r = if isBBlack l || isBBlack r
@@ -396,9 +273,6 @@ balance_node t =
396 _ -> t
273 _ -> t
397
274
398 else t
275 else t
399−
400−
401−
402
276
403-- make the top node black
277-- make the top node black
404blacken : Dict k v -> Dict k v
278blacken : Dict k v -> Dict k v
@@ -417,14 +291,6 @@ no changes are made. -}
417remove : comparable -> Dict comparable v -> Dict comparable v
291remove : comparable -> Dict comparable v -> Dict comparable v
418remove k t = blacken <| del k t
292remove k t = blacken <| del k t
419
293
420−{--
421− if not (invariants_hold t) then
422− Native.Error.raise "invariants broken before remove"
423− else (let t' = ensureBlackRoot (del t) in
424− if invariants_hold t' then t' else
425− Native.Error.raise "invariants broken after remove")
426−--}
427−
428{-| Apply a function to all values in a dictionary. -}
294{-| Apply a function to all values in a dictionary. -}
429map : (a -> b) -> Dict comparable a -> Dict comparable b
295map : (a -> b) -> Dict comparable a -> Dict comparable b
430map f t =
296map f t =