Skip to content

Commit

Permalink
Implement keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed May 9, 2013
1 parent ae1bb88 commit 48438b4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Data/CritBit/Tree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module Data.CritBit.Tree

-- * Conversion
-- , elems
-- , keys
, keys
-- , assocs
-- , keysSet
-- , fromSet
Expand Down Expand Up @@ -500,6 +500,14 @@ foldrWithKeyWith maybeSeq f z0 (CritBit root) = go root z0
go Empty z = z
{-# INLINE foldrWithKeyWith #-}

-- | /O(n)/. Return all keys of the map in ascending order.
--
-- > keys (fromList [("b",5), ("a",3)]) == ["a","b"]
-- > keys empty == []
keys :: CritBit k v -> [k]
keys m = foldrWithKey f [] m
where f k _ ks = k : ks

unionL :: (CritBitKey k) => CritBit k v -> CritBit k v -> CritBit k v
unionL a b = foldlWithKey' (\m k v -> insert k v m) b a
{-# INLINABLE unionL #-}
Expand Down
1 change: 1 addition & 0 deletions benchmarks/Benchmarks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ main = do
(H.foldlWithKey' f 0) id
, bgroup "foldl'" $ function whnf (C.foldl' (+) 0) (Map.foldl' (+) 0)
(H.foldl' (+) 0) id
, bgroup "keys" $ function nf C.keys Map.keys H.keys Trie.keys
, bgroup "union" $ twoMaps C.unionR Map.union H.union Trie.unionR
]
, bgroup "text" [
Expand Down
4 changes: 4 additions & 0 deletions tests/Properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ t_foldlWithKey' _ (KV kvs) =
where
f (l,s) k v = (k:l,s+v)

t_keys :: (CritBitKey k, Ord k) => k -> KV k -> Bool
t_keys _ (KV kvs) = C.keys (C.fromList kvs) == Map.keys (Map.fromList kvs)

propertiesFor :: (Arbitrary k, CritBitKey k, Ord k, Show k) => k -> [Test]
propertiesFor t = [
testProperty "t_fromList_toList" $ t_fromList_toList t
Expand All @@ -118,6 +121,7 @@ propertiesFor t = [
, testProperty "t_foldlWithKey" $ t_foldlWithKey t
, testProperty "t_foldl'" $ t_foldl' t
, testProperty "t_foldlWithKey'" $ t_foldlWithKey' t
, testProperty "t_keys" $ t_keys t
]

properties :: [Test]
Expand Down

0 comments on commit 48438b4

Please sign in to comment.