Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement keys

  • Loading branch information...
commit 48438b48ca9bc5d96c1987afe7acdf4dada823f3 1 parent ae1bb88
Bryan O'Sullivan authored
10 Data/CritBit/Tree.hs
View
@@ -88,7 +88,7 @@ module Data.CritBit.Tree
-- * Conversion
-- , elems
- -- , keys
+ , keys
-- , assocs
-- , keysSet
-- , fromSet
@@ -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 #-}
1  benchmarks/Benchmarks.hs
View
@@ -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" [
4 tests/Properties.hs
View
@@ -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
@@ -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]
Please sign in to comment.
Something went wrong with that request. Please try again.