|
2 | 2 | -- | <http://www.cs.princeton.edu/~dpw/courses/cos326-12/ass/2-3-trees.pdf> |
3 | 3 |
|
4 | 4 | module Data.Map |
5 | | - ( Map(), |
6 | | - showTree, |
7 | | - empty, |
8 | | - isEmpty, |
9 | | - singleton, |
10 | | - checkValid, |
11 | | - insert, |
12 | | - lookup, |
13 | | - toList, |
14 | | - fromList, |
15 | | - fromListWith, |
16 | | - delete, |
17 | | - member, |
18 | | - alter, |
19 | | - update, |
20 | | - keys, |
21 | | - values, |
22 | | - union, |
23 | | - unionWith, |
24 | | - unions, |
25 | | - map, |
26 | | - size |
| 5 | + ( Map() |
| 6 | + , showTree |
| 7 | + , empty |
| 8 | + , isEmpty |
| 9 | + , singleton |
| 10 | + , checkValid |
| 11 | + , insert |
| 12 | + , lookup |
| 13 | + , toList |
| 14 | + , fromList |
| 15 | + , fromListWith |
| 16 | + , delete |
| 17 | + , member |
| 18 | + , alter |
| 19 | + , update |
| 20 | + , keys |
| 21 | + , values |
| 22 | + , union |
| 23 | + , unionWith |
| 24 | + , unions |
| 25 | + , map |
| 26 | + , size |
27 | 27 | ) where |
28 | 28 |
|
29 | | -import qualified Data.Array as A |
30 | | -import Data.Maybe |
31 | | -import Data.Tuple |
32 | | -import Data.Monoid (Monoid) |
33 | 29 | import Data.Foldable (foldl, foldMap, foldr, Foldable) |
| 30 | +import Data.Int (Int()) |
| 31 | +import Data.Maybe (Maybe(..), maybe, isJust) |
| 32 | +import Data.Monoid (Monoid) |
34 | 33 | import Data.Traversable (traverse, Traversable) |
| 34 | +import Data.Tuple (Tuple(..), uncurry) |
| 35 | +import qualified Data.Array as A |
35 | 36 |
|
36 | 37 | -- | `Map k v` represents maps from keys of type `k` to values of type `v`. |
37 | 38 | data Map k v |
@@ -103,12 +104,12 @@ singleton k v = Two Leaf k v Leaf |
103 | 104 | -- | |
104 | 105 | -- | This function is provided for internal use. |
105 | 106 | checkValid :: forall k v. Map k v -> Boolean |
106 | | -checkValid tree = A.length (A.nub (allHeights tree)) == 1 |
| 107 | +checkValid tree = A.length (A.nub (allHeights tree)) == one |
107 | 108 | where |
108 | | - allHeights :: forall k v. Map k v -> [Number] |
109 | | - allHeights Leaf = [0] |
110 | | - allHeights (Two left _ _ right) = A.map (\n -> n + 1) (allHeights left ++ allHeights right) |
111 | | - allHeights (Three left _ _ mid _ _ right) = A.map (\n -> n + 1) (allHeights left ++ allHeights mid ++ allHeights right) |
| 109 | + allHeights :: forall k v. Map k v -> [Int] |
| 110 | + allHeights Leaf = [zero] |
| 111 | + allHeights (Two left _ _ right) = A.map (\n -> n + one) (allHeights left ++ allHeights right) |
| 112 | + allHeights (Three left _ _ mid _ _ right) = A.map (\n -> n + one) (allHeights left ++ allHeights mid ++ allHeights right) |
112 | 113 |
|
113 | 114 | -- | Lookup a value for the specified key |
114 | 115 | lookup :: forall k v. (Ord k) => k -> Map k v -> Maybe v |
@@ -172,17 +173,17 @@ delete = down [] |
172 | 173 | where |
173 | 174 | down :: forall k v. (Ord k) => [TreeContext k v] -> k -> Map k v -> Map k v |
174 | 175 | down ctx _ Leaf = fromZipper ctx Leaf |
175 | | - down ctx k (Two Leaf k1 _ Leaf) |
| 176 | + down ctx k (Two Leaf k1 _ Leaf) |
176 | 177 | | k == k1 = up ctx Leaf |
177 | | - down ctx k (Two left k1 v1 right) |
| 178 | + down ctx k (Two left k1 v1 right) |
178 | 179 | | k == k1 = let max = maxNode left |
179 | 180 | in removeMaxNode (TwoLeft max.key max.value right : ctx) left |
180 | 181 | | k < k1 = down (TwoLeft k1 v1 right : ctx) k left |
181 | 182 | | otherwise = down (TwoRight left k1 v1 : ctx) k right |
182 | | - down ctx k (Three Leaf k1 v1 Leaf k2 v2 Leaf) |
| 183 | + down ctx k (Three Leaf k1 v1 Leaf k2 v2 Leaf) |
183 | 184 | | k == k1 = fromZipper ctx (Two Leaf k2 v2 Leaf) |
184 | 185 | | k == k2 = fromZipper ctx (Two Leaf k1 v1 Leaf) |
185 | | - down ctx k (Three left k1 v1 mid k2 v2 right) |
| 186 | + down ctx k (Three left k1 v1 mid k2 v2 right) |
186 | 187 | | k == k1 = let max = maxNode left |
187 | 188 | in removeMaxNode (ThreeLeft max.key max.value mid k2 v2 right : ctx) left |
188 | 189 | | k == k2 = let max = maxNode mid |
@@ -283,5 +284,5 @@ map :: forall k a b. (a -> b) -> Map k a -> Map k b |
283 | 284 | map = (<$>) |
284 | 285 |
|
285 | 286 | -- | Calculate the number of key/value pairs in a map |
286 | | -size :: forall k v. Map k v -> Number |
| 287 | +size :: forall k v. Map k v -> Int |
287 | 288 | size = A.length <<< values |
0 commit comments