[39e468f] | 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 |
|
---|
[1fafb3e] | 32 | #include <algorithm>
|
---|
[23dcc14] | 33 | #include <deque>
|
---|
| 34 | #include <memory>
|
---|
| 35 | #include <type_traits>
|
---|
| 36 | #include <utility>
|
---|
| 37 | #include <vector>
|
---|
| 38 |
|
---|
[39e468f] | 39 | namespace std
|
---|
| 40 | {
|
---|
[23dcc14] | 41 | /**
|
---|
| 42 | * 23.6.3, class template queue:
|
---|
| 43 | */
|
---|
| 44 |
|
---|
[39e468f] | 45 | template<class T, class Container = deque<T>>
|
---|
[23dcc14] | 46 | class queue
|
---|
| 47 | {
|
---|
| 48 | public:
|
---|
| 49 | using value_type = typename Container::value_type;
|
---|
| 50 | using reference = typename Container::reference;
|
---|
| 51 | using const_reference = typename Container::const_reference;
|
---|
| 52 | using size_type = typename Container::size_type;
|
---|
| 53 | using container_type = Container;
|
---|
| 54 |
|
---|
[1fafb3e] | 55 | protected:
|
---|
| 56 | container_type c;
|
---|
| 57 |
|
---|
| 58 | public:
|
---|
[23dcc14] | 59 | explicit queue(const container_type& cc)
|
---|
| 60 | : c{cc}
|
---|
| 61 | { /* DUMMY BODY */ }
|
---|
| 62 |
|
---|
| 63 | explicit queue(container_type&& cc = container_type{})
|
---|
| 64 | : c{move(cc)}
|
---|
| 65 | { /* DUMMY BODY */ }
|
---|
| 66 |
|
---|
| 67 | template<
|
---|
| 68 | class Alloc,
|
---|
| 69 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 70 | >
|
---|
| 71 | explicit queue(const Alloc& alloc)
|
---|
| 72 | : c{alloc}
|
---|
| 73 | { /* DUMMY BODY */}
|
---|
| 74 |
|
---|
| 75 | template<
|
---|
| 76 | class Alloc,
|
---|
| 77 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 78 | >
|
---|
| 79 | queue(const container_type& cc, const Alloc& alloc)
|
---|
| 80 | : c{cc, alloc}
|
---|
| 81 | { /* DUMMY BODY */}
|
---|
| 82 |
|
---|
| 83 | template<
|
---|
| 84 | class Alloc,
|
---|
| 85 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 86 | >
|
---|
| 87 | queue(container_type&& cc, const Alloc& alloc)
|
---|
| 88 | : c{move(cc), alloc}
|
---|
| 89 | { /* DUMMY BODY */}
|
---|
| 90 |
|
---|
| 91 | template<
|
---|
| 92 | class Alloc,
|
---|
| 93 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 94 | >
|
---|
| 95 | queue(const queue& other, const Alloc& alloc)
|
---|
| 96 | : c{other.c, alloc}
|
---|
| 97 | { /* DUMMY BODY */}
|
---|
| 98 |
|
---|
| 99 | template<
|
---|
| 100 | class Alloc,
|
---|
| 101 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 102 | >
|
---|
| 103 | queue(queue&& other, const Alloc& alloc)
|
---|
| 104 | : c{move(other.c), alloc}
|
---|
| 105 | { /* DUMMY BODY */}
|
---|
| 106 |
|
---|
| 107 | bool empty() const
|
---|
| 108 | {
|
---|
| 109 | return c.empty();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | size_type size() const
|
---|
| 113 | {
|
---|
| 114 | return c.size();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | reference front()
|
---|
| 118 | {
|
---|
| 119 | return c.front();
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | const_reference front() const
|
---|
| 123 | {
|
---|
| 124 | return c.front();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | reference back()
|
---|
| 128 | {
|
---|
| 129 | return c.back();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | const_reference back() const
|
---|
| 133 | {
|
---|
| 134 | return c.back();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | void push(const value_type& val)
|
---|
| 138 | {
|
---|
| 139 | c.push_back(val);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | void push(value_type&& val)
|
---|
| 143 | {
|
---|
| 144 | c.push_back(forward<value_type>(val));
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | template<class... Args>
|
---|
| 148 | void emplace(Args&&... args)
|
---|
| 149 | {
|
---|
| 150 | c.emplace_back(forward<Args>(args)...);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | void pop()
|
---|
| 154 | {
|
---|
| 155 | c.pop_front();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void swap(queue& other)
|
---|
| 159 | noexcept(noexcept(swap(c, other.c)))
|
---|
| 160 | {
|
---|
| 161 | std::swap(c, other.c);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | private:
|
---|
| 165 | template<class U, class C>
|
---|
| 166 | friend bool operator==(const queue<U, C>&, const queue<U, C>&);
|
---|
| 167 |
|
---|
| 168 | template<class U, class C>
|
---|
| 169 | friend bool operator<(const queue<U, C>&, const queue<U, C>&);
|
---|
| 170 |
|
---|
| 171 | template<class U, class C>
|
---|
| 172 | friend bool operator!=(const queue<U, C>&, const queue<U, C>&);
|
---|
| 173 |
|
---|
| 174 | template<class U, class C>
|
---|
| 175 | friend bool operator>(const queue<U, C>&, const queue<U, C>&);
|
---|
| 176 |
|
---|
| 177 | template<class U, class C>
|
---|
| 178 | friend bool operator>=(const queue<U, C>&, const queue<U, C>&);
|
---|
| 179 |
|
---|
| 180 | template<class U, class C>
|
---|
| 181 | friend bool operator<=(const queue<U, C>&, const queue<U, C>&);
|
---|
| 182 | };
|
---|
| 183 |
|
---|
| 184 | template<class T, class Container, class Alloc>
|
---|
| 185 | struct uses_allocator<queue<T, Container>, Alloc>
|
---|
| 186 | : uses_allocator<Container, Alloc>
|
---|
| 187 | { /* DUMMY BODY */ };
|
---|
[39e468f] | 188 |
|
---|
[1fafb3e] | 189 | /**
|
---|
| 190 | * 23.6.4, class template priority_queue:
|
---|
| 191 | */
|
---|
| 192 |
|
---|
[39e468f] | 193 | template<
|
---|
| 194 | class T, class Container = vector<T>,
|
---|
| 195 | class Compare = less<typename Container::value_type>
|
---|
| 196 | >
|
---|
[1fafb3e] | 197 | class priority_queue
|
---|
| 198 | {
|
---|
| 199 | public:
|
---|
| 200 | using value_type = typename Container::value_type;
|
---|
| 201 | using reference = typename Container::reference;
|
---|
| 202 | using const_reference = typename Container::const_reference;
|
---|
| 203 | using size_type = typename Container::size_type;
|
---|
| 204 | using container_type = Container;
|
---|
| 205 |
|
---|
| 206 | /* protected: */
|
---|
| 207 | using compare_type = Compare;
|
---|
| 208 |
|
---|
| 209 | compare_type comp;
|
---|
| 210 | container_type c;
|
---|
| 211 |
|
---|
| 212 | public:
|
---|
| 213 | priority_queue(const compare_type& cmp, const container_type& cc)
|
---|
| 214 | : comp{cmp}, c{cc}
|
---|
| 215 | {
|
---|
| 216 | make_heap(c.begin(), c.end(), comp);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | explicit priority_queue(const compare_type& cmp = compare_type{},
|
---|
| 220 | container_type&& cc = container_type{})
|
---|
| 221 | : comp{cmp}, c{move(cc)}
|
---|
| 222 | {
|
---|
| 223 | make_heap(c.begin(), c.end(), comp);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | template<class InputIterator>
|
---|
| 227 | priority_queue(InputIterator first, InputIterator last,
|
---|
| 228 | const compare_type& cmp,
|
---|
| 229 | const container_type& cc)
|
---|
| 230 | : comp{cmp}, c{cc}
|
---|
| 231 | {
|
---|
| 232 | c.insert(c.end(), first, last);
|
---|
| 233 | make_heap(c.begin(), c.end(), comp);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | template<class InputIterator>
|
---|
| 237 | priority_queue(InputIterator first, InputIterator last,
|
---|
| 238 | const compare_type& cmp = compare_type{},
|
---|
| 239 | container_type&& cc = container_type{})
|
---|
| 240 | : comp{cmp}, c{move(cc)}
|
---|
| 241 | {
|
---|
| 242 | c.insert(c.end(), first, last);
|
---|
| 243 | make_heap(c.begin(), c.end(), comp);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | template<
|
---|
| 247 | class Alloc,
|
---|
| 248 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 249 | >
|
---|
| 250 | explicit priority_queue(const Alloc& alloc)
|
---|
| 251 | : comp{}, c{alloc}
|
---|
| 252 | { /* DUMMY BODY */ }
|
---|
| 253 |
|
---|
| 254 | template<
|
---|
| 255 | class Alloc,
|
---|
| 256 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 257 | >
|
---|
| 258 | priority_queue(const compare_type& cmp, const Alloc& alloc)
|
---|
| 259 | : comp{cmp}, c{alloc}
|
---|
| 260 | { /* DUMMY BODY */ }
|
---|
| 261 |
|
---|
| 262 | template<
|
---|
| 263 | class Alloc,
|
---|
| 264 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 265 | >
|
---|
| 266 | priority_queue(const compare_type& cmp, const container_type& cc,
|
---|
| 267 | const Alloc& alloc)
|
---|
| 268 | : comp{cmp}, c{cc, alloc}
|
---|
| 269 | { /* DUMMY BODY */ }
|
---|
| 270 |
|
---|
| 271 | template<
|
---|
| 272 | class Alloc,
|
---|
| 273 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 274 | >
|
---|
| 275 | priority_queue(const compare_type& cmp, container_type&& cc,
|
---|
| 276 | const Alloc& alloc)
|
---|
| 277 | : comp{cmp}, c{move(cc), alloc}
|
---|
| 278 | { /* DUMMY BODY */ }
|
---|
| 279 |
|
---|
| 280 | template<
|
---|
| 281 | class Alloc,
|
---|
| 282 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 283 | >
|
---|
| 284 | priority_queue(const priority_queue& other, const Alloc& alloc)
|
---|
| 285 | : comp{other.comp}, c{other.c, alloc}
|
---|
| 286 | { /* DUMMY BODY */ }
|
---|
| 287 |
|
---|
| 288 | template<
|
---|
| 289 | class Alloc,
|
---|
| 290 | class = enable_if_t<uses_allocator<container_type, Alloc>::value, void>
|
---|
| 291 | >
|
---|
| 292 | priority_queue(priority_queue&& other, const Alloc& alloc)
|
---|
| 293 | : comp{move(other.comp)}, c{move(other.c), alloc}
|
---|
| 294 | { /* DUMMY BODY */ }
|
---|
| 295 |
|
---|
| 296 | bool empty() const
|
---|
| 297 | {
|
---|
| 298 | return c.empty();
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | size_type size() const
|
---|
| 302 | {
|
---|
| 303 | return c.size();
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | const_reference top() const
|
---|
| 307 | {
|
---|
| 308 | return c.front();
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | void push(const value_type& val)
|
---|
| 312 | {
|
---|
| 313 | c.push_back(val);
|
---|
| 314 | push_heap(c.begin(), c.end(), comp);
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | void push(value_type&& val)
|
---|
| 318 | {
|
---|
| 319 | c.push_back(forward<value_type>(val));
|
---|
| 320 | push_heap(c.begin(), c.end(), comp);
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | template<class... Args>
|
---|
| 324 | void emplace(Args&&... args)
|
---|
| 325 | {
|
---|
| 326 | c.emplace_back(forward<Args>(args)...);
|
---|
| 327 | push_heap(c.begin(), c.end(), comp);
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | void pop()
|
---|
| 331 | {
|
---|
| 332 | pop_heap(c.begin(), c.end(), comp);
|
---|
| 333 | c.pop_back();
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | void swap(priority_queue& other)
|
---|
| 337 | noexcept(noexcept(swap(c, other.c)) && noexcept(swap(comp, other.comp)))
|
---|
| 338 | {
|
---|
| 339 | std::swap(c, other.c);
|
---|
| 340 | std::swap(comp, other.comp);
|
---|
| 341 | }
|
---|
| 342 | };
|
---|
| 343 |
|
---|
| 344 | template<class T, class Container, class Compare, class Alloc>
|
---|
| 345 | struct uses_allocator<priority_queue<T, Container, Compare>, Alloc>
|
---|
| 346 | : uses_allocator<Container, Alloc>
|
---|
| 347 | { /* DUMMY BODY */ };
|
---|
[39e468f] | 348 |
|
---|
| 349 | template<class T, class Container>
|
---|
| 350 | bool operator==(const queue<T, Container>& lhs,
|
---|
| 351 | const queue<T, Container>& rhs)
|
---|
| 352 | {
|
---|
| 353 | return lhs.c == rhs.c;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | template<class T, class Container>
|
---|
| 357 | bool operator<(const queue<T, Container>& lhs,
|
---|
| 358 | const queue<T, Container>& rhs)
|
---|
| 359 | {
|
---|
| 360 | return lhs.c < rhs.c;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | template<class T, class Container>
|
---|
| 364 | bool operator!=(const queue<T, Container>& lhs,
|
---|
| 365 | const queue<T, Container>& rhs)
|
---|
| 366 | {
|
---|
| 367 | return lhs.c != rhs.c;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | template<class T, class Container>
|
---|
| 371 | bool operator>(const queue<T, Container>& lhs,
|
---|
| 372 | const queue<T, Container>& rhs)
|
---|
| 373 | {
|
---|
| 374 | return lhs.c > rhs.c;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | template<class T, class Container>
|
---|
| 378 | bool operator>=(const queue<T, Container>& lhs,
|
---|
| 379 | const queue<T, Container>& rhs)
|
---|
| 380 | {
|
---|
| 381 | return lhs.c >= rhs.c;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | template<class T, class Container>
|
---|
| 385 | bool operator<=(const queue<T, Container>& lhs,
|
---|
| 386 | const queue<T, Container>& rhs)
|
---|
| 387 | {
|
---|
| 388 | return lhs.c <= rhs.c;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | template<class T, class Container>
|
---|
| 392 | void swap(queue<T, Container>& lhs, queue<T, Container>& rhs)
|
---|
| 393 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
| 394 | {
|
---|
| 395 | lhs.swap(rhs);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | template<class T, class Container, class Compare>
|
---|
| 399 | void swap(priority_queue<T, Container, Compare>& lhs,
|
---|
| 400 | priority_queue<T, Container, Compare>& rhs)
|
---|
| 401 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
| 402 | {
|
---|
| 403 | lhs.swap(rhs);
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | #endif
|
---|