-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_ptr.h
More file actions
299 lines (232 loc) · 6.08 KB
/
Copy pathstack_ptr.h
File metadata and controls
299 lines (232 loc) · 6.08 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
289
290
291
292
293
294
295
296
297
298
299
#ifndef STACK_PTR_H_INCLUDED
#define STACK_PTR_H_INCLUDED
using namespace std;
template<typename T>
class StackPtr{
private:
unsigned long _cRefs;
size_t _size;
int _indexTop;
int _indexBottom;
public:
T * _data;
StackPtr(size_t = 11);
StackPtr(T *, size_t, int indexTop, int indexBottom);
StackPtr(StackPtr &&);
StackPtr & operator=(StackPtr &&);
StackPtr(const StackPtr &);
StackPtr & operator=(const StackPtr &);
~StackPtr();
size_t get_size() const;
unsigned long get_refs() const{return _cRefs;}
void set_refs(unsigned long rfs){_cRefs = rfs;}
int get_index_top() const {return _indexTop;}
int get_index_bottom() const {return _indexBottom;}
T & operator[](const size_t);
void reallocation();
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();
friend void swap(StackPtr<T> & f_s, StackPtr<T> & s_s) throw(){
swap(f_s._cRefs, s_s._cRefs);
swap(f_s._size, s_s._size);
swap(f_s._indexTop, s_s._indexTop);
swap(f_s._indexBottom, s_s._indexBottom);
}
friend std::ostream & operator<<(std::ostream & cout, const StackPtr<T> & rhs){
if(rhs._indexTop > -1)
for (int i = rhs._indexTop; i > -1 ; --i)
cout << " | " << rhs._data[i] << endl;
else
cout<<"null\n"<<endl;
cout<<endl;
if(rhs._indexBottom < rhs._size)
for (int i = rhs._indexBottom; i < rhs._size; ++i)
cout << " | " << rhs._data[i] << endl;
else
cout<<"null\n"<<endl;
cout<< "cRefs " << rhs._cRefs << endl;
cout<<endl;
return cout;
}
};
template <typename T>
bool StackPtr<T>::get_top(T & elem){
if(_indexTop < 0)
return false;
elem = _data[_indexTop];
return true;
}
template <typename T>
bool StackPtr<T>::get_bottom(T & elem){
if(_indexBottom >= _size)
return false;
elem = _data[_indexBottom];
return true;
}
template<typename T>
StackPtr<T>::StackPtr(const StackPtr & other):
_size(other._size),
_cRefs(other._cRefs),
_indexTop(other._indexTop),
_indexBottom(other._indexBottom){
_data = other._data;
}
template<typename T>
StackPtr<T> & StackPtr<T>::operator=(const StackPtr<T> & other){
if (this != &other){
_size = other._size;
_cRefs = other._cRefs;
_indexTop = other._indexTop;
_indexBottom = other._indexBottom;
_data = other._data;
}
return *this;
}
template<typename T>
StackPtr<T> & StackPtr<T>::operator=(StackPtr<T> && other){
if(_data)
delete[] _data;
_data = other._data;
other._data = nullptr;
_cRefs = 1;
_size = other._size;
_indexTop = other._indexTop;
_indexBottom = other._indexBottom;
return *this;
}
template<typename T>
StackPtr<T>::StackPtr(StackPtr<T>&& other){
if(_data)
delete[] _data;
_data = other._data;
other._data = nullptr;
_cRefs = 1;
_size = other._size;
_indexTop = other._indexTop;
_indexBottom = other._indexBottom;
}
template <typename T>
bool StackPtr<T>::push_top(const T & value){
if (_indexTop + 1 < _indexBottom){
_data[++_indexTop] = value;
return true;
}else
{
try{
reallocation();
_data[++_indexTop] = value;
return true;
}
catch(exception& e){
cout<<"Error in push_top "<< e.what() << endl;
return false;
}
}
}
template <typename T>
bool StackPtr<T>::push_bottom(const T & value){
if (_indexBottom - 1 > _indexTop){
_data[--_indexBottom] = value;
return true;
}else
{
try{
reallocation();
_data[--_indexBottom] = value;
return true;
}
catch(exception& e){
std::cout<<"Error in push_bottom "<<std::endl;
return false;
}
}
}
template<typename T>
void StackPtr<T>::pop_top(){
if(_indexTop >= 0){
T t = _data[_indexTop];
_data[_indexTop--] = 0;
}
}
template <typename T>
void StackPtr<T>::printStack(){
if(_indexTop > -1)
for (int i = _indexTop; i > -1 ; --i)
cout << " | " << _data[i] << endl;
else
cout<<"null\n"<<endl;
cout<<endl;
if(_indexBottom < _size)
for (int i = _indexBottom; i < _size; ++i)
cout << " | " << _data[i] << endl;
else
cout<<"null\n"<<endl;
cout<< "cRefs" << _cRefs << endl;
cout<<endl;
}
template<typename T>
void StackPtr<T>::reallocation(){
int badSize = _size;
_size *= 2;
try{
T * newStackPtr = new T[_size];
for(int i = 0; i < _indexTop + 1; ++i)
newStackPtr[i] = _data[i];
for(int i = _indexBottom; i < badSize; ++i)
newStackPtr[i + badSize] = _data[i];
delete[] _data;
_data = newStackPtr;
_indexBottom += badSize;
}catch(exception & e){
cout << e.what() << endl;
}
}
template<typename T>
StackPtr<T>::StackPtr(size_t sz){
cout << "constructor Stackptr " << sz << endl;
if(_data)
delete[] _data;
_data = new T[sz];
_cRefs = 1;
_indexTop = -1;
_indexBottom = sz;
_size = sz;
}
template<typename T>
StackPtr<T>::~StackPtr(){
cout << "destructor StackPtr "<< _size << endl;
delete[] _data;
}
template<typename T>
T & StackPtr<T>::operator[](size_t ind){
try{
return _data[ind];
}catch(exception& e){
cout << "Error in operator[] " << e.what() << endl;
}
}
template<typename T>
StackPtr<T>::StackPtr(T * arr, size_t sz, int iTop, int iBottom){
_data = arr;
_cRefs = 1;
_indexTop = iTop;
_indexBottom = iBottom;
_size = sz;
}
template<typename T>
size_t StackPtr<T>::get_size() const{
return _size;
}
template<typename T>
void StackPtr<T>::pop_bottom(){
if(_indexBottom < _size){
T t = _data[_indexBottom];
_data[_indexBottom++] = 0;
}
}
#endif // STACK_PTR_H_INCLUDED