Skip to content

fix: preserve segment tree query order - #1071

Open
tianrking wants to merge 2 commits into
TheAlgorithms:masterfrom
tianrking:codex/segment-tree-query-order
Open

tianrking wants to merge 2 commits into
TheAlgorithms:masterfrom
tianrking:codex/segment-tree-query-order

Conversation

@tianrking

@tianrking tianrking commented Oct 4, 2026 •

Copy link
Copy Markdown

Description

SegmentTree query appends right-hand fragments in discovery order, which can differ from array order. For associative noncommutative merges such as affine composition, the transforms 2x+1, 3x+4, 5x+2, 7x+3 return 30x+19 for query0..3 instead of 30x+37.

Accumulate left and right fragments separately: append left fragments, prepend right fragments, then merge left followed by right. Preserve the existing public API, storage, point updates, range errors and Option handling without using Default as an identity.

Four regressions cover explicit fragment boundaries, exhaustive small ranges over balanced/uneven sizes, point and no-op updates, and empty/singleton merge calls. They also verify the existing terminal len..len InvalidRange error without calling the merge function.

Native Linux proof uses official nightly-2026-10-04 (rustc1.101.0-nightly db8f076d2):

  • Original production with final tests: three ordering regressions fail and four cases pass, including the terminal error contract; Cargo101.
  • Exact final source: explicitly checks out b56256f and verifies source blob5b5985712f808db4cda72d58ac6c2cb51ffa0f43 before/after. Focused7, unit2608 (11 pre-existing ignored), docs90 (5 pre-existing ignored), changed-file rustfmt and diagnostic Clippy pass.
  • Original-HEAD comparison proves identical upstream nightly baseline failures: full-workspace fmt exits1 at unchanged big_integer/poly1305.rs:13; strict all-targets Clippy exits101 with16 identical error messages, locations and lint IDs. The final source retains exactly those diagnostics.
  • Diagnostic Clippy passes with only six proved baseline categories allowed at the command line: deprecated, clippy::definition_in_module_root, clippy::needless_bool, clippy::rest_pattern_accessible_field, clippy::manual_bit_width, clippy::redundant_else. No source/config lint suppression or unrelated repair is included. The helper run retains the original strict/fmt failures and is therefore FAILURE overall; strict nightly checks are not claimed as passing.
  • Upstream PR build independently passes its fmt/clippy/test configuration. Its synthetic merge checkout has the same full source tree as the final PR commit. Other PR checks remain visible separately.

The validation workflow exists only on a separate fork branch, outside this one-file PR. Native Linux proof does not claim Windows validation. Implementation and tests use assistance from OpenAI Codex.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I ran the commands using the latest Rust nightly.
  • I ran cargo clippy --all -- -D warnings and fixed every issue (the proved original nightly baseline is documented above).
  • Full workspace cargo fmt is clean on latest nightly (the proved original nightly baseline is documented above).
  • I ran cargo test on the exact final PR commit and all non-ignored tests passed.
  • I checked CONTRIBUTING.md and my code follows its guidelines.

This repairs an existing algorithm already exported in data_structures/mod.rs and listed in DIRECTORY.md; new-algorithm export/list additions do not apply.

Accumulate left and right query fragments separately and prepend right fragments before combining both sides in input order. This preserves associative noncommutative merges without requiring an identity value.

Cover affine composition across tree shapes and query boundaries, exhaustive small ranges, point and no-op updates, and empty or singleton merge calls.
@tianrking
tianrking requested a review from imp2002 as a code owner October 4, 2026 19:47
@codecov-commenter

codecov-commenter commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.90%. Comparing base (2345c66) to head (b56256f).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1071   +/-   ##
=======================================
  Coverage   95.89%   95.90%           
=======================================
  Files         396      396           
  Lines       30440    30538   +98     
=======================================
+ Hits        29191    29288   +97     
- Misses       1249     1250    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants