generated from puzzlef/louvain-communities
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_queue.hxx
134 lines (107 loc) · 2.5 KB
/
_queue.hxx
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
#pragma once
#include <iterator>
using std::iterator_traits;
// DEQUE VIEW
// ----------
// Bounded (circular) deque view capable of storing N elements.
template <class I>
class DequeView {
// Data.
protected:
using T = typename iterator_traits<I>::value_type;
const I xb, xe;
I ib, ie;
size_t n;
// Basic operations.
public:
using value_type = T;
inline size_t size() const { return n; }
inline bool empty() const { return n==0; }
// Read operations.
public:
inline auto back() const { return ie==xb? *(xe-1) : *(ie-1); }
inline auto front() const { return *ib; }
// Write operations.
public:
inline void push_back(const T& v) {
if (ib!=ie || n==0) ++n;
*(ie++) = v;
if (ie==xe) ie = xb;
}
inline void push_front(const T& v) {
if (ib==xb) ib = xe;
*(--ib) = v;
if (ib!=ie || n==0) ++n;
}
inline auto pop_back() {
if (ie==xb) ie = xe;
if (n>0) --n;
return *(--ie);
}
inline auto pop_front() {
if (n>0) --n;
auto v = *(ib++);
if (ib==xe) ib = xb;
return v;
}
// Lifetime operations.
DequeView(I xb, I xe) :
xb(xb), xe(xe), ib(xb), ie(xb), n(0) {}
};
template <class I>
inline auto deque_view(I xb, I xe) {
return DequeView<I>(xb, xe);
}
template <class J>
inline auto dequeView(J& x) {
return deque_view(x.begin(), x.end());
}
// UNSIZED DEQUE VIEW
// ------------------
// Bounded (circular) deque view capable of storing N-1 elements.
template <class I>
class UnsizedDequeView {
// Data.
protected:
using T = typename iterator_traits<I>::value_type;
const I xb, xe;
I ib, ie;
// Basic operations
public:
using value_type = T;
inline bool empty() const { return ib==ie; }
// Read operations.
public:
inline auto back() const { return ie==xb? *(xe-1) : *(ie-1); }
inline auto front() const { return *ib; }
// Write operations.
public:
inline void push_back(const T& v) {
*(ie++) = v;
if (ie==xe) ie = xb;
}
inline void push_front(const T& v) {
if (ib==xb) ib = xe;
*(--ib) = v;
}
inline auto pop_back() {
if (ie==xb) ie = xe;
return *(--ie);
}
inline auto pop_front() {
auto v = *(ib++);
if (ib==xe) ib = xb;
return v;
}
// Lifetime operations.
UnsizedDequeView(I xb, I xe) :
xb(xb), xe(xe), ib(xb), ie(xb) {}
};
template <class I>
inline auto unsized_deque_view(I xb, I xe) {
return UnsizedDequeView<I>(xb, xe);
}
template <class J>
inline auto unsizedDequeView(J& x) {
return unsized_deque_view(x.begin(), x.end());
}