diff --git a/src/ast/rewriter/ast_counter.cpp b/src/ast/rewriter/ast_counter.cpp index 17ac30b60..84d24506e 100644 --- a/src/ast/rewriter/ast_counter.cpp +++ b/src/ast/rewriter/ast_counter.cpp @@ -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 counter::get_max_positive() const { + std::optional 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 { diff --git a/src/ast/rewriter/ast_counter.h b/src/ast/rewriter/ast_counter.h index fb997c35b..797b118ac 100644 --- a/src/ast/rewriter/ast_counter.h +++ b/src/ast/rewriter/ast_counter.h @@ -23,6 +23,7 @@ Revision History: #pragma once +#include #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 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. diff --git a/src/muz/base/dl_util.cpp b/src/muz/base/dl_util.cpp index 449543e89..975d4e721 100644 --- a/src/muz/base/dl_util.cpp +++ b/src/muz/base/dl_util.cpp @@ -486,7 +486,7 @@ namespace datalog { DEBUG_CODE( counter ctr; ctr.count(permutation); - SASSERT(permutation.empty() || ctr.get_max_positive()==permutation.size()-1); + SASSERT(permutation.empty() || ctr.get_max_positive().value_or(0)==permutation.size()-1); SASSERT(permutation.empty() || ctr.get_positive_count()==permutation.size()); ); unsigned sz = permutation.size(); diff --git a/src/muz/rel/dl_mk_explanations.cpp b/src/muz/rel/dl_mk_explanations.cpp index d61b5036e..d205ddc58 100644 --- a/src/muz/rel/dl_mk_explanations.cpp +++ b/src/muz/rel/dl_mk_explanations.cpp @@ -585,7 +585,7 @@ namespace datalog { } counter ctr; ctr.count(joined_col_cnt, tgt_cols); - if (ctr.get_max_counter_value()>1 || (joined_col_cnt && ctr.get_max_positive()!=joined_col_cnt-1)) { + if (ctr.get_max_counter_value()>1 || (joined_col_cnt && ctr.get_max_positive().value_or(0)!=joined_col_cnt-1)) { return nullptr; } return alloc(intersection_filter_fn, *this); @@ -712,8 +712,8 @@ namespace datalog { rule * mk_explanations::get_e_rule(rule * r) { rule_counter ctr; ctr.count_rule_vars(r); - unsigned max_var; - unsigned next_var = ctr.get_max_positive(max_var) ? (max_var+1) : 0; + auto max_var = ctr.get_max_positive(); + unsigned next_var = max_var.has_value() ? (*max_var + 1) : 0; unsigned head_var = next_var++; app_ref e_head(get_e_lit(r->get_head(), head_var), m_manager); diff --git a/src/muz/rel/dl_mk_similarity_compressor.cpp b/src/muz/rel/dl_mk_similarity_compressor.cpp index b99c957da..77d106e0c 100644 --- a/src/muz/rel/dl_mk_similarity_compressor.cpp +++ b/src/muz/rel/dl_mk_similarity_compressor.cpp @@ -382,9 +382,9 @@ namespace datalog { rule_counter ctr; ctr.count_rule_vars(r); - unsigned max_var_idx, new_var_idx_base; - if (ctr.get_max_positive(max_var_idx)) { - new_var_idx_base = max_var_idx+1; + unsigned new_var_idx_base; + if (auto max_var_idx = ctr.get_max_positive()) { + new_var_idx_base = *max_var_idx + 1; } else { new_var_idx_base = 0; diff --git a/src/muz/rel/dl_sparse_table.cpp b/src/muz/rel/dl_sparse_table.cpp index 30de4cd1f..b59260863 100644 --- a/src/muz/rel/dl_sparse_table.cpp +++ b/src/muz/rel/dl_sparse_table.cpp @@ -386,7 +386,7 @@ namespace datalog { } counter ctr; ctr.count(key_len, key_cols); - if (ctr.get_max_counter_value()!=1 || ctr.get_max_positive()!=non_func_cols-1) { + if (ctr.get_max_counter_value()!=1 || ctr.get_max_positive().value_or(0)!=non_func_cols-1) { return false; } SASSERT(ctr.get_positive_count() == non_func_cols); @@ -467,7 +467,7 @@ namespace datalog { //Maybe we might keep a list of indexes that contain functional columns and on an update reset //only those. SASSERT(key_len == 0 || - counter().count(key_len, key_cols).get_max_positive()=s1.first_functional() - || counter().count(col_cnt, cols2).get_max_positive()>=s2.first_functional(); + return counter().count(col_cnt, cols1).get_max_positive().value_or(0)>=s1.first_functional() + || counter().count(col_cnt, cols2).get_max_positive().value_or(0)>=s2.first_functional(); } @@ -1138,7 +1138,7 @@ namespace datalog { ctr.count(m_cols2); m_joining_neg_non_functional = ctr.get_max_counter_value() == 1 && ctr.get_positive_count() == neg_first_func - && (neg_first_func == 0 || ctr.get_max_positive() == neg_first_func-1); + && (neg_first_func == 0 || ctr.get_max_positive().value_or(0) == neg_first_func-1); } /**