659d66e7ce545391baae014ddca6b979552f4b65
[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.StrictFold
338 import Utils.Containers.Internal.StrictPair
339 #if !MIN_VERSION_base(4,8,0)
340 import Data.Functor((<$>))
341 #endif
342 import Control.Applicative (Applicative (..), liftA2)
343
344 {--------------------------------------------------------------------
345 Query
346 --------------------------------------------------------------------}
347
348 -- | /O(min(n,W))/. The expression @('findWithDefault' def k map)@
349 -- returns the value at key @k@ or returns @def@ when the key is not an
350 -- element of the map.
351 --
352 -- > findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
353 -- > findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
354
355 -- See IntMap.Internal.Note: Local 'go' functions and capturing]
356 findWithDefault :: a -> Key -> IntMap a -> a
357 findWithDefault def !k = go
358 where
359 go (Bin p m l r) | nomatch k p m = def
360 | zero k m = go l
361 | otherwise = go r
362 go (Tip kx x) | k == kx = x
363 | otherwise = def
364 go Nil = def
365
366 {--------------------------------------------------------------------
367 Construction
368 --------------------------------------------------------------------}
369 -- | /O(1)/. A map of one element.
370 --
371 -- > singleton 1 'a' == fromList [(1, 'a')]
372 -- > size (singleton 1 'a') == 1
373
374 singleton :: Key -> a -> IntMap a
375 singleton k !x
376 = Tip k x
377 {-# INLINE singleton #-}
378
379 {--------------------------------------------------------------------
380 Insert
381 --------------------------------------------------------------------}
382 -- | /O(min(n,W))/. Insert a new key\/value pair in the map.
383 -- If the key is already present in the map, the associated value is
384 -- replaced with the supplied value, i.e. 'insert' is equivalent to
385 -- @'insertWith' 'const'@.
386 --
387 -- > insert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
388 -- > insert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
389 -- > insert 5 'x' empty == singleton 5 'x'
390
391 insert :: Key -> a -> IntMap a -> IntMap a
392 insert !k !x t =
393 case t of
394 Bin p m l r
395 | nomatch k p m -> link k (Tip k x) p t
396 | zero k m -> Bin p m (insert k x l) r
397 | otherwise -> Bin p m l (insert k x r)
398 Tip ky _
399 | k==ky -> Tip k x
400 | otherwise -> link k (Tip k x) ky t
401 Nil -> Tip k x
402
403 -- right-biased insertion, used by 'union'
404 -- | /O(min(n,W))/. Insert with a combining function.
405 -- @'insertWith' f key value mp@
406 -- will insert the pair (key, value) into @mp@ if key does
407 -- not exist in the map. If the key does exist, the function will
408 -- insert @f new_value old_value@.
409 --
410 -- > insertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
411 -- > insertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
412 -- > insertWith (++) 5 "xxx" empty == singleton 5 "xxx"
413
414 insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
415 insertWith f k x t
416 = insertWithKey (\_ x' y' -> f x' y') k x t
417
418 -- | /O(min(n,W))/. Insert with a combining function.
419 -- @'insertWithKey' f key value mp@
420 -- will insert the pair (key, value) into @mp@ if key does
421 -- not exist in the map. If the key does exist, the function will
422 -- insert @f key new_value old_value@.
423 --
424 -- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
425 -- > insertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
426 -- > insertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
427 -- > insertWithKey f 5 "xxx" empty == singleton 5 "xxx"
428 --
429 -- If the key exists in the map, this function is lazy in @x@ but strict
430 -- in the result of @f@.
431
432 insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
433 insertWithKey f !k x t =
434 case t of
435 Bin p m l r
436 | nomatch k p m -> link k (singleton k x) p t
437 | zero k m -> Bin p m (insertWithKey f k x l) r
438 | otherwise -> Bin p m l (insertWithKey f k x r)
439 Tip ky y
440 | k==ky -> Tip k $! f k x y
441 | otherwise -> link k (singleton k x) ky t
442 Nil -> singleton k x
443
444 -- | /O(min(n,W))/. The expression (@'insertLookupWithKey' f k x map@)
445 -- is a pair where the first element is equal to (@'lookup' k map@)
446 -- and the second element equal to (@'insertWithKey' f k x map@).
447 --
448 -- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
449 -- > insertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
450 -- > insertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "xxx")])
451 -- > insertLookupWithKey f 5 "xxx" empty == (Nothing, singleton 5 "xxx")
452 --
453 -- This is how to define @insertLookup@ using @insertLookupWithKey@:
454 --
455 -- > let insertLookup kx x t = insertLookupWithKey (\_ a _ -> a) kx x t
456 -- > insertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
457 -- > insertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a"), (7, "x")])
458
459 insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)
460 insertLookupWithKey f0 !k0 x0 t0 = toPair $ go f0 k0 x0 t0
461 where
462 go f k x t =
463 case t of
464 Bin p m l r
465 | nomatch k p m -> Nothing :*: link k (singleton k x) p t
466 | zero k m -> let (found :*: l') = go f k x l in (found :*: Bin p m l' r)
467 | otherwise -> let (found :*: r') = go f k x r in (found :*: Bin p m l r')
468 Tip ky y
469 | k==ky -> (Just y :*: (Tip k $! f k x y))
470 | otherwise -> (Nothing :*: link k (singleton k x) ky t)
471 Nil -> Nothing :*: (singleton k x)
472
473
474 {--------------------------------------------------------------------
475 Deletion
476 --------------------------------------------------------------------}
477 -- | /O(min(n,W))/. Adjust a value at a specific key. When the key is not
478 -- a member of the map, the original map is returned.
479 --
480 -- > adjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
481 -- > adjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
482 -- > adjust ("new " ++) 7 empty == empty
483
484 adjust :: (a -> a) -> Key -> IntMap a -> IntMap a
485 adjust f k m
486 = adjustWithKey (\_ x -> f x) k m
487
488 -- | /O(min(n,W))/. Adjust a value at a specific key. When the key is not
489 -- a member of the map, the original map is returned.
490 --
491 -- > let f key x = (show key) ++ ":new " ++ x
492 -- > adjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
493 -- > adjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
494 -- > adjustWithKey f 7 empty == empty
495
496 adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap a
497 adjustWithKey f !k t =
498 case t of
499 Bin p m l r
500 | nomatch k p m -> t
501 | zero k m -> Bin p m (adjustWithKey f k l) r
502 | otherwise -> Bin p m l (adjustWithKey f k r)
503 Tip ky y
504 | k==ky -> Tip ky $! f k y
505 | otherwise -> t
506 Nil -> Nil
507
508 -- | /O(min(n,W))/. The expression (@'update' f k map@) updates the value @x@
509 -- at @k@ (if it is in the map). If (@f x@) is 'Nothing', the element is
510 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
511 --
512 -- > let f x = if x == "a" then Just "new a" else Nothing
513 -- > update f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
514 -- > update f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
515 -- > update f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
516
517 update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap a
518 update f
519 = updateWithKey (\_ x -> f x)
520
521 -- | /O(min(n,W))/. The expression (@'update' f k map@) updates the value @x@
522 -- at @k@ (if it is in the map). If (@f k x@) is 'Nothing', the element is
523 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
524 --
525 -- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
526 -- > updateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
527 -- > updateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
528 -- > updateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
529
530 updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
531 updateWithKey f !k t =
532 case t of
533 Bin p m l r
534 | nomatch k p m -> t
535 | zero k m -> binCheckLeft p m (updateWithKey f k l) r
536 | otherwise -> binCheckRight p m l (updateWithKey f k r)
537 Tip ky y
538 | k==ky -> case f k y of
539 Just !y' -> Tip ky y'
540 Nothing -> Nil
541 | otherwise -> t
542 Nil -> Nil
543
544 -- | /O(min(n,W))/. Lookup and update.
545 -- The function returns original value, if it is updated.
546 -- This is different behavior than 'Data.Map.updateLookupWithKey'.
547 -- Returns the original key value if the map entry is deleted.
548 --
549 -- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
550 -- > updateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:new a")])
551 -- > updateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing, fromList [(3, "b"), (5, "a")])
552 -- > updateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
553
554 updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a,IntMap a)
555 updateLookupWithKey f0 !k0 t0 = toPair $ go f0 k0 t0
556 where
557 go f k t =
558 case t of
559 Bin p m l r
560 | nomatch k p m -> (Nothing :*: t)
561 | zero k m -> let (found :*: l') = go f k l in (found :*: binCheckLeft p m l' r)
562 | otherwise -> let (found :*: r') = go f k r in (found :*: binCheckRight p m l r')
563 Tip ky y
564 | k==ky -> case f k y of
565 Just !y' -> (Just y :*: Tip ky y')
566 Nothing -> (Just y :*: Nil)
567 | otherwise -> (Nothing :*: t)
568 Nil -> (Nothing :*: Nil)
569
570
571
572 -- | /O(min(n,W))/. The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence thereof.
573 -- 'alter' can be used to insert, delete, or update a value in an 'IntMap'.
574 -- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@.
575 alter :: (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
576 alter f !k t =
577 case t of
578 Bin p m l r
579 | nomatch k p m -> case f Nothing of
580 Nothing -> t
581 Just !x -> link k (Tip k x) p t
582 | zero k m -> binCheckLeft p m (alter f k l) r
583 | otherwise -> binCheckRight p m l (alter f k r)
584 Tip ky y
585 | k==ky -> case f (Just y) of
586 Just !x -> Tip ky x
587 Nothing -> Nil
588 | otherwise -> case f Nothing of
589 Just !x -> link k (Tip k x) ky t
590 Nothing -> t
591 Nil -> case f Nothing of
592 Just !x -> Tip k x
593 Nothing -> Nil
594
595 -- | /O(log n)/. The expression (@'alterF' f k map@) alters the value @x@ at
596 -- @k@, or absence thereof. 'alterF' can be used to inspect, insert, delete,
597 -- or update a value in an 'IntMap'. In short : @'lookup' k <$> 'alterF' f k m = f
598 -- ('lookup' k m)@.
599 --
600 -- Example:
601 --
602 -- @
603 -- interactiveAlter :: Int -> IntMap String -> IO (IntMap String)
604 -- interactiveAlter k m = alterF f k m where
605 -- f Nothing -> do
606 -- putStrLn $ show k ++
607 -- " was not found in the map. Would you like to add it?"
608 -- getUserResponse1 :: IO (Maybe String)
609 -- f (Just old) -> do
610 -- putStrLn "The key is currently bound to " ++ show old ++
611 -- ". Would you like to change or delete it?"
612 -- getUserresponse2 :: IO (Maybe String)
613 -- @
614 --
615 -- 'alterF' is the most general operation for working with an individual
616 -- key that may or may not be in a given map.
617
618 -- Note: 'alterF' is a flipped version of the 'at' combinator from
619 -- 'Control.Lens.At'.
620 --
621 -- @since 0.5.8
622
623 alterF :: Functor f
624 => (Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)
625 -- This implementation was modified from 'Control.Lens.At'.
626 alterF f k m = (<$> f mv) $ \fres ->
627 case fres of
628 Nothing -> maybe m (const (delete k m)) mv
629 Just !v' -> insert k v' m
630 where mv = lookup k m
631
632
633 {--------------------------------------------------------------------
634 Union
635 --------------------------------------------------------------------}
636 -- | The union of a list of maps, with a combining operation.
637 --
638 -- > unionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
639 -- > == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
640
641 unionsWith :: (a->a->a) -> [IntMap a] -> IntMap a
642 unionsWith f ts
643 = foldlStrict (unionWith f) empty ts
644
645 -- | /O(n+m)/. The union with a combining function.
646 --
647 -- > unionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
648
649 unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
650 unionWith f m1 m2
651 = unionWithKey (\_ x y -> f x y) m1 m2
652
653 -- | /O(n+m)/. The union with a combining function.
654 --
655 -- > let f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
656 -- > unionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
657
658 unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
659 unionWithKey f m1 m2
660 = mergeWithKey' Bin (\(Tip k1 x1) (Tip _k2 x2) -> Tip k1 $! f k1 x1 x2) id id m1 m2
661
662 {--------------------------------------------------------------------
663 Difference
664 --------------------------------------------------------------------}
665
666 -- | /O(n+m)/. Difference with a combining function.
667 --
668 -- > let f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
669 -- > differenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
670 -- > == singleton 3 "b:B"
671
672 differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
673 differenceWith f m1 m2
674 = differenceWithKey (\_ x y -> f x y) m1 m2
675
676 -- | /O(n+m)/. Difference with a combining function. When two equal keys are
677 -- encountered, the combining function is applied to the key and both values.
678 -- If it returns 'Nothing', the element is discarded (proper set difference).
679 -- If it returns (@'Just' y@), the element is updated with a new value @y@.
680 --
681 -- > let f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
682 -- > differenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
683 -- > == singleton 3 "3:b|B"
684
685 differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
686 differenceWithKey f m1 m2
687 = mergeWithKey f id (const Nil) m1 m2
688
689 {--------------------------------------------------------------------
690 Intersection
691 --------------------------------------------------------------------}
692
693 -- | /O(n+m)/. The intersection with a combining function.
694 --
695 -- > intersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
696
697 intersectionWith :: (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
698 intersectionWith f m1 m2
699 = intersectionWithKey (\_ x y -> f x y) m1 m2
700
701 -- | /O(n+m)/. The intersection with a combining function.
702 --
703 -- > let f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
704 -- > intersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
705
706 intersectionWithKey :: (Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
707 intersectionWithKey f m1 m2
708 = mergeWithKey' bin (\(Tip k1 x1) (Tip _k2 x2) -> Tip k1 $! f k1 x1 x2) (const Nil) (const Nil) m1 m2
709
710 {--------------------------------------------------------------------
711 MergeWithKey
712 --------------------------------------------------------------------}
713
714 -- | /O(n+m)/. A high-performance universal combining function. Using
715 -- 'mergeWithKey', all combining functions can be defined without any loss of
716 -- efficiency (with exception of 'union', 'difference' and 'intersection',
717 -- where sharing of some nodes is lost with 'mergeWithKey').
718 --
719 -- Please make sure you know what is going on when using 'mergeWithKey',
720 -- otherwise you can be surprised by unexpected code growth or even
721 -- corruption of the data structure.
722 --
723 -- When 'mergeWithKey' is given three arguments, it is inlined to the call
724 -- site. You should therefore use 'mergeWithKey' only to define your custom
725 -- combining functions. For example, you could define 'unionWithKey',
726 -- 'differenceWithKey' and 'intersectionWithKey' as
727 --
728 -- > myUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -> Just (f k x1 x2)) id id m1 m2
729 -- > myDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
730 -- > myIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -> Just (f k x1 x2)) (const empty) (const empty) m1 m2
731 --
732 -- When calling @'mergeWithKey' combine only1 only2@, a function combining two
733 -- 'IntMap's is created, such that
734 --
735 -- * if a key is present in both maps, it is passed with both corresponding
736 -- values to the @combine@ function. Depending on the result, the key is either
737 -- present in the result with specified value, or is left out;
738 --
739 -- * a nonempty subtree present only in the first map is passed to @only1@ and
740 -- the output is added to the result;
741 --
742 -- * a nonempty subtree present only in the second map is passed to @only2@ and
743 -- the output is added to the result.
744 --
745 -- The @only1@ and @only2@ methods /must return a map with a subset (possibly empty) of the keys of the given map/.
746 -- The values can be modified arbitrarily. Most common variants of @only1@ and
747 -- @only2@ are 'id' and @'const' 'empty'@, but for example @'map' f@ or
748 -- @'filterWithKey' f@ could be used for any @f@.
749
750 mergeWithKey :: (Key -> a -> b -> Maybe c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c)
751 -> IntMap a -> IntMap b -> IntMap c
752 mergeWithKey f g1 g2 = mergeWithKey' bin combine g1 g2
753 where -- We use the lambda form to avoid non-exhaustive pattern matches warning.
754 combine = \(Tip k1 x1) (Tip _k2 x2) -> case f k1 x1 x2 of Nothing -> Nil
755 Just !x -> Tip k1 x
756 {-# INLINE combine #-}
757 {-# INLINE mergeWithKey #-}
758
759 {--------------------------------------------------------------------
760 Min\/Max
761 --------------------------------------------------------------------}
762
763 -- | /O(log n)/. Update the value at the minimal key.
764 --
765 -- > updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
766 -- > updateMinWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
767
768 updateMinWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a
769 updateMinWithKey f t =
770 case t of Bin p m l r | m < 0 -> binCheckRight p m l (go f r)
771 _ -> go f t
772 where
773 go f' (Bin p m l r) = binCheckLeft p m (go f' l) r
774 go f' (Tip k y) = case f' k y of
775 Just !y' -> Tip k y'
776 Nothing -> Nil
777 go _ Nil = error "updateMinWithKey Nil"
778
779 -- | /O(log n)/. Update the value at the maximal key.
780 --
781 -- > updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
782 -- > updateMaxWithKey (\ _ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
783
784 updateMaxWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a
785 updateMaxWithKey f t =
786 case t of Bin p m l r | m < 0 -> binCheckLeft p m (go f l) r
787 _ -> go f t
788 where
789 go f' (Bin p m l r) = binCheckRight p m l (go f' r)
790 go f' (Tip k y) = case f' k y of
791 Just !y' -> Tip k y'
792 Nothing -> Nil
793 go _ Nil = error "updateMaxWithKey Nil"
794
795 -- | /O(log n)/. Update the value at the maximal key.
796 --
797 -- > updateMax (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
798 -- > updateMax (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
799
800 updateMax :: (a -> Maybe a) -> IntMap a -> IntMap a
801 updateMax f = updateMaxWithKey (const f)
802
803 -- | /O(log n)/. Update the value at the minimal key.
804 --
805 -- > updateMin (\ a -> Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
806 -- > updateMin (\ _ -> Nothing) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
807
808 updateMin :: (a -> Maybe a) -> IntMap a -> IntMap a
809 updateMin f = updateMinWithKey (const f)
810
811
812 {--------------------------------------------------------------------
813 Mapping
814 --------------------------------------------------------------------}
815 -- | /O(n)/. Map a function over all values in the map.
816 --
817 -- > map (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
818
819 map :: (a -> b) -> IntMap a -> IntMap b
820 map f = go
821 where
822 go (Bin p m l r) = Bin p m (go l) (go r)
823 go (Tip k x) = Tip k $! f x
824 go Nil = Nil
825
826 #ifdef __GLASGOW_HASKELL__
827 {-# NOINLINE [1] map #-}
828 {-# RULES
829 "map/map" forall f g xs . map f (map g xs) = map (\x -> f $! g x) xs
830 "map/mapL" forall f g xs . map f (L.map g xs) = map (\x -> f (g x)) xs
831 #-}
832 #endif
833
834 -- | /O(n)/. Map a function over all values in the map.
835 --
836 -- > let f key x = (show key) ++ ":" ++ x
837 -- > mapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
838
839 mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b
840 mapWithKey f t
841 = case t of
842 Bin p m l r -> Bin p m (mapWithKey f l) (mapWithKey f r)
843 Tip k x -> Tip k $! f k x
844 Nil -> Nil
845
846 #ifdef __GLASGOW_HASKELL__
847 -- Pay close attention to strictness here. We need to force the
848 -- intermediate result for map f . map g, and we need to refrain
849 -- from forcing it for map f . L.map g, etc.
850 --
851 -- TODO Consider moving map and mapWithKey to IntMap.Internal so we can write
852 -- non-orphan RULES for things like L.map f (map g xs). We'd need a new function
853 -- for this, and we'd have to pay attention to simplifier phases. Something like
854 --
855 -- lsmap :: (b -> c) -> (a -> b) -> IntMap a -> IntMap c
856 -- lsmap _ _ Nil = Nil
857 -- lsmap f g (Tip k x) = let !gx = g x in Tip k (f gx)
858 -- lsmap f g (Bin p m l r) = Bin p m (lsmap f g l) (lsmap f g r)
859 {-# NOINLINE [1] mapWithKey #-}
860 {-# RULES
861 "mapWithKey/mapWithKey" forall f g xs . mapWithKey f (mapWithKey g xs) =
862 mapWithKey (\k a -> f k $! g k a) xs
863 "mapWithKey/mapWithKeyL" forall f g xs . mapWithKey f (L.mapWithKey g xs) =
864 mapWithKey (\k a -> f k (g k a)) xs
865 "mapWithKey/map" forall f g xs . mapWithKey f (map g xs) =
866 mapWithKey (\k a -> f k $! g a) xs
867 "mapWithKey/mapL" forall f g xs . mapWithKey f (L.map g xs) =
868 mapWithKey (\k a -> f k (g a)) xs
869 "map/mapWithKey" forall f g xs . map f (mapWithKey g xs) =
870 mapWithKey (\k a -> f $! g k a) xs
871 "map/mapWithKeyL" forall f g xs . map f (L.mapWithKey g xs) =
872 mapWithKey (\k a -> f (g k a)) xs
873 #-}
874 #endif
875
876 -- | /O(n)/.
877 -- @'traverseWithKey' f s == 'fromList' <$> 'traverse' (\(k, v) -> (,) k <$> f k v) ('toList' m)@
878 -- That is, behaves exactly like a regular 'traverse' except that the traversing
879 -- function also has access to the key associated with a value.
880 --
881 -- > traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
882 -- > traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')]) == Nothing
883 traverseWithKey :: Applicative t => (Key -> a -> t b) -> IntMap a -> t (IntMap b)
884 traverseWithKey f = go
885 where
886 go Nil = pure Nil
887 go (Tip k v) = (\ !v' -> Tip k v') <$> f k v
888 go (Bin p m l r) = liftA2 (Bin p m) (go l) (go r)
889 {-# INLINE traverseWithKey #-}
890
891 -- | /O(n)/. The function @'mapAccum'@ threads an accumulating
892 -- argument through the map in ascending order of keys.
893 --
894 -- > let f a b = (a ++ b, b ++ "X")
895 -- > mapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
896
897 mapAccum :: (a -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
898 mapAccum f = mapAccumWithKey (\a' _ x -> f a' x)
899
900 -- | /O(n)/. The function @'mapAccumWithKey'@ threads an accumulating
901 -- argument through the map in ascending order of keys.
902 --
903 -- > let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
904 -- > mapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
905
906 mapAccumWithKey :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
907 mapAccumWithKey f a t
908 = mapAccumL f a t
909
910 -- | /O(n)/. The function @'mapAccumL'@ threads an accumulating
911 -- argument through the map in ascending order of keys. Strict in
912 -- the accumulating argument and the both elements of the
913 -- result of the function.
914 mapAccumL :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
915 mapAccumL f0 a0 t0 = toPair $ go f0 a0 t0
916 where
917 go f a t
918 = case t of
919 Bin p m l r -> let (a1 :*: l') = go f a l
920 (a2 :*: r') = go f a1 r
921 in (a2 :*: Bin p m l' r')
922 Tip k x -> let !(a',!x') = f a k x in (a' :*: Tip k x')
923 Nil -> (a :*: Nil)
924
925 -- | /O(n)/. The function @'mapAccumR'@ threads an accumulating
926 -- argument through the map in descending order of keys.
927 mapAccumRWithKey :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
928 mapAccumRWithKey f0 a0 t0 = toPair $ go f0 a0 t0
929 where
930 go f a t
931 = case t of
932 Bin p m l r -> let (a1 :*: r') = go f a r
933 (a2 :*: l') = go f a1 l
934 in (a2 :*: Bin p m l' r')
935 Tip k x -> let !(a',!x') = f a k x in (a' :*: Tip k x')
936 Nil -> (a :*: Nil)
937
938 -- | /O(n*log n)/.
939 -- @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@.
940 --
941 -- The size of the result may be smaller if @f@ maps two or more distinct
942 -- keys to the same new key. In this case the associated values will be
943 -- combined using @c@.
944 --
945 -- > mapKeysWith (++) (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
946 -- > mapKeysWith (++) (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
947
948 mapKeysWith :: (a -> a -> a) -> (Key->Key) -> IntMap a -> IntMap a
949 mapKeysWith c f = fromListWith c . foldrWithKey (\k x xs -> (f k, x) : xs) []
950
951 {--------------------------------------------------------------------
952 Filter
953 --------------------------------------------------------------------}
954 -- | /O(n)/. Map values and collect the 'Just' results.
955 --
956 -- > let f x = if x == "a" then Just "new a" else Nothing
957 -- > mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
958
959 mapMaybe :: (a -> Maybe b) -> IntMap a -> IntMap b
960 mapMaybe f = mapMaybeWithKey (\_ x -> f x)
961
962 -- | /O(n)/. Map keys\/values and collect the 'Just' results.
963 --
964 -- > let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing
965 -- > mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
966
967 mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap b
968 mapMaybeWithKey f (Bin p m l r)
969 = bin p m (mapMaybeWithKey f l) (mapMaybeWithKey f r)
970 mapMaybeWithKey f (Tip k x) = case f k x of
971 Just !y -> Tip k y
972 Nothing -> Nil
973 mapMaybeWithKey _ Nil = Nil
974
975 -- | /O(n)/. Map values and separate the 'Left' and 'Right' results.
976 --
977 -- > let f a = if a < "c" then Left a else Right a
978 -- > mapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
979 -- > == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
980 -- >
981 -- > mapEither (\ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
982 -- > == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
983
984 mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
985 mapEither f m
986 = mapEitherWithKey (\_ x -> f x) m
987
988 -- | /O(n)/. Map keys\/values and separate the 'Left' and 'Right' results.
989 --
990 -- > let f k a = if k < 5 then Left (k * 2) else Right (a ++ a)
991 -- > mapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
992 -- > == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
993 -- >
994 -- > mapEitherWithKey (\_ a -> Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
995 -- > == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
996
997 mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
998 mapEitherWithKey f0 t0 = toPair $ go f0 t0
999 where
1000 go f (Bin p m l r)
1001 = bin p m l1 r1 :*: bin p m l2 r2
1002 where
1003 (l1 :*: l2) = go f l
1004 (r1 :*: r2) = go f r
1005 go f (Tip k x) = case f k x of
1006 Left !y -> (Tip k y :*: Nil)
1007 Right !z -> (Nil :*: Tip k z)
1008 go _ Nil = (Nil :*: Nil)
1009
1010 {--------------------------------------------------------------------
1011 Conversions
1012 --------------------------------------------------------------------}
1013
1014 -- | /O(n)/. Build a map from a set of keys and a function which for each key
1015 -- computes its value.
1016 --
1017 -- > fromSet (\k -> replicate k 'a') (Data.IntSet.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
1018 -- > fromSet undefined Data.IntSet.empty == empty
1019
1020 fromSet :: (Key -> a) -> IntSet.IntSet -> IntMap a
1021 fromSet _ IntSet.Nil = Nil
1022 fromSet f (IntSet.Bin p m l r) = Bin p m (fromSet f l) (fromSet f r)
1023 fromSet f (IntSet.Tip kx bm) = buildTree f kx bm (IntSet.suffixBitMask + 1)
1024 where -- This is slightly complicated, as we to convert the dense
1025 -- representation of IntSet into tree representation of IntMap.
1026 --
1027 -- We are given a nonzero bit mask 'bmask' of 'bits' bits with prefix 'prefix'.
1028 -- We split bmask into halves corresponding to left and right subtree.
1029 -- If they are both nonempty, we create a Bin node, otherwise exactly
1030 -- one of them is nonempty and we construct the IntMap from that half.
1031 buildTree g !prefix !bmask bits = case bits of
1032 0 -> Tip prefix $! g prefix
1033 _ -> case intFromNat ((natFromInt bits) `shiftRL` 1) of
1034 bits2 | bmask .&. ((1 `shiftLL` bits2) - 1) == 0 ->
1035 buildTree g (prefix + bits2) (bmask `shiftRL` bits2) bits2
1036 | (bmask `shiftRL` bits2) .&. ((1 `shiftLL` bits2) - 1) == 0 ->
1037 buildTree g prefix bmask bits2
1038 | otherwise ->
1039 Bin prefix bits2 (buildTree g prefix bmask bits2) (buildTree g (prefix + bits2) (bmask `shiftRL` bits2) bits2)
1040
1041 {--------------------------------------------------------------------
1042 Lists
1043 --------------------------------------------------------------------}
1044 -- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs.
1045 --
1046 -- > fromList [] == empty
1047 -- > fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
1048 -- > fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
1049
1050 fromList :: [(Key,a)] -> IntMap a
1051 fromList xs
1052 = foldlStrict ins empty xs
1053 where
1054 ins t (k,x) = insert k x t
1055
1056 -- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'.
1057 --
1058 -- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
1059 -- > fromListWith (++) [] == empty
1060
1061 fromListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a
1062 fromListWith f xs
1063 = fromListWithKey (\_ x y -> f x y) xs
1064
1065 -- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs with a combining function. See also fromAscListWithKey'.
1066 --
1067 -- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
1068 -- > fromListWith (++) [] == empty
1069
1070 fromListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
1071 fromListWithKey f xs
1072 = foldlStrict ins empty xs
1073 where
1074 ins t (k,x) = insertWithKey f k x t
1075
1076 -- | /O(n)/. Build a map from a list of key\/value pairs where
1077 -- the keys are in ascending order.
1078 --
1079 -- > fromAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
1080 -- > fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
1081
1082 fromAscList :: [(Key,a)] -> IntMap a
1083 fromAscList xs
1084 = fromAscListWithKey (\_ x _ -> x) xs
1085
1086 -- | /O(n)/. Build a map from a list of key\/value pairs where
1087 -- the keys are in ascending order, with a combining function on equal keys.
1088 -- /The precondition (input list is ascending) is not checked./
1089 --
1090 -- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
1091
1092 fromAscListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a
1093 fromAscListWith f xs
1094 = fromAscListWithKey (\_ x y -> f x y) xs
1095
1096 -- | /O(n)/. Build a map from a list of key\/value pairs where
1097 -- the keys are in ascending order, with a combining function on equal keys.
1098 -- /The precondition (input list is ascending) is not checked./
1099 --
1100 -- > fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
1101
1102 fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
1103 fromAscListWithKey _ [] = Nil
1104 fromAscListWithKey f (x0 : xs0) = fromDistinctAscList (combineEq x0 xs0)
1105 where
1106 -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
1107 combineEq z [] = [z]
1108 combineEq z@(kz,zz) (x@(kx,xx):xs)
1109 | kx==kz = let !yy = f kx xx zz in combineEq (kx,yy) xs
1110 | otherwise = z:combineEq x xs
1111
1112 -- | /O(n)/. Build a map from a list of key\/value pairs where
1113 -- the keys are in ascending order and all distinct.
1114 -- /The precondition (input list is strictly ascending) is not checked./
1115 --
1116 -- > fromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
1117
1118 fromDistinctAscList :: [(Key,a)] -> IntMap a
1119 fromDistinctAscList [] = Nil
1120 fromDistinctAscList (z0 : zs0) = work z0 zs0 Nada
1121 where
1122 work (kx,!vx) [] stk = finish kx (Tip kx vx) stk
1123 work (kx,!vx) (z@(kz,_):zs) stk = reduce z zs (branchMask kx kz) kx (Tip kx vx) stk
1124
1125 reduce :: (Key,a) -> [(Key,a)] -> Mask -> Prefix -> IntMap a -> Stack a -> IntMap a
1126 reduce z zs _ px tx Nada = work z zs (Push px tx Nada)
1127 reduce z zs m px tx stk@(Push py ty stk') =
1128 let mxy = branchMask px py
1129 pxy = mask px mxy
1130 in if shorter m mxy
1131 then reduce z zs m pxy (Bin pxy mxy ty tx) stk'
1132 else work z zs (Push px tx stk)
1133
1134 finish _ t Nada = t
1135 finish px tx (Push py ty stk) = finish p (link py ty px tx) stk
1136 where m = branchMask px py
1137 p = mask px m
1138
1139 data Stack a = Push {-# UNPACK #-} !Prefix !(IntMap a) !(Stack a) | Nada