-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtqueue.h
More file actions
124 lines (107 loc) · 2.44 KB
/
tqueue.h
File metadata and controls
124 lines (107 loc) · 2.44 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
#ifndef __DATA_STRUCT_TQUEUE_H__
#define __DATA_STRUCT_TQUEUE_H__
namespace ds
{
template<typename T>
class tqueue_t
{
public:
tqueue_t()
{
m_tail = 0;
m_capacity = 0;
m_head = m_tail = 0;
}
tqueue_t(const tqueue_t& s)
{
m_tail = 0;
m_capacity = 0;
m_head = m_tail = 0;
*this = s;
}
~tqueue_t()
{
if(m_data)
{
delete[] m_data;
}
}
void clear()
{
m_head = m_tail = 0;
}
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;
}
tqueue_t& operator = (const tqueue_t& s)
{
resize(m_capacity);
m_head = s.m_head;
m_tail = s.m_tail;
for(size_t x = m_head; x < m_tail; ++ x)
{
m_data[x] = s.m_data[x];
}
}
bool empty()
{
return (m_head == m_tail);
}
bool full()
{
return (m_head == N);
}
size_t count()
{
return (m_head - m_tail);
}
T& alloc()
{
resize(m_capacity);
size_t x = m_head ++;
return m_data[x];
}
T& front()
{
return m_data[m_tail];
}
void pop_front()
{
++ m_tail;
}
void print()
{
printf("[(%s:%s:%d) ++++]\n", __FILE__, __PRETTY_FUNCTION__, __LINE__);
printf("[m_head(%zu)]\n", m_head);
printf("[m_tail(%zu)]\n", m_tail);
printf("[m_capacity(%zu)]\n", m_capacity);
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_capacity;
size_t m_head;
size_t m_tail;
};
}
#endif