@@ -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
@@ -259,38 +259,63 @@ fromListConstr = mkConstr hashSetDataType "fromList" [] Prefix
259
259
hashSetDataType :: DataType
260
260
hashSetDataType = mkDataType " Data.HashSet.Base.HashSet" [fromListConstr]
261
261
262
- -- | /O(1)/ Construct an empty set.
262
+ -- | Construct an empty set.
263
+ --
264
+ -- >>> HashSet.empty
265
+ -- fromList []
266
+ --
267
+ -- __Complexity:__ /O(1)/
263
268
empty :: HashSet a
264
269
empty = HashSet H. empty
265
270
266
- -- | /O(1)/ Construct a set with a single element.
271
+ -- | Construct a set with a single element.
272
+ --
273
+ -- >>> HashSet.singleton 1
274
+ -- fromList [1]
275
+ --
276
+ -- __Complexity:__ /O(1)/
267
277
singleton :: Hashable a => a -> HashSet a
268
278
singleton a = HashSet (H. singleton a () )
269
279
{-# INLINABLE singleton #-}
270
280
271
- -- | /O(1)/ Convert to the equivalent 'HashMap'.
281
+ -- | Convert to set to the equivalent 'HashMap' with @()@ values.
282
+ --
283
+ -- >>> HashSet.toMap (HashSet.singleton 1)
284
+ -- fromList [(1,())]
285
+ --
286
+ -- __Complexity:__ /O(1)/
272
287
toMap :: HashSet a -> HashMap a ()
273
288
toMap = asMap
274
289
275
- -- | /O(1)/ Convert from the equivalent 'HashMap'.
290
+ -- | Convert from the equivalent 'HashMap' with @()@ values.
291
+ --
292
+ -- >>> HashSet.fromMap (HashMap.singleton 1 ())
293
+ -- fromList [1]
294
+ --
295
+ -- __Complexity:__ /O(1)/
276
296
fromMap :: HashMap a () -> HashSet a
277
297
fromMap = HashSet
278
298
279
- -- | /O(n)/ Produce a 'HashSet' of all the keys in the given 'HashMap'.
299
+ -- | Produce a 'HashSet' of all the keys in the given 'HashMap'.
300
+ --
301
+ -- >>> HashSet.keysSet (HashMap.fromList [(1, "a"), (2, "b")]
302
+ -- fromList [1,2]
303
+ --
304
+ -- __Complexity:__ /O(n)/
280
305
--
281
306
-- @since 0.2.10.0
282
307
keysSet :: HashMap k a -> HashSet k
283
308
keysSet m = fromMap (() <$ m)
284
309
285
- -- | /O(n+m)/ Construct a set containing all elements from both sets.
310
+ -- | Construct a set containing all elements from both sets.
286
311
--
287
312
-- To obtain good performance, the smaller set must be presented as
288
313
-- the first argument.
289
314
--
290
- -- ==== __Examples__
291
- --
292
315
-- >>> union (fromList [1,2]) (fromList [2,3])
293
316
-- fromList [1,2,3]
317
+ --
318
+ -- __Complexity:__ /O(n+m)/
294
319
union :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
295
320
union s1 s2 = HashSet $ H. union (asMap s1) (asMap s2)
296
321
{-# INLINE union #-}
@@ -302,103 +327,164 @@ unions :: (Eq a, Hashable a) => [HashSet a] -> HashSet a
302
327
unions = List. foldl' union empty
303
328
{-# INLINE unions #-}
304
329
305
- -- | /O(1)/ Return 'True' if this set is empty, 'False' otherwise.
330
+ -- | Return 'True' if this set is empty, 'False' otherwise.
331
+ --
332
+ -- >>> HashSet.null HashSet.empty
333
+ -- True
334
+ -- >>> HashSet.null (HashSet.singleton 1)
335
+ -- False
336
+ --
337
+ -- __Complexity:__ /O(1)/
306
338
null :: HashSet a -> Bool
307
339
null = H. null . asMap
308
340
{-# INLINE null #-}
309
341
310
- -- | /O(n)/ Return the number of elements in this set.
342
+ -- | Return the number of elements in this set.
343
+ --
344
+ -- >>> HashSet.size HashSet.empty
345
+ -- 0
346
+ -- >>> HashSet.size (HashSet.fromList [1,2,3])
347
+ -- 3
348
+ --
349
+ -- __Complexity:__ /O(n)/
311
350
size :: HashSet a -> Int
312
351
size = H. size . asMap
313
352
{-# INLINE size #-}
314
353
315
- -- | /O(log n)/ Return 'True' if the given value is present in this
354
+ -- | Return 'True' if the given value is present in this
316
355
-- set, 'False' otherwise.
356
+ --
357
+ -- >>> HashSet.member 1 (Hashset.fromList [1,2,3])
358
+ -- True
359
+ -- >>> HashSet.member 1 (Hashset.fromList [4,5,6])
360
+ -- False
361
+ --
362
+ -- __Complexity:__ /O(log n)/
317
363
member :: (Eq a , Hashable a ) => a -> HashSet a -> Bool
318
364
member a s = case H. lookup a (asMap s) of
319
365
Just _ -> True
320
366
_ -> False
321
367
{-# INLINABLE member #-}
322
368
323
- -- | /O(log n)/ Add the specified value to this set.
369
+ -- | Add the specified value to this set.
370
+ --
371
+ -- >>> HashSet.insert 1 HashSet.empty
372
+ -- fromList [1]
373
+ --
374
+ -- __Complexity:__ /O(log n)/
324
375
insert :: (Eq a , Hashable a ) => a -> HashSet a -> HashSet a
325
376
insert a = HashSet . H. insert a () . asMap
326
377
{-# INLINABLE insert #-}
327
378
328
- -- | /O(log n)/ Remove the specified value from this set if
329
- -- present.
379
+ -- | Remove the specified value from this set if present.
380
+ --
381
+ -- >>> HashSet.delete 1 (HashSet.fromList [1,2,3])
382
+ -- fromList [2,3]
383
+ -- >>> HashSet.delete 1 (HashSet.fromList [4,5,6])
384
+ -- fromList [4,5,6]
385
+ --
386
+ -- __Complexity:__ /O(log n)/
330
387
delete :: (Eq a , Hashable a ) => a -> HashSet a -> HashSet a
331
388
delete a = HashSet . H. delete a . asMap
332
389
{-# INLINABLE delete #-}
333
390
334
- -- | /O(n)/ Transform this set by applying a function to every value.
391
+ -- | Transform this set by applying a function to every value.
335
392
-- The resulting set may be smaller than the source.
393
+ --
394
+ -- >>> HashSet.map show (HashSet.fromList [1,2,3])
395
+ -- HashSet.fromList ["1","2","3"]
396
+ --
397
+ -- __Complexity:__ /O(n)/
336
398
map :: (Hashable b , Eq b ) => (a -> b ) -> HashSet a -> HashSet b
337
399
map f = fromList . List. map f . toList
338
400
{-# INLINE map #-}
339
401
340
- -- | /O(n)/ Difference of two sets. Return elements of the first set
402
+ -- | Difference of two sets. Return elements of the first set
341
403
-- not existing in the second.
404
+ --
405
+ -- >>> HashSet.difference (HashSet.fromList [1,2,3]) (HashSet.fromList [2,3,4])
406
+ -- fromList [1]
407
+ --
408
+ -- __Complexity:__ /O(n)/
342
409
difference :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
343
410
difference (HashSet a) (HashSet b) = HashSet (H. difference a b)
344
411
{-# INLINABLE difference #-}
345
412
346
- -- | /O(n)/ Intersection of two sets. Return elements present in both
413
+ -- | Intersection of two sets. Return elements present in both
347
414
-- the first set and the second.
415
+ --
416
+ -- >>> HashSet.intersection (HashSet.fromList [1,2,3]) (HashSet.fromList [2,3,4])
417
+ -- fromList [2,3]
418
+ --
419
+ -- __Complexity:__ /O(n)/
348
420
intersection :: (Eq a , Hashable a ) => HashSet a -> HashSet a -> HashSet a
349
421
intersection (HashSet a) (HashSet b) = HashSet (H. intersection a b)
350
422
{-# INLINABLE intersection #-}
351
423
352
- -- | /O(n)/ Reduce this set by applying a binary operator to all
424
+ -- | Reduce this set by applying a binary operator to all
353
425
-- elements, using the given starting value (typically the
354
426
-- left-identity of the operator). Each application of the operator
355
427
-- is evaluated before before using the result in the next
356
428
-- application. This function is strict in the starting value.
429
+ --
430
+ -- __Complexity:__ /O(n)/
357
431
foldl' :: (a -> b -> a ) -> a -> HashSet b -> a
358
432
foldl' f z0 = H. foldlWithKey' g z0 . asMap
359
433
where g z k _ = f z k
360
434
{-# INLINE foldl' #-}
361
435
362
- -- | /O(n)/ Reduce this set by applying a binary operator to all
436
+ -- | Reduce this set by applying a binary operator to all
363
437
-- elements, using the given starting value (typically the
364
438
-- right-identity of the operator). Each application of the operator
365
439
-- is evaluated before before using the result in the next
366
440
-- application. This function is strict in the starting value.
441
+ --
442
+ -- __Complexity:__ /O(n)/
367
443
foldr' :: (b -> a -> a ) -> a -> HashSet b -> a
368
444
foldr' f z0 = H. foldrWithKey' g z0 . asMap
369
445
where g k _ z = f k z
370
446
{-# INLINE foldr' #-}
371
447
372
- -- | /O(n)/ Reduce this set by applying a binary operator to all
448
+ -- | Reduce this set by applying a binary operator to all
373
449
-- elements, using the given starting value (typically the
374
450
-- right-identity of the operator).
451
+ --
452
+ -- __Complexity:__ /O(n)/
375
453
foldr :: (b -> a -> a ) -> a -> HashSet b -> a
376
454
foldr f z0 = foldrWithKey g z0 . asMap
377
455
where g k _ z = f k z
378
456
{-# INLINE foldr #-}
379
457
380
- -- | /O(n)/ Reduce this set by applying a binary operator to all
458
+ -- | Reduce this set by applying a binary operator to all
381
459
-- elements, using the given starting value (typically the
382
460
-- left-identity of the operator).
461
+ --
462
+ -- __Complexity:__ /O(n)/
383
463
foldl :: (a -> b -> a ) -> a -> HashSet b -> a
384
464
foldl f z0 = foldlWithKey g z0 . asMap
385
465
where g z k _ = f z k
386
466
{-# INLINE foldl #-}
387
467
388
- -- | /O(n)/ Filter this set by retaining only elements satisfying a
468
+ -- | Filter this set by retaining only elements satisfying a
389
469
-- predicate.
470
+ --
471
+ -- __Complexity:__ /O(n)/
390
472
filter :: (a -> Bool ) -> HashSet a -> HashSet a
391
473
filter p = HashSet . H. filterWithKey q . asMap
392
474
where q k _ = p k
393
475
{-# INLINE filter #-}
394
476
395
- -- | /O(n)/ Return a list of this set's elements. The list is
477
+ -- | Return a list of this set's elements. The list is
396
478
-- produced lazily.
479
+ --
480
+ -- __Complexity:__ /O(n)/
397
481
toList :: HashSet a -> [a ]
398
482
toList t = build (\ c z -> foldrWithKey ((const . ) c) z (asMap t))
399
483
{-# INLINE toList #-}
400
484
401
- -- | /O(n*min(W, n))/ Construct a set from a list of elements.
485
+ -- | Construct a set from a list of elements.
486
+ --
487
+ -- __Complexity:__ /O(n*min(W, n))/
402
488
fromList :: (Eq a , Hashable a ) => [a ] -> HashSet a
403
489
fromList = HashSet . List. foldl' (\ m k -> H. insert k () m) H. empty
404
490
{-# INLINE fromList #-}
0 commit comments