Skip to content

Commit

Permalink
Сorrections
Browse files Browse the repository at this point in the history
Ветки объединены в одну
Функция copy вынесена за пределы класса
Изменена гарантия operator= на strong
  • Loading branch information
Alena Egorova committed Sep 21, 2016
1 parent 389d35d commit 72c587f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
56 changes: 32 additions & 24 deletions include/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,50 @@
#ifndef STACK_CPP
#define STACK_CPP

template<typename T>
inline stack<T>::stack() : count_(0) {
template<typename T> /*noexcept*/
inline stack<T>::stack() : count_(0),
array_size_(0),
array_(nullptr) {
}

template<typename T>
inline stack<T>::stack(stack const & rhs) : array_size_(rhs.array_size_),
count_(rhs.count_),
array_(copy(rhs.array_, rhs.count_, rhs.array_size_)) {
template<typename T> /*strong*/
inline stack<T>::stack(stack const & rhs) :
array_size_(rhs.array_size_),
count_(rhs.count_),
array_(copy(rhs.array_, rhs.count_, rhs.array_size_)) {
}

template<typename T>
template<typename T> /*noexcept*/
inline stack<T>::~stack() {
delete[] array_;
}

template<typename T>
template<typename T> /*noexcept*/
inline auto stack<T>::count() const noexcept -> size_t {
return count_;
}

template<typename T>
template<typename T> /*strong*/
auto stack<T>::top() const -> const T& {
if (count_ == 0) {
throw std::range_error("stack is empty");
}
return array_[count_ - 1];
else {
return array_[count_ - 1];
}
}

template<typename T>
template<typename T> /*strong*/
inline auto stack<T>::pop() -> void {
if (count_ == 0) {
throw std::logic_error("stack is empty");
}
--count_;
else {
--count_;
}
}

template<typename T>
template<typename T> /*strong*/
inline auto stack<T>::push(T const & value) -> void {
if (count_ == array_size_) {
size_t size = array_size_ * 2 + (array_size_ == 0);
Expand All @@ -52,15 +59,15 @@ inline auto stack<T>::push(T const & value) -> void {
++count_;
}

template<typename T>
template<typename T> /*strong*/
inline auto stack<T>::operator=(stack const & rhs) -> stack & {
if (this != &rhs) {
(stack(rhs)).swap(*this);
}
return *this;
}

template<typename T>
template<typename T> /*noexcept*/
auto stack<T>::operator==(stack const & rhs) -> bool {
if ((rhs.count_ != count_) || (rhs.array_size_ != array_size_)) {
return false;
Expand All @@ -75,8 +82,15 @@ auto stack<T>::operator==(stack const & rhs) -> bool {
return true;
}

template<typename T>
auto stack<T>::copy(const T * rhs, size_t sizeLeft, size_t sizeRight) -> T * {
template<typename T> /*noexcept*/
auto stack<T>::swap(stack & rhs) -> void {
std::swap(rhs.array_size_, array_size_);
std::swap(rhs.array_, array_);
std::swap(rhs.count_, count_);
}

template<typename T> /*strong*/
auto copy(const T * rhs, size_t sizeLeft, size_t sizeRight) -> T * {
T * m_array = new T[sizeRight];
try {
std::copy(rhs, rhs + sizeLeft, m_array);
Expand All @@ -88,10 +102,4 @@ auto stack<T>::copy(const T * rhs, size_t sizeLeft, size_t sizeRight) -> T * {
return m_array;
}

template<typename T>
auto stack<T>::swap(stack & rhs) -> void {
std::swap(rhs.array_size_, array_size_);
std::swap(rhs.array_, array_);
std::swap(rhs.count_, count_);
}
#endif // STACK_CPP
#endif // STACK_CPP
8 changes: 5 additions & 3 deletions include/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

using std::size_t;
using std::ostream;

template<typename T>
auto copy(const T * rhs, size_t sizeLeft, size_t sizeRight)->T *; /*strong*/

template<typename T>
class stack {
Expand All @@ -14,19 +17,18 @@ class stack {
stack(stack const & rhs); /*strong*/
~stack(); /*noexcept*/

auto count() const noexcept -> size_t; /*noexcept*/
auto count() const noexcept->size_t; /*noexcept*/
auto top() const -> const T&; /*strong*/
auto pop() -> void; /*strong*/
auto push(T const & value) -> void; /*strong*/

auto operator=(stack const & rhs) -> stack &; /*noexcept*/
auto operator=(stack const & rhs)->stack &; /*strong*/
auto operator==(stack const & rhs) -> bool; /*noexcept*/
private:
T * array_;
size_t array_size_;
size_t count_;

auto copy(const T * rhs, size_t sizeLeft, size_t sizeRight)->T *; /*strong*/
auto swap(stack & rhs) -> void; /*noexcept*/
};

Expand Down

0 comments on commit 72c587f

Please sign in to comment.