3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-29 11:55:51 +00:00

fix vector<> to support non-POD types

adjust code to std::move and avoid unnecessary/illegal
This commit is contained in:
Nuno Lopes 2017-10-10 17:24:22 +01:00
parent 4d1acadabb
commit 9b54b4e784
35 changed files with 253 additions and 95 deletions

View file

@ -26,6 +26,7 @@ Revision History:
#include "util/debug.h"
#include<algorithm>
#include<type_traits>
#include<memory.h>
#include "util/memory_manager.h"
#include "util/hash.h"
@ -74,9 +75,24 @@ class vector {
if (new_capacity <= old_capacity || new_capacity_T <= old_capacity_T) {
throw default_exception("Overflow encountered when expanding vector");
}
SZ *mem = (SZ*)memory::reallocate(reinterpret_cast<SZ*>(m_data)-2, new_capacity_T);
SZ *mem, *old_mem = reinterpret_cast<SZ*>(m_data) - 2;
if (std::is_trivially_copyable<T>::value) {
mem = (SZ*)memory::reallocate(old_mem, new_capacity_T);
} else {
mem = (SZ*)memory::allocate(new_capacity_T);
}
auto old_data = m_data;
auto old_size = size();
*mem = new_capacity;
m_data = reinterpret_cast<T *>(mem + 2);
if (!std::is_trivially_copyable<T>::value) {
static_assert(std::is_move_constructible<T>::value, "");
int i = 0;
for (auto I = old_data; I != old_data + old_size; ++I) {
new (&m_data[i++]) T(std::move(*I));
}
memory::deallocate(old_mem);
}
}
}
@ -148,6 +164,10 @@ public:
SASSERT(size() == source.size());
}
vector(vector&& other) noexcept : m_data(0) {
std::swap(m_data, other.m_data);
}
vector(SZ s, T const * data):
m_data(0) {
for (SZ i = 0; i < s; i++) {
@ -179,6 +199,16 @@ public:
return *this;
}
vector & operator=(vector && source) {
if (this == &source) {
return *this;
}
destroy();
m_data = 0;
std::swap(m_data, source.m_data);
return *this;
}
void reset() {
if (m_data) {
if (CallDestructors) {
@ -292,6 +322,11 @@ public:
m_data[idx] = val;
}
void set(SZ idx, T && val) {
SASSERT(idx < size());
m_data[idx] = std::move(val);
}
T & back() {
SASSERT(!empty());
return operator[](size() - 1);
@ -318,6 +353,14 @@ public:
reinterpret_cast<SZ *>(m_data)[SIZE_IDX]++;
}
void push_back(T && elem) {
if (m_data == 0 || reinterpret_cast<SZ *>(m_data)[SIZE_IDX] == reinterpret_cast<SZ *>(m_data)[CAPACITY_IDX]) {
expand_vector();
}
new (m_data + reinterpret_cast<SZ *>(m_data)[SIZE_IDX]) T(std::move(elem));
reinterpret_cast<SZ *>(m_data)[SIZE_IDX]++;
}
void insert(T const & elem) {
push_back(elem);
}
@ -357,7 +400,8 @@ public:
}
}
void resize(SZ s, T const & elem=T()) {
template<typename Args>
void resize(SZ s, Args args...) {
SZ sz = size();
if (s <= sz) { shrink(s); return; }
while (s > capacity()) {
@ -367,8 +411,23 @@ public:
reinterpret_cast<SZ *>(m_data)[SIZE_IDX] = s;
iterator it = m_data + sz;
iterator end = m_data + s;
for(; it != end; ++it) {
new (it) T(elem);
for (; it != end; ++it) {
new (it) T(std::forward<Args>(args));
}
}
void resize(SZ s) {
SZ sz = size();
if (s <= sz) { shrink(s); return; }
while (s > capacity()) {
expand_vector();
}
SASSERT(m_data != 0);
reinterpret_cast<SZ *>(m_data)[SIZE_IDX] = s;
iterator it = m_data + sz;
iterator end = m_data + s;
for (; it != end; ++it) {
new (it) T();
}
}
@ -439,10 +498,15 @@ public:
return m_data[idx];
}
void reserve(SZ s, T const & d = T()) {
void reserve(SZ s, T const & d) {
if (s > size())
resize(s, d);
}
void reserve(SZ s) {
if (s > size())
resize(s);
}
};
template<typename T>
@ -452,7 +516,12 @@ public:
ptr_vector(unsigned s):vector<T *, false>(s) {}
ptr_vector(unsigned s, T * elem):vector<T *, false>(s, elem) {}
ptr_vector(ptr_vector const & source):vector<T *, false>(source) {}
ptr_vector(ptr_vector && other) noexcept : vector<T*, false>(std::move(other)) {}
ptr_vector(unsigned s, T * const * data):vector<T *, false>(s, const_cast<T**>(data)) {}
ptr_vector & operator=(ptr_vector const & source) {
vector<T *, false>::operator=(source);
return *this;
}
};
template<typename T, typename SZ = unsigned>
@ -462,7 +531,12 @@ public:
svector(SZ s):vector<T, false, SZ>(s) {}
svector(SZ s, T const & elem):vector<T, false, SZ>(s, elem) {}
svector(svector const & source):vector<T, false, SZ>(source) {}
svector(svector && other) noexcept : vector<T, false, SZ>(std::move(other)) {}
svector(SZ s, T const * data):vector<T, false, SZ>(s, data) {}
svector & operator=(svector const & source) {
vector<T, false, SZ>::operator=(source);
return *this;
}
};
typedef svector<int> int_vector;
@ -494,23 +568,4 @@ struct vector_hash : public vector_hash_tpl<Hash, vector<typename Hash::data> >
template<typename Hash>
struct svector_hash : public vector_hash_tpl<Hash, svector<typename Hash::data> > {};
#include <vector>
// Specialize vector<std::string> to be an instance of std::vector instead.
// This will catch any regression of issue #564 and #420.
template <>
class vector<std::string, true, unsigned> : public std::vector<std::string> {
public:
vector(vector<std::string, true, unsigned> const& other): std::vector<std::string>(other) {}
vector(size_t sz, char const* s): std::vector<std::string>(sz, s) {}
vector() {}
void reset() { clear(); }
};
#endif /* VECTOR_H_ */