13
13
-- Portability : portable
14
14
--
15
15
-- This module provides efficient containers-based functions on the list type.
16
+ --
17
+ -- In the documentation, \(n\) is the number of elements in the list while
18
+ -- \(d\) is the number of distinct elements in the list. \(W\) is the number
19
+ -- of bits in an 'Int'.
16
20
-----------------------------------------------------------------------------
17
21
18
22
module Data.Containers.ListUtils (
@@ -33,19 +37,20 @@ import GHC.Exts ( build )
33
37
-- *** Ord-based nubbing ***
34
38
35
39
36
- -- | \( O(n \log n \). The @nubOrd@ function removes duplicate elements from a list.
37
- -- In particular, it keeps only the first occurrence of each element. By using a
38
- -- 'Set' internally it has better asymptotics than the standard 'Data.List.nub'
39
- -- function.
40
+ -- | \( O(n \log d) \). The @nubOrd@ function removes duplicate elements from a
41
+ -- list. In particular, it keeps only the first occurrence of each element. By
42
+ -- using a 'Set' internally it has better asymptotics than the standard
43
+ -- 'Data.List.nub' function.
40
44
--
41
45
-- ==== Strictness
42
46
--
43
47
-- @nubOrd@ is strict in the elements of the list.
44
48
--
45
49
-- ==== Efficiency note
46
50
--
47
- -- When applicable, it is almost always better to use 'nubInt' or 'nubIntOn' instead
48
- -- of this function. For example, the best way to nub a list of characters is
51
+ -- When applicable, it is almost always better to use 'nubInt' or 'nubIntOn'
52
+ -- instead of this function, although it can be a little worse in certain
53
+ -- pathological cases. For example, to nub a list of characters, use
49
54
--
50
55
-- @ nubIntOn fromEnum xs @
51
56
nubOrd :: Ord a => [a ] -> [a ]
@@ -114,7 +119,7 @@ constNubOn x _ = x
114
119
-- *** Int-based nubbing ***
115
120
116
121
117
- -- | \( O(n \min(n ,W)) \). The @nubInt@ function removes duplicate 'Int'
122
+ -- | \( O(n \min(d ,W)) \). The @nubInt@ function removes duplicate 'Int'
118
123
-- values from a list. In particular, it keeps only the first occurrence
119
124
-- of each element. By using an 'IntSet' internally, it attains better
120
125
-- asymptotics than the standard 'Data.List.nub' function.
@@ -130,7 +135,8 @@ nubInt = nubIntOn id
130
135
131
136
-- | The @nubIntOn@ function behaves just like 'nubInt' except it performs
132
137
-- comparisons not on the original datatype, but a user-specified projection
133
- -- from that datatype.
138
+ -- from that datatype. For example, @nubIntOn 'fromEnum'@ can be used to
139
+ -- nub characters and typical fixed-with numerical types efficiently.
134
140
--
135
141
-- ==== Strictness
136
142
--
0 commit comments