@@ -115,15 +115,29 @@ checkValid tree = length (nub (allHeights tree)) == one
115115
116116-- | Lookup a value for the specified key
117117lookup :: forall k v . (Ord k ) => k -> Map k v -> Maybe v
118- lookup _ Leaf = Nothing
119- lookup k (Two _ k1 v _) | k == k1 = Just v
120- lookup k (Two left k1 _ _) | k < k1 = lookup k left
121- lookup k (Two _ _ _ right) = lookup k right
122- lookup k (Three _ k1 v1 _ _ _ _) | k == k1 = Just v1
123- lookup k (Three _ _ _ _ k2 v2 _) | k == k2 = Just v2
124- lookup k (Three left k1 _ _ _ _ _) | k < k1 = lookup k left
125- lookup k (Three _ k1 _ mid k2 _ _) | k1 < k && k <= k2 = lookup k mid
126- lookup k (Three _ _ _ _ _ _ right) = lookup k right
118+ lookup k tree =
119+ let comp :: k -> k -> Ordering
120+ comp = compare
121+ in case tree of
122+ Leaf -> Nothing
123+ Two left k1 v right ->
124+ case comp k k1 of
125+ EQ -> Just v
126+ LT -> lookup k left
127+ _ -> lookup k right
128+ Three left k1 v1 mid k2 v2 right ->
129+ case comp k k1 of
130+ EQ -> Just v1
131+ c1 ->
132+ case comp k k2 of
133+ EQ -> Just v2
134+ c2 ->
135+ case c1 of
136+ LT -> lookup k left
137+ _ ->
138+ case c2 of
139+ GT -> lookup k right
140+ _ -> lookup k mid
127141
128142-- | Test if a key is a member of a map
129143member :: forall k v . (Ord k ) => k -> Map k v -> Boolean
@@ -138,36 +152,54 @@ data TreeContext k v
138152
139153fromZipper :: forall k v . (Ord k ) => List (TreeContext k v ) -> Map k v -> Map k v
140154fromZipper Nil tree = tree
141- fromZipper (Cons (TwoLeft k1 v1 right) ctx) left = fromZipper ctx (Two left k1 v1 right)
142- fromZipper (Cons (TwoRight left k1 v1) ctx) right = fromZipper ctx (Two left k1 v1 right)
143- fromZipper (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) left = fromZipper ctx (Three left k1 v1 mid k2 v2 right)
144- fromZipper (Cons (ThreeMiddle left k1 v1 k2 v2 right) ctx) mid = fromZipper ctx (Three left k1 v1 mid k2 v2 right)
145- fromZipper (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) right = fromZipper ctx (Three left k1 v1 mid k2 v2 right)
155+ fromZipper (Cons x ctx) tree =
156+ case x of
157+ TwoLeft k1 v1 right -> fromZipper ctx (Two tree k1 v1 right)
158+ TwoRight left k1 v1 -> fromZipper ctx (Two left k1 v1 tree)
159+ ThreeLeft k1 v1 mid k2 v2 right -> fromZipper ctx (Three tree k1 v1 mid k2 v2 right)
160+ ThreeMiddle left k1 v1 k2 v2 right -> fromZipper ctx (Three left k1 v1 tree k2 v2 right)
161+ ThreeRight left k1 v1 mid k2 v2 -> fromZipper ctx (Three left k1 v1 mid k2 v2 tree)
146162
147163data KickUp k v = KickUp (Map k v ) k v (Map k v )
148164
149165-- | Insert a key/value pair into a map
150166insert :: forall k v . (Ord k ) => k -> v -> Map k v -> Map k v
151167insert = down Nil
152168 where
169+ comp :: k -> k -> Ordering
170+ comp = compare
171+
153172 down :: List (TreeContext k v ) -> k -> v -> Map k v -> Map k v
154173 down ctx k v Leaf = up ctx (KickUp Leaf k v Leaf )
155- down ctx k v (Two left k1 _ right) | k == k1 = fromZipper ctx (Two left k v right)
156- down ctx k v (Two left k1 v1 right) | k < k1 = down (Cons (TwoLeft k1 v1 right) ctx) k v left
157- down ctx k v (Two left k1 v1 right) = down (Cons (TwoRight left k1 v1) ctx) k v right
158- down ctx k v (Three left k1 _ mid k2 v2 right) | k == k1 = fromZipper ctx (Three left k v mid k2 v2 right)
159- down ctx k v (Three left k1 v1 mid k2 _ right) | k == k2 = fromZipper ctx (Three left k1 v1 mid k v right)
160- down ctx k v (Three left k1 v1 mid k2 v2 right) | k < k1 = down (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) k v left
161- down ctx k v (Three left k1 v1 mid k2 v2 right) | k1 < k && k <= k2 = down (Cons (ThreeMiddle left k1 v1 k2 v2 right) ctx) k v mid
162- down ctx k v (Three left k1 v1 mid k2 v2 right) = down (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) k v right
174+ down ctx k v (Two left k1 v1 right) =
175+ case comp k k1 of
176+ EQ -> fromZipper ctx (Two left k v right)
177+ LT -> down (Cons (TwoLeft k1 v1 right) ctx) k v left
178+ _ -> down (Cons (TwoRight left k1 v1) ctx) k v right
179+ down ctx k v (Three left k1 v1 mid k2 v2 right) =
180+ case comp k k1 of
181+ EQ -> fromZipper ctx (Three left k v mid k2 v2 right)
182+ c1 ->
183+ case comp k k2 of
184+ EQ -> fromZipper ctx (Three left k1 v1 mid k v right)
185+ c2 ->
186+ case c1 of
187+ LT -> down (Cons (ThreeLeft k1 v1 mid k2 v2 right) ctx) k v left
188+ GT ->
189+ case c2 of
190+ LT -> down (Cons (ThreeMiddle left k1 v1 k2 v2 right) ctx) k v mid
191+ _ -> down (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) k v right
192+ _ -> down (Cons (ThreeRight left k1 v1 mid k2 v2) ctx) k v right
163193
164194 up :: List (TreeContext k v ) -> KickUp k v -> Map k v
165195 up Nil (KickUp left k v right) = Two left k v right
166- up (Cons (TwoLeft k1 v1 right) ctx) (KickUp left k v mid) = fromZipper ctx (Three left k v mid k1 v1 right)
167- up (Cons (TwoRight left k1 v1) ctx) (KickUp mid k v right) = fromZipper ctx (Three left k1 v1 mid k v right)
168- up (Cons (ThreeLeft k1 v1 c k2 v2 d) ctx) (KickUp a k v b) = up ctx (KickUp (Two a k v b) k1 v1 (Two c k2 v2 d))
169- up (Cons (ThreeMiddle a k1 v1 k2 v2 d) ctx) (KickUp b k v c) = up ctx (KickUp (Two a k1 v1 b) k v (Two c k2 v2 d))
170- up (Cons (ThreeRight a k1 v1 b k2 v2) ctx) (KickUp c k v d) = up ctx (KickUp (Two a k1 v1 b) k2 v2 (Two c k v d))
196+ up (Cons x ctx) (KickUp m1 k v m2) =
197+ case x of
198+ TwoLeft k1 v1 right -> fromZipper ctx (Three m1 k v m2 k1 v1 right)
199+ TwoRight left k1 v1 -> fromZipper ctx (Three left k1 v1 m1 k v m2)
200+ ThreeLeft k1 v1 c k2 v2 d -> up ctx (KickUp (Two m1 k v m2) k1 v1 (Two c k2 v2 d))
201+ ThreeMiddle a k1 v1 k2 v2 d -> up ctx (KickUp (Two a k1 v1 m1) k v (Two m2 k2 v2 d))
202+ ThreeRight a k1 v1 b k2 v2 -> up ctx (KickUp (Two a k1 v1 b) k2 v2 (Two m1 k v m2))
171203
172204-- | Delete a key and its corresponding value from a map
173205delete :: forall k v . (Ord k ) => k -> Map k v -> Map k v
0 commit comments