Skip to content

Fix set::min()/max() comparator bug and non-const operator[] (#28)#32

Merged
jkalias merged 2 commits into
mainfrom
claude/open-issues-list-z2d0z7
Jun 13, 2026
Merged

Fix set::min()/max() comparator bug and non-const operator[] (#28)#32
jkalias merged 2 commits into
mainfrom
claude/open-issues-list-z2d0z7

Conversation

@jkalias

@jkalias jkalias commented Jun 13, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes the two correctness defects in include/set.h tracked in #28.

1. set::min() / max() ignored the set's comparator

They used std::min_element / std::max_element, which rank by operator< rather than the set's TCompare. For a set with a custom comparator this returned the wrong element, required operator< 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 compile

It did auto it = std::advance(begin(), index);, but std::advance returns void, 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

  • Added MinRespectsCustomComparator / MaxRespectsCustomComparator using a descending int comparator (which differs from operator<), so they isolate the actual defect.
  • Added NonConstSubscripting, which subscripts a non-const set (previously a compile error).

Verification

  • Builds clean under both C++11 and C++17.
  • Full suite: 300/300 pass, including the 3 new tests.

Note

The pre-existing MinCustomType / MaxCustomType tests keep their #if defined(__clang__) / #if __linux__ branches: person_comparator is defined as a < b (hash-based, i.e. identical to operator<), so their cross-platform divergence comes from std::hash differing 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

claude added 2 commits June 13, 2026 15:52
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.
@jkalias

jkalias commented Jun 13, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 8a0e78a407

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@jkalias jkalias merged commit 0ebec4c into main Jun 13, 2026
12 checks passed
@jkalias jkalias deleted the claude/open-issues-list-z2d0z7 branch June 13, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Correctness bugs: set::min()/max() ignore comparator; non-const set::operator[] does not compile

2 participants