3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-06-20 21:03:39 +00:00

encapsulate anum functionality

This commit is contained in:
Nikolaj Bjorner 2024-01-20 12:59:50 -08:00
parent 548be4c1f9
commit 17545233e6
2 changed files with 26 additions and 19 deletions

View file

@ -204,13 +204,13 @@ namespace algebraic_numbers {
} }
void del(numeral & a) { void del(numeral & a) {
if (a.m_cell == nullptr) if (a.is_null())
return; return;
if (a.is_basic()) if (a.is_basic())
del(a.to_basic()); del(a.to_basic());
else else
del(a.to_algebraic()); del(a.to_algebraic());
a.m_cell = nullptr; a.clear();
} }
void reset(numeral & a) { void reset(numeral & a) {
@ -218,7 +218,7 @@ namespace algebraic_numbers {
} }
bool is_zero(numeral const & a) { bool is_zero(numeral const & a) {
return a.m_cell == nullptr; return a.is_null();
} }
bool is_pos(numeral const & a) { bool is_pos(numeral const & a) {
@ -359,7 +359,7 @@ namespace algebraic_numbers {
} }
void swap(numeral & a, numeral & b) noexcept { void swap(numeral & a, numeral & b) noexcept {
std::swap(a.m_cell, b.m_cell); a.swap(b);
} }
basic_cell * mk_basic_cell(mpq & n) { basic_cell * mk_basic_cell(mpq & n) {
@ -432,13 +432,13 @@ namespace algebraic_numbers {
} }
if (a.is_basic()) { if (a.is_basic()) {
if (is_zero(a)) if (is_zero(a))
a.m_cell = mk_basic_cell(n); a = mk_basic_cell(n);
else else
qm().set(a.to_basic()->m_value, n); qm().set(a.to_basic()->m_value, n);
} }
else { else {
del(a); del(a);
a.m_cell = mk_basic_cell(n); a = mk_basic_cell(n);
} }
} }
@ -492,7 +492,7 @@ namespace algebraic_numbers {
else { else {
if (a.is_basic()) { if (a.is_basic()) {
del(a); del(a);
a.m_cell = TAG(void*, mk_algebraic_cell(sz, p, lower, upper, minimal), ROOT); a = mk_algebraic_cell(sz, p, lower, upper, minimal);
} }
else { else {
SASSERT(sz > 2); SASSERT(sz > 2);
@ -526,7 +526,7 @@ namespace algebraic_numbers {
del(a); del(a);
void * mem = m_allocator.allocate(sizeof(algebraic_cell)); void * mem = m_allocator.allocate(sizeof(algebraic_cell));
algebraic_cell * c = new (mem) algebraic_cell(); algebraic_cell * c = new (mem) algebraic_cell();
a.m_cell = TAG(void *, c, ROOT); a = c;
copy(c, b.to_algebraic()); copy(c, b.to_algebraic());
SASSERT(acell_inv(*c)); SASSERT(acell_inv(*c));
} }
@ -796,7 +796,7 @@ namespace algebraic_numbers {
scoped_mpq r(qm()); scoped_mpq r(qm());
to_mpq(qm(), lower(c), r); to_mpq(qm(), lower(c), r);
del(c); del(c);
a.m_cell = mk_basic_cell(r); a = mk_basic_cell(r);
return false; return false;
} }
} }
@ -817,7 +817,7 @@ namespace algebraic_numbers {
scoped_mpq r(qm()); scoped_mpq r(qm());
to_mpq(qm(), lower(c), r); to_mpq(qm(), lower(c), r);
del(c); del(c);
a.m_cell = mk_basic_cell(r); a = mk_basic_cell(r);
return false; return false;
} }
SASSERT(acell_inv(*c)); SASSERT(acell_inv(*c));

View file

@ -360,19 +360,25 @@ namespace algebraic_numbers {
struct basic_cell; struct basic_cell;
struct algebraic_cell; struct algebraic_cell;
enum anum_kind { BASIC = 0, ROOT };
class anum {
enum anum_kind { BASIC = 0, ROOT };
void* m_cell;
public:
anum() :m_cell(nullptr) {}
anum(basic_cell* cell) :m_cell(TAG(void*, cell, BASIC)) { }
anum(algebraic_cell * cell):m_cell(TAG(void*, cell, ROOT)) { }
class anum {
friend struct manager::imp;
friend class manager;
void * m_cell;
anum(basic_cell * cell):m_cell(TAG(void*, cell, BASIC)) {}
anum(algebraic_cell * cell):m_cell(TAG(void*, cell, ROOT)) {}
bool is_basic() const { return GET_TAG(m_cell) == BASIC; } bool is_basic() const { return GET_TAG(m_cell) == BASIC; }
basic_cell * to_basic() const { SASSERT(is_basic()); return UNTAG(basic_cell*, m_cell); } basic_cell * to_basic() const { SASSERT(is_basic()); return UNTAG(basic_cell*, m_cell); }
algebraic_cell * to_algebraic() const { SASSERT(!is_basic()); return UNTAG(algebraic_cell*, m_cell); } algebraic_cell * to_algebraic() const { SASSERT(!is_basic()); return UNTAG(algebraic_cell*, m_cell); }
public:
anum():m_cell(nullptr) {} bool is_null() const { return m_cell == nullptr; }
void clear() { m_cell = nullptr; }
void swap(anum & other) { std::swap(m_cell, other.m_cell); }
anum& operator=(basic_cell* cell) { SASSERT(is_null()); m_cell = TAG(void*, cell, BASIC); return *this; }
anum& operator=(algebraic_cell* cell) { SASSERT(is_null()); m_cell = TAG(void*, cell, ROOT); return *this; }
}; };
}; };
@ -428,6 +434,7 @@ AN_MK_BINARY(operator/, div)
#undef AN_MK_BINARY #undef AN_MK_BINARY
#undef AN_MK_BINARY_CORE #undef AN_MK_BINARY_CORE
inline scoped_anum root(scoped_anum const & a, unsigned k) { inline scoped_anum root(scoped_anum const & a, unsigned k) {
scoped_anum r(a.m()); scoped_anum r(a.m());
a.m().root(a, k, r); a.m().root(a, k, r);