| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_CONTAINER_STACK_H ) |
| 23 |
#define INCLUDED_CONTAINER_STACK_H |
| 24 |
|
| 25 |
#include "memory/allocator.h" |
| 26 |
#include <algorithm> |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
template<typename Type> |
| 37 |
class Stack : public DefaultAllocator<Type> |
| 38 |
{ |
| 39 |
typedef DefaultAllocator<Type> Allocator; |
| 40 |
|
| 41 |
enum |
| 42 |
{ |
| 43 |
DEFAULT_CAPACITY = 4, |
| 44 |
}; |
| 45 |
|
| 46 |
typedef Type* pointer; |
| 47 |
typedef const Type* const_pointer; |
| 48 |
|
| 49 |
public: |
| 50 |
typedef const_pointer const_iterator; |
| 51 |
private: |
| 52 |
|
| 53 |
pointer m_data; |
| 54 |
pointer m_end; |
| 55 |
std::size_t m_capacity; |
| 56 |
|
| 57 |
|
| 58 |
void insert( const Type& value ){ |
| 59 |
Allocator::construct( m_end++, value ); |
| 60 |
} |
| 61 |
void insert_overflow( const Type& value ){ |
| 62 |
const std::size_t new_capacity = ( m_capacity ) ? m_capacity + m_capacity : std::size_t( DEFAULT_CAPACITY ); |
| 63 |
const pointer new_data = Allocator::allocate( new_capacity ); |
| 64 |
const pointer new_end = std::copy( m_data, m_end, new_data ); |
| 65 |
|
| 66 |
destroy(); |
| 67 |
Allocator::deallocate( m_data, m_capacity ); |
| 68 |
|
| 69 |
m_capacity = new_capacity; |
| 70 |
m_data = new_data; |
| 71 |
m_end = new_end; |
| 72 |
insert( value ); |
| 73 |
} |
| 74 |
void destroy(){ |
| 75 |
for ( pointer p = m_data; p != m_end; ++p ) |
| 76 |
{ |
| 77 |
Allocator::destroy( p ); |
| 78 |
} |
| 79 |
} |
| 80 |
void construct( const Stack& other ){ |
| 81 |
pointer p = m_data; |
| 82 |
for ( const_iterator i = other.begin(); i != other.end(); ++i ) |
| 83 |
{ |
| 84 |
Allocator::construct( p++, *i ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
public: |
| 89 |
|
| 90 |
Stack() : |
| 91 |
m_data( 0 ), |
| 92 |
m_end( 0 ), |
| 93 |
m_capacity( 0 ){ |
| 94 |
} |
| 95 |
Stack( const Type& value ) : |
| 96 |
m_data( 0 ), |
| 97 |
m_end( 0 ), |
| 98 |
m_capacity( 0 ){ |
| 99 |
push( value ); |
| 100 |
} |
| 101 |
Stack( const Stack& other ) : |
| 102 |
DefaultAllocator<Type>( other ){ |
| 103 |
m_capacity = other.m_capacity; |
| 104 |
m_data = Allocator::allocate( m_capacity ); |
| 105 |
construct( other ); |
| 106 |
m_end = m_data + other.size(); |
| 107 |
} |
| 108 |
~Stack(){ |
| 109 |
destroy(); |
| 110 |
Allocator::deallocate( m_data, m_capacity ); |
| 111 |
} |
| 112 |
|
| 113 |
const_iterator begin() const { |
| 114 |
return m_data; |
| 115 |
} |
| 116 |
const_iterator end() const { |
| 117 |
return m_end; |
| 118 |
} |
| 119 |
|
| 120 |
bool empty() const { |
| 121 |
return end() == begin(); |
| 122 |
} |
| 123 |
void clear(){ |
| 124 |
destroy(); |
| 125 |
m_end = m_data; |
| 126 |
} |
| 127 |
|
| 128 |
std::size_t size() const { |
| 129 |
return m_end - m_data; |
| 130 |
} |
| 131 |
Type operator[]( const std::size_t i ) const { |
| 132 |
return m_data[i]; |
| 133 |
} |
| 134 |
|
| 135 |
void push( const Type& value ){ |
| 136 |
if ( size() == m_capacity ) { |
| 137 |
insert_overflow( value ); |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
insert( value ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
void pop(){ |
| 146 |
Allocator::destroy( --m_end ); |
| 147 |
} |
| 148 |
|
| 149 |
Type& top(){ |
| 150 |
return *( m_end - 1 ); |
| 151 |
} |
| 152 |
|
| 153 |
const Type& top() const { |
| 154 |
return *( m_end - 1 ); |
| 155 |
} |
| 156 |
|
| 157 |
Type& parent(){ |
| 158 |
return *( m_end - 2 ); |
| 159 |
} |
| 160 |
|
| 161 |
const Type& parent() const { |
| 162 |
return *( m_end - 2 ); |
| 163 |
} |
| 164 |
|
| 165 |
void swap( Stack& other ){ |
| 166 |
std::swap( m_data, other.m_data ); |
| 167 |
std::swap( m_end, other.m_end ); |
| 168 |
std::swap( m_capacity, other.m_capacity ); |
| 169 |
} |
| 170 |
#if 1 |
| 171 |
Stack& operator=( const Stack& other ){ |
| 172 |
Stack temp( other ); |
| 173 |
temp.swap( *this ); |
| 174 |
return *this; |
| 175 |
} |
| 176 |
#else |
| 177 |
Stack& operator=( const Stack& other ){ |
| 178 |
if ( &other != this ) { |
| 179 |
destroy(); |
| 180 |
|
| 181 |
if ( other.size() > m_capacity ) { |
| 182 |
Allocator::deallocate( m_data, m_capacity ); |
| 183 |
m_capacity = other.m_capacity; |
| 184 |
m_data = Allocator::allocate( m_capacity ); |
| 185 |
} |
| 186 |
m_end = m_data + other.size(); |
| 187 |
|
| 188 |
construct( other ); |
| 189 |
} |
| 190 |
return *this; |
| 191 |
} |
| 192 |
#endif |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
template<typename Type> |
| 197 |
inline bool operator<( const Stack<Type>& self, const Stack<Type>& other ){ |
| 198 |
return std::lexicographical_compare( self.begin(), self.end(), other.begin(), other.end() ); |
| 199 |
} |
| 200 |
|
| 201 |
namespace std |
| 202 |
{ |
| 203 |
|
| 204 |
|
| 205 |
template<typename Type> |
| 206 |
inline void swap( Stack<Type>& self, Stack<Type>& other ){ |
| 207 |
self.swap( other ); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
#endif |