Changeset 1fafb3e in mainline


Ignore:
Timestamp:
2018-07-05T21:41:22Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b22ccaa
Parents:
f9823e2
git-author:
Dzejrou <dzejrou@…> (2018-04-27 22:16:52)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:22)
Message:

cpp: added priority_queue

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/queue.hpp

    rf9823e2 r1fafb3e  
    3030#define LIBCPP_QUEUE
    3131
     32#include <algorithm>
    3233#include <deque>
    3334#include <memory>
     
    5253            using container_type  = Container;
    5354
     55        protected:
     56            container_type c;
     57
     58        public:
    5459            explicit queue(const container_type& cc)
    5560                : c{cc}
     
    151156            }
    152157
    153         protected:
    154             container_type c;
    155 
    156         public: // The noexcept part of swap's declaration needs c to be declared.
    157158            void swap(queue& other)
    158159                noexcept(noexcept(swap(c, other.c)))
     
    186187    { /* DUMMY BODY */ };
    187188
     189    /**
     190     * 23.6.4, class template priority_queue:
     191     */
     192
    188193    template<
    189194        class T, class Container = vector<T>,
    190195        class Compare = less<typename Container::value_type>
    191196    >
    192     class priority_queue;
     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 */ };
    193348
    194349    template<class T, class Container>
Note: See TracChangeset for help on using the changeset viewer.