This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_mitosis.h
81 lines (77 loc) · 1.73 KB
/
stack_mitosis.h
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
#ifndef __STACK_STUDENT_H__
#define __STACK_STUDENT_H__
#include <vector>
namespace CP
{
template <typename T>
class stack
{
private:
void mitosis(int, int);
T *mData;
size_t mSize, mCap;
public:
void pop();
void push(T &&);
T top();
};
}
template <typename T>
void CP::stack<T>::mitosis(int a, int b)
{
// if (mCap >= mSize + b - a + 1)
// {
// T *tmp = new T[mCap], *p = tmp;
// for (int i = mSize - b - 1; i <= mSize - a - 1; ++i)
// {
// *(p++) = mData[i];
// *(p++) = mData[i];
// }
// for (int i = mSize - a; i < mSize; ++i)
// {
// *(p++) = mData[i];
// }
// for (int i = mSize - b - 1; i < mSize + b - a + 1; ++i)
// {
// mData[i] = *(tmp++);
// }
// delete[] tmp;
// mSize += b - a + 1;
// }
// else
// {
// while (mCap < mSize + b - a + 1)
// {
// mCap *= 2;
// }
// T *newData = new T[mCap], *p = newData;
// for (int i = 0; i < mSize; ++i)
// {
// if (mSize - b - 1 <= i && i <= mSize - a - 1)
// {
// *(p++) = mData[i];
// }
// *(p++) = mData[i];
// }
// mSize += b - a + 1;
// delete[] mData;
// mData = newData;
// }
std::vector<T> tmp;
for (int i = 0; i < a; ++i)
{
tmp.push_back(top());
pop();
}
for (int i = 0; i < b - a + 1; ++i)
{
tmp.push_back(top());
tmp.push_back(top());
pop();
}
for (auto it = tmp.rbegin(); it != tmp.rend(); ++it)
{
push(*it);
}
}
#endif