[commit: packages/containers] master: faster IntSet.size (492e7fd)

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


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

On branch  : master
Link       : http://git.haskell.org/packages/containers.git/commitdiff/492e7fd0b7b7cbe7a01075f75678e0449fcc3f95

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

commit 492e7fd0b7b7cbe7a01075f75678e0449fcc3f95
Author: Mike Ledger <mike at quasimal.com>
Date:   Wed Mar 29 15:35:47 2017 +1100

    faster IntSet.size


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

492e7fd0b7b7cbe7a01075f75678e0449fcc3f95
 Data/IntSet/Internal.hs | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/Data/IntSet/Internal.hs b/Data/IntSet/Internal.hs
index f6a1296..7eece21 100644
--- a/Data/IntSet/Internal.hs
+++ b/Data/IntSet/Internal.hs
@@ -311,9 +311,11 @@ null _   = False
 
 -- | /O(n)/. Cardinality of the set.
 size :: IntSet -> Int
-size (Bin _ _ l r) = size l + size r
-size (Tip _ bm) = bitcount 0 bm
-size Nil = 0
+size = go 0
+  where
+    go !acc (Bin _ _ l r) = go (go acc l) r
+    go acc (Tip _ bm) = acc + bitcount 0 bm
+    go acc Nil = acc
 
 -- | /O(min(n,W))/. Is the value a member of the set?
 



More information about the ghc-commits mailing list