Skip to content

Commit 97d4508

Browse files
authored
Vector updates from polysat branch (#7066)
* vector: add erase_if * vector: generalize operator<< * vector: fix missing destructor call
1 parent 4c9f705 commit 97d4508

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/util/vector.h

+18-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Revision History:
3030
#include <functional>
3131
#include <memory>
3232
#include <type_traits>
33+
#include <utility>
3334
#include "util/memory_manager.h"
3435
#include "util/hash.h"
3536
#include "util/z3_exception.h"
@@ -558,7 +559,7 @@ class vector {
558559
for(; pos != e; ++pos, ++prev) {
559560
*prev = std::move(*pos);
560561
}
561-
reinterpret_cast<SZ *>(m_data)[SIZE_IDX]--;
562+
pop_back();
562563
}
563564

564565
void erase(T const & elem) {
@@ -568,6 +569,20 @@ class vector {
568569
}
569570
}
570571

572+
/** Erase all elements that satisfy the given predicate. Returns the number of erased elements. */
573+
template <typename UnaryPredicate>
574+
SZ erase_if(UnaryPredicate should_erase) {
575+
iterator i = begin();
576+
iterator const e = end();
577+
for (iterator j = begin(); j != e; ++j)
578+
if (!should_erase(std::as_const(*j)))
579+
*(i++) = std::move(*j);
580+
SZ const count = e - i;
581+
SASSERT_EQ(i - begin(), size() - count);
582+
shrink(size() - count);
583+
return count;
584+
}
585+
571586
void shrink(SZ s) {
572587
if (m_data) {
573588
SASSERT(s <= reinterpret_cast<SZ *>(m_data)[SIZE_IDX]);
@@ -756,7 +771,8 @@ using bool_vector = svector<bool>;
756771

757772
template<typename T>
758773
inline std::ostream& operator<<(std::ostream& out, svector<T> const& v) {
759-
for (unsigned u : v) out << u << " ";
774+
for (auto const& x : v)
775+
out << x << " ";
760776
return out;
761777
}
762778

0 commit comments

Comments
 (0)