-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
288 lines (225 loc) · 7.29 KB
/
Copy pathstack.h
File metadata and controls
288 lines (225 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#ifndef STACK_INCLUDED
#define STACK_INCLUDED
#include"stack_ptr.h"
#define unshareable 0
using namespace std;
template<typename T>
class TwoSideStack
{
private:
StackPtr<T> *_stackPtr;
public:
TwoSideStack(size_t = 10);
TwoSideStack(TwoSideStack &&);
TwoSideStack & operator=(TwoSideStack &&);
TwoSideStack(const TwoSideStack &);
TwoSideStack & operator=(const TwoSideStack &);
friend std::ostream & operator<<(std::ostream & os, const TwoSideStack<T> & rhs)
{
os << *rhs._stackPtr;
return os;
}
friend void swap(TwoSideStack<T> & f_s, TwoSideStack<T> & s_s) throw() {
swap(f_s._stackPtr, s_s._stackPtr);
}
~TwoSideStack();
bool push_top(const T);
bool push_bottom(const T);
void pop_top();
void pop_bottom();
bool get_top(T & );
bool get_bottom(T & );
void printStack();
void reallocation();
void deep_copy();
class iterator{
protected:
T * _it_data_ptr;
T * _begin;
T * _end;
T * _top;
T * _bottom;
public:
iterator(T * data, T * begin, T * end, T * top, T * bottom){
_it_data_ptr = data;
_begin = begin;
_end = end;
_top = top;
_bottom = bottom;
}
iterator& operator++(){
if(_it_data_ptr == _bottom - 1)
throw;
if(_it_data_ptr == _begin)
_it_data_ptr = _end;
else
--_it_data_ptr;
return *this;
}
iterator& operator--(){
if(_it_data_ptr == _top + 1)
throw;
if(_it_data_ptr == _end)
_it_data_ptr = _begin;
else
++_it_data_ptr;
return *this;
}
iterator operator++(int){
if(_it_data_ptr == _bottom - 1)
throw;
T * t_m = _it_data_ptr;
if(_it_data_ptr == _begin)
_it_data_ptr = _end;
else
--_it_data_ptr;
return iterator(t_m, _begin, _end, _top, _bottom);
}
iterator operator--(int){
if(_it_data_ptr == _top + 1)
throw;
T * t_m = _it_data_ptr;
if(_it_data_ptr == _end)
_it_data_ptr = _begin;
else
++_it_data_ptr;
return iterator(t_m, _begin, _end, _top, _bottom);
}
bool operator<(const iterator & it)const{
if((_it_data_ptr <= _top && it._it_data_ptr <= _top) ||
(_it_data_ptr >= _bottom && it._it_data_ptr >= _bottom))
return _it_data_ptr > it._it_data_ptr;
return _it_data_ptr < it._it_data_ptr;
}
bool operator==(const iterator & it) const{
return _it_data_ptr == it._it_data_ptr;
}
bool operator>(const iterator & it)const{
return !((*this < it) || (*this == it));
}
bool operator>=(const iterator & it)const{
if(_it_data_ptr < _bottom && _it_data_ptr > _top)
return false;
return !(*this < it);
}
bool operator<=(const iterator & it)const{
if(_it_data_ptr < _bottom && _it_data_ptr > _top)
return false;
return (*this < it) || (*this == it);
}
bool operator!=(const iterator & it)const{
return !(*this == it);
}
T & operator*()const {
return *_it_data_ptr;
}
};
class const_iterator: public iterator{
public:
const T & operator*()const {
return *(this->_it_data_ptr);
}
const_iterator(T * data, T * begin, T * end, T * top, T * bottom):
iterator(data, begin, end, top, bottom){}
};
iterator top(){
return iterator(_stackPtr->_data + _stackPtr->get_index_top(),
_stackPtr->_data, _stackPtr->_data + _stackPtr->get_size() - 1,
_stackPtr->_data + _stackPtr->get_index_top(),
_stackPtr->_data + _stackPtr->get_index_bottom());
}
iterator bottom(){
return iterator(_stackPtr->_data + _stackPtr->get_index_bottom(),
_stackPtr->_data, _stackPtr->_data + _stackPtr->get_size() - 1,
_stackPtr->_data + _stackPtr->get_index_top(),
_stackPtr->_data + _stackPtr->get_index_bottom());
}
const_iterator top_const(){
return const_iterator(_stackPtr->_data + _stackPtr->get_index_top(),
_stackPtr->_data, _stackPtr->_data + _stackPtr->get_size() - 1,
_stackPtr->_data + _stackPtr->get_index_top(),
_stackPtr->_data + _stackPtr->get_index_bottom());
}
const_iterator bottom_const(){
return const_iterator(_stackPtr->_data + _stackPtr->get_index_bottom(),
_stackPtr->_data, _stackPtr->_data + _stackPtr->get_size() - 1,
_stackPtr->_data + _stackPtr->get_index_top(),
_stackPtr->_data + _stackPtr->get_index_bottom());
}
};
template <typename T>
bool TwoSideStack<T>::get_top(T & elem){
return _stackPtr->get_top(elem);
}
template <typename T>
bool TwoSideStack<T>::get_bottom(T & elem){
return _stackPtr->get_bottom(elem);
}
template <typename T>
TwoSideStack<T> & TwoSideStack<T>::operator=(TwoSideStack<T> && other){
this->_stackPtr = other._stackPtr;
}
template <typename T>
TwoSideStack<T>::TwoSideStack(const TwoSideStack<T> & other){
cout << "constructor copy" << endl;
this->_stackPtr = other._stackPtr;
_stackPtr->set_refs(_stackPtr->get_refs() + 1);
}
template <typename T>
TwoSideStack<T> & TwoSideStack<T>::operator=(const TwoSideStack<T> & other){
_stackPtr->set_refs(_stackPtr->get_refs() + 1);
this->_stackPtr = other._stackPtr;
}
template <typename T>
void TwoSideStack<T>::deep_copy(){
if(_stackPtr->get_refs() > 1)
{
_stackPtr->set_refs(_stackPtr->get_refs() - 1);
size_t sz = _stackPtr->get_size();
int iTop = _stackPtr->get_index_top();
int iBottom = _stackPtr->get_index_bottom();
T * arr = new T[sz];
std::copy(_stackPtr->_data, _stackPtr->_data + sz, arr);
_stackPtr = new StackPtr<T>(arr, sz, iTop, iBottom);
}
}
template <typename T>
TwoSideStack<T>::TwoSideStack(size_t s){
_stackPtr = new StackPtr<T>(s);
}
template <typename T>
TwoSideStack<T>::~TwoSideStack(){
if(_stackPtr->get_refs() == 1)
delete _stackPtr;
else
_stackPtr->set_refs(_stackPtr->get_refs() - 1);
}
template <typename T>
bool TwoSideStack<T>::push_top(const T value){
deep_copy();
return _stackPtr->push_top(value);
}
template <typename T>
bool TwoSideStack<T>::push_bottom(const T value){
deep_copy();
return _stackPtr->push_bottom(value);
}
template <typename T>
void TwoSideStack<T>::reallocation(){
_stackPtr->reallocation();
}
template<typename T>
void TwoSideStack<T>::pop_top(){
deep_copy();
_stackPtr->pop_top();
}
template<typename T>
void TwoSideStack<T>::pop_bottom(){
deep_copy();
_stackPtr->pop_bottom();
}
template <typename T>
void TwoSideStack<T>::printStack(){
_stackPtr->printStack();
}
#endif // STACK_INCLUDED