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