-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtstack.h
More file actions
125 lines (109 loc) · 2.39 KB
/
tstack.h
File metadata and controls
125 lines (109 loc) · 2.39 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
#ifndef __DATA_STRUCT_TSTACK_H__
#define __DATA_STRUCT_TSTACK_H__
namespace ds
{
template<typename T>
class tstack_t
{
public:
tstack_t()
{
m_data = 0;
m_capacity = 0;
clear();
}
tstack_t(const tstack_t& s)
{
m_data = 0;
m_capacity = 0;
m_top = 0;
*this = s;
}
~tstack_t()
{
if(m_data)
{
delete[] m_data;
}
}
void clear()
{
m_top = 0;
}
tstack_t& operator = (const tstack_t& s)
{
if(s.m_top <= 0)
{
return *this;
}
resize(s.m_capacity);
m_top = s.m_top;
for(size_t x = 0; x < m_top; ++ x)
{
m_data[x] = s.m_data[x];
}
return *this;
}
void resize(size_t n)
{
size_t c = (n + 1023) / 16 * 16;
if(c <= m_capacity)
{
return;
}
T* data = new T[c];
for(size_t x = 0; x < m_top; ++ x)
{
data[x] = m_data[x];
}
if(m_data)
{
delete[] m_data;
}
m_data = data;
m_capacity = c;
}
size_t count()
{
return m_top;
}
bool empty()
{
return (m_top == 0);
}
bool full()
{
return (m_top >= m_capacity);
}
T& alloc()
{
resize(m_capacity);
size_t x = m_top ++;
return m_data[x];
}
T& top()
{
return m_data[m_top - 1];
}
void pop()
{
-- m_top;
}
void print()
{
printf("[(%s:%s:%d) ++++]\n", __FILE__, __PRETTY_FUNCTION__, __LINE__);
printf("[m_top(%zu)]\n", m_top);
printf("[m_bottom(%zu)]\n", m_bottom);
for (size_t x = 0; x < m_top; ++ x)
{
m_data[x].print();
}
printf("[(%s:%s:%d) ----]\n", __FILE__, __PRETTY_FUNCTION__, __LINE__);
}
private:
T* m_data;
size_t m_top;
size_t m_capacity;
};
}
#endif