[commit: packages/containers] master: faster IntMap.size (9be1604)

git at git.haskell.org git at git.haskell.org
Mon Apr 17 21:49:04 UTC 2017


Repository : ssh://git@git.haskell.org/containers

On branch  : master
Link       : http://git.haskell.org/packages/containers.git/commitdiff/9be1604fd2b4cd146de02962c9b94baa80e99d15

>---------------------------------------------------------------

commit 9be1604fd2b4cd146de02962c9b94baa80e99d15
Author: Mike Ledger <mike at quasimal.com>
Date:   Wed Mar 29 15:34:20 2017 +1100

    faster IntMap.size


>---------------------------------------------------------------

9be1604fd2b4cd146de02962c9b94baa80e99d15
 Data/IntMap/Internal.hs | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/Data/IntMap/Internal.hs b/Data/IntMap/Internal.hs
index 9271ba1..c7d1180 100644
--- a/Data/IntMap/Internal.hs
+++ b/Data/IntMap/Internal.hs
@@ -512,9 +512,11 @@ null _   = False
 -- > size (singleton 1 'a')                       == 1
 -- > size (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
 size :: IntMap a -> Int
-size (Bin _ _ l r) = size l + size r
-size (Tip _ _) = 1
-size Nil = 0
+size = go 0
+  where
+    go !acc (Bin _ _ l r) = go (go acc l) r
+    go acc (Tip _ _) = 1 + acc
+    go acc Nil = acc
 
 -- | /O(min(n,W))/. Is the key a member of the map?
 --



More information about the ghc-commits mailing list