3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-07-03 22:06:11 +00:00

[WIP] Find and update std::optional usage in code base (#8272)

* Initial plan

* Add try_get_value for std::map and use it in var_register.h and dioph_eq.cpp

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Add try_get_value overload for unordered_map with custom hash and use in lar_solver.cpp

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Remove redundant try_get_value template overload

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Remove std::map include and try_get_value overload from lp_utils.h

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
This commit is contained in:
Copilot 2026-01-21 19:55:36 -08:00 committed by GitHub
parent b0313ca80a
commit c392592831
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 47 additions and 47 deletions

View file

@ -1000,11 +1000,11 @@ namespace lp {
void find_changed_terms_and_more_changed_rows() { void find_changed_terms_and_more_changed_rows() {
for (unsigned j : m_changed_f_columns) { for (unsigned j : m_changed_f_columns) {
const auto it = m_columns_to_terms.find(j); if (auto terms = try_get_value(m_columns_to_terms, j)) {
if (it != m_columns_to_terms.end()) for (unsigned k : *terms) {
for (unsigned k : it->second) {
mark_term_change(k); mark_term_change(k);
} }
}
if (!m_var_register.external_is_used(j)) if (!m_var_register.external_is_used(j))
continue; continue;
for (const auto& p : m_e_matrix.column(this->lar_solver_to_local(j))) { for (const auto& p : m_e_matrix.column(this->lar_solver_to_local(j))) {

View file

@ -332,34 +332,38 @@ namespace lp {
if (!column_has_term(be.m_j)) { if (!column_has_term(be.m_j)) {
if (coeff_map.size() != 1) if (coeff_map.size() != 1)
return false; return false;
auto it = coeff_map.find(be.m_j); if (auto ratio_opt = try_get_value(coeff_map, be.m_j)) {
if (it == coeff_map.end()) return false; mpq ratio = *ratio_opt;
mpq ratio = it->second; if (ratio < zero_of_type<mpq>()) {
if (ratio < zero_of_type<mpq>()) { kind = static_cast<lconstraint_kind>(-kind);
kind = static_cast<lconstraint_kind>(-kind); }
rs_of_evidence /= ratio;
} else {
return false;
} }
rs_of_evidence /= ratio;
} }
else { else {
lar_term const& t = get_term(be.m_j); lar_term const& t = get_term(be.m_j);
auto first_coeff = t.begin(); auto first_coeff = t.begin();
unsigned j = (*first_coeff).j(); unsigned j = (*first_coeff).j();
auto it = coeff_map.find(j); if (auto ratio_opt = try_get_value(coeff_map, j)) {
if (it == coeff_map.end()) mpq ratio = *ratio_opt / (*first_coeff).coeff();
for (auto p : t) {
if (auto coeff_opt = try_get_value(coeff_map, p.j())) {
if (p.coeff() * ratio != *coeff_opt)
return false;
} else {
return false;
}
}
if (ratio < zero_of_type<mpq>()) {
kind = static_cast<lconstraint_kind>(-kind);
}
rs_of_evidence /= ratio;
// rs_of_evidence += t->m_v * ratio;
} else {
return false; return false;
mpq ratio = it->second / (*first_coeff).coeff();
for (auto p : t) {
it = coeff_map.find(p.j());
if (it == coeff_map.end())
return false;
if (p.coeff() * ratio != it->second)
return false;
} }
if (ratio < zero_of_type<mpq>()) {
kind = static_cast<lconstraint_kind>(-kind);
}
rs_of_evidence /= ratio;
// rs_of_evidence += t->m_v * ratio;
} }
return kind == be.kind() && rs_of_evidence == be.m_bound; return kind == be.kind() && rs_of_evidence == be.m_bound;
@ -2716,10 +2720,9 @@ namespace lp {
bool lar_solver::fetch_normalized_term_column(const lar_term& c, std::pair<mpq, lpvar>& a_j) const { bool lar_solver::fetch_normalized_term_column(const lar_term& c, std::pair<mpq, lpvar>& a_j) const {
TRACE(lar_solver_terms, print_term_as_indices(c, tout << "looking for term ") << "\n";); TRACE(lar_solver_terms, print_term_as_indices(c, tout << "looking for term ") << "\n";);
SASSERT(c.is_normalized()); SASSERT(c.is_normalized());
auto it = m_imp->m_normalized_terms_to_columns.find(c); if (auto result = try_get_value(m_imp->m_normalized_terms_to_columns, c)) {
if (it != m_imp->m_normalized_terms_to_columns.end()) { TRACE(lar_solver_terms, tout << "got " << *result << "\n";);
TRACE(lar_solver_terms, tout << "got " << it->second << "\n";); a_j = *result;
a_j = it->second;
return true; return true;
} }
TRACE(lar_solver_terms, tout << "have not found\n";); TRACE(lar_solver_terms, tout << "have not found\n";);

View file

@ -52,8 +52,8 @@ std::ostream& print_vector(const C * t, unsigned size, std::ostream & out) {
} }
template <typename A, typename B> template <typename A, typename B, typename Hash = std::hash<A>, typename KeyEqual = std::equal_to<A>>
std::optional<B> try_get_value(const std::unordered_map<A,B> & map, const A& key) { std::optional<B> try_get_value(const std::unordered_map<A,B,Hash,KeyEqual> & map, const A& key) {
const auto it = map.find(key); const auto it = map.find(key);
if (it == map.end()) if (it == map.end())
return std::nullopt; return std::nullopt;

View file

@ -18,6 +18,7 @@ Revision History:
--*/ --*/
#pragma once #pragma once
#include "math/lp/lp_types.h" #include "math/lp/lp_types.h"
#include "math/lp/lp_utils.h"
namespace lp { namespace lp {
@ -53,9 +54,8 @@ public:
unsigned add_var(unsigned user_var, bool is_int) { unsigned add_var(unsigned user_var, bool is_int) {
if (user_var != UINT_MAX) { if (user_var != UINT_MAX) {
auto t = m_external_to_local.find(user_var); if (auto local = try_get_value(m_external_to_local, user_var)) {
if (t != m_external_to_local.end()) { return *local;
return t->second;
} }
} }
@ -96,29 +96,26 @@ public:
} }
bool external_is_used(unsigned ext_j) const { bool external_is_used(unsigned ext_j) const {
auto it = m_external_to_local.find(ext_j); return try_get_value(m_external_to_local, ext_j).has_value();
return it != m_external_to_local.end();
} }
bool external_is_used(unsigned ext_j, unsigned & local_j ) const { bool external_is_used(unsigned ext_j, unsigned & local_j ) const {
auto it = m_external_to_local.find(ext_j); if (auto local = try_get_value(m_external_to_local, ext_j)) {
if ( it == m_external_to_local.end()) { local_j = *local;
local_j = UINT_MAX; return true;
return false;
} }
local_j = it->second; local_j = UINT_MAX;
return true; return false;
} }
bool external_is_used(unsigned ext_j, unsigned & local_j, bool & is_int ) const { bool external_is_used(unsigned ext_j, unsigned & local_j, bool & is_int ) const {
auto it = m_external_to_local.find(ext_j); if (auto local = try_get_value(m_external_to_local, ext_j)) {
if ( it == m_external_to_local.end()){ local_j = *local;
local_j = UINT_MAX; is_int = m_local_to_external[local_j].is_integer();
return false; return true;
} }
local_j = it->second; local_j = UINT_MAX;
is_int = m_local_to_external[local_j].is_integer(); return false;
return true;
} }
bool has_int_var() const { bool has_int_var() const {