3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-01-23 18:44:02 +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() {
for (unsigned j : m_changed_f_columns) {
const auto it = m_columns_to_terms.find(j);
if (it != m_columns_to_terms.end())
for (unsigned k : it->second) {
if (auto terms = try_get_value(m_columns_to_terms, j)) {
for (unsigned k : *terms) {
mark_term_change(k);
}
}
if (!m_var_register.external_is_used(j))
continue;
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 (coeff_map.size() != 1)
return false;
auto it = coeff_map.find(be.m_j);
if (it == coeff_map.end()) return false;
mpq ratio = it->second;
if (ratio < zero_of_type<mpq>()) {
kind = static_cast<lconstraint_kind>(-kind);
if (auto ratio_opt = try_get_value(coeff_map, be.m_j)) {
mpq ratio = *ratio_opt;
if (ratio < zero_of_type<mpq>()) {
kind = static_cast<lconstraint_kind>(-kind);
}
rs_of_evidence /= ratio;
} else {
return false;
}
rs_of_evidence /= ratio;
}
else {
lar_term const& t = get_term(be.m_j);
auto first_coeff = t.begin();
unsigned j = (*first_coeff).j();
auto it = coeff_map.find(j);
if (it == coeff_map.end())
if (auto ratio_opt = try_get_value(coeff_map, j)) {
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;
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;
@ -2716,10 +2720,9 @@ namespace lp {
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";);
SASSERT(c.is_normalized());
auto it = m_imp->m_normalized_terms_to_columns.find(c);
if (it != m_imp->m_normalized_terms_to_columns.end()) {
TRACE(lar_solver_terms, tout << "got " << it->second << "\n";);
a_j = it->second;
if (auto result = try_get_value(m_imp->m_normalized_terms_to_columns, c)) {
TRACE(lar_solver_terms, tout << "got " << *result << "\n";);
a_j = *result;
return true;
}
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>
std::optional<B> try_get_value(const std::unordered_map<A,B> & map, const A& key) {
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,Hash,KeyEqual> & map, const A& key) {
const auto it = map.find(key);
if (it == map.end())
return std::nullopt;

View file

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