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] 
1462  1462  keys 
1463  1463  = foldWithKey (\k _ ks > k:ks) [] 
1464  1464  
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. 
1466  1466   
1467  1467   > keysSet (fromList [(5,"a"), (3,"b")]) == Data.IntSet.fromList [3,5] 
1468  1468   > keysSet empty == Data.IntSet.empty 
1469  1469  
1470  1470  keysSet :: IntMap a > IntSet.IntSet 
1471   keysSet m = IntSet.fromDistinctAscList (keys m) 
 1471  keysSet 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) 
1472  1475  
1473  1476    /O(n)/. Build an IntMap from an IntSet, using the supplied function to 
1474  1477   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  
 1482  fromSet :: forall a. (Key > a) > IntSet.IntSet > IntMap a 
 1483  fromSet 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) 
1477  1489  
1478  1490    /O(n)/. Return all key\/value pairs in the map in ascending key order. 
1479  1491   