@@ -275,8 +275,10 @@ class IMAP
275275 # occurrence in entries.
276276 #
277277 # <i>Set cardinality:</i>
278- # - #count (aliased as #size): Returns the count of numbers in the set.
279- # Duplicated numbers are not counted.
278+ # - #cardinality: Returns the number of distinct members in the set.
279+ # <tt>*</tt> is counted as its own entry, distinct from UINT32_MAX.
280+ # - #count (aliased as #size): Returns the count of distinct numbers in the
281+ # set. <tt>*</tt> is counted as UINT32_MAX.
280282 # - #empty?: Returns whether the set has no members. \IMAP syntax does not
281283 # allow empty sequence sets.
282284 # - #valid?: Returns whether the set has any members.
@@ -1336,28 +1338,71 @@ def each_ordered_number(&block)
13361338 # Related: #elements, #ranges, #numbers
13371339 def to_set ; Set . new ( numbers ) end
13381340
1341+ # Returns the number of members in the set.
1342+ #
1343+ # Unlike #count, <tt>"*"</tt> is considered to be distinct from
1344+ # <tt>2³² - 1</tt> (the maximum 32-bit unsigned integer value).
1345+ #
1346+ # set = Net::IMAP::SequenceSet[1..10]
1347+ # set.count #=> 10
1348+ # set.cardinality #=> 10
1349+ #
1350+ # set = Net::IMAP::SequenceSet["4294967295,*"]
1351+ # set.count #=> 1
1352+ # set.cardinality #=> 2
1353+ #
1354+ # set = Net::IMAP::SequenceSet[1..]
1355+ # set.count #=> 4294967295
1356+ # set.cardinality #=> 4294967296
1357+ #
1358+ # Related: #count, #count_with_duplicates
1359+ def cardinality = minmaxes . sum ( @set_data . count ) { _2 - _1 }
1360+
13391361 # Returns the count of #numbers in the set.
13401362 #
1341- # <tt>*</tt> will be counted as <tt>2**32 - 1</tt> (the maximum 32-bit
1342- # unsigned integer value).
1363+ # Unlike #cardinality, <tt>"*"</tt> is considered to be equal to
1364+ # <tt>2³² - 1</tt> (the maximum 32-bit unsigned integer value).
1365+ #
1366+ # set = Net::IMAP::SequenceSet[1..10]
1367+ # set.count #=> 10
1368+ # set.cardinality #=> 10
1369+ #
1370+ # set = Net::IMAP::SequenceSet["4294967295,*"]
1371+ # set.count #=> 1
1372+ # set.cardinality #=> 2
13431373 #
1344- # Related: #count_with_duplicates
1374+ # set = Net::IMAP::SequenceSet[1..]
1375+ # set.count #=> 4294967295
1376+ # set.cardinality #=> 4294967296
1377+ #
1378+ # Related: #cardinality, #count_with_duplicates
13451379 def count
1346- minmaxes . sum ( minmaxes . count ) { _2 - _1 } +
1347- ( include_star? && include? ( UINT32_MAX ) ? -1 : 0 )
1380+ cardinality + ( include_star? && include? ( UINT32_MAX ) ? -1 : 0 )
13481381 end
13491382
13501383 alias size count
13511384
13521385 # Returns the count of numbers in the ordered #entries, including any
13531386 # repeated numbers.
13541387 #
1355- # <tt>*</tt> will be counted as <tt>2**32 - 1</tt> (the maximum 32-bit
1356- # unsigned integer value).
1388+ # When #string is normalized, this returns the same as #count.
1389+ # Like #count, <tt>"*"</tt> is be considered to be equal to
1390+ # <tt>2³² - 1</tt> (the maximum 32-bit unsigned integer value).
1391+ #
1392+ # In a range, <tt>"*"</tt> is _not_ considered a duplicate:
1393+ # set = Net::IMAP::SequenceSet["4294967295:*"]
1394+ # set.count_with_duplicates #=> 1
1395+ # set.count #=> 1
1396+ # set.cardinality #=> 2
13571397 #
1358- # When #string is normalized, this behaves the same as #count.
1398+ # In a separate entry, <tt>"*"</tt> _is_ considered a duplicate:
1399+ # set = Net::IMAP::SequenceSet["4294967295,*"]
1400+ # set.count_with_duplicates #=> 2
1401+ # set.count #=> 1
1402+ # set.cardinality #=> 2
13591403 #
1360- # Related: #entries, #count_duplicates, #has_duplicates?
1404+ # Related: #count, #cardinality, #count_duplicates, #has_duplicates?,
1405+ # #entries
13611406 def count_with_duplicates
13621407 return count unless @string
13631408 each_entry_minmax . sum { |min , max |
@@ -1382,7 +1427,7 @@ def count_duplicates
13821427 #
13831428 # Always returns +false+ when #string is normalized.
13841429 #
1385- # Related: #entries, #count_with_duplicates, #count_duplicates?
1430+ # Related: #entries, #count_with_duplicates, #count_duplicates
13861431 def has_duplicates?
13871432 return false unless @string
13881433 count_with_duplicates != count
0 commit comments