-
Notifications
You must be signed in to change notification settings - Fork 38
/
priority_queue.hpp
150 lines (123 loc) · 3.96 KB
/
priority_queue.hpp
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
147
148
149
150
#ifndef _PRIORITY_QUEUE_H_
#define _PRIORITY_QUEUE_H_
#include "heap.hpp"
#include "vector.hpp"
#include <exception>
#include <string>
namespace mystl {
class priority_queue_exception : public std::exception
{
public:
explicit priority_queue_exception( const std::string &message )
: message_( message )
{
}
virtual const char * what() const noexcept override
{
return message_.c_str();
}
private:
std::string message_;
};
template <typename T, typename Container = mystl::vector<T>, typename Comp = std::less<T>>
class priority_queue
{
public:
using container_type = Container;
using value_type = typename Container::value_type;
using reference = typename Container::reference;
using const_reference = typename Container::const_reference;
using size_type = typename Container::size_type;
protected:
Comp comp_; // for compare elements
Container container_; // the underlying container
public:
explicit priority_queue( const Comp &comp = Comp{}, const Container &c = Container{} )
: comp_( comp ),
container_( c )
{
mystl::make_heap( container_.begin(), container_.end(), comp_ );
}
explicit priority_queue( const Comp &comp, Container &&container )
: comp_( comp ),
container_( std::move( container ) )
{
mystl::make_heap( container_.begin(), container_.end(), comp_ );
}
template <typename InputIterator>
priority_queue( InputIterator beg, InputIterator end, const Comp &comp = Comp(), const Container &container = Container() )
: comp_( comp ),
container_( container )
{
container_.insert( container_.end(), beg, end );
mystl::make_heap( container_.begin(), container_.end(), comp_ );
}
template <typename InputIterator>
priority_queue( InputIterator beg, InputIterator end, const Comp &comp, Container &&container )
: comp_( comp ),
container_( std::move( container ) )
{
container_.insert( container_.end(), beg, end );
mystl::make_heap( container_.begin(), container_.end(), comp_ );
}
~priority_queue() = default;
bool empty() const
{
return container_.empty();
}
size_type size() const
{
return container_.size();
}
/*
priority_queue don't provide reference top();
because it don't want user change elemtn by reference
*/
const_reference top() const
{
if( empty() )
{
throw priority_queue_exception( "priority_queue::top(): the container is empty!" );
}
return container_.front();
}
void pop()
{
if( empty() )
{
throw priority_queue_exception( "priority_queue::pop(): the container is empty!" );
}
mystl::pop_heap( container_.begin(), container_.end(), comp_ );
container_.pop_back();
}
void push( const value_type &elem )
{
auto copy = elem;
push( std::move( copy ) );
}
void push( value_type &&elem )
{
emplace( std::move( elem ) );
}
template <typename... Args>
void emplace( Args&&... args )
{
container_.emplace_back( std::forward<Args>( args )... );
mystl::push_heap( container_.begin(), container_.end(), comp_ );
}
void swap( priority_queue &other )
noexcept( noexcept( swap( container_, other.container_ ) ) && noexcept( swap( comp_, other.comp_ ) ) )
{
using std::swap;
swap( container_, other.container_ );
swap( comp_, other.comp_ );
}
};
template <typename T, typename Container, typename Comp>
void swap( priority_queue<T, Container, Comp> &x, priority_queue<T, Container, Comp> &y )
noexcept( noexcept( x.swap( y ) ) )
{
x.swap( y );
}
}; // namespace mystl
#endif /* _PRIORITY_QUEUE_H_ */