@@ -189,6 +189,10 @@ class IMAP
189
189
# - #at: Returns the number at a given offset in the sorted set.
190
190
# - #find_index: Returns the given number's offset in the sorted set.
191
191
#
192
+ # <i>Accessing value by offset in ordered entries</i>
193
+ # - #find_ordered_index: Returns the index of the given number's first
194
+ # occurrence in entries.
195
+ #
192
196
# <i>Set cardinality:</i>
193
197
# - #count (aliased as #size): Returns the count of numbers in the set.
194
198
# Duplicated numbers are not counted.
@@ -1086,30 +1090,45 @@ def has_duplicates?
1086
1090
# Returns the (sorted and deduplicated) index of +number+ in the set, or
1087
1091
# +nil+ if +number+ isn't in the set.
1088
1092
#
1089
- # Related: #[], #at
1093
+ # Related: #[], #at, #find_ordered_index
1090
1094
def find_index ( number )
1091
1095
number = to_tuple_int number
1092
- each_tuple_with_index do |min , max , idx_min |
1096
+ each_tuple_with_index ( @tuples ) do |min , max , idx_min |
1093
1097
number < min and return nil
1094
1098
number <= max and return from_tuple_int ( idx_min + ( number - min ) )
1095
1099
end
1096
1100
nil
1097
1101
end
1098
1102
1103
+ # Returns the first index of +number+ in the ordered #entries, or
1104
+ # +nil+ if +number+ isn't in the set.
1105
+ #
1106
+ # Related: #find_index
1107
+ def find_ordered_index ( number )
1108
+ number = to_tuple_int number
1109
+ each_tuple_with_index ( each_entry_tuple ) do |min , max , idx_min |
1110
+ if min <= number && number <= max
1111
+ return from_tuple_int ( idx_min + ( number - min ) )
1112
+ end
1113
+ end
1114
+ nil
1115
+ end
1116
+
1099
1117
private
1100
1118
1101
- def each_tuple_with_index
1119
+ def each_tuple_with_index ( tuples )
1102
1120
idx_min = 0
1103
- @tuples . each do |min , max |
1104
- yield min , max , idx_min , ( idx_max = idx_min + ( max - min ) )
1121
+ tuples . each do |min , max |
1122
+ idx_max = idx_min + ( max - min )
1123
+ yield min , max , idx_min , idx_max
1105
1124
idx_min = idx_max + 1
1106
1125
end
1107
1126
idx_min
1108
1127
end
1109
1128
1110
- def reverse_each_tuple_with_index
1129
+ def reverse_each_tuple_with_index ( tuples )
1111
1130
idx_max = -1
1112
- @ tuples. reverse_each do |min , max |
1131
+ tuples . reverse_each do |min , max |
1113
1132
yield min , max , ( idx_min = idx_max - ( max - min ) ) , idx_max
1114
1133
idx_max = idx_min - 1
1115
1134
end
@@ -1130,11 +1149,11 @@ def reverse_each_tuple_with_index
1130
1149
def at ( index )
1131
1150
index = Integer ( index . to_int )
1132
1151
if index . negative?
1133
- reverse_each_tuple_with_index do |min , max , idx_min , idx_max |
1152
+ reverse_each_tuple_with_index ( @tuples ) do |min , max , idx_min , idx_max |
1134
1153
idx_min <= index and return from_tuple_int ( min + ( index - idx_min ) )
1135
1154
end
1136
1155
else
1137
- each_tuple_with_index do |min , _ , idx_min , idx_max |
1156
+ each_tuple_with_index ( @tuples ) do |min , _ , idx_min , idx_max |
1138
1157
index <= idx_max and return from_tuple_int ( min + ( index - idx_min ) )
1139
1158
end
1140
1159
end
0 commit comments