Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions include/clue/fast_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ template<typename T>
struct relocate_policy<T, false> {
static void move_disjoint(T* dst, T* src, T* src_end) {
while (src != src_end) {
new(dst++) T( std::move(*src++) );
new(dst++) T( std::move(*src) );
(src++)->~T();
}
}

static void move_fwd(T* dst, T* src, T* src_end) {
while (src != src_end) {
new(dst++) T( std::move(*src++) );
new(dst++) T( std::move(*src) );
(src++)->~T();
}
}

Expand All @@ -75,7 +77,8 @@ struct relocate_policy<T, false> {
src += (n - 1);
dst += (n - 1);
while (src != src_rend) {
new(dst--) T( std::move(*src--) );
new(dst--) T( std::move(*src) );
(src--)->~T();
}
}
}
Expand Down Expand Up @@ -352,7 +355,6 @@ class fast_vector final {
relocater::move_disjoint(pb_, other.begin(), other.end());
pn_ = pb_ + n;
}
other.destroy();
other.reset();
}
}
Expand Down Expand Up @@ -591,7 +593,7 @@ class fast_vector final {
relocater::move_disjoint(ss_.begin(), pb_, pn_);

// release memory
alloc_.deallocate(pb_, n);
alloc_.deallocate(pb_, cur_cap);

// set pointers on static array
reset();
Expand All @@ -616,14 +618,13 @@ class fast_vector final {
// move elements to tmp
size_type n = size();
if (n > 0) {
pe_ = pb_;
relocater::move_disjoint(tmp.begin(), pb_, pb_ + n);
tmp.pn_ = tmp.pb_ + n;
}

// release own memory
if (use_dynamic()) {
alloc_.deallocate(pb_, n);
alloc_.deallocate(pb_, capacity());
}
reset();

Expand Down Expand Up @@ -651,6 +652,7 @@ class fast_vector final {
// 2. move the elements in range [pos, end) towards back
// 3. set end() <- end() + n
// 4. return a non-const version of pos
// after move_back, the elements/iterators in [pos, pos + n) are invalid.
iterator move_back(const_iterator pos, size_type n) {
iterator p = const_cast<iterator>(pos);
if (n > 0) {
Expand Down
10 changes: 7 additions & 3 deletions tests/test_fast_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class Val final {
public:
long* pv_;

explicit Val(std::nullptr_t) : pv_(nullptr) {}
explicit Val(std::nullptr_t) : pv_(nullptr) {
count_object++;
}

explicit Val() : pv_(new long(init)) {
count_object++;
Expand All @@ -28,14 +30,16 @@ class Val final {

explicit Val(Val&& r) noexcept : pv_(r.pv_) {
r.pv_ = nullptr;
count_object++;
}

~Val() {
if (pv_) {
CLUE_ASSERT(count_object > 0);

delete pv_;
count_object--;
}
CLUE_ASSERT(count_object > 0);
count_object--;
}

long get() const { return *pv_; }
Expand Down