3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-07-12 10:06:23 +00:00
z3/src/math
Lev Nachmanson 2490e86d3f
nlsat/anum: share mutation-aware merge sort in one helper (#10006)
## Summary

Follow-up to #10001 addressing @NikolajBjorner's review comment:

> isn't this nearly identical AI generated code to the other file? There
has to be some modular approach to deal with sorting vectors?

#10001 introduced two nearly-identical copies of a bounds-safe,
mutation-aware index-permutation merge sort:
- `algebraic_numbers.cpp::merge_sort_roots_perm`
- `nlsat/levelwise.cpp::merge_sort_perm`

Both exist because the comparator (`anum_manager::compare`/`lt`) is
**not pure**: it mutates the algebraic numbers it compares (refining
isolating intervals) and may throw on the resource limit, which makes
`std::sort` undefined behavior (the original SIGSEGV).

## Change

Extract the algorithm into a single shared helper
`util/index_sort_with_mutations.h` (`stable_index_merge_sort`). The long
rationale for why `std::sort` is unsafe and merge sort is safe now lives
in exactly one place. Both call sites become thin wrappers that build
the scratch buffer and forward their local comparator.

No behavioral change: same stable O(n log n) merge sort over an index
permutation.

## Verification

CMake/Ninja Release build:
- `test-z3 /seq algebraic_numbers` — PASS
- `test-z3 /seq algebraic` — PASS
- NRA/NIA smoke solves with `nlsat.lws=true` return expected sat/unsat.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-30 08:40:33 -07:00
..
dd Remove unused swap() methods (#8538) 2026-02-08 18:53:43 +00:00
grobner Standardize for-loop increments to prefix form (++i) (#8199) 2026-01-14 19:55:31 -08:00
hilbert Centralize and document TRACE tags using X-macros (#7657) 2025-05-28 14:31:25 +01:00
interval Fix 13 compiler warnings: sign-comparison and unused parameters (#8215) 2026-01-16 16:00:42 -08:00
lp [code-simplifier] Simplify int_cube: remove goto, use aggregate/brace init (#9874) 2026-06-29 19:18:04 -07:00
polynomial nlsat/anum: share mutation-aware merge sort in one helper (#10006) 2026-06-30 08:40:33 -07:00
realclosure Code simplifications in sls_euf_plugin.cpp and realclosure.cpp 2026-03-10 16:17:24 +00:00
simplex Fix static analysis findings: uninitialized vars, bitwise shift UB, garbage values 2026-03-02 00:13:55 +00:00
subpaving Standardize for-loop increments to prefix form (++i) (#8199) 2026-01-14 19:55:31 -08:00