-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathitemPool.cpp
104 lines (89 loc) · 2.35 KB
/
itemPool.cpp
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
#include <iostream>
using namespace std;
template<typename T>
class Queue
{
public:
Queue()
{
_front = _rear = new QueueItem();
}
~Queue()
{
QueueItem *cur = _front;
while (cur != nullptr)
{
_front = _front->_next;
delete cur;
cur = _front;
}
}
void push(const T &val) // 入队操作
{
QueueItem *item = new QueueItem(val);
_rear->_next = item; // 类似于尾插法
_rear = item;
}
void pop() // 出队操作
{
if (empty())
return;
QueueItem *first = _front->_next;
_front->_next = first->_next;
if (_front->_next == nullptr) {
_rear = _front;
}
delete first;
}
T front() const
{
return _front->_next->_data;
}
bool empty() const { return _front == _rear; }
private:
// 产生一个 QueueItem 的对象池(10000 个 QueueItem 节点)
struct QueueItem
{
QueueItem(T data = T()) : _data(data), _next(nullptr) {}
// 给 QueueItem 提供自定义的内存管理
void *operator new(size_t size)
{
if (_itemPool == nullptr)
{
_itemPool = (QueueItem*) new char[POOL_ITEM_SIZE * sizeof(QueueItem)];
QueueItem *p = _itemPool;
for (; p < _itemPool + POOL_ITEM_SIZE - 1; ++ p) {
p->_next = p + 1;
}
p->_next = nullptr;
}
QueueItem *p = _itemPool;
_itemPool = _itemPool->_next;
return p;
}
void operator delete(void *ptr)
{
QueueItem *p = (QueueItem*)ptr;
p->_next = _itemPool; // 相当于接到链表头
_itemPool = p;
}
T _data;
QueueItem *_next;
static QueueItem *_itemPool; // 指向对象池首个未分配的节点
static const int POOL_ITEM_SIZE = 10000;
};
QueueItem *_front; // 指向头节点的前一个?
QueueItem *_rear; // 指向队尾
};
template<typename T>
typename Queue<T>::QueueItem *Queue<T>::QueueItem::_itemPool = nullptr;
int main()
{
Queue<int> que;
for (int i = 0; i < 10000; ++ i) {
que.push(i);
que.pop();
}
cout << que.empty() << endl;
return 0;
}