source: mainline/uspace/lib/cpp/include/impl/queue.hpp@ 23dcc14

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 23dcc14 was 23dcc14, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added queue

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_QUEUE
30#define LIBCPP_QUEUE
31
32#include <deque>
33#include <memory>
34#include <type_traits>
35#include <utility>
36#include <vector>
37
38namespace std
39{
40 /**
41 * 23.6.3, class template queue:
42 */
43
44 template<class T, class Container = deque<T>>
45 class queue
46 {
47 public:
48 using value_type = typename Container::value_type;
49 using reference = typename Container::reference;
50 using const_reference = typename Container::const_reference;
51 using size_type = typename Container::size_type;
52 using container_type = Container;
53
54 explicit queue(const container_type& cc)
55 : c{cc}
56 { /* DUMMY BODY */ }
57
58 explicit queue(container_type&& cc = container_type{})
59 : c{move(cc)}
60 { /* DUMMY BODY */ }
61
62 template<
63 class Alloc,
64 class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
65 >
66 explicit queue(const Alloc& alloc)
67 : c{alloc}
68 { /* DUMMY BODY */}
69
70 template<
71 class Alloc,
72 class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
73 >
74 queue(const container_type& cc, const Alloc& alloc)
75 : c{cc, alloc}
76 { /* DUMMY BODY */}
77
78 template<
79 class Alloc,
80 class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
81 >
82 queue(container_type&& cc, const Alloc& alloc)
83 : c{move(cc), alloc}
84 { /* DUMMY BODY */}
85
86 template<
87 class Alloc,
88 class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
89 >
90 queue(const queue& other, const Alloc& alloc)
91 : c{other.c, alloc}
92 { /* DUMMY BODY */}
93
94 template<
95 class Alloc,
96 class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
97 >
98 queue(queue&& other, const Alloc& alloc)
99 : c{move(other.c), alloc}
100 { /* DUMMY BODY */}
101
102 bool empty() const
103 {
104 return c.empty();
105 }
106
107 size_type size() const
108 {
109 return c.size();
110 }
111
112 reference front()
113 {
114 return c.front();
115 }
116
117 const_reference front() const
118 {
119 return c.front();
120 }
121
122 reference back()
123 {
124 return c.back();
125 }
126
127 const_reference back() const
128 {
129 return c.back();
130 }
131
132 void push(const value_type& val)
133 {
134 c.push_back(val);
135 }
136
137 void push(value_type&& val)
138 {
139 c.push_back(forward<value_type>(val));
140 }
141
142 template<class... Args>
143 void emplace(Args&&... args)
144 {
145 c.emplace_back(forward<Args>(args)...);
146 }
147
148 void pop()
149 {
150 c.pop_front();
151 }
152
153 protected:
154 container_type c;
155
156 public: // The noexcept part of swap's declaration needs c to be declared.
157 void swap(queue& other)
158 noexcept(noexcept(swap(c, other.c)))
159 {
160 std::swap(c, other.c);
161 }
162
163 private:
164 template<class U, class C>
165 friend bool operator==(const queue<U, C>&, const queue<U, C>&);
166
167 template<class U, class C>
168 friend bool operator<(const queue<U, C>&, const queue<U, C>&);
169
170 template<class U, class C>
171 friend bool operator!=(const queue<U, C>&, const queue<U, C>&);
172
173 template<class U, class C>
174 friend bool operator>(const queue<U, C>&, const queue<U, C>&);
175
176 template<class U, class C>
177 friend bool operator>=(const queue<U, C>&, const queue<U, C>&);
178
179 template<class U, class C>
180 friend bool operator<=(const queue<U, C>&, const queue<U, C>&);
181 };
182
183 template<class T, class Container, class Alloc>
184 struct uses_allocator<queue<T, Container>, Alloc>
185 : uses_allocator<Container, Alloc>
186 { /* DUMMY BODY */ };
187
188 template<
189 class T, class Container = vector<T>,
190 class Compare = less<typename Container::value_type>
191 >
192 class priority_queue;
193
194 template<class T, class Container>
195 bool operator==(const queue<T, Container>& lhs,
196 const queue<T, Container>& rhs)
197 {
198 return lhs.c == rhs.c;
199 }
200
201 template<class T, class Container>
202 bool operator<(const queue<T, Container>& lhs,
203 const queue<T, Container>& rhs)
204 {
205 return lhs.c < rhs.c;
206 }
207
208 template<class T, class Container>
209 bool operator!=(const queue<T, Container>& lhs,
210 const queue<T, Container>& rhs)
211 {
212 return lhs.c != rhs.c;
213 }
214
215 template<class T, class Container>
216 bool operator>(const queue<T, Container>& lhs,
217 const queue<T, Container>& rhs)
218 {
219 return lhs.c > rhs.c;
220 }
221
222 template<class T, class Container>
223 bool operator>=(const queue<T, Container>& lhs,
224 const queue<T, Container>& rhs)
225 {
226 return lhs.c >= rhs.c;
227 }
228
229 template<class T, class Container>
230 bool operator<=(const queue<T, Container>& lhs,
231 const queue<T, Container>& rhs)
232 {
233 return lhs.c <= rhs.c;
234 }
235
236 template<class T, class Container>
237 void swap(queue<T, Container>& lhs, queue<T, Container>& rhs)
238 noexcept(noexcept(lhs.swap(rhs)))
239 {
240 lhs.swap(rhs);
241 }
242
243 template<class T, class Container, class Compare>
244 void swap(priority_queue<T, Container, Compare>& lhs,
245 priority_queue<T, Container, Compare>& rhs)
246 noexcept(noexcept(lhs.swap(rhs)))
247 {
248 lhs.swap(rhs);
249 }
250}
251
252#endif
Note: See TracBrowser for help on using the repository browser.