forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.hpp
169 lines (153 loc) · 3.3 KB
/
queue.hpp
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
// solid/utility/queue.hpp
//
// Copyright (c) 2007, 2008 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#ifndef UTILITY_QUEUE_HPP
#define UTILITY_QUEUE_HPP
#include <cstdlib>
#include "solid/system/convertors.hpp"
#include "solid/system/cassert.hpp"
#include "solid/system/debug.hpp"
namespace solid{
//! A simple and fast queue with interface similar to std::queue
/*!
The advantages are:
- twice faster then the std one
- while pushing new objects, the allready pushed are not relocated
in memory (no reallocation is performed)
*/
template <class T, size_t NBits = 5>
class Queue{
enum{
NodeMask = BitsToMask(NBits),
NodeSize = BitsToCount(NBits)
};
struct Node{
Node(Node *_pnext = nullptr): next(_pnext){}
Node *next;
char data[NodeSize * sizeof(T)];
};
public:
typedef T& reference;
typedef T const& const_reference;
public:
Queue():sz(0), popsz(0), pb(nullptr), pf(nullptr), ptn(nullptr){}
Queue(Queue &&_rq):sz(_rq.sz), popsz(_rq.popsz), pb(_rq.pb), pf(_rq.pf), ptn(_rq.ptn){
_rq.sz = 0;
_rq.popsz = 0;
_rq.pb = nullptr;
_rq.pf = nullptr;
_rq.ptn = nullptr;
}
~Queue(){
while(sz) pop();
Node *pn = pf ? (Node*)(((char*)pf) - popsz * sizeof(T) - sizeof(Node*)): nullptr;
while(ptn){
Node *tn = ptn->next;
SOLID_ASSERT(ptn != pn);
delete ptn;
ptn = tn;
}
delete pn;
}
Queue& operator=(Queue &&_rq){
sz = _rq.sz;
popsz = _rq.popsz;
pb = _rq.pb;
pf = _rq.pf;
ptn = _rq.ptn;
_rq.sz = 0;
_rq.popsz = 0;
_rq.pb = nullptr;
_rq.pf = nullptr;
_rq.ptn = nullptr;
return *this;
}
bool empty()const { return !sz;}
size_t size() const { return sz;}
void push(const T &_t){
if((sz + popsz) & NodeMask) ++pb;
else pb = pushNode(pb);
++sz;
new(pb) T(_t);
}
void push(T &&_t){
if((sz + popsz) & NodeMask) ++pb;
else pb = pushNode(pb);
++sz;
new(pb) T(std::move(_t));
}
reference back(){
return *pb;
}
const_reference back()const{
return *pb;
}
void pop(){
pf->~T();
--sz;
if((++popsz) & NodeMask) ++pf;
else{ pf = popNode(pf);popsz = 0;}
}
reference front(){
return *pf;
}
const_reference front()const{
return *pf;
}
private:
T* pushNode(void *_p){
Node *pn = _p ? (Node*)(((char*)_p) - NodeSize * sizeof(T) + sizeof(T) - sizeof(Node*)): nullptr;
if(ptn){
Node *tn = ptn;
ptn = ptn->next;
tn->next = nullptr;
if(pn){
pn->next = tn;
return (T*)tn->data;
}else{
return (pf = (T*)tn->data);
}
}else{
if(pn){
pn->next = new Node;
pn = pn->next;
return (T*)pn->data;
}else{
pn = new Node;
return (pf = (T*)pn->data);
}
}
}
T* popNode(void *_p){
//SOLID_ASSERT(_p);
SOLID_ASSERT(_p);
Node *pn = (Node*)(((char*)_p) - NodeSize * sizeof(T) + sizeof(T) - sizeof(Node*));
Node *ppn = pn->next;
pn->next = ptn; ptn = pn;//cache the node
if(ppn){
return (T*)(ppn->data);
}else{
SOLID_ASSERT(!sz);
pb = nullptr;
//pf = nullptr;
return nullptr;
}
}
private:
Queue(const Queue&);
Queue& operator=(const Queue&);
private:
size_t sz;
size_t popsz;
T *pb;//back
T *pf;//front
Node *ptn;//empty nodes
};
}//namespace solid
#endif