3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-01-28 12:58:43 +00:00

Refactor counter::get_max_positive to use std::optional (#8289)

* Initial plan

* Refactor counter::get_max_positive to use std::optional

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

* Fix C++17 compatibility by replacing transform() with has_value()/value()

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

* Address code review: avoid calling get_max_positive twice

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-22 17:39:53 -08:00 committed by GitHub
parent 6feb3391a2
commit 7f91a3321d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 22 additions and 24 deletions

View file

@ -49,21 +49,18 @@ void counter::collect_positive(uint_set & acc) const {
acc.insert(kv.m_key);
}
bool counter::get_max_positive(unsigned & res) const {
bool found = false;
std::optional<unsigned> counter::get_max_positive() const {
std::optional<unsigned> result;
for (auto const& kv : *this) {
if (kv.m_value > 0 && (!found || kv.m_key > res) ) {
found = true;
res = kv.m_key;
if (kv.m_value > 0 && (!result || kv.m_key > *result)) {
result = kv.m_key;
}
}
return found;
return result;
}
unsigned counter::get_max_positive() const {
unsigned max_pos;
VERIFY(get_max_positive(max_pos));
return max_pos;
unsigned counter::get_max_positive_or_zero() const {
return get_max_positive().value_or(0);
}
int counter::get_max_counter_value() const {

View file

@ -23,6 +23,7 @@ Revision History:
#pragma once
#include <optional>
#include "ast/ast.h"
#include "util/map.h"
#include "util/uint_set.h"
@ -55,8 +56,8 @@ public:
void collect_positive(uint_set & acc) const;
unsigned get_positive_count() const;
bool get_max_positive(unsigned & res) const;
unsigned get_max_positive() const;
std::optional<unsigned> get_max_positive() const;
unsigned get_max_positive_or_zero() const;
/**
Since the default counter value of a counter is zero, the result is never negative.