Skip to content

Commit

Permalink
Vector updates from polysat branch (#7066)
Browse files Browse the repository at this point in the history
* vector: add erase_if

* vector: generalize operator<<

* vector: fix missing destructor call
  • Loading branch information
JakobR committed Dec 19, 2023
1 parent 4c9f705 commit 97d4508
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/util/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Revision History:
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
#include "util/memory_manager.h"
#include "util/hash.h"
#include "util/z3_exception.h"
Expand Down Expand Up @@ -558,7 +559,7 @@ class vector {
for(; pos != e; ++pos, ++prev) {
*prev = std::move(*pos);
}
reinterpret_cast<SZ *>(m_data)[SIZE_IDX]--;
pop_back();
}

void erase(T const & elem) {
Expand All @@ -568,6 +569,20 @@ class vector {
}
}

/** Erase all elements that satisfy the given predicate. Returns the number of erased elements. */
template <typename UnaryPredicate>
SZ erase_if(UnaryPredicate should_erase) {
iterator i = begin();
iterator const e = end();
for (iterator j = begin(); j != e; ++j)
if (!should_erase(std::as_const(*j)))
*(i++) = std::move(*j);
SZ const count = e - i;
SASSERT_EQ(i - begin(), size() - count);
shrink(size() - count);
return count;
}

void shrink(SZ s) {
if (m_data) {
SASSERT(s <= reinterpret_cast<SZ *>(m_data)[SIZE_IDX]);
Expand Down Expand Up @@ -756,7 +771,8 @@ using bool_vector = svector<bool>;

template<typename T>
inline std::ostream& operator<<(std::ostream& out, svector<T> const& v) {
for (unsigned u : v) out << u << " ";
for (auto const& x : v)
out << x << " ";
return out;
}

Expand Down

0 comments on commit 97d4508

Please sign in to comment.