diff --git a/src/cmd_context/pdecl.h b/src/cmd_context/pdecl.h index d994a1e8f..8742424c3 100644 --- a/src/cmd_context/pdecl.h +++ b/src/cmd_context/pdecl.h @@ -36,7 +36,7 @@ protected: void inc_ref() { m_ref_count++; } void dec_ref() { SASSERT(m_ref_count > 0); --m_ref_count; } virtual bool is_psort() const { return false; } - virtual size_t obj_size() const = 0; + virtual size_t obj_size() const { UNREACHABLE(); return sizeof(*this); } pdecl(unsigned id, unsigned num_params):m_id(id), m_num_params(num_params), m_ref_count(0) {} virtual void finalize(pdecl_manager & m) {} virtual ~pdecl() {} @@ -74,9 +74,9 @@ public: virtual bool is_sort_wrapper() const { return false; } virtual sort * instantiate(pdecl_manager & m, sort * const * s) { return nullptr; } // we use hash-consing for psorts. - virtual char const * hcons_kind() const = 0; - virtual unsigned hcons_hash() const = 0; - virtual bool hcons_eq(psort const * other) const = 0; + virtual char const * hcons_kind() const { UNREACHABLE(); return nullptr; } + virtual unsigned hcons_hash() const { UNREACHABLE(); return 0; } + virtual bool hcons_eq(psort const * other) const { UNREACHABLE(); return false; } void reset_cache(pdecl_manager& m) override; }; diff --git a/src/util/ref_vector.h b/src/util/ref_vector.h index 491f1515f..f45b44dae 100644 --- a/src/util/ref_vector.h +++ b/src/util/ref_vector.h @@ -306,28 +306,52 @@ public: // prevent abuse: ref_vector & operator=(ref_vector const & other) = delete; - ref_vector&& filter(std::function& predicate) { + bool containsp(std::function& predicate) const { + for (auto const& t : *this) + if (predicate(t)) + return true; + return false; + } + + ref_vector filter_pure(std::function& predicate) const { ref_vector result(m()); for (auto& t : *this) - if (predicate(t)) result.push_back(t); + if (predicate(t)) + result.push_back(t); return result; } + ref_vector& filter_update(std::function& predicate) { + unsigned j = 0; + for (auto& t : *this) + if (predicate(t)) + set(j++, t); + shrink(j); + return *this; + } + template - vector&& map(std::function& f) { + vector mapv_pure(std::function& f) const { vector result; for (auto& t : *this) result.push_back(f(t)); return result; } - ref_vector&& map(std::function& f) { + ref_vector map_pure(std::function& f) const { ref_vector result(m()); for (auto& t : *this) result.push_back(f(t)); return result; } + ref_vector& map_update(std::function& f) { + unsigned j = 0; + for (auto& t : *this) + set(j++, f(t)); + return *this; + } + }; diff --git a/src/util/vector.h b/src/util/vector.h index 7724841c1..acbcd1357 100644 --- a/src/util/vector.h +++ b/src/util/vector.h @@ -231,21 +231,51 @@ public: return *this; } - vector&& filter(std::function& predicate) { + bool containsp(std::function& predicate) const { + for (auto const& t : *this) + if (predicate(t)) + return true; + return false; + } + + /** + * retain elements that satisfy predicate. aka 'where'. + */ + vector filter_pure(std::function& predicate) const { vector result; for (auto& t : *this) - if (predicate(t)) result.push_back(t); + if (predicate(t)) + result.push_back(t); return result; } + vector& filter_update(std::function& predicate) { + unsigned j = 0; + for (auto& t : *this) + if (predicate(t)) + set(j++, t); + shrink(j); + return *this; + } + + /** + * update elements using f, aka 'select' + */ template - vector&& map(std::function& f) { + vector map_pure(std::function& f) const { vector result; for (auto& t : *this) result.push_back(f(t)); return result; } + vector& map_update(std::function& f) { + unsigned j = 0; + for (auto& t : *this) + set(j++, f(t)); + return *this; + } + void reset() { if (m_data) { if (CallDestructors) {