Ticket #5242: 0006-Speed-up-keysSet-and-fromSet-now-that-IntMap-can-acc.patch

File 0006-Speed-up-keysSet-and-fromSet-now-that-IntMap-can-acc.patch, 1.7 KB (added by liyang, 4 years ago)
  • Data/IntMap.hs

    From e43eea570a0d56b7817e6446cb063c182d9e175e Mon Sep 17 00:00:00 2001
    From: Liyang HU <[email protected]>
    Date: Sun, 12 Jun 2011 23:03:30 +0900
    Subject: [PATCH] Speed up keysSet and fromSet, now that IntMap can access IntSet internals.
    
    ---
     Data/IntMap.hs |   20 ++++++++++++++++----
     1 files changed, 16 insertions(+), 4 deletions(-)
    
    diff --git a/Data/IntMap.hs b/Data/IntMap.hs
    index 845c5c5..8d221cd 100644
    a b keys :: IntMap a -> [Key] 
    14621462keys
    14631463  = foldWithKey (\k _ ks -> k:ks) []
    14641464
    1465 -- | /O(n*min(n,W))/. The set of all keys of the map.
     1465-- | /O(n)/. The set of all keys of the map.
    14661466--
    14671467-- > keysSet (fromList [(5,"a"), (3,"b")]) == Data.IntSet.fromList [3,5]
    14681468-- > keysSet empty == Data.IntSet.empty
    14691469
    14701470keysSet :: IntMap a -> IntSet.IntSet
    1471 keysSet m = IntSet.fromDistinctAscList (keys m)
     1471keysSet m = case m of
     1472    Nil -> IntSet.Nil
     1473    Tip k _v -> IntSet.Tip k
     1474    Bin p m l r -> IntSet.Bin p m (keysSet l) (keysSet r)
    14721475
    14731476-- | /O(n)/. Build an IntMap from an IntSet, using the supplied function to
    14741477-- generate the values.
    1475 fromSet :: (Key -> a) -> IntSet.IntSet -> IntMap a
    1476 fromSet f = fromAscList . fmap (\ k -> (k, f k)) . IntSet.toAscList
     1478--
     1479-- > fromSet (join (*)) (Data.IntSet.fromList [3,5]) == fromMap [(3,9), (5,25)]
     1480-- > fromSet f Data.IntSet.empty = empty
     1481
     1482fromSet :: forall a. (Key -> a) -> IntSet.IntSet -> IntMap a
     1483fromSet f = go where
     1484    go :: IntSet.IntSet -> IntMap a
     1485    go s = case s of
     1486        IntSet.Nil -> Nil
     1487        IntSet.Tip k -> Tip k (f k)
     1488        IntSet.Bin p m l r -> Bin p m (go l) (go r)
    14771489
    14781490-- | /O(n)/. Return all key\/value pairs in the map in ascending key order.
    14791491--