Fix incorrect comparison logic in operator< / difference_from#696
Open
lyskov-ai wants to merge 2 commits into
Open
Fix incorrect comparison logic in operator< / difference_from#696lyskov-ai wants to merge 2 commits into
lyskov-ai wants to merge 2 commits into
Conversation
Four independent classes had broken comparison logic that violated either the contract of `operator<` (strict weak ordering) or the intended semantics of `difference_from`: * `core/chemical/sdf/mol_util.cc`: `BondData::operator<` was `(lower < other.lower) || (upper < other.upper)`. This is not a strict weak ordering — for `(5,1)` vs `(3,7)`, both compare less than each other, breaking antisymmetry. `BondData` is used inside `std::set<BondData>` (see `parse_bond_type_data`), so the broken ordering directly affects the set. Replaced with a proper lexicographic compare via `std::tie`. * `core/scoring/motif/motif_hash_stuff.cc`: `ResPairMotif::operator<` returned `0 < memcmp(...)`, i.e. it reported "less than" exactly when memcmp said "greater than" — the comparison was inverted. Switched to `memcmp(...) < 0`, which matches the correct convention (and matches `MotifHit::operator<` in the same file). * `core/io/StructFileReaderOptions.cc`: `operator<` was using `==` instead of `!=` for the short-circuit "return false" lines. The intent of the pattern is "if a < b return true; if a != b return false; otherwise fall through to the next member" (this is exactly what the parent `StructFileRepOptions::operator<` does), but the `==` form returned false when members were equal — short-circuiting before the rest of the members were ever compared, *and* failed to return false when members satisfied `>`. Fixed every `==` to `!=` and added the missing `!=` follow-up after `glycam_pdb_format_`. * `core/chemical/gasteiger/GasteigerAtomTypeData.cc`: `difference_from` short-circuited "return Other" when `charge_ == OTHER.charge_ || element_type_ != ... || ...`. The first clause is inverted: the function should return Other when these fundamental properties *differ*, not when they match. Otherwise, two types with equal charge but otherwise comparable properties were misclassified as `Other` and the rest of the function (which uses `charge_` to compute lone-pair / s-orbital / p-orbital differences) was unreachable. Fixed `==` to `!=`.
Brief one-paragraph notes above each of the four sites describing the bug and why the corrected form is the right one, so a future reader doesn't accidentally revert to the simpler-looking but broken version.
roccomoretti
approved these changes
May 18, 2026
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
Four independent classes had broken comparison logic that violated either the contract of `operator<` (strict weak ordering) or the intended semantics of `difference_from`. Each is a separate, surgical fix bundled into a single PR because they all share the same "comparison-operator misuse" theme.
`core/chemical/sdf/mol_util.cc` — `BondData::operator<` was `(lower < other.lower) || (upper < other.upper)`. This is not a strict weak ordering: for example, `(5, 1)` and `(3, 7)` each compare less than the other, breaking antisymmetry. `BondData` is held in `std::set` (see `parse_bond_type_data`), so the broken ordering directly affects the set. Replaced with a proper lexicographic compare via `std::tie`.
`core/scoring/motif/motif_hash_stuff.cc` — `ResPairMotif::operator<` returned `0 < memcmp(...)`, which is true exactly when `memcmp` says this > other; the comparison was inverted. Switched to `memcmp(...) < 0` (matches `MotifHit::operator<` in the same file).
`core/io/StructFileReaderOptions.cc` — `operator<` used `==` instead of `!=` for the short-circuit `return false` lines. The intended pattern (used correctly by the parent `StructFileRepOptions::operator<` and by `ImportPoseOptions::operator<`) is `if a < b return true; if a != b return false; // fall through`. The `==` form returns false when members are equal, short-circuiting before the rest of the members are compared, and falls through when one member is greater. Fixed every `==` to `!=` and added the missing `!=` follow-up after `glycam_pdb_format_`.
`core/chemical/gasteiger/GasteigerAtomTypeData.cc` — `difference_from` short-circuited `return Other` when `charge_ == OTHER.charge_ || element_type_ != ... || ...`. The first clause is inverted: the function should return `Other` when fundamental properties differ, not when they match. The bug also made the lone-pair / s-orbital / p-orbital classification logic below this check unreachable for any two types with equal charge. Fixed `==` to `!=`.