19
19
-- A set of /hashable/ values. A set cannot contain duplicate items.
20
20
-- A 'HashSet' makes no guarantees as to the order of its elements.
21
21
--
22
- -- The implementation is based on /hash array mapped trie /. A
22
+ -- The implementation is based on /hash array mapped tries /. A
23
23
-- 'HashSet' is often faster than other tree-based set types,
24
24
-- especially when value comparison is expensive, as in the case of
25
25
-- strings.
@@ -36,10 +36,6 @@ module Data.HashSet.Base
36
36
, empty
37
37
, singleton
38
38
39
- -- * Combine
40
- , union
41
- , unions
42
-
43
39
-- * Basic interface
44
40
, null
45
41
, size
@@ -50,6 +46,10 @@ module Data.HashSet.Base
50
46
-- * Transformations
51
47
, map
52
48
49
+ -- * Combine
50
+ , union
51
+ , unions
52
+
53
53
-- * Difference and intersection
54
54
, difference
55
55
, intersection
@@ -260,24 +260,39 @@ hashSetDataType :: DataType
260
260
hashSetDataType = mkDataType " Data.HashSet.Base.HashSet" [fromListConstr]
261
261
262
262
-- | /O(1)/ Construct an empty set.
263
+ --
264
+ -- >>> HashSet.empty
265
+ -- fromList []
263
266
empty :: HashSet a
264
267
empty = HashSet H. empty
265
268
266
269
-- | /O(1)/ Construct a set with a single element.
270
+ --
271
+ -- >>> HashSet.singleton 1
272
+ -- fromList [1]
267
273
singleton :: Hashable a => a -> HashSet a
268
274
singleton a = HashSet (H. singleton a () )
269
275
{-# INLINABLE singleton #-}
270
276
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,())]
272
281
toMap :: HashSet a -> HashMap a ()
273
282
toMap = asMap
274
283
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]
276
288
fromMap :: HashMap a () -> HashSet a
277
289
fromMap = HashSet
278
290
279
291
-- | /O(n)/ Produce a 'HashSet' of all the keys in the given 'HashMap'.
280
292
--
293
+ -- >>> HashSet.keysSet (HashMap.fromList [(1, "a"), (2, "b")]
294
+ -- fromList [1,2]
295
+ --
281
296
-- @since 0.2.10.0
282
297
keysSet :: HashMap k a -> HashSet k
283
298
keysSet m = fromMap (() <$ m)
@@ -287,8 +302,6 @@ keysSet m = fromMap (() <$ m)
287
302
-- To obtain good performance, the smaller set must be presented as
288
303
-- the first argument.
289
304
--
290
- -- ==== __Examples__
291
- --
292
305
-- >>> union (fromList [1,2]) (fromList [2,3])
293
306
-- fromList [1,2,3]
294
307
union :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
@@ -303,48 +316,79 @@ unions = List.foldl' union empty
303
316
{-# INLINE unions #-}
304
317
305
318
-- | /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
306
324
null :: HashSet a -> Bool
307
325
null = H. null . asMap
308
326
{-# INLINE null #-}
309
327
310
328
-- | /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
311
334
size :: HashSet a -> Int
312
335
size = H. size . asMap
313
336
{-# INLINE size #-}
314
337
315
338
-- | /O(log n)/ Return 'True' if the given value is present in this
316
339
-- 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
317
345
member :: (Eq a , Hashable a ) => a -> HashSet a -> Bool
318
346
member a s = case H. lookup a (asMap s) of
319
347
Just _ -> True
320
348
_ -> False
321
349
{-# INLINABLE member #-}
322
350
323
351
-- | /O(log n)/ Add the specified value to this set.
352
+ --
353
+ -- >>> HashSet.insert 1 HashSet.empty
354
+ -- fromList [1]
324
355
insert :: (Eq a , Hashable a ) => a -> HashSet a -> HashSet a
325
356
insert a = HashSet . H. insert a () . asMap
326
357
{-# INLINABLE insert #-}
327
358
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]
330
365
delete :: (Eq a , Hashable a ) => a -> HashSet a -> HashSet a
331
366
delete a = HashSet . H. delete a . asMap
332
367
{-# INLINABLE delete #-}
333
368
334
369
-- | /O(n)/ Transform this set by applying a function to every value.
335
370
-- 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"]
336
374
map :: (Hashable b , Eq b ) => (a -> b ) -> HashSet a -> HashSet b
337
375
map f = fromList . List. map f . toList
338
376
{-# INLINE map #-}
339
377
340
378
-- | /O(n)/ Difference of two sets. Return elements of the first set
341
379
-- not existing in the second.
380
+ --
381
+ -- >>> HashSet.difference (HashSet.fromList [1,2,3]) (HashSet.fromList [2,3,4])
382
+ -- fromList [1]
342
383
difference :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
343
384
difference (HashSet a) (HashSet b) = HashSet (H. difference a b)
344
385
{-# INLINABLE difference #-}
345
386
346
387
-- | /O(n)/ Intersection of two sets. Return elements present in both
347
388
-- the first set and the second.
389
+ --
390
+ -- >>> HashSet.intersection (HashSet.fromList [1,2,3]) (HashSet.fromList [2,3,4])
391
+ -- fromList [2,3]
348
392
intersection :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
349
393
intersection (HashSet a) (HashSet b) = HashSet (H. intersection a b)
350
394
{-# INLINABLE intersection #-}
0 commit comments