Skip to content

Commit 5e6e5f9

Browse files
committed
Add keysSet
* Add `keysSet` * Rearrange modules to make things work.
1 parent 596fb69 commit 5e6e5f9

File tree

7 files changed

+200
-10
lines changed

7 files changed

+200
-10
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Add `HashMap.alterF`.
44

5+
* Add `HashMap.keysSet`.
6+
57
## 0.2.9.0
68

79
* Add `Ord/Ord1/Ord2` instances. (Thanks, Oleg Grenrus)

Data/HashMap/Lazy.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ module Data.HashMap.Lazy
3434
, singleton
3535

3636
-- * Basic interface
37-
, HM.null
37+
, null
3838
, size
3939
, member
40-
, HM.lookup
40+
, lookup
4141
, lookupDefault
4242
, (!)
4343
, insert
@@ -56,7 +56,7 @@ module Data.HashMap.Lazy
5656
, unions
5757

5858
-- * Transformations
59-
, HM.map
59+
, map
6060
, mapWithKey
6161
, traverseWithKey
6262

@@ -70,11 +70,11 @@ module Data.HashMap.Lazy
7070
-- * Folds
7171
, foldl'
7272
, foldlWithKey'
73-
, HM.foldr
73+
, foldr
7474
, foldrWithKey
7575

7676
-- * Filter
77-
, HM.filter
77+
, filter
7878
, filterWithKey
7979
, mapMaybe
8080
, mapMaybeWithKey
@@ -87,9 +87,14 @@ module Data.HashMap.Lazy
8787
, toList
8888
, fromList
8989
, fromListWith
90+
91+
-- ** HashSets
92+
, HS.keysSet
9093
) where
9194

9295
import Data.HashMap.Base as HM
96+
import qualified Data.HashSet.Base as HS
97+
import Prelude ()
9398

9499
-- $strictness
95100
--

Data/HashMap/Strict.hs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{-# LANGUAGE Safe #-}
2+
3+
------------------------------------------------------------------------
4+
-- |
5+
-- Module : Data.HashMap.Strict
6+
-- Copyright : 2010-2012 Johan Tibell
7+
-- License : BSD-style
8+
-- Maintainer : [email protected]
9+
-- Stability : provisional
10+
-- Portability : portable
11+
--
12+
-- A map from /hashable/ keys to values. A map cannot contain
13+
-- duplicate keys; each key can map to at most one value. A 'HashMap'
14+
-- makes no guarantees as to the order of its elements.
15+
--
16+
-- The implementation is based on /hash array mapped tries/. A
17+
-- 'HashMap' is often faster than other tree-based set types,
18+
-- especially when key comparison is expensive, as in the case of
19+
-- strings.
20+
--
21+
-- Many operations have a average-case complexity of /O(log n)/. The
22+
-- implementation uses a large base (i.e. 16) so in practice these
23+
-- operations are constant time.
24+
module Data.HashMap.Strict
25+
(
26+
-- * Strictness properties
27+
-- $strictness
28+
29+
HashMap
30+
31+
-- * Construction
32+
, empty
33+
, singleton
34+
35+
-- * Basic interface
36+
, null
37+
, size
38+
, member
39+
, lookup
40+
, lookupDefault
41+
, (!)
42+
, insert
43+
, insertWith
44+
, delete
45+
, adjust
46+
, update
47+
, alter
48+
, alterF
49+
50+
-- * Combine
51+
-- ** Union
52+
, union
53+
, unionWith
54+
, unionWithKey
55+
, unions
56+
57+
-- * Transformations
58+
, map
59+
, mapWithKey
60+
, traverseWithKey
61+
62+
-- * Difference and intersection
63+
, difference
64+
, differenceWith
65+
, intersection
66+
, intersectionWith
67+
, intersectionWithKey
68+
69+
-- * Folds
70+
, foldl'
71+
, foldlWithKey'
72+
, foldr
73+
, foldrWithKey
74+
75+
-- * Filter
76+
, filter
77+
, filterWithKey
78+
, mapMaybe
79+
, mapMaybeWithKey
80+
81+
-- * Conversions
82+
, keys
83+
, elems
84+
85+
-- ** Lists
86+
, toList
87+
, fromList
88+
, fromListWith
89+
90+
-- ** HashSets
91+
, HS.keysSet
92+
) where
93+
94+
import Data.HashMap.Strict.Base as HM
95+
import qualified Data.HashSet.Base as HS
96+
import Prelude ()

Data/HashMap/Strict/Base.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
-- Many operations have a average-case complexity of /O(log n)/. The
2424
-- implementation uses a large base (i.e. 16) so in practice these
2525
-- operations are constant time.
26-
module Data.HashMap.Strict
26+
module Data.HashMap.Strict.Base
2727
(
2828
-- * Strictness properties
2929
-- $strictness

Data/HashSet.hs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{-# LANGUAGE CPP #-}
2+
#if __GLASGOW_HASKELL__ >= 702
3+
{-# LANGUAGE Safe #-}
4+
#endif
5+
6+
------------------------------------------------------------------------
7+
-- |
8+
-- Module : Data.HashSet
9+
-- Copyright : 2011 Bryan O'Sullivan
10+
-- License : BSD-style
11+
-- Maintainer : [email protected]
12+
-- Stability : provisional
13+
-- Portability : portable
14+
--
15+
-- A set of /hashable/ values. A set cannot contain duplicate items.
16+
-- A 'HashSet' makes no guarantees as to the order of its elements.
17+
--
18+
-- The implementation is based on /hash array mapped trie/. A
19+
-- 'HashSet' is often faster than other tree-based set types,
20+
-- especially when value comparison is expensive, as in the case of
21+
-- strings.
22+
--
23+
-- Many operations have a average-case complexity of /O(log n)/. The
24+
-- implementation uses a large base (i.e. 16) so in practice these
25+
-- operations are constant time.
26+
27+
module Data.HashSet
28+
(
29+
HashSet
30+
31+
-- * Construction
32+
, empty
33+
, singleton
34+
35+
-- * Combine
36+
, union
37+
, unions
38+
39+
-- * Basic interface
40+
, null
41+
, size
42+
, member
43+
, insert
44+
, delete
45+
46+
-- * Transformations
47+
, map
48+
49+
-- * Difference and intersection
50+
, difference
51+
, intersection
52+
53+
-- * Folds
54+
, foldl'
55+
, foldr
56+
57+
-- * Filter
58+
, filter
59+
60+
-- * Conversions
61+
62+
-- ** Lists
63+
, toList
64+
, fromList
65+
66+
-- * HashMaps
67+
, toMap
68+
, fromMap
69+
) where
70+
71+
import Data.HashSet.Base
72+
import Prelude ()

Data/HashSet/Base.hs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
------------------------------------------------------------------------
1111
-- |
12-
-- Module : Data.HashSet
12+
-- Module : Data.HashSet.Base
1313
-- Copyright : 2011 Bryan O'Sullivan
1414
-- License : BSD-style
1515
-- Maintainer : [email protected]
@@ -28,7 +28,7 @@
2828
-- implementation uses a large base (i.e. 16) so in practice these
2929
-- operations are constant time.
3030

31-
module Data.HashSet
31+
module Data.HashSet.Base
3232
(
3333
HashSet
3434

@@ -70,6 +70,9 @@ module Data.HashSet
7070
-- * HashMaps
7171
, toMap
7272
, fromMap
73+
74+
-- Exported from Data.HashMap.{Strict, Lazy}
75+
, keysSet
7376
) where
7477

7578
import Control.DeepSeq (NFData(..))
@@ -84,7 +87,7 @@ import Data.Monoid (Monoid(..))
8487
import GHC.Exts (build)
8588
import Prelude hiding (filter, foldr, map, null)
8689
import qualified Data.Foldable as Foldable
87-
import qualified Data.HashMap.Lazy as H
90+
import qualified Data.HashMap.Base as H
8891
import qualified Data.List as List
8992
import Data.Typeable (Typeable)
9093
import Text.Read
@@ -101,6 +104,8 @@ import Data.Functor.Classes
101104
import qualified Data.Hashable.Lifted as H
102105
#endif
103106

107+
import Data.Functor ((<$))
108+
104109
-- | A set of values. A set cannot contain duplicate values.
105110
newtype HashSet a = HashSet {
106111
asMap :: HashMap a ()
@@ -133,7 +138,7 @@ instance Ord1 HashSet where
133138
#endif
134139

135140
instance Foldable.Foldable HashSet where
136-
foldr = Data.HashSet.foldr
141+
foldr = Data.HashSet.Base.foldr
137142
{-# INLINE foldr #-}
138143

139144
#if __GLASGOW_HASKELL__ >= 711
@@ -210,6 +215,12 @@ toMap = asMap
210215
fromMap :: HashMap a () -> HashSet a
211216
fromMap = HashSet
212217

218+
-- | /O(n)/ Produce a 'HashSet' of all the keys in the given 'HashMap'.
219+
--
220+
-- @since 0.2.10.0
221+
keysSet :: HashMap k a -> HashSet k
222+
keysSet m = fromMap (() <$ m)
223+
213224
-- | /O(n+m)/ Construct a set containing all elements from both sets.
214225
--
215226
-- To obtain good performance, the smaller set must be presented as

unordered-containers.cabal

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ library
3434
other-modules:
3535
Data.HashMap.Array
3636
Data.HashMap.Base
37+
Data.HashMap.Strict.Base
3738
Data.HashMap.List
3839
Data.HashMap.Unsafe
3940
Data.HashMap.UnsafeShift
41+
Data.HashSet.Base
4042

4143
build-depends:
4244
base >= 4.7 && < 5,
@@ -177,9 +179,11 @@ benchmark benchmarks
177179
Data.HashMap.Base
178180
Data.HashMap.Lazy
179181
Data.HashMap.Strict
182+
Data.HashMap.Strict.Base
180183
Data.HashMap.Unsafe
181184
Data.HashMap.UnsafeShift
182185
Data.HashSet
186+
Data.HashSet.Base
183187
Util.ByteString
184188
Util.Int
185189
Util.String

0 commit comments

Comments
 (0)