/// \brief A stack whose storage capacity is variable at run-time. Similar to std::vector.
///
/// - Pushing or popping elements is a constant-time operation (on average).
/// - The storage capacity of the stack will grow when a new element is added beyond the current capacity. Iterators are invalidated when the storage capacity grows.
/// - DefaultConstructible, Copyable, Assignable.
/// - Compatible with the containers and algorithms in the Standard Template Library (STL) - http://www.sgi.com/tech/stl/
///
/// \param Type: The type to be stored in the stack. Must provide a copy-constructor.
/// \brief Pushes \p value onto the stack at the top element. If reserved storage is insufficient for the new element, this will invalidate all iterators.
voidpush(constType&value)
{
if(size()==m_capacity)
{
insert_overflow(value);
}
else
{
insert(value);
}
}
/// \brief Removes the top element of the stack.
voidpop()
{
Allocator::destroy(--m_end);
}
/// \brief Returns the top element of the mutable stack.
Type&top()
{
return*(m_end-1);
}
/// \brief Returns the top element of the non-mutable stack.
constType&top()const
{
return*(m_end-1);
}
/// \brief Returns the element below the top element of the mutable stack.
Type&parent()
{
return*(m_end-2);
}
/// \brief Returns the element below the top element of the non-mutable stack.
constType&parent()const
{
return*(m_end-2);
}
/// \brief Swaps the values of this stack and \p other.
voidswap(Stack&other)
{
std::swap(m_data,other.m_data);
std::swap(m_end,other.m_end);
std::swap(m_capacity,other.m_capacity);
}
#if 1 // use copy-swap technique
Stack&operator=(constStack&other)
{
Stacktemp(other);
temp.swap(*this);
return*this;
}
#else // avoids memory allocation if capacity is already sufficient.
Stack&operator=(constStack&other)
{
if(&other!=this)
{
destroy();
if(other.size()>m_capacity)
{
Allocator::deallocate(m_data,m_capacity);
m_capacity=other.m_capacity;
m_data=Allocator::allocate(m_capacity);
}
m_end=m_data+other.size();
construct(other);
}
return*this;
}
#endif
};
/// \brief Returns true if \p self is lexicographically less than \p other.