Skip to content

Fix incorrect comparison logic in operator< / difference_from#696

Open
lyskov-ai wants to merge 2 commits into
RosettaCommons:mainfrom
lyskov-ai:fix/incorrect-comparison-operators
Open

Fix incorrect comparison logic in operator< / difference_from#696
lyskov-ai wants to merge 2 commits into
RosettaCommons:mainfrom
lyskov-ai:fix/incorrect-comparison-operators

Conversation

@lyskov-ai
Copy link
Copy Markdown
Contributor

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 `!=`.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants