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
|
template <class T, class Container = tinystl::vector<T>, class Compare = tinystl::less<typename Container::value_type>> class priority_queue {
public: typedef Container container_type; typedef Compare value_compare; typedef typename Container::value_type value_type; typedef typename Container::size_type size_type; typedef typename Container::reference reference; typedef typename Container::const_reference const_reference;
static_assert(std::is_same<T, value_type>::value, "the value_type of Container should be same with T");
private: container_type c_; value_compare comp_;
public: priority_queue() = default; priority_queue(const Compare& c) : c_(), comp_(c) {} explicit priority_queue(size_type n) : c_(n) { tinystl::make_heap(c_.begin(), c_.end(), comp_); } priority_queue(size_type n, const value_type& value) : c_(n, value) { tinystl::make_heap(c_.begin(), c_.end(), comp_); }
template <class InputIterator> priority_queue(InputIterator first, InputIterator last) : c_(first, last) { tinystl::make_heap(c_.begin(), c_.end(), comp_); }
priority_queue(std::initializer_list<value_type> ilist) : c_(ilist) { tinystl::make_heap(c_.begin(), c_.end(), comp_); }
priority_queue(const Container& c) : c_(c) { tinystl::make_heap(c_.begin(), c_.end(), comp_); }
priority_queue(Container&& c) : c_(tinystl::move(c)) { tinystl::make_heap(c_.begin(), c_.end(), comp_); }
priority_queue(const priority_queue& rhs) : c_(rhs.c_), comp_(rhs.comp_) { tinystl::make_heap(c_.begin(), c_.end(), comp_); }
priority_queue(priority_queue&& rhs) : c_(tinystl::move(rhs.c_)), comp_(rhs.comp_) { tinystl::make_heap(c_.begin(), c_.end(), comp_); }
priority_queue& operator=(const priority_queue& rhs) { c_ = rhs.c_; comp_ = rhs.comp_; tinystl::make_heap(c_.begin(), c_.end(), comp_); return *this; }
priority_queue& operator=(priority_queue&& rhs) { c_ = tinystl::move(rhs.c_); comp_ = rhs.comp_; tinystl::make_heap(c_.begin(), c_.end(), comp_); return *this; }
priority_queue& operator=(std::initializer_list<value_type> ilist) { c_ = ilist; tinystl::make_heap(c_.begin(), c_.end(), comp_); return *this; }
~priority_queue() = default;
public: const_reference top() const { return c_.front(); } bool empty() const { return c_.empty(); } size_type size() const { return c_.size(); }
template <class... Args> void emplace(Args&&... args) { c_.emplace_back(tinystl::forward<Args>(args)...); tinystl::push_heap(c_.begin(), c_.end(), comp_); }
void push(const value_type& value) { c_.push_back(value); tinystl::push_heap(c_.begin(), c_.end(), comp_); }
void push(value_type&& value) { c_.push_back(tinystl::move(value)); tinystl::push_heap(c_.begin(), c_.end(), comp_); }
void pop() { tinystl::pop_heap(c_.begin(), c_.end(), comp_); c_.pop_back(); }
void clear() { while (!empty()) { pop(); } }
void swap(priority_queue& rhs) noexcept(noexcept(tinystl::swap(c_, rhs.c_)) && noexcept(tinystl::swap(comp_, rhs.comp_))) { tinystl::swap(c_, rhs.c_); tinystl::swap(comp_, rhs.comp_); }
public: friend bool operator==(const priority_queue& lhs, const priority_queue& rhs) { return lhs.c_ == rhs.c_; }
friend bool operator!=(const priority_queue& lhs, const priority_queue& rhs) { return !(lhs == rhs); } };
template <class T, class Container, class Compare> bool operator==(const priority_queue<T, Container, Compare>& lhs, const priority_queue<T, Container, Compare>& rhs) { return lhs == rhs; }
template <class T, class Container, class Compare> bool operator!=(const priority_queue<T, Container, Compare>& lhs, const priority_queue<T, Container, Compare>& rhs) { return !(lhs == rhs); }
template <class T, class Container, class Compare> void swap(priority_queue<T, Container, Compare>& lhs, priority_queue<T, Container, Compare>& rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); }
|