Skip to content

Conversation

@nevans
Copy link
Collaborator

@nevans nevans commented Dec 16, 2025

This issue was one of the motivations for #563 (and #564), but then I forgot about the bug, so it wasn't fixed in time for 0.6.0!


When SequenceSet#max(n) is called with n > cardinality, it should return a duplicate of the whole set. But, #max(n) is implemented using #slice(-n..), and (copying the behavior of Array), when a slicing starts from an out-of-range index, it returns nil.

It was using -[count, size].min to keep the index from going out-of-range. Prior to #564, #size was the same as #count, so it would give incorrect results when the set contains an endless range. After #564, this gives incorrect results when the ordered list contains duplicates.

This change should also give a small performance boost, because it bypasses the complexity of #slice(range) and just calls #dup (or returns self when the set is frozen).

When `SequenceSet#max(n)` is called with `n > cardinality`, it _should_
return a duplicate of the whole set.  But, `#max(n)` is implemented
using `#slice(-n..)`, and (copying the behavior of `Array`), when a
slicing starts from an out-of-range index, it returns `nil`.

It was using `-[count, size].min` to keep the index from going
out-of-range.  Prior to #564, `#size` was the same as `#count`, so it
would give incorrect results when the set contains an endless range.
After #564, this gives incorrect results when the ordered list contains
duplicates.

This change should also give a small performance boost, because it
bypasses the complexity of `#slice(range)` and just calls `#dup` (or
returns `self` when the set is frozen).

This issue was one of the motivations for #563 (and #564), but then I
forgot about the bug, so it wasn't fixed in time for 0.6.0!
@nevans nevans added bug Something isn't working sequence-set Any code the IMAP `sequence-set` data type or grammar rule, especially the SequenceSet class. labels Dec 16, 2025
@nevans nevans merged commit ce176b4 into master Dec 16, 2025
32 checks passed
@nevans nevans deleted the fix-sequence_set-max-count-gt-cardinality branch December 16, 2025 23:52
@nevans nevans changed the title 🐛 Fix SequenceSet#max(n), cardinality < n <= size 🐛 Fix SequenceSet#max(n) when cardinality < n <= size Dec 16, 2025
nevans added a commit that referenced this pull request Dec 17, 2025
This backports #580 to `v0.5-stable`.

When `SequenceSet#max(n)` is called with `n > cardinality`, it _should_
return a duplicate of the whole set.  But, `#max(n)` is implemented
using `#slice(-n..)`, and (copying the behavior of `Array`), when a
slicing starts from an out-of-range index, it returns `nil`.

It was using `-[count, size].min` to keep the index from going
out-of-range.  Prior to 0.6.0, `#size` is the same as `#count`, so it
gives incorrect results when the set contains an endless range.
`SequenceSet#cardinality` has not been backported to 0.5, so this makes
that calculation inline.

This change should also give a small performance boost, because it
bypasses the complexity of `#slice(range)` and just calls `#dup` (or
returns `self` when the set is frozen).
nevans added a commit that referenced this pull request Dec 17, 2025
This backports #580 to `v0.5-stable`.

When `SequenceSet#max(n)` is called with `n > cardinality`, it _should_
return a duplicate of the whole set.  But, `#max(n)` is implemented
using `#slice(-n..)`, and (copying the behavior of `Array`), when a
slicing starts from an out-of-range index, it returns `nil`.

It was using `-[count, size].min` to keep the index from going
out-of-range.  Prior to 0.6.0, `#size` is the same as `#count`, so it
gives incorrect results when the set contains an endless range.
`SequenceSet#cardinality` has not been backported to 0.5, so this makes
that calculation inline.

This change should also give a small performance boost, because it
bypasses the complexity of `#slice(range)` and just calls `#dup` (or
returns `self` when the set is frozen).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working sequence-set Any code the IMAP `sequence-set` data type or grammar rule, especially the SequenceSet class.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants