Fix set::min()/max() comparator bug and non-const operator[] (#28)#32
Merged
Conversation
set::min()/max() used std::min_element/std::max_element, which rank by operator< instead of the set's TCompare. For sets with a custom comparator this returned the wrong element, required operator< even when a comparator was supplied, and was O(n). They now return *begin() / *std::prev(end()), which respects the comparator and is O(1). The non-const set::operator[] did `auto it = std::advance(begin(), index)`, but std::advance returns void, so the overload failed to compile whenever instantiated (subscripting a non-const set). It now advances an iterator in place, matching the const overload, and works on both C++11 and C++17. Added tests: min/max with a comparator that differs from operator< (descending int comparator), and non-const subscripting.
GitHub's windows-latest image no longer provides the "Visual Studio 17 2022" generator, so CMake configure failed with "could not find any instance of Visual Studio" before compiling. Pin the Windows matrix entries to windows-2022.
Owner
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the two correctness defects in
include/set.htracked in #28.1.
set::min()/max()ignored the set's comparatorThey used
std::min_element/std::max_element, which rank byoperator<rather than the set'sTCompare. For a set with a custom comparator this returned the wrong element, requiredoperator<even when a comparator was supplied, and ran in O(n). They now return*begin()/*std::prev(end())(guarded by an emptiness check), which respects the comparator and is O(1).2. Non-const
set::operator[]did not compileIt did
auto it = std::advance(begin(), index);, butstd::advancereturnsvoid, so the overload failed to compile whenever instantiated (i.e. subscripting a non-const set). It now advances an iterator in place, matching the const overload, and works on both C++11 and C++17.Tests
MinRespectsCustomComparator/MaxRespectsCustomComparatorusing a descending int comparator (which differs fromoperator<), so they isolate the actual defect.NonConstSubscripting, which subscripts a non-const set (previously a compile error).Verification
Note
The pre-existing
MinCustomType/MaxCustomTypetests keep their#if defined(__clang__)/#if __linux__branches:person_comparatoris defined asa < b(hash-based, i.e. identical tooperator<), so their cross-platform divergence comes fromstd::hashdiffering between libc++ and libstdc++ — unrelated to this bug — and this fix leaves their results unchanged.Closes #28.
https://claude.ai/code/session_011y3XcVg3UCcgi5JMEo2tso
Generated by Claude Code