1919-- A set of /hashable/ values. A set cannot contain duplicate items.
2020-- A 'HashSet' makes no guarantees as to the order of its elements.
2121--
22- -- The implementation is based on /hash array mapped trie /. A
22+ -- The implementation is based on /hash array mapped tries /. A
2323-- 'HashSet' is often faster than other tree-based set types,
2424-- especially when value comparison is expensive, as in the case of
2525-- strings.
@@ -36,10 +36,6 @@ module Data.HashSet.Base
3636 , empty
3737 , singleton
3838
39- -- * Combine
40- , union
41- , unions
42-
4339 -- * Basic interface
4440 , null
4541 , size
@@ -50,6 +46,10 @@ module Data.HashSet.Base
5046 -- * Transformations
5147 , map
5248
49+ -- * Combine
50+ , union
51+ , unions
52+
5353 -- * Difference and intersection
5454 , difference
5555 , intersection
@@ -260,24 +260,39 @@ hashSetDataType :: DataType
260260hashSetDataType = mkDataType " Data.HashSet.Base.HashSet" [fromListConstr]
261261
262262-- | /O(1)/ Construct an empty set.
263+ --
264+ -- >>> HashSet.empty
265+ -- fromList []
263266empty :: HashSet a
264267empty = HashSet H. empty
265268
266269-- | /O(1)/ Construct a set with a single element.
270+ --
271+ -- >>> HashSet.singleton 1
272+ -- fromList [1]
267273singleton :: Hashable a => a -> HashSet a
268274singleton a = HashSet (H. singleton a () )
269275{-# INLINABLE singleton #-}
270276
271- -- | /O(1)/ Convert to the equivalent 'HashMap'.
277+ -- | /O(1)/ Convert to set to the equivalent 'HashMap' with @()@ values.
278+ --
279+ -- >>> HashSet.toMap (HashSet.singleton 1)
280+ -- fromList [(1,())]
272281toMap :: HashSet a -> HashMap a ()
273282toMap = asMap
274283
275- -- | /O(1)/ Convert from the equivalent 'HashMap'.
284+ -- | /O(1)/ Convert from the equivalent 'HashMap' with @()@ values.
285+ --
286+ -- >>> HashSet.fromMap (HashMap.singleton 1 ())
287+ -- fromList [1]
276288fromMap :: HashMap a () -> HashSet a
277289fromMap = HashSet
278290
279291-- | /O(n)/ Produce a 'HashSet' of all the keys in the given 'HashMap'.
280292--
293+ -- >>> HashSet.keysSet (HashMap.fromList [(1, "a"), (2, "b")]
294+ -- fromList [1,2]
295+ --
281296-- @since 0.2.10.0
282297keysSet :: HashMap k a -> HashSet k
283298keysSet m = fromMap (() <$ m)
@@ -287,8 +302,6 @@ keysSet m = fromMap (() <$ m)
287302-- To obtain good performance, the smaller set must be presented as
288303-- the first argument.
289304--
290- -- ==== __Examples__
291- --
292305-- >>> union (fromList [1,2]) (fromList [2,3])
293306-- fromList [1,2,3]
294307union :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
@@ -303,48 +316,79 @@ unions = List.foldl' union empty
303316{-# INLINE unions #-}
304317
305318-- | /O(1)/ Return 'True' if this set is empty, 'False' otherwise.
319+ --
320+ -- >>> HashSet.null HashSet.empty
321+ -- True
322+ -- >>> HashSet.null (HashSet.singleton 1)
323+ -- False
306324null :: HashSet a -> Bool
307325null = H. null . asMap
308326{-# INLINE null #-}
309327
310328-- | /O(n)/ Return the number of elements in this set.
329+ --
330+ -- >>> HashSet.size HashSet.empty
331+ -- 0
332+ -- >>> HashSet.size (HashSet.fromList [1,2,3])
333+ -- 3
311334size :: HashSet a -> Int
312335size = H. size . asMap
313336{-# INLINE size #-}
314337
315338-- | /O(log n)/ Return 'True' if the given value is present in this
316339-- set, 'False' otherwise.
340+ --
341+ -- >>> HashSet.member 1 (Hashset.fromList [1,2,3])
342+ -- True
343+ -- >>> HashSet.member 1 (Hashset.fromList [4,5,6])
344+ -- False
317345member :: (Eq a , Hashable a ) => a -> HashSet a -> Bool
318346member a s = case H. lookup a (asMap s) of
319347 Just _ -> True
320348 _ -> False
321349{-# INLINABLE member #-}
322350
323351-- | /O(log n)/ Add the specified value to this set.
352+ --
353+ -- >>> HashSet.insert 1 HashSet.empty
354+ -- fromList [1]
324355insert :: (Eq a , Hashable a ) => a -> HashSet a -> HashSet a
325356insert a = HashSet . H. insert a () . asMap
326357{-# INLINABLE insert #-}
327358
328- -- | /O(log n)/ Remove the specified value from this set if
329- -- present.
359+ -- | /O(log n)/ Remove the specified value from this set if present.
360+ --
361+ -- >>> HashSet.delete 1 (HashSet.fromList [1,2,3])
362+ -- fromList [2,3]
363+ -- >>> HashSet.delete 1 (HashSet.fromList [4,5,6])
364+ -- fromList [4,5,6]
330365delete :: (Eq a , Hashable a ) => a -> HashSet a -> HashSet a
331366delete a = HashSet . H. delete a . asMap
332367{-# INLINABLE delete #-}
333368
334369-- | /O(n)/ Transform this set by applying a function to every value.
335370-- The resulting set may be smaller than the source.
371+ --
372+ -- >>> HashSet.map show (HashSet.fromList [1,2,3])
373+ -- HashSet.fromList ["1","2","3"]
336374map :: (Hashable b , Eq b ) => (a -> b ) -> HashSet a -> HashSet b
337375map f = fromList . List. map f . toList
338376{-# INLINE map #-}
339377
340378-- | /O(n)/ Difference of two sets. Return elements of the first set
341379-- not existing in the second.
380+ --
381+ -- >>> HashSet.difference (HashSet.fromList [1,2,3]) (HashSet.fromList [2,3,4])
382+ -- fromList [1]
342383difference :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
343384difference (HashSet a) (HashSet b) = HashSet (H. difference a b)
344385{-# INLINABLE difference #-}
345386
346387-- | /O(n)/ Intersection of two sets. Return elements present in both
347388-- the first set and the second.
389+ --
390+ -- >>> HashSet.intersection (HashSet.fromList [1,2,3]) (HashSet.fromList [2,3,4])
391+ -- fromList [2,3]
348392intersection :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
349393intersection (HashSet a) (HashSet b) = HashSet (H. intersection a b)
350394{-# INLINABLE intersection #-}
0 commit comments