Commit f072317c authored by David Feuer's avatar David Feuer Committed by GitHub
Browse files

Merge pull request #421 from mikeplus64/master

faster {IntMap,IntSet} size functions
parents f42e9321 300af250
......@@ -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?
--
......
......@@ -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?
......
......@@ -142,6 +142,7 @@ main = defaultMain
, testProperty "alter" prop_alter
, testProperty "index" prop_index
, testProperty "null" prop_null
, testProperty "size" prop_size
, testProperty "member" prop_member
, testProperty "notmember" prop_notmember
, testProperty "lookup" prop_lookup
......@@ -906,6 +907,11 @@ prop_index xs = length xs > 0 ==>
prop_null :: IMap -> Bool
prop_null m = null m == (size m == 0)
prop_size :: UMap -> Property
prop_size im = sz === foldl' (\i _ -> i + 1) (0 :: Int) im .&&.
sz === List.length (toList im)
where sz = size im
prop_member :: [Int] -> Int -> Bool
prop_member xs n =
let m = fromList (zip xs xs)
......
......@@ -261,8 +261,10 @@ prop_isSubsetOf a b = isSubsetOf a b == Set.isSubsetOf (toSet a) (toSet b)
prop_isSubsetOf2 :: IntSet -> IntSet -> Bool
prop_isSubsetOf2 a b = isSubsetOf a (union a b)
prop_size :: IntSet -> Bool
prop_size s = size s == List.length (toList s)
prop_size :: IntSet -> Property
prop_size s = sz === foldl' (\i _ -> i + 1) (0 :: Int) s .&&.
sz === List.length (toList s)
where sz = size s
prop_findMax :: IntSet -> Property
prop_findMax s = not (null s) ==> findMax s == maximum (toList s)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment