2012-10-05 02:01:20 +00:00
|
|
|
|
2012-10-18 09:19:50 +00:00
|
|
|
module Dict (empty,singleton,insert
|
2012-10-21 11:52:46 +00:00
|
|
|
,lookup,findWithDefault
|
2012-10-18 09:19:50 +00:00
|
|
|
,remove,member
|
2012-10-19 07:13:28 +00:00
|
|
|
,foldl,foldr,map
|
2012-10-18 09:19:50 +00:00
|
|
|
,union,intersect,diff
|
|
|
|
,keys,values
|
|
|
|
,toList,fromList
|
|
|
|
) where
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-09-10 06:07:49 +00:00
|
|
|
{-| A dictionary mapping unique keys to values. The keys can be any comparable
|
2013-09-10 02:12:27 +00:00
|
|
|
type. This includes `Int`, `Float`, `Time`, `Char`, `String`, and tuples or
|
|
|
|
lists of comparable types.
|
|
|
|
|
|
|
|
Insert, remove, and query operations all take *O(log n)* time.
|
|
|
|
|
|
|
|
# Build
|
|
|
|
@docs empty, singleton, insert, remove
|
|
|
|
|
|
|
|
# Query
|
|
|
|
@docs member, lookup, findWithDefault
|
|
|
|
|
|
|
|
# Combine
|
|
|
|
@docs union, intersect, diff
|
|
|
|
|
|
|
|
# Lists
|
|
|
|
@docs keys, values, toList, fromList
|
|
|
|
|
|
|
|
# Transform
|
|
|
|
@docs map, foldl, foldr
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
2013-07-26 14:38:11 +00:00
|
|
|
import open Basics
|
|
|
|
import open Maybe
|
|
|
|
import Native.Error
|
2013-03-17 05:24:18 +00:00
|
|
|
import List as List
|
2013-07-29 21:23:04 +00:00
|
|
|
import Native.Utils
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2012-10-10 21:37:42 +00:00
|
|
|
data NColor = Red | Black
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-04-10 05:15:06 +00:00
|
|
|
data Dict k v = RBNode NColor k v (Dict k v) (Dict k v) | RBEmpty
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Create an empty dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
empty : Dict comparable v
|
2013-04-10 05:15:06 +00:00
|
|
|
empty = RBEmpty
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2012-10-19 07:13:28 +00:00
|
|
|
{-- Helpers for checking invariants
|
2012-10-10 21:37:42 +00:00
|
|
|
|
|
|
|
-- Check that the tree has an equal number of black nodes on each path
|
2013-05-11 20:42:45 +00:00
|
|
|
equal_pathLen t =
|
2012-10-10 21:37:42 +00:00
|
|
|
let path_numBlacks t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBEmpty -> 1
|
|
|
|
RBNode col _ _ l r ->
|
2012-10-10 21:37:42 +00:00
|
|
|
let { bl = path_numBlacks l ; br = path_numBlacks r } in
|
|
|
|
if bl /= br || bl == 0-1 || br == 0-1
|
|
|
|
then 0-1
|
|
|
|
else bl + (if col == Red then 0 else 1)
|
|
|
|
in 0-1 /= path_numBlacks t
|
|
|
|
|
2013-05-11 20:42:45 +00:00
|
|
|
rootBlack t =
|
2012-10-10 21:37:42 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBEmpty -> True
|
|
|
|
RBNode Black _ _ _ _ -> True
|
|
|
|
_ -> False
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-05-11 20:42:45 +00:00
|
|
|
redBlack_children t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
{ RBNode Red _ _ (RBNode Red _ _ _ _) _ -> False
|
|
|
|
; RBNode Red _ _ _ (RBNode Red _ _ _ _) -> False
|
|
|
|
; RBEmpty -> True
|
|
|
|
; RBNode _ _ _ l r -> redBlack_children l && redBlack_children r
|
2012-10-10 21:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
findExtreme f t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
{ RBEmpty -> Nothing
|
|
|
|
; RBNode c k _ l r ->
|
2012-10-10 21:37:42 +00:00
|
|
|
case findExtreme f (f (l,r)) of
|
|
|
|
{ Nothing -> Just k
|
|
|
|
; Just k' -> Just k' }
|
|
|
|
}
|
2013-05-11 20:42:45 +00:00
|
|
|
|
2012-10-10 21:37:42 +00:00
|
|
|
findminRbt t = findExtreme fst t
|
|
|
|
findmaxRbt t = findExtreme snd t
|
|
|
|
|
|
|
|
-- "Option LT than"
|
|
|
|
-- Returns True if either xo or yo is Nothing
|
|
|
|
-- Otherwise returns the result of comparing the values using f
|
|
|
|
|
|
|
|
optionRelation f u xo yo =
|
|
|
|
case (xo,yo) of
|
|
|
|
{ (Nothing,_) -> u
|
|
|
|
; (_,Nothing) -> u
|
|
|
|
; (Just x, Just y) -> f x y }
|
|
|
|
|
|
|
|
olt xo yo = optionRelation (< ) True xo yo
|
|
|
|
olte xo yo = optionRelation (<=) True xo yo
|
|
|
|
|
|
|
|
ordered t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
{ RBEmpty -> True
|
|
|
|
; RBNode c k v l r ->
|
2012-10-10 21:37:42 +00:00
|
|
|
let (lmax,rmin) = (findmaxRbt l, findminRbt r) in
|
|
|
|
olte lmax (Just k) && olte (Just k) rmin && ordered l && ordered r
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Check that there aren't any right red nodes in the tree *)
|
2013-05-11 20:42:45 +00:00
|
|
|
leftLeaning t =
|
2012-10-10 21:37:42 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
{ RBEmpty -> True
|
|
|
|
; RBNode _ _ _ (RBNode Black _ _ _ _) (RBNode Red _ _ _ _) -> False
|
|
|
|
; RBNode _ _ _ RBEmpty (RBNode Red _ _ _ _) -> False
|
|
|
|
; RBNode _ _ _ l r -> (leftLeaning l) && (leftLeaning r)
|
2012-10-10 21:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
invariants_hold t =
|
2013-05-11 20:42:45 +00:00
|
|
|
ordered t && rootBlack t && redBlack_children t &&
|
2012-10-10 21:37:42 +00:00
|
|
|
equal_pathLen t && leftLeaning t
|
|
|
|
|
|
|
|
--** End invariant helpers *****
|
2012-10-19 07:13:28 +00:00
|
|
|
--}
|
2012-10-10 21:37:42 +00:00
|
|
|
|
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
min : Dict k v -> (k,v)
|
2012-10-10 21:37:42 +00:00
|
|
|
min t =
|
2013-05-11 20:42:45 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ k v RBEmpty _ -> (k,v)
|
|
|
|
RBNode _ _ _ l _ -> min l
|
2013-07-26 14:38:11 +00:00
|
|
|
RBEmpty -> Native.Error.raise "(min Empty) is not defined"
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2012-10-19 07:13:28 +00:00
|
|
|
{--
|
2012-10-10 21:37:42 +00:00
|
|
|
max t =
|
2013-05-11 20:42:45 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
{ RBNode _ k v _ RBEmpty -> (k,v)
|
|
|
|
; RBNode _ _ _ _ r -> max r
|
2013-07-26 14:38:11 +00:00
|
|
|
; RBEmpty -> Native.Error.raise "(max Empty) is not defined"
|
2012-10-10 21:37:42 +00:00
|
|
|
}
|
2012-10-19 07:13:28 +00:00
|
|
|
--}
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Lookup the value associated with a key. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
lookup : comparable -> Dict comparable v -> Maybe v
|
2012-10-10 21:37:42 +00:00
|
|
|
lookup k t =
|
|
|
|
case t of
|
2013-07-26 14:38:11 +00:00
|
|
|
RBEmpty -> Nothing
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ k' v l r ->
|
2013-07-29 21:23:04 +00:00
|
|
|
case Native.Utils.compare k k' of
|
2012-12-02 05:18:25 +00:00
|
|
|
LT -> lookup k l
|
2013-07-26 14:38:11 +00:00
|
|
|
EQ -> Just v
|
2012-12-02 05:18:25 +00:00
|
|
|
GT -> lookup k r
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Find the value associated with a key. If the key is not found,
|
|
|
|
return the default value. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
findWithDefault : v -> comparable -> Dict comparable v -> v
|
2012-10-18 09:19:50 +00:00
|
|
|
findWithDefault base k t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBEmpty -> base
|
|
|
|
RBNode _ k' v l r ->
|
2013-07-29 21:23:04 +00:00
|
|
|
case Native.Utils.compare k k' of
|
2012-12-02 05:18:25 +00:00
|
|
|
LT -> findWithDefault base k l
|
|
|
|
EQ -> v
|
|
|
|
GT -> findWithDefault base k r
|
2012-10-18 09:19:50 +00:00
|
|
|
|
2012-10-21 11:52:46 +00:00
|
|
|
{--
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Find the value associated with a key. If the key is not found, there will be a runtime error.
|
2012-10-18 09:19:50 +00:00
|
|
|
find k t =
|
|
|
|
case t of
|
2013-07-26 14:38:11 +00:00
|
|
|
{ RBEmpty -> Native.Error.raise "Key was not found in dictionary!"
|
2013-04-10 05:15:06 +00:00
|
|
|
; RBNode _ k' v l r ->
|
2013-07-29 21:23:04 +00:00
|
|
|
case Native.Utils.compare k k' of
|
2012-10-18 09:19:50 +00:00
|
|
|
{ LT -> find k l
|
|
|
|
; EQ -> v
|
|
|
|
; GT -> find k r }
|
|
|
|
}
|
2012-10-21 11:52:46 +00:00
|
|
|
--}
|
2012-10-18 09:19:50 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Determine if a key is in a dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
member : comparable -> Dict comparable v -> Bool
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Does t contain k?
|
2013-07-26 14:38:11 +00:00
|
|
|
member k t = isJust <| lookup k t
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
rotateLeft : Dict k v -> Dict k v
|
2012-10-10 21:37:42 +00:00
|
|
|
rotateLeft t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode cy ky vy a (RBNode cz kz vz b c) -> RBNode cy kz vz (RBNode Red ky vy a b) c
|
2013-07-26 14:38:11 +00:00
|
|
|
_ -> Native.Error.raise "rotateLeft of a node without enough children"
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-05-11 20:42:45 +00:00
|
|
|
-- rotateRight -- the reverse, and
|
2012-10-10 21:37:42 +00:00
|
|
|
-- makes Y have Z's color, and makes Z Red.
|
2013-03-17 05:24:18 +00:00
|
|
|
rotateRight : Dict k v -> Dict k v
|
2012-10-10 21:37:42 +00:00
|
|
|
rotateRight t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode cz kz vz (RBNode cy ky vy a b) c -> RBNode cz ky vy a (RBNode Red kz vz b c)
|
2013-07-26 14:38:11 +00:00
|
|
|
_ -> Native.Error.raise "rotateRight of a node without enough children"
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
rotateLeftIfNeeded : Dict k v -> Dict k v
|
2012-10-10 21:37:42 +00:00
|
|
|
rotateLeftIfNeeded t =
|
2013-05-11 20:42:45 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ _ (RBNode Red _ _ _ _) -> rotateLeft t
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> t
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
rotateRightIfNeeded : Dict k v -> Dict k v
|
2012-10-10 21:37:42 +00:00
|
|
|
rotateRightIfNeeded t =
|
2013-05-11 20:42:45 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ (RBNode Red _ _ (RBNode Red _ _ _ _) _) _ -> rotateRight t
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> t
|
2012-10-10 21:37:42 +00:00
|
|
|
|
|
|
|
otherColor c = case c of { Red -> Black ; Black -> Red }
|
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
color_flip : Dict k v -> Dict k v
|
2012-10-10 21:37:42 +00:00
|
|
|
color_flip t =
|
|
|
|
case t of
|
2013-05-11 20:42:45 +00:00
|
|
|
RBNode c1 bk bv (RBNode c2 ak av la ra) (RBNode c3 ck cv lc rc) ->
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode (otherColor c1) bk bv
|
|
|
|
(RBNode (otherColor c2) ak av la ra)
|
|
|
|
(RBNode (otherColor c3) ck cv lc rc)
|
2013-07-26 14:38:11 +00:00
|
|
|
_ -> Native.Error.raise "color_flip called on a Empty or Node with a Empty child"
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
color_flipIfNeeded : Dict k v -> Dict k v
|
2013-05-11 20:42:45 +00:00
|
|
|
color_flipIfNeeded t =
|
2012-10-10 21:37:42 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ (RBNode Red _ _ _ _) (RBNode Red _ _ _ _) -> color_flip t
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> t
|
2012-10-10 21:37:42 +00:00
|
|
|
|
|
|
|
fixUp t = color_flipIfNeeded (rotateRightIfNeeded (rotateLeftIfNeeded t))
|
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
ensureBlackRoot : Dict k v -> Dict k v
|
2013-05-11 20:42:45 +00:00
|
|
|
ensureBlackRoot t =
|
2012-10-10 21:37:42 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode Red k v l r -> RBNode Black k v l r
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> t
|
2013-05-11 20:42:45 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Insert a key-value pair into a dictionary. Replaces value when there is
|
|
|
|
a collision. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
insert : comparable -> v -> Dict comparable v -> Dict comparable v
|
2013-04-10 05:15:06 +00:00
|
|
|
insert k v t = -- Invariant: t is a valid left-leaning rb tree
|
2012-10-10 21:37:42 +00:00
|
|
|
let ins t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBEmpty -> RBNode Red k v RBEmpty RBEmpty
|
|
|
|
RBNode c k' v' l r ->
|
2013-07-29 21:23:04 +00:00
|
|
|
let h = case Native.Utils.compare k k' of
|
2013-04-10 05:15:06 +00:00
|
|
|
LT -> RBNode c k' v' (ins l) r
|
|
|
|
EQ -> RBNode c k' v l r -- replace
|
|
|
|
GT -> RBNode c k' v' l (ins r)
|
2012-12-02 05:18:25 +00:00
|
|
|
in fixUp h
|
2012-10-19 07:13:28 +00:00
|
|
|
in ensureBlackRoot (ins t)
|
|
|
|
{--
|
|
|
|
if not (invariants_hold t) then
|
2013-07-26 14:38:11 +00:00
|
|
|
Native.Error.raise "invariants broken before insert"
|
2012-10-10 21:37:42 +00:00
|
|
|
else (let new_t = ensureBlackRoot (ins t) in
|
|
|
|
if not (invariants_hold new_t) then
|
2013-07-26 14:38:11 +00:00
|
|
|
Native.Error.raise "invariants broken after insert"
|
2012-10-10 21:37:42 +00:00
|
|
|
else new_t)
|
2012-10-19 07:13:28 +00:00
|
|
|
--}
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Create a dictionary with one key-value pair. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
singleton : comparable -> v -> Dict comparable v
|
2013-04-10 05:15:06 +00:00
|
|
|
singleton k v = insert k v RBEmpty
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
isRed : Dict k v -> Bool
|
2012-10-10 21:37:42 +00:00
|
|
|
isRed t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode Red _ _ _ _ -> True
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> False
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
isRedLeft : Dict k v -> Bool
|
2012-10-10 21:37:42 +00:00
|
|
|
isRedLeft t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ (RBNode Red _ _ _ _) _ -> True
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> False
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
isRedLeftLeft : Dict k v -> Bool
|
2012-10-10 21:37:42 +00:00
|
|
|
isRedLeftLeft t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ (RBNode _ _ _ (RBNode Red _ _ _ _) _) _ -> True
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> False
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
isRedRight : Dict k v -> Bool
|
2012-10-10 21:37:42 +00:00
|
|
|
isRedRight t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ _ (RBNode Red _ _ _ _) -> True
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> False
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
isRedRightLeft : Dict k v -> Bool
|
2012-10-10 21:37:42 +00:00
|
|
|
isRedRightLeft t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ _ (RBNode _ _ _ (RBNode Red _ _ _ _) _) -> True
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> False
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
moveRedLeft : Dict k v -> Dict k v
|
2013-05-11 20:42:45 +00:00
|
|
|
moveRedLeft t =
|
2012-10-10 21:37:42 +00:00
|
|
|
let t' = color_flip t in
|
|
|
|
case t' of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode c k v l r ->
|
2012-10-10 21:37:42 +00:00
|
|
|
case r of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ (RBNode Red _ _ _ _) _ ->
|
|
|
|
color_flip (rotateLeft (RBNode c k v l (rotateRight r)))
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> t'
|
|
|
|
_ -> t'
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
moveRedRight : Dict k v -> Dict k v
|
2012-10-10 21:37:42 +00:00
|
|
|
moveRedRight t =
|
|
|
|
let t' = color_flip t in
|
|
|
|
if isRedLeftLeft t' then color_flip (rotateRight t') else t'
|
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
moveRedLeftIfNeeded : Dict k v -> Dict k v
|
2012-12-13 06:52:32 +00:00
|
|
|
moveRedLeftIfNeeded t =
|
2013-05-06 09:30:50 +00:00
|
|
|
if isRedLeft t || isRedLeftLeft t then t else moveRedLeft t
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
moveRedRightIfNeeded : Dict k v -> Dict k v
|
2012-12-13 06:52:32 +00:00
|
|
|
moveRedRightIfNeeded t =
|
2013-05-06 09:30:50 +00:00
|
|
|
if isRedRight t || isRedRightLeft t then t else moveRedRight t
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
deleteMin : Dict k v -> Dict k v
|
2013-05-11 20:42:45 +00:00
|
|
|
deleteMin t =
|
2012-10-10 21:37:42 +00:00
|
|
|
let del t =
|
2013-05-11 20:42:45 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode _ _ _ RBEmpty _ -> RBEmpty
|
2012-12-02 05:18:25 +00:00
|
|
|
_ -> case moveRedLeftIfNeeded t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode c k v l r -> fixUp (RBNode c k v (del l) r)
|
|
|
|
RBEmpty -> RBEmpty
|
2012-10-10 21:37:42 +00:00
|
|
|
in ensureBlackRoot (del t)
|
|
|
|
|
2012-10-19 07:13:28 +00:00
|
|
|
{--
|
2012-10-10 21:37:42 +00:00
|
|
|
deleteMax t =
|
|
|
|
let del t =
|
|
|
|
let t' = if isRedLeft t then rotateRight t else t in
|
|
|
|
case t' of
|
2013-04-10 05:15:06 +00:00
|
|
|
{ RBNode _ _ _ _ RBEmpty -> RBEmpty
|
2012-10-10 21:37:42 +00:00
|
|
|
; _ -> let t'' = moveRedRightIfNeeded t' in
|
|
|
|
case t'' of
|
2013-04-10 05:15:06 +00:00
|
|
|
{ RBNode c k v l r -> fixUp (RBNode c k v l (del r))
|
|
|
|
; RBEmpty -> RBEmpty } }
|
2012-10-10 21:37:42 +00:00
|
|
|
in ensureBlackRoot (del t)
|
2012-10-19 07:13:28 +00:00
|
|
|
--}
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Remove a key-value pair from a dictionary. If the key is not found,
|
|
|
|
no changes are made. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
remove : comparable -> Dict comparable v -> Dict comparable v
|
2013-05-11 20:42:45 +00:00
|
|
|
remove k t =
|
2012-12-13 06:52:32 +00:00
|
|
|
let eq_and_noRightNode t =
|
2013-04-10 05:15:06 +00:00
|
|
|
case t of { RBNode _ k' _ _ RBEmpty -> k == k' ; _ -> False }
|
|
|
|
eq t = case t of { RBNode _ k' _ _ _ -> k == k' ; _ -> False }
|
2013-05-11 20:42:45 +00:00
|
|
|
delLT t = case moveRedLeftIfNeeded t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode c k' v l r -> fixUp (RBNode c k' v (del l) r)
|
2013-07-26 14:38:11 +00:00
|
|
|
RBEmpty -> Native.Error.raise "delLT on Empty"
|
2012-12-02 05:18:25 +00:00
|
|
|
delEQ t = case t of -- Replace with successor
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode c _ _ l r -> let (k',v') = min r in
|
|
|
|
fixUp (RBNode c k' v' l (deleteMin r))
|
2013-07-26 14:38:11 +00:00
|
|
|
RBEmpty -> Native.Error.raise "delEQ called on a Empty"
|
2012-12-02 05:18:25 +00:00
|
|
|
delGT t = case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBNode c k' v l r -> fixUp (RBNode c k' v l (del r))
|
2013-07-26 14:38:11 +00:00
|
|
|
RBEmpty -> Native.Error.raise "delGT called on a Empty"
|
2013-05-11 20:42:45 +00:00
|
|
|
del t = case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBEmpty -> RBEmpty
|
|
|
|
RBNode _ k' _ _ _ ->
|
2012-12-13 06:52:32 +00:00
|
|
|
if k < k' then delLT t else
|
|
|
|
let u = if isRedLeft t then rotateRight t else t in
|
2013-04-10 05:15:06 +00:00
|
|
|
if eq_and_noRightNode u then RBEmpty else
|
2012-12-13 06:52:32 +00:00
|
|
|
let t' = moveRedRightIfNeeded t in
|
|
|
|
if eq t' then delEQ t' else delGT t'
|
|
|
|
in if member k t then ensureBlackRoot (del t) else t
|
2012-10-19 07:13:28 +00:00
|
|
|
{--
|
|
|
|
if not (invariants_hold t) then
|
2013-07-26 14:38:11 +00:00
|
|
|
Native.Error.raise "invariants broken before remove"
|
2012-10-10 21:37:42 +00:00
|
|
|
else (let t' = ensureBlackRoot (del t) in
|
|
|
|
if invariants_hold t' then t' else
|
2013-07-26 14:38:11 +00:00
|
|
|
Native.Error.raise "invariants broken after remove")
|
2012-10-19 07:13:28 +00:00
|
|
|
--}
|
2012-10-18 09:19:50 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Apply a function to all values in a dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
map : (a -> b) -> Dict comparable a -> Dict comparable b
|
2012-10-18 09:19:50 +00:00
|
|
|
map f t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBEmpty -> RBEmpty
|
|
|
|
RBNode c k v l r -> RBNode c k (f v) (map f l) (map f r)
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Fold over the key-value pairs in a dictionary, in order from lowest
|
|
|
|
key to highest key. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
foldl : (comparable -> v -> b -> b) -> b -> Dict comparable v -> b
|
2012-10-19 07:13:28 +00:00
|
|
|
foldl f acc t =
|
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBEmpty -> acc
|
|
|
|
RBNode _ k v l r -> foldl f (f k v (foldl f acc l)) r
|
2012-10-19 07:13:28 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Fold over the key-value pairs in a dictionary, in order from highest
|
|
|
|
key to lowest key. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
foldr : (comparable -> v -> b -> b) -> b -> Dict comparable v -> b
|
2012-10-19 07:13:28 +00:00
|
|
|
foldr f acc t =
|
2012-10-10 21:37:42 +00:00
|
|
|
case t of
|
2013-04-10 05:15:06 +00:00
|
|
|
RBEmpty -> acc
|
|
|
|
RBNode _ k v l r -> foldr f (f k v (foldr f acc r)) l
|
2012-10-18 09:19:50 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Combine two dictionaries. If there is a collision, preference is given
|
|
|
|
to the first dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
union : Dict comparable v -> Dict comparable v -> Dict comparable v
|
2012-12-13 06:52:32 +00:00
|
|
|
union t1 t2 = foldl insert t2 t1
|
2013-02-05 11:22:02 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Keep a key-value pair when its key appears in the second dictionary.
|
|
|
|
Preference is given to values in the first dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
intersect : Dict comparable v -> Dict comparable v -> Dict comparable v
|
2013-03-17 05:24:18 +00:00
|
|
|
intersect t1 t2 =
|
|
|
|
let combine k v t = if k `member` t2 then insert k v t else t
|
|
|
|
in foldl combine empty t1
|
2013-02-05 11:22:02 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Keep a key-value pair when its key does not appear in the second dictionary.
|
|
|
|
Preference is given to the first dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
diff : Dict comparable v -> Dict comparable v -> Dict comparable v
|
2012-12-13 06:52:32 +00:00
|
|
|
diff t1 t2 = foldl (\k v t -> remove k t) t1 t2
|
2012-10-18 09:19:50 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Get all of the keys in a dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
keys : Dict comparable v -> [comparable]
|
2013-02-05 11:22:02 +00:00
|
|
|
keys t = foldr (\k v acc -> k :: acc) [] t
|
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Get all of the values in a dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
values : Dict comparable v -> [v]
|
2013-02-05 11:22:02 +00:00
|
|
|
values t = foldr (\k v acc -> v :: acc) [] t
|
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Convert a dictionary into an association list of key-value pairs. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
toList : Dict comparable v -> [(comparable,v)]
|
2013-02-05 11:22:02 +00:00
|
|
|
toList t = foldr (\k v acc -> (k,v) :: acc) [] t
|
2012-10-18 09:19:50 +00:00
|
|
|
|
2013-09-10 02:12:27 +00:00
|
|
|
{-| Convert an association list into a dictionary. -}
|
2013-07-26 14:38:11 +00:00
|
|
|
fromList : [(comparable,v)] -> Dict comparable v
|
2013-04-10 05:15:06 +00:00
|
|
|
fromList assocs = List.foldl (\(k,v) d -> insert k v d) empty assocs
|