@@ -198,6 +198,14 @@ class IMAP
198
198
# - #full?: Returns whether the set contains every possible value, including
199
199
# <tt>*</tt>.
200
200
#
201
+ # <i>Denormalized properties:</i>
202
+ # - #has_duplicates?: Returns whether the ordered entries repeat any
203
+ # numbers.
204
+ # - #count_duplicates: Returns the count of repeated numbers in the ordered
205
+ # entries.
206
+ # - #count_with_duplicates: Returns the count of numbers in the ordered
207
+ # entries, including any repeated numbers.
208
+ #
201
209
# === Methods for Iterating
202
210
#
203
211
# <i>Normalized (sorted and coalesced):</i>
@@ -923,9 +931,7 @@ def numbers; each_number.to_a end
923
931
# Related: #entries, #each_element
924
932
def each_entry ( &block ) # :yields: integer or range or :*
925
933
return to_enum ( __method__ ) unless block_given?
926
- return each_element ( &block ) unless @string
927
- @string . split ( "," ) . each do yield tuple_to_entry str_to_tuple _1 end
928
- self
934
+ each_entry_tuple do yield tuple_to_entry _1 end
929
935
end
930
936
931
937
# Yields each number or range (or <tt>:*</tt>) in #elements to the block
@@ -943,6 +949,16 @@ def each_element # :yields: integer or range or :*
943
949
944
950
private
945
951
952
+ def each_entry_tuple ( &block )
953
+ return to_enum ( __method__ ) unless block_given?
954
+ if @string
955
+ @string . split ( "," ) do block . call str_to_tuple _1 end
956
+ else
957
+ @tuples . each ( &block )
958
+ end
959
+ self
960
+ end
961
+
946
962
def tuple_to_entry ( ( min , max ) )
947
963
if min == STAR_INT then :*
948
964
elsif max == STAR_INT then min ..
@@ -1001,12 +1017,49 @@ def to_set; Set.new(numbers) end
1001
1017
# If <tt>*</tt> and <tt>2**32 - 1</tt> (the maximum 32-bit unsigned
1002
1018
# integer value) are both in the set, they will only be counted once.
1003
1019
def count
1004
- @tuples . sum ( @tuples . count ) { _2 - _1 } +
1005
- ( include_star? && include? ( UINT32_MAX ) ? -1 : 0 )
1020
+ count_numbers_in_tuples ( @tuples )
1006
1021
end
1007
1022
1008
1023
alias size count
1009
1024
1025
+ # Returns the count of numbers in the ordered #entries, including any
1026
+ # repeated numbers.
1027
+ #
1028
+ # When #string is normalized, this behaves the same as #count.
1029
+ #
1030
+ # Related: #entries, #count_duplicates, #has_duplicates?
1031
+ def count_with_duplicates
1032
+ return count unless @string
1033
+ count_numbers_in_tuples ( each_entry_tuple )
1034
+ end
1035
+
1036
+ # Returns the count of repeated numbers in the ordered #entries.
1037
+ #
1038
+ # When #string is normalized, this is zero.
1039
+ #
1040
+ # Related: #entries, #count_with_duplicates, #has_duplicates?
1041
+ def count_duplicates
1042
+ return 0 unless @string
1043
+ count_with_duplicates - count
1044
+ end
1045
+
1046
+ # :call-seq: has_duplicates? -> true | false
1047
+ #
1048
+ # Returns whether or not the ordered #entries repeat any numbers.
1049
+ #
1050
+ # Always returns +false+ when #string is normalized.
1051
+ #
1052
+ # Related: #entries, #count_with_duplicates, #count_duplicates?
1053
+ def has_duplicates?
1054
+ return false unless @string
1055
+ count_with_duplicates != count
1056
+ end
1057
+
1058
+ private def count_numbers_in_tuples ( tuples )
1059
+ tuples . sum ( tuples . count ) { _2 - _1 } +
1060
+ ( include_star? && include? ( UINT32_MAX ) ? -1 : 0 )
1061
+ end
1062
+
1010
1063
# Returns the index of +number+ in the set, or +nil+ if +number+ isn't in
1011
1064
# the set.
1012
1065
#
0 commit comments