Skip to content

Commit

Permalink
Update ex14_18_StrVec.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Jun 28, 2015
1 parent f19f9dd commit e52e853
Showing 1 changed file with 58 additions and 58 deletions.
116 changes: 58 additions & 58 deletions ch14/ex14_18_StrVec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,143 +3,143 @@

void StrVec::push_back(const std::string &s)
{
chk_n_alloc();
alloc.construct(first_free++, s);
chk_n_alloc();
alloc.construct(first_free++, s);
}

std::pair<std::string*, std::string*>
StrVec::alloc_n_copy(const std::string *b, const std::string *e)
{
auto data = alloc.allocate(e-b);
return { data, std::uninitialized_copy(b, e, data) };
auto data = alloc.allocate(e - b);
return{ data, std::uninitialized_copy(b, e, data) };
}

void StrVec::free()
{
if (elements) {
for_each(elements, first_free, [this](std::string &rhs){ alloc.destroy(&rhs); });
alloc.deallocate(elements, cap - elements);
}
if (elements) {
for_each(elements, first_free, [this](std::string &rhs){ alloc.destroy(&rhs); });
alloc.deallocate(elements, cap - elements);
}
}

void StrVec::range_initialize(const std::string *first, const std::string *last)
{
auto newdata = alloc_n_copy(first, last);
elements = newdata.first;
first_free = cap = newdata.second;
auto newdata = alloc_n_copy(first, last);
elements = newdata.first;
first_free = cap = newdata.second;
}

StrVec::StrVec(const StrVec &rhs)
{
range_initialize(rhs.begin(), rhs.end());
range_initialize(rhs.begin(), rhs.end());
}

StrVec::StrVec(std::initializer_list<std::string> il)
{
range_initialize(il.begin(), il.end());
range_initialize(il.begin(), il.end());
}

StrVec::~StrVec()
{
free();
free();
}

StrVec& StrVec::operator = (const StrVec &rhs)
{
auto data = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = data.first;
first_free = cap = data.second;
return *this;
auto data = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = data.first;
first_free = cap = data.second;
return *this;
}

void StrVec::alloc_n_move(size_t new_cap)
{
auto newdata = alloc.allocate(new_cap);
auto dest = newdata;
auto elem = elements;
for (size_t i = 0; i != size(); ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
first_free = dest;
cap = elements + new_cap;
auto newdata = alloc.allocate(new_cap);
auto dest = newdata;
auto elem = elements;
for (size_t i = 0; i != size(); ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
first_free = dest;
cap = elements + new_cap;
}

void StrVec::reallocate()
{
auto newcapacity = size() ? 2 * size() : 1;
alloc_n_move(newcapacity);
auto newcapacity = size() ? 2 * size() : 1;
alloc_n_move(newcapacity);
}

void StrVec::reserve(size_t new_cap)
{
if (new_cap <= capacity()) return;
alloc_n_move(new_cap);
if (new_cap <= capacity()) return;
alloc_n_move(new_cap);
}

void StrVec::resize(size_t count)
{
resize(count, std::string());
resize(count, std::string());
}

void StrVec::resize(size_t count, const std::string &s)
{
if (count > size()) {
if (count > capacity()) reserve(count * 2);
for (size_t i = size(); i != count; ++i)
alloc.construct(first_free++, s);
}
else if (count < size()) {
while (first_free != elements + count)
alloc.destroy(--first_free);
}
if (count > size()) {
if (count > capacity()) reserve(count * 2);
for (size_t i = size(); i != count; ++i)
alloc.construct(first_free++, s);
}
else if (count < size()) {
while (first_free != elements + count)
alloc.destroy(--first_free);
}
}

StrVec::StrVec(StrVec &&s) NOEXCEPT : elements(s.elements), first_free(s.first_free), cap(s.cap)
{
// leave s in a state in which it is safe to run the destructor.
s.elements = s.first_free = s.cap = nullptr;
// leave s in a state in which it is safe to run the destructor.
s.elements = s.first_free = s.cap = nullptr;
}

StrVec& StrVec::operator = (StrVec &&rhs) NOEXCEPT
{
if (this != &rhs) {
free();
elements = rhs.elements;
first_free = rhs.first_free;
cap = rhs.cap;
rhs.elements = rhs.first_free = rhs.cap = nullptr;
}
return *this;
if (this != &rhs) {
free();
elements = rhs.elements;
first_free = rhs.first_free;
cap = rhs.cap;
rhs.elements = rhs.first_free = rhs.cap = nullptr;
}
return *this;
}

bool operator==(const StrVec &lhs, const StrVec &rhs)
{
return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()));
return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()));
}

bool operator!=(const StrVec &lhs, const StrVec &rhs)
{
return !(lhs == rhs);
return !(lhs == rhs);
}

bool operator<(const StrVec &lhs, const StrVec &rhs)
{
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}

bool operator>(const StrVec &lhs, const StrVec &rhs)
{
return rhs < lhs;
return rhs < lhs;
}

bool operator<=(const StrVec &lhs, const StrVec &rhs)
{
return !(rhs < lhs);
return !(rhs < lhs);
}

bool operator>=(const StrVec &lhs, const StrVec &rhs)
{
return !(lhs < rhs);
return !(lhs < rhs);
}

0 comments on commit e52e853

Please sign in to comment.