0 | module Libraries.Data.IntMap
12 | data Tree : Nat -> Type -> Type where
13 | Leaf : Key -> v -> Tree Z v
14 | Branch2 : Tree n v -> Key -> Tree n v -> Tree (S n) v
15 | Branch3 : Tree n v -> Key -> Tree n v -> Key -> Tree n v -> Tree (S n) v
23 | branch4 a b c d e f g =
24 | Branch2 (Branch2 a b c) d (Branch2 e f g)
33 | branch5 a b c d e f g h i =
34 | Branch2 (Branch2 a b c) d (Branch3 e f g h i)
44 | branch6 a b c d e f g h i j k =
45 | Branch3 (Branch2 a b c) d (Branch2 e f g) h (Branch2 i j k)
56 | branch7 a b c d e f g h i j k l m =
57 | Branch3 (Branch3 a b c d e) f (Branch2 g h i) j (Branch2 k l m)
59 | merge1 : Tree n v -> Key -> Tree (S n) v -> Key -> Tree (S n) v -> Tree (S (S n)) v
60 | merge1 a b (Branch2 c d e) f (Branch2 g h i) = branch5 a b c d e f g h i
61 | merge1 a b (Branch2 c d e) f (Branch3 g h i j k) = branch6 a b c d e f g h i j k
62 | merge1 a b (Branch3 c d e f g) h (Branch2 i j k) = branch6 a b c d e f g h i j k
63 | merge1 a b (Branch3 c d e f g) h (Branch3 i j k l m) = branch7 a b c d e f g h i j k l m
65 | merge2 : Tree (S n) v -> Key -> Tree n v -> Key -> Tree (S n) v -> Tree (S (S n)) v
66 | merge2 (Branch2 a b c) d e f (Branch2 g h i) = branch5 a b c d e f g h i
67 | merge2 (Branch2 a b c) d e f (Branch3 g h i j k) = branch6 a b c d e f g h i j k
68 | merge2 (Branch3 a b c d e) f g h (Branch2 i j k) = branch6 a b c d e f g h i j k
69 | merge2 (Branch3 a b c d e) f g h (Branch3 i j k l m) = branch7 a b c d e f g h i j k l m
71 | merge3 : Tree (S n) v -> Key -> Tree (S n) v -> Key -> Tree n v -> Tree (S (S n)) v
72 | merge3 (Branch2 a b c) d (Branch2 e f g) h i = branch5 a b c d e f g h i
73 | merge3 (Branch2 a b c) d (Branch3 e f g h i) j k = branch6 a b c d e f g h i j k
74 | merge3 (Branch3 a b c d e) f (Branch2 g h i) j k = branch6 a b c d e f g h i j k
75 | merge3 (Branch3 a b c d e) f (Branch3 g h i j k) l m = branch7 a b c d e f g h i j k l m
77 | treeLookup : Key -> Tree n v -> Maybe v
78 | treeLookup k (Leaf k' v) =
83 | treeLookup k (Branch2 t1 k' t2) =
88 | treeLookup k (Branch3 t1 k1 t2 k2 t3) =
91 | else if k <= k2 then
96 | treeInsert' : Key -> v -> Tree n v -> Either (Tree n v) (Tree n v, Key, Tree n v)
97 | treeInsert' k v (Leaf k' v') =
98 | case compare k k' of
99 | LT => Right (Leaf k v, k, Leaf k' v')
100 | EQ => Left (Leaf k v)
101 | GT => Right (Leaf k' v', k', Leaf k v)
102 | treeInsert' k v (Branch2 t1 k' t2) =
104 | case treeInsert' k v t1 of
105 | Left t1' => Left (Branch2 t1' k' t2)
106 | Right (a, b, c) => Left (Branch3 a b c k' t2)
108 | case treeInsert' k v t2 of
109 | Left t2' => Left (Branch2 t1 k' t2')
110 | Right (a, b, c) => Left (Branch3 t1 k' a b c)
111 | treeInsert' k v (Branch3 t1 k1 t2 k2 t3) =
113 | case treeInsert' k v t1 of
114 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
115 | Right (a, b, c) => Right (Branch2 a b c, k1, Branch2 t2 k2 t3)
118 | case treeInsert' k v t2 of
119 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
120 | Right (a, b, c) => Right (Branch2 t1 k1 a, b, Branch2 c k2 t3)
122 | case treeInsert' k v t3 of
123 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
124 | Right (a, b, c) => Right (Branch2 t1 k1 t2, k2, Branch2 a b c)
126 | treeInsert : Key -> v -> Tree n v -> Either (Tree n v) (Tree (S n) v)
128 | case treeInsert' k v t of
130 | Right (a, b, c) => Right (Branch2 a b c)
132 | delType : Nat -> Type -> Type
134 | delType (S n) v = Tree n v
136 | treeDelete : {n : _} -> Key -> Tree n v -> Either (Tree n v) (delType n v)
137 | treeDelete k (Leaf k' v) =
142 | treeDelete {n=S Z} k (Branch2 t1 k' t2) =
144 | case treeDelete k t1 of
145 | Left t1' => Left (Branch2 t1' k' t2)
146 | Right () => Right t2
148 | case treeDelete k t2 of
149 | Left t2' => Left (Branch2 t1 k' t2')
150 | Right () => Right t1
151 | treeDelete {n=S Z} k (Branch3 t1 k1 t2 k2 t3) =
153 | case treeDelete k t1 of
154 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
155 | Right () => Left (Branch2 t2 k2 t3)
156 | else if k <= k2 then
157 | case treeDelete k t2 of
158 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
159 | Right () => Left (Branch2 t1 k1 t3)
161 | case treeDelete k t3 of
162 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
163 | Right () => Left (Branch2 t1 k1 t2)
164 | treeDelete {n=S (S _)} k (Branch2 t1 k' t2) =
166 | case treeDelete k t1 of
167 | Left t1' => Left (Branch2 t1' k' t2)
170 | Branch2 a b c => Right (Branch3 t1' k' a b c)
171 | Branch3 a b c d e => Left (branch4 t1' k' a b c d e)
173 | case treeDelete k t2 of
174 | Left t2' => Left (Branch2 t1 k' t2')
177 | Branch2 a b c => Right (Branch3 a b c k' t2')
178 | Branch3 a b c d e => Left (branch4 a b c d e k' t2')
179 | treeDelete {n=(S (S _))} k (Branch3 t1 k1 t2 k2 t3) =
181 | case treeDelete k t1 of
182 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
183 | Right t1' => Left (merge1 t1' k1 t2 k2 t3)
184 | else if k <= k2 then
185 | case treeDelete k t2 of
186 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
187 | Right t2' => Left (merge2 t1 k1 t2' k2 t3)
189 | case treeDelete k t3 of
190 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
191 | Right t3' => Left (merge3 t1 k1 t2 k2 t3')
193 | treeToList : Tree n v -> List (Key, v)
194 | treeToList = treeToList' []
196 | treeToList' : forall n . List (Key, v) -> Tree n v -> List (Key, v)
197 | treeToList' rest (Leaf k v) = (k, v) :: rest
198 | treeToList' rest (Branch2 t1 _ t2)
199 | = treeToList' (treeToList' rest t2) t1
200 | treeToList' rest (Branch3 t1 _ t2 _ t3)
201 | = treeToList' (treeToList' (treeToList' rest t3) t2) t1
204 | data IntMap : Type -> Type where
206 | M : (n : Nat) -> Tree n v -> IntMap v
213 | lookup : Int -> IntMap v -> Maybe v
214 | lookup _ Empty = Nothing
215 | lookup k (M _ t) = treeLookup k t
218 | insert : Int -> v -> IntMap v -> IntMap v
219 | insert k v Empty = M Z (Leaf k v)
220 | insert k v (M _ t) =
221 | case treeInsert k v t of
222 | Left t' => (M _ t')
223 | Right t' => (M _ t')
226 | singleton : Int -> v -> IntMap v
227 | singleton k v = insert k v empty
230 | insertFrom : List (Int, v) -> IntMap v -> IntMap v
231 | insertFrom = flip $
foldl $
flip $
uncurry insert
234 | delete : Int -> IntMap v -> IntMap v
235 | delete _ Empty = Empty
237 | case treeDelete k t of
238 | Left t' => (M _ t')
240 | delete k (M (S _) t) =
241 | case treeDelete k t of
242 | Left t' => (M _ t')
243 | Right t' => (M _ t')
246 | fromList : List (Int, v) -> IntMap v
247 | fromList l = foldl (flip (uncurry insert)) empty l
250 | toList : IntMap v -> List (Int, v)
252 | toList (M _ t) = treeToList t
256 | keys : IntMap v -> List Int
257 | keys = map fst . toList
261 | values : IntMap v -> List v
262 | values = map snd . toList
264 | treeMap : (a -> b) -> Tree n a -> Tree n b
265 | treeMap f (Leaf k v) = Leaf k (f v)
266 | treeMap f (Branch2 t1 k t2) = Branch2 (treeMap f t1) k (treeMap f t2)
267 | treeMap f (Branch3 t1 k1 t2 k2 t3)
268 | = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
271 | implementation Functor IntMap where
272 | map _ Empty = Empty
273 | map f (M n t) = M _ (treeMap f t)
278 | mergeWith : (v -> v -> v) -> IntMap v -> IntMap v -> IntMap v
279 | mergeWith f x y = insertFrom inserted x where
280 | inserted : List (Key, v)
283 | let v' = (maybe id f $
lookup k x) v
289 | merge : Semigroup v => IntMap v -> IntMap v -> IntMap v
290 | merge = mergeWith (<+>)
294 | mergeLeft : IntMap v -> IntMap v -> IntMap v
295 | mergeLeft = mergeWith const
303 | Semigroup v => Semigroup (IntMap v) where
307 | (Semigroup v) => Monoid (IntMap v) where