remove foldlStrict, generalize type of unions, see #520 (#524)
[packages/containers.git] / Data / IntMap / Strict.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE BangPatterns #-}
3 #if !defined(TESTING) && __GLASGOW_HASKELL__ >= 703
4 {-# LANGUAGE Trustworthy #-}
5 #endif
6
7 #include "containers.h"
8
9 -----------------------------------------------------------------------------
10 -- |
11 -- Module : Data.IntMap.Strict
12 -- Copyright : (c) Daan Leijen 2002
13 -- (c) Andriy Palamarchuk 2008
14 -- License : BSD-style
15 -- Maintainer : libraries@haskell.org
16 -- Portability : portable
17 --
18 --
19 -- = Finite Int Maps (strict interface)
20 --
21 -- The @'IntMap' v@ type represents a finite map (sometimes called a dictionary)
22 -- from key of type @Int@ to values of type @v@.
23 --
24 -- Each function in this module is careful to force values before installing
25 -- them in a 'Map'. This is usually more efficient when laziness is not
26 -- necessary. When laziness /is/ required, use the functions in
27 -- "Data.IntMap.Lazy".
28 --
29 -- In particular, the functions in this module obey the following law:
30 --
31 -- - If all values stored in all maps in the arguments are in WHNF, then all
32 -- values stored in all maps in the results will be in WHNF once those maps
33 -- are evaluated.
34 --
35 -- For a walkthrough of the most commonly used functions see the
36 -- <https://haskell-containers.readthedocs.io/en/latest/map.html maps introduction>.
37 --
38 -- This module is intended to be imported qualified, to avoid name clashes with
39 -- Prelude functions:
40 --
41 -- > import Data.IntMap.Strict (IntMap)
42 -- > import qualified Data.IntMap.Strict as IntMap
43 --
44 -- Note that the implementation is generally /left-biased/. Functions that take
45 -- two maps as arguments and combine them, such as `union` and `intersection`,
46 -- prefer the values in the first argument to those in the second.
47 --
48 --
49 -- == Detailed performance information
50 --
51 -- The amortized running time is given for each operation, with /n/ referring to
52 -- the number of entries in the map and /W/ referring to the number of bits in
53 -- an 'Int' (32 or 64).
54 --
55 -- Benchmarks comparing "Data.IntMap.Strict" with other dictionary
56 -- implementations can be found at https://github.com/haskell-perf/dictionaries.
57 --
58 --
59 -- == Warning
60 --
61 -- The 'IntMap' type is shared between the lazy and strict modules, meaning that
62 -- the same 'IntMap' value can be passed to functions in both modules. This
63 -- means that the 'Functor', 'Traversable' and 'Data' instances are the same as
64 -- for the "Data.IntMap.Lazy" module, so if they are used the resulting map may
65 -- contain suspended values (thunks).
66 --
67 --
68 -- == Implementation
69 --
70 -- The implementation is based on /big-endian patricia trees/. This data
71 -- structure performs especially well on binary operations like 'union' and
72 -- 'intersection'. Additionally, benchmarks show that it is also (much) faster
73 -- on insertions and deletions when compared to a generic size-balanced map
74 -- implementation (see "Data.Map").
75 --
76 -- * Chris Okasaki and Andy Gill, \"/Fast Mergeable Integer Maps/\",
77 -- Workshop on ML, September 1998, pages 77-86,
78 -- <http://citeseer.ist.psu.edu/okasaki98fast.html>
79 --
80 -- * D.R. Morrison, \"/PATRICIA -- Practical Algorithm To Retrieve
81 -- Information Coded In Alphanumeric/\", Journal of the ACM, 15(4),
82 -- October 1968, pages 514-534.
83 --
84 -----------------------------------------------------------------------------
85
86 -- See the notes at the beginning of Data.IntMap.Internal.
87
88 module Data.IntMap.Strict (
89 -- * Map type
90 #if !defined(TESTING)
91 IntMap, Key -- instance Eq,Show
92 #else
93 IntMap(..), Key -- instance Eq,Show
94 #endif
95
96 -- * Construction
97 , empty
98 , singleton
99 , fromSet
100
101 -- ** From Unordered Lists
102 , fromList
103 , fromListWith
104 , fromListWithKey
105
106 -- ** From Ascending Lists
107 , fromAscList
108 , fromAscListWith
109 , fromAscListWithKey
110 , fromDistinctAscList
111
112 -- * Insertion
113 , insert
114 , insertWith
115 , insertWithKey
116 , insertLookupWithKey
117
118 -- * Deletion\/Update
119 , delete
120 , adjust
121 , adjustWithKey
122 , update
123 , updateWithKey
124 , updateLookupWithKey
125 , alter
126 , alterF
127
128 -- * Query
129 -- ** Lookup
130 , lookup
131 , (!?)
132 , (!)
133 , findWithDefault
134 , member
135 , notMember
136 , lookupLT
137 , lookupGT
138 , lookupLE
139 , lookupGE
140
141 -- ** Size
142 , null
143 , size
144
145 -- * Combine
146
147 -- ** Union
148 , union
149 , unionWith
150 , unionWithKey
151 , unions
152 , unionsWith
153
154 -- ** Difference
155 , difference
156 , (\\)
157 , differenceWith
158 , differenceWithKey
159
160 -- ** Intersection
161 , intersection
162 , intersectionWith
163 , intersectionWithKey
164
165 -- ** Universal combining function
166 , mergeWithKey
167
168 -- * Traversal
169 -- ** Map
170 , map
171 , mapWithKey
172 , traverseWithKey
173 , mapAccum
174 , mapAccumWithKey
175 , mapAccumRWithKey
176 , mapKeys
177 , mapKeysWith
178 , mapKeysMonotonic
179
180 -- * Folds
181 , foldr
182 , foldl
183 , foldrWithKey
184 , foldlWithKey
185 , foldMapWithKey
186
187 -- ** Strict folds
188 , foldr'
189 , foldl'
190 , foldrWithKey'
191 , foldlWithKey'
192
193 -- * Conversion
194 , elems
195 , keys
196 , assocs
197 , keysSet
198
199 -- ** Lists
200 , toList
201
202 -- ** Ordered lists
203 , toAscList
204 , toDescList
205
206 -- * Filter
207 , filter
208 , filterWithKey
209 , restrictKeys
210 , withoutKeys
211 , partition
212 , partitionWithKey
213
214 , mapMaybe
215 , mapMaybeWithKey
216 , mapEither
217 , mapEitherWithKey
218
219 , split
220 , splitLookup
221 , splitRoot
222
223 -- * Submap
224 , isSubmapOf, isSubmapOfBy
225 , isProperSubmapOf, isProperSubmapOfBy
226
227 -- * Min\/Max
228 , lookupMin
229 , lookupMax
230 , findMin
231 , findMax
232 , deleteMin
233 , deleteMax
234 , deleteFindMin
235 , deleteFindMax
236 , updateMin
237 , updateMax
238 , updateMinWithKey
239 , updateMaxWithKey
240 , minView
241 , maxView
242 , minViewWithKey
243 , maxViewWithKey
244
245 -- * Debugging
246 , showTree
247 , showTreeWith
248 ) where
249
250 import Prelude hiding (lookup,map,filter,foldr,foldl,null)
251
252 import Data.Bits
253 import qualified Data.IntMap.Internal as L
254 import Data.IntMap.Internal
255 ( IntMap (..)
256 , Key
257 , Prefix
258 , Mask
259 , mask
260 , branchMask
261 , shorter
262 , nomatch
263 , zero
264 , natFromInt
265 , intFromNat
266 , bin
267 , binCheckLeft
268 , binCheckRight
269 , link
270
271 , (\\)
272 , (!)
273 , (!?)
274 , empty
275 , assocs
276 , filter
277 , filterWithKey
278 , findMin
279 , findMax
280 , foldMapWithKey
281 , foldr
282 , foldl
283 , foldr'
284 , foldl'
285 , foldlWithKey
286 , foldrWithKey
287 , foldlWithKey'
288 , foldrWithKey'
289 , keysSet
290 , mergeWithKey'
291 , delete
292 , deleteMin
293 , deleteMax
294 , deleteFindMax
295 , deleteFindMin
296 , difference
297 , elems
298 , intersection
299 , isProperSubmapOf
300 , isProperSubmapOfBy
301 , isSubmapOf
302 , isSubmapOfBy
303 , lookup
304 , lookupLE
305 , lookupGE
306 , lookupLT
307 , lookupGT
308 , lookupMin
309 , lookupMax
310 , minView
311 , maxView
312 , minViewWithKey
313 , maxViewWithKey
314 , keys
315 , mapKeys
316 , mapKeysMonotonic
317 , member
318 , notMember
319 , null
320 , partition
321 , partitionWithKey
322 , restrictKeys
323 , size
324 , split
325 , splitLookup
326 , splitRoot
327 , toAscList
328 , toDescList
329 , toList
330 , union
331 , unions
332 , withoutKeys
333 )
334 import Data.IntMap.Internal.DeprecatedDebug (showTree, showTreeWith)
335 import qualified Data.IntSet.Internal as IntSet
336 import Utils.Containers.Internal.BitUtil
337 import Utils.Containers.Internal.StrictPair
338 #if !MIN_VERSION_base(4,8,0)
339 import Data.Functor((<$>))
340 #endif
341 import Control.Applicative (Applicative (..), liftA2)
342 import qualified Data.Foldable as Foldable
343 import Data.Foldable (Foldable())
344
345 {--------------------------------------------------------------------
346 Query
347 --------------------------------------------------------------------}
348
349 -- | /O(min(n,W))/. The expression @('findWithDefault' def k map)@
350 -- returns the value at key @k@ or returns @def@ when the key is not an
351 -- element of the map.
352 --
353 -- > findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
354 -- > findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
355
356 -- See IntMap.Internal.Note: Local 'go' functions and capturing]
357 findWithDefault :: a -> Key -> IntMap a -> a
358 findWithDefault def !k = go
359 where
360 go (Bin p m l r) | nomatch k p m = def
361 | zero k m = go l
362 | otherwise = go r
363 go (Tip kx x) | k == kx = x
364 | otherwise = def
365 go Nil = def
366
367 {--------------------------------------------------------------------
368 Construction
369 --------------------------------------------------------------------}
370 -- | /O(1)/. A map of one element.
371 --
372 -- > singleton 1 'a' == fromList [(1, 'a')]
373 -- > size (singleton 1 'a') == 1
374
375 singleton :: Key -> a -> IntMap a
376 singleton k !x
377 = Tip k x
378 {-# INLINE singleton #-}
379
380 {--------------------------------------------------------------------
381 Insert
382 --------------------------------------------------------------------}
383 -- | /O(min(n,W))/. Insert a new key\/value pair in the map.
384 -- If the key is already present in the map, the associated value is
385 -- replaced with the supplied value, i.e. 'insert' is equivalent to
386 -- @'insertWith' 'const'@.
387 --
388 -- > insert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
389 -- > insert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
390 -- > insert 5 'x' empty == singleton 5 'x'
391
392 insert :: Key -> a -> IntMap a -> IntMap a
393 insert !k !x t =
394 case t of
395 Bin p m l r
396 | nomatch k p m -> link k (Tip k x) p t
397 | zero k m -> Bin p m (insert k x l) r
398 | otherwise -> Bin p m l (insert k x r)
399 Tip ky _
400 | k==ky -> Tip k x
401 | otherwise -> link k (Tip k x) ky t
402 Nil -> Tip k x
403
404 -- right-biased insertion, used by 'union'
405 -- | /O(min(n,W))/. Insert with a combining function.
406 -- @'insertWith' f key value mp@
407 -- will insert the pair (key, value) into @mp@ if key does
408 -- not exist in the map. If the key does exist, the function will
409 -- insert @f new_value old_value@.
410 --
411 -- > insertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
412 -- > insertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
413 -- > insertWith (++) 5 "xxx" empty == singleton 5 "xxx"
414
415 insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
416 insertWith f k x t
417 = insertWithKey (\_ x' y' -> f x' y') k x t
418
419 -- | /O(min(n,W))/. Insert with a combining function.
420 -- @'insertWithKey' f key value mp@
421 -- will insert the pair (key, value) into @mp@ if key does
422 -- not exist in the map. If the key does exist, the function will
423 -- insert @f key new_value old_value@.
424 --
425 -- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
426 -- > insertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
427 -- > insertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
428 -- > insertWithKey f 5 "xxx" empty == singleton 5 "xxx"
429 --
430 -- If the key exists in the map, this function is lazy in @x@ but strict
431 -- in the result of @f@.
432
433 insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
434 insertWithKey f !k x t =
435 case t of
436 Bin p m l r
437 | nomatch k p m -> link k (singleton k x) p t
438 | zero k m -> Bin p m (insertWithKey f k x l) r
439 | otherwise -> Bin p m l (insertWithKey f k x r)
440 Tip ky y
441 | k==ky -> Tip k $! f k x y
442 | otherwise -> link k (singleton k x) ky t
443 Nil -> singleton k x
444
445 -- | /O(min(n,W))/. The expression (@'insertLookupWithKey' f k x map@)
446 -- is a pair where the first element is equal to (@'lookup' k map@)
447 -- and the second element equal to (@'insertWithKey' f k x map@).
448 --
449 -- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
450 -- > insertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
451 -- > insertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "xxx")])
452 -- > insertLookupWithKey f 5 "xxx" empty == (Nothing, singleton 5 "xxx")
453 --
454 -- This is how to define @insertLookup@ using @insertLookupWithKey@:
455 --
456 -- > let insertLookup kx x t = insertLookupWithKey (\_ a _ -> a) kx x t
457 -- > insertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
458 -- > insertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "x")])
459
460 insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)
461 insertLookupWithKey f0 !k0 x0 t0 = toPair $ go f0 k0 x0 t0
462 where
463 go f k x t =
464 case t of
465 Bin p m l r
466 | nomatch k p m -> Nothing :*: link k (singleton k x) p t
467 | zero k m -> let (found :*: l') = go f k x l in (found :*: Bin p m l' r)
468 | otherwise -> let (found :*: r') = go f k x r in (found :*: Bin p m l r')
469 Tip ky y
470 | k==ky -> (Just y :*: (Tip k $! f k x y))
471 | otherwise -> (Nothing :*: link k (singleton k x) ky t)
472 Nil -> Nothing :*: (singleton k x)
473
474
475 {--------------------------------------------------------------------
476 Deletion
477 --------------------------------------------------------------------}
478 -- | /O(min(n,W))/. Adjust a value at a specific key. When the key is not
479 -- a member of the map, the original map is returned.
480 --
481 -- > adjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
482 -- > adjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
483 -- > adjust ("new " ++) 7 empty == empty
484
485 adjust :: (a -> a) -> Key -> IntMap a -> IntMap a
486 adjust f k m
487 = adjustWithKey (\_ x -> f x) k m
488
489 -- | /O(min(n,W))/. Adjust a value at a specific key. When the key is not
490 -- a member of the map, the original map is returned.
491 --
492 -- > let f key x = (show key) ++ ":new " ++ x
493 -- > adjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
494 -- > adjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
495 -- > adjustWithKey f 7 empty == empty
496
497 adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap a
498 adjustWithKey f !k t =
499 case t of
500 Bin p m l r
501 | nomatch k p m -> t
502 | zero k m -> Bin p m (adjustWithKey f k l) r
503 | otherwise -> Bin p m l (adjustWithKey f k r)
504 Tip ky y
505 | k==ky -> Tip ky $! f k y
506 | otherwise -> t
507 Nil -> Nil
508
509 -- | /O(min(n,W))/. The expression (@'update' f k map@) updates the value @x@
510 -- at @k@ (if it is in the map). If (@f x@) is 'Nothing', the element is
511 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
512 --
513 -- > let f x = if x == "a" then Just "new a" else Nothing
514 -- > update f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
515 -- > update f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
516 -- > update f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
517
518 update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap a
519 update f
520 = updateWithKey (\_ x -> f x)
521
522 -- | /O(min(n,W))/. The expression (@'update' f k map@) updates the value @x@
523 -- at @k@ (if it is in the map). If (@f k x@) is 'Nothing', the element is
524 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
525 --
526 -- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
527 -- > updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
528 -- > updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
529 -- > updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
530
531 updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
532 updateWithKey f !k t =
533 case t of
534 Bin p m l r
535 | nomatch k p m -> t
536 | zero k m -> binCheckLeft p m (updateWithKey f k l) r
537 | otherwise -> binCheckRight p m l (updateWithKey f k r)
538 Tip ky y
539 | k==ky -> case f k y of
540 Just !y' -> Tip ky y'
541 Nothing -> Nil
542 | otherwise -> t
543 Nil -> Nil
544
545 -- | /O(min(n,W))/. Lookup and update.
546 -- The function returns original value, if it is updated.
547 -- This is different behavior than 'Data.Map.updateLookupWithKey'.
548 -- Returns the original key value if the map entry is deleted.
549 --
550 -- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
551 -- > updateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:new a")])
552 -- > updateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a")])
553 -- > updateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
554
555 updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a,IntMap a)
556 updateLookupWithKey f0 !k0 t0 = toPair $ go f0 k0 t0
557 where
558 go f k t =
559 case t of
560 Bin p m l r
561 | nomatch k p m -> (Nothing :*: t)
562 | zero k m -> let (found :*: l') = go f k l in (found :*: binCheckLeft p m l' r)
563 | otherwise -> let (found :*: r') = go f k r in (found :*: binCheckRight p m l r')
564 Tip ky y
565 | k==ky -> case f k y of
566 Just !y' -> (Just y :*: Tip ky y')
567 Nothing -> (Just y :*: Nil)
568 | otherwise -> (Nothing :*: t)
569 Nil -> (Nothing :*: Nil)
570
571
572
573 -- | /O(min(n,W))/. The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence thereof.
574 -- 'alter' can be used to insert, delete, or update a value in an 'IntMap'.
575 -- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@.
576 alter :: (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
577 alter f !k t =
578 case t of
579 Bin p m l r
580 | nomatch k p m -> case f Nothing of
581 Nothing -> t
582 Just !x -> link k (Tip k x) p t
583 | zero k m -> binCheckLeft p m (alter f k l) r
584 | otherwise -> binCheckRight p m l (alter f k r)
585 Tip ky y
586 | k==ky -> case f (Just y) of
587 Just !x -> Tip ky x
588 Nothing -> Nil
589 | otherwise -> case f Nothing of
590 Just !x -> link k (Tip k x) ky t
591 Nothing -> t
592 Nil -> case f Nothing of
593 Just !x -> Tip k x
594 Nothing -> Nil
595
596 -- | /O(log n)/. The expression (@'alterF' f k map@) alters the value @x@ at
597 -- @k@, or absence thereof. 'alterF' can be used to inspect, insert, delete,
598 -- or update a value in an 'IntMap'. In short : @'lookup' k <$> 'alterF' f k m = f
599 -- ('lookup' k m)@.
600 --
601 -- Example:
602 --
603 -- @
604 -- interactiveAlter :: Int -> IntMap String -> IO (IntMap String)
605 -- interactiveAlter k m = alterF f k m where
606 -- f Nothing -> do
607 -- putStrLn $ show k ++
608 -- " was not found in the map. Would you like to add it?"
609 -- getUserResponse1 :: IO (Maybe String)
610 -- f (Just old) -> do
611 -- putStrLn "The key is currently bound to " ++ show old ++
612 -- ". Would you like to change or delete it?"
613 -- getUserresponse2 :: IO (Maybe String)
614 -- @
615 --
616 -- 'alterF' is the most general operation for working with an individual
617 -- key that may or may not be in a given map.
618
619 -- Note: 'alterF' is a flipped version of the 'at' combinator from
620 -- 'Control.Lens.At'.
621 --
622 -- @since 0.5.8
623
624 alterF :: Functor f
625 => (Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)
626 -- This implementation was modified from 'Control.Lens.At'.
627 alterF f k m = (<$> f mv) $ \fres ->
628 case fres of
629 Nothing -> maybe m (const (delete k m)) mv
630 Just !v' -> insert k v' m
631 where mv = lookup k m
632
633
634 {--------------------------------------------------------------------
635 Union
636 --------------------------------------------------------------------}
637 -- | The union of a list of maps, with a combining operation.
638 --
639 -- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
640 -- > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
641
642 unionsWith :: Foldable f => (a->a->a) -> f (IntMap a) -> IntMap a
643 unionsWith f ts
644 = Foldable.foldl' (unionWith f) empty ts
645
646 -- | /O(n+m)/. The union with a combining function.
647 --
648 -- > unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
649
650 unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
651 unionWith f m1 m2
652 = unionWithKey (\_ x y -> f x y) m1 m2
653
654 -- | /O(n+m)/. The union with a combining function.
655 --
656 -- > let f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
657 -- > unionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
658
659 unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
660 unionWithKey f m1 m2
661 = mergeWithKey' Bin (\(Tip k1 x1) (Tip _k2 x2) -> Tip k1 $! f k1 x1 x2) id id m1 m2
662
663 {--------------------------------------------------------------------
664 Difference
665 --------------------------------------------------------------------}
666
667 -- | /O(n+m)/. Difference with a combining function.
668 --
669 -- > let f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
670 -- > differenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
671 -- > == singleton 3 "b:B"
672
673 differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
674 differenceWith f m1 m2
675 = differenceWithKey (\_ x y -> f x y) m1 m2
676
677 -- | /O(n+m)/. Difference with a combining function. When two equal keys are
678 -- encountered, the combining function is applied to the key and both values.
679 -- If it returns 'Nothing', the element is discarded (proper set difference).
680 -- If it returns (@'Just' y@), the element is updated with a new value @y@.
681 --
682 -- > let f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
683 -- > differenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
684 -- > == singleton 3 "3:b|B"
685
686 differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
687 differenceWithKey f m1 m2
688 = mergeWithKey f id (const Nil) m1 m2
689
690 {--------------------------------------------------------------------
691 Intersection
692 --------------------------------------------------------------------}
693
694 -- | /O(n+m)/. The intersection with a combining function.
695 --
696 -- > intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
697
698 intersectionWith :: (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
699 intersectionWith f m1 m2
700 = intersectionWithKey (\_ x y -> f x y) m1 m2
701
702 -- | /O(n+m)/. The intersection with a combining function.
703 --
704 -- > let f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
705 -- > intersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
706
707 intersectionWithKey :: (Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
708 intersectionWithKey f m1 m2
709 = mergeWithKey' bin (\(Tip k1 x1) (Tip _k2 x2) -> Tip k1 $! f k1 x1 x2) (const Nil) (const Nil) m1 m2
710
711 {--------------------------------------------------------------------
712 MergeWithKey
713 --------------------------------------------------------------------}
714
715 -- | /O(n+m)/. A high-performance universal combining function. Using
716 -- 'mergeWithKey', all combining functions can be defined without any loss of
717 -- efficiency (with exception of 'union', 'difference' and 'intersection',
718 -- where sharing of some nodes is lost with 'mergeWithKey').
719 --
720 -- Please make sure you know what is going on when using 'mergeWithKey',
721 -- otherwise you can be surprised by unexpected code growth or even
722 -- corruption of the data structure.
723 --
724 -- When 'mergeWithKey' is given three arguments, it is inlined to the call
725 -- site. You should therefore use 'mergeWithKey' only to define your custom
726 -- combining functions. For example, you could define 'unionWithKey',
727 -- 'differenceWithKey' and 'intersectionWithKey' as
728 --
729 -- > myUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -> Just (f k x1 x2)) id id m1 m2
730 -- > myDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
731 -- > myIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -> Just (f k x1 x2)) (const empty) (const empty) m1 m2
732 --
733 -- When calling @'mergeWithKey' combine only1 only2@, a function combining two
734 -- 'IntMap's is created, such that
735 --
736 -- * if a key is present in both maps, it is passed with both corresponding
737 -- values to the @combine@ function. Depending on the result, the key is either
738 -- present in the result with specified value, or is left out;
739 --
740 -- * a nonempty subtree present only in the first map is passed to @only1@ and
741 -- the output is added to the result;
742 --
743 -- * a nonempty subtree present only in the second map is passed to @only2@ and
744 -- the output is added to the result.
745 --
746 -- The @only1@ and @only2@ methods /must return a map with a subset (possibly empty) of the keys of the given map/.
747 -- The values can be modified arbitrarily. Most common variants of @only1@ and
748 -- @only2@ are 'id' and @'const' 'empty'@, but for example @'map' f@ or
749 -- @'filterWithKey' f@ could be used for any @f@.
750
751 mergeWithKey :: (Key -> a -> b -> Maybe c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c)
752 -> IntMap a -> IntMap b -> IntMap c
753 mergeWithKey f g1 g2 = mergeWithKey' bin combine g1 g2
754 where -- We use the lambda form to avoid non-exhaustive pattern matches warning.
755 combine = \(Tip k1 x1) (Tip _k2 x2) -> case f k1 x1 x2 of Nothing -> Nil
756 Just !x -> Tip k1 x
757 {-# INLINE combine #-}
758 {-# INLINE mergeWithKey #-}
759
760 {--------------------------------------------------------------------
761 Min\/Max
762 --------------------------------------------------------------------}
763
764 -- | /O(log n)/. Update the value at the minimal key.
765 --
766 -- > updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
767 -- > updateMinWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
768
769 updateMinWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a
770 updateMinWithKey f t =
771 case t of Bin p m l r | m < 0 -> binCheckRight p m l (go f r)
772 _ -> go f t
773 where
774 go f' (Bin p m l r) = binCheckLeft p m (go f' l) r
775 go f' (Tip k y) = case f' k y of
776 Just !y' -> Tip k y'
777 Nothing -> Nil
778 go _ Nil = error "updateMinWithKey Nil"
779
780 -- | /O(log n)/. Update the value at the maximal key.
781 --
782 -- > updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
783 -- > updateMaxWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
784
785 updateMaxWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a
786 updateMaxWithKey f t =
787 case t of Bin p m l r | m < 0 -> binCheckLeft p m (go f l) r
788 _ -> go f t
789 where
790 go f' (Bin p m l r) = binCheckRight p m l (go f' r)
791 go f' (Tip k y) = case f' k y of
792 Just !y' -> Tip k y'
793 Nothing -> Nil
794 go _ Nil = error "updateMaxWithKey Nil"
795
796 -- | /O(log n)/. Update the value at the maximal key.
797 --
798 -- > updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
799 -- > updateMax (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
800
801 updateMax :: (a -> Maybe a) -> IntMap a -> IntMap a
802 updateMax f = updateMaxWithKey (const f)
803
804 -- | /O(log n)/. Update the value at the minimal key.
805 --
806 -- > updateMin (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
807 -- > updateMin (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
808
809 updateMin :: (a -> Maybe a) -> IntMap a -> IntMap a
810 updateMin f = updateMinWithKey (const f)
811
812
813 {--------------------------------------------------------------------
814 Mapping
815 --------------------------------------------------------------------}
816 -- | /O(n)/. Map a function over all values in the map.
817 --
818 -- > map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
819
820 map :: (a -> b) -> IntMap a -> IntMap b
821 map f = go
822 where
823 go (Bin p m l r) = Bin p m (go l) (go r)
824 go (Tip k x) = Tip k $! f x
825 go Nil = Nil
826
827 #ifdef __GLASGOW_HASKELL__
828 {-# NOINLINE [1] map #-}
829 {-# RULES
830 "map/map" forall f g xs . map f (map g xs) = map (\x -> f $! g x) xs
831 "map/mapL" forall f g xs . map f (L.map g xs) = map (\x -> f (g x)) xs
832 #-}
833 #endif
834
835 -- | /O(n)/. Map a function over all values in the map.
836 --
837 -- > let f key x = (show key) ++ ":" ++ x
838 -- > mapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
839
840 mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b
841 mapWithKey f t
842 = case t of
843 Bin p m l r -> Bin p m (mapWithKey f l) (mapWithKey f r)
844 Tip k x -> Tip k $! f k x
845 Nil -> Nil
846
847 #ifdef __GLASGOW_HASKELL__
848 -- Pay close attention to strictness here. We need to force the
849 -- intermediate result for map f . map g, and we need to refrain
850 -- from forcing it for map f . L.map g, etc.
851 --
852 -- TODO Consider moving map and mapWithKey to IntMap.Internal so we can write
853 -- non-orphan RULES for things like L.map f (map g xs). We'd need a new function
854 -- for this, and we'd have to pay attention to simplifier phases. Something like
855 --
856 -- lsmap :: (b -> c) -> (a -> b) -> IntMap a -> IntMap c
857 -- lsmap _ _ Nil = Nil
858 -- lsmap f g (Tip k x) = let !gx = g x in Tip k (f gx)
859 -- lsmap f g (Bin p m l r) = Bin p m (lsmap f g l) (lsmap f g r)
860 {-# NOINLINE [1] mapWithKey #-}
861 {-# RULES
862 "mapWithKey/mapWithKey" forall f g xs . mapWithKey f (mapWithKey g xs) =
863 mapWithKey (\k a -> f k $! g k a) xs
864 "mapWithKey/mapWithKeyL" forall f g xs . mapWithKey f (L.mapWithKey g xs) =
865 mapWithKey (\k a -> f k (g k a)) xs
866 "mapWithKey/map" forall f g xs . mapWithKey f (map g xs) =
867 mapWithKey (\k a -> f k $! g a) xs
868 "mapWithKey/mapL" forall f g xs . mapWithKey f (L.map g xs) =
869 mapWithKey (\k a -> f k (g a)) xs
870 "map/mapWithKey" forall f g xs . map f (mapWithKey g xs) =
871 mapWithKey (\k a -> f $! g k a) xs
872 "map/mapWithKeyL" forall f g xs . map f (L.mapWithKey g xs) =
873 mapWithKey (\k a -> f (g k a)) xs
874 #-}
875 #endif
876
877 -- | /O(n)/.
878 -- @'traverseWithKey' f s == 'fromList' <$> 'traverse' (\(k, v) -> (,) k <$> f k v) ('toList' m)@
879 -- That is, behaves exactly like a regular 'traverse' except that the traversing
880 -- function also has access to the key associated with a value.
881 --
882 -- > traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
883 -- > traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')]) == Nothing
884 traverseWithKey :: Applicative t => (Key -> a -> t b) -> IntMap a -> t (IntMap b)
885 traverseWithKey f = go
886 where
887 go Nil = pure Nil
888 go (Tip k v) = (\ !v' -> Tip k v') <$> f k v
889 go (Bin p m l r) = liftA2 (Bin p m) (go l) (go r)
890 {-# INLINE traverseWithKey #-}
891
892 -- | /O(n)/. The function @'mapAccum'@ threads an accumulating
893 -- argument through the map in ascending order of keys.
894 --
895 -- > let f a b = (a ++ b, b ++ "X")
896 -- > mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
897
898 mapAccum :: (a -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
899 mapAccum f = mapAccumWithKey (\a' _ x -> f a' x)
900
901 -- | /O(n)/. The function @'mapAccumWithKey'@ threads an accumulating
902 -- argument through the map in ascending order of keys.
903 --
904 -- > let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
905 -- > mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
906
907 mapAccumWithKey :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
908 mapAccumWithKey f a t
909 = mapAccumL f a t
910
911 -- | /O(n)/. The function @'mapAccumL'@ threads an accumulating
912 -- argument through the map in ascending order of keys. Strict in
913 -- the accumulating argument and the both elements of the
914 -- result of the function.
915 mapAccumL :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
916 mapAccumL f0 a0 t0 = toPair $ go f0 a0 t0
917 where
918 go f a t
919 = case t of
920 Bin p m l r -> let (a1 :*: l') = go f a l
921 (a2 :*: r') = go f a1 r
922 in (a2 :*: Bin p m l' r')
923 Tip k x -> let !(a',!x') = f a k x in (a' :*: Tip k x')
924 Nil -> (a :*: Nil)
925
926 -- | /O(n)/. The function @'mapAccumR'@ threads an accumulating
927 -- argument through the map in descending order of keys.
928 mapAccumRWithKey :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
929 mapAccumRWithKey f0 a0 t0 = toPair $ go f0 a0 t0
930 where
931 go f a t
932 = case t of
933 Bin p m l r -> let (a1 :*: r') = go f a r
934 (a2 :*: l') = go f a1 l
935 in (a2 :*: Bin p m l' r')
936 Tip k x -> let !(a',!x') = f a k x in (a' :*: Tip k x')
937 Nil -> (a :*: Nil)
938
939 -- | /O(n*log n)/.
940 -- @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@.
941 --
942 -- The size of the result may be smaller if @f@ maps two or more distinct
943 -- keys to the same new key. In this case the associated values will be
944 -- combined using @c@.
945 --
946 -- > mapKeysWith (++) (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
947 -- > mapKeysWith (++) (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
948
949 mapKeysWith :: (a -> a -> a) -> (Key->Key) -> IntMap a -> IntMap a
950 mapKeysWith c f = fromListWith c . foldrWithKey (\k x xs -> (f k, x) : xs) []
951
952 {--------------------------------------------------------------------
953 Filter
954 --------------------------------------------------------------------}
955 -- | /O(n)/. Map values and collect the 'Just' results.
956 --
957 -- > let f x = if x == "a" then Just "new a" else Nothing
958 -- > mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
959
960 mapMaybe :: (a -> Maybe b) -> IntMap a -> IntMap b
961 mapMaybe f = mapMaybeWithKey (\_ x -> f x)
962
963 -- | /O(n)/. Map keys\/values and collect the 'Just' results.
964 --
965 -- > let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing
966 -- > mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
967
968 mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap b
969 mapMaybeWithKey f (Bin p m l r)
970 = bin p m (mapMaybeWithKey f l) (mapMaybeWithKey f r)
971 mapMaybeWithKey f (Tip k x) = case f k x of
972 Just !y -> Tip k y
973 Nothing -> Nil
974 mapMaybeWithKey _ Nil = Nil
975
976 -- | /O(n)/. Map values and separate the 'Left' and 'Right' results.
977 --
978 -- > let f a = if a < "c" then Left a else Right a
979 -- > mapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
980 -- > == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
981 -- >
982 -- > mapEither (\ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
983 -- > == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
984
985 mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
986 mapEither f m
987 = mapEitherWithKey (\_ x -> f x) m
988
989 -- | /O(n)/. Map keys\/values and separate the 'Left' and 'Right' results.
990 --
991 -- > let f k a = if k < 5 then Left (k * 2) else Right (a ++ a)
992 -- > mapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
993 -- > == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
994 -- >
995 -- > mapEitherWithKey (\_ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
996 -- > == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
997
998 mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
999 mapEitherWithKey f0 t0 = toPair $ go f0 t0
1000 where
1001 go f (Bin p m l r)
1002 = bin p m l1 r1 :*: bin p m l2 r2
1003 where
1004 (l1 :*: l2) = go f l
1005 (r1 :*: r2) = go f r
1006 go f (Tip k x) = case f k x of
1007 Left !y -> (Tip k y :*: Nil)
1008 Right !z -> (Nil :*: Tip k z)
1009 go _ Nil = (Nil :*: Nil)
1010
1011 {--------------------------------------------------------------------
1012 Conversions
1013 --------------------------------------------------------------------}
1014
1015 -- | /O(n)/. Build a map from a set of keys and a function which for each key
1016 -- computes its value.
1017 --
1018 -- > fromSet (\k -> replicate k 'a') (Data.IntSet.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
1019 -- > fromSet undefined Data.IntSet.empty == empty
1020
1021 fromSet :: (Key -> a) -> IntSet.IntSet -> IntMap a
1022 fromSet _ IntSet.Nil = Nil
1023 fromSet f (IntSet.Bin p m l r) = Bin p m (fromSet f l) (fromSet f r)
1024 fromSet f (IntSet.Tip kx bm) = buildTree f kx bm (IntSet.suffixBitMask + 1)
1025 where -- This is slightly complicated, as we to convert the dense
1026 -- representation of IntSet into tree representation of IntMap.
1027 --
1028 -- We are given a nonzero bit mask 'bmask' of 'bits' bits with prefix 'prefix'.
1029 -- We split bmask into halves corresponding to left and right subtree.
1030 -- If they are both nonempty, we create a Bin node, otherwise exactly
1031 -- one of them is nonempty and we construct the IntMap from that half.
1032 buildTree g !prefix !bmask bits = case bits of
1033 0 -> Tip prefix $! g prefix
1034 _ -> case intFromNat ((natFromInt bits) `shiftRL` 1) of
1035 bits2 | bmask .&. ((1 `shiftLL` bits2) - 1) == 0 ->
1036 buildTree g (prefix + bits2) (bmask `shiftRL` bits2) bits2
1037 | (bmask `shiftRL` bits2) .&. ((1 `shiftLL` bits2) - 1) == 0 ->
1038 buildTree g prefix bmask bits2
1039 | otherwise ->
1040 Bin prefix bits2 (buildTree g prefix bmask bits2) (buildTree g (prefix + bits2) (bmask `shiftRL` bits2) bits2)
1041
1042 {--------------------------------------------------------------------
1043 Lists
1044 --------------------------------------------------------------------}
1045 -- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs.
1046 --
1047 -- > fromList [] == empty
1048 -- > fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
1049 -- > fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
1050
1051 fromList :: [(Key,a)] -> IntMap a
1052 fromList xs
1053 = Foldable.foldl' ins empty xs
1054 where
1055 ins t (k,x) = insert k x t
1056
1057 -- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'.
1058 --
1059 -- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
1060 -- > fromListWith (++) [] == empty
1061
1062 fromListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a
1063 fromListWith f xs
1064 = fromListWithKey (\_ x y -> f x y) xs
1065
1066 -- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs with a combining function. See also fromAscListWithKey'.
1067 --
1068 -- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
1069 -- > fromListWith (++) [] == empty
1070
1071 fromListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
1072 fromListWithKey f xs
1073 = Foldable.foldl' ins empty xs
1074 where
1075 ins t (k,x) = insertWithKey f k x t
1076
1077 -- | /O(n)/. Build a map from a list of key\/value pairs where
1078 -- the keys are in ascending order.
1079 --
1080 -- > fromAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
1081 -- > fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
1082
1083 fromAscList :: [(Key,a)] -> IntMap a
1084 fromAscList xs
1085 = fromAscListWithKey (\_ x _ -> x) xs
1086
1087 -- | /O(n)/. Build a map from a list of key\/value pairs where
1088 -- the keys are in ascending order, with a combining function on equal keys.
1089 -- /The precondition (input list is ascending) is not checked./
1090 --
1091 -- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
1092
1093 fromAscListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a
1094 fromAscListWith f xs
1095 = fromAscListWithKey (\_ x y -> f x y) xs
1096
1097 -- | /O(n)/. Build a map from a list of key\/value pairs where
1098 -- the keys are in ascending order, with a combining function on equal keys.
1099 -- /The precondition (input list is ascending) is not checked./
1100 --
1101 -- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
1102
1103 fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
1104 fromAscListWithKey _ [] = Nil
1105 fromAscListWithKey f (x0 : xs0) = fromDistinctAscList (combineEq x0 xs0)
1106 where
1107 -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
1108 combineEq z [] = [z]
1109 combineEq z@(kz,zz) (x@(kx,xx):xs)
1110 | kx==kz = let !yy = f kx xx zz in combineEq (kx,yy) xs
1111 | otherwise = z:combineEq x xs
1112
1113 -- | /O(n)/. Build a map from a list of key\/value pairs where
1114 -- the keys are in ascending order and all distinct.
1115 -- /The precondition (input list is strictly ascending) is not checked./
1116 --
1117 -- > fromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
1118
1119 fromDistinctAscList :: [(Key,a)] -> IntMap a
1120 fromDistinctAscList [] = Nil
1121 fromDistinctAscList (z0 : zs0) = work z0 zs0 Nada
1122 where
1123 work (kx,!vx) [] stk = finish kx (Tip kx vx) stk
1124 work (kx,!vx) (z@(kz,_):zs) stk = reduce z zs (branchMask kx kz) kx (Tip kx vx) stk
1125
1126 reduce :: (Key,a) -> [(Key,a)] -> Mask -> Prefix -> IntMap a -> Stack a -> IntMap a
1127 reduce z zs _ px tx Nada = work z zs (Push px tx Nada)
1128 reduce z zs m px tx stk@(Push py ty stk') =
1129 let mxy = branchMask px py
1130 pxy = mask px mxy
1131 in if shorter m mxy
1132 then reduce z zs m pxy (Bin pxy mxy ty tx) stk'
1133 else work z zs (Push px tx stk)
1134
1135 finish _ t Nada = t
1136 finish px tx (Push py ty stk) = finish p (link py ty px tx) stk
1137 where m = branchMask px py
1138 p = mask px m
1139
1140 data Stack a = Push {-# UNPACK #-} !Prefix !(IntMap a) !(Stack a) | Nada