Skip to content

Commit

Permalink
add new permutation class invariant (#7299)
Browse files Browse the repository at this point in the history
* new heap invariants

* change ENSURE to SASSERT for unit test heap

* change SASSERT to VERIFY

* new permutation invariant

* remove heap changes

* use bool_vector
  • Loading branch information
ChuyueSun committed Jul 20, 2024
1 parent 5003d41 commit 49dc1bb
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/util/permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void permutation::swap(unsigned i, unsigned j) noexcept {
unsigned j_prime = m_p[j];
std::swap(m_p[i], m_p[j]);
std::swap(m_inv_p[i_prime], m_inv_p[j_prime]);
SASSERT(check_invariant());
}

/**
Expand Down Expand Up @@ -66,11 +67,19 @@ void permutation::display(std::ostream & out) const {
bool permutation::check_invariant() const {
SASSERT(m_p.size() == m_inv_p.size());
unsigned n = m_p.size();
bool_vector check_vector(n, false); // To check for duplicate and out-of-range values
for (unsigned i = 0; i < n; i++) {
unsigned pi = m_p[i];
SASSERT(m_p[i] < n);
SASSERT(m_inv_p[i] < n);
SASSERT(m_p[m_inv_p[i]] == i);
SASSERT(m_inv_p[m_p[i]] == i);
// Check the inversion hasn't been checked yet
if (check_vector[pi]) {
return false;
}
// Mark this value as checked
check_vector[pi] = true;
}
return true;
}

0 comments on commit 49dc1bb

Please sign in to comment.