Deskflow 1.22.0.197
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
PriorityQueue.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
4 * SPDX-FileCopyrightText: (C) 2003 Chris Schoeneman
5 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
6 */
7
8#pragma once
9
10#include <algorithm>
11#include <vector>
12
14
19template <
20 class T, class Container = std::vector<T>,
21#if defined(_MSC_VER)
22 class Compare = std::greater<Container::value_type>>
23#else
24 class Compare = std::greater<typename Container::value_type>>
25#endif
27{
28public:
29 using value_type = Container::value_type;
30 using size_type = Container::size_type;
31 using iterator = Container::iterator;
32 using const_iterator = Container::const_iterator;
33 using container_type = Container;
34
35 PriorityQueue() = default;
36 explicit PriorityQueue(Container &swappedIn)
37 {
38 swap(swappedIn);
39 }
40 ~PriorityQueue() = default;
41
43
44
46 void push(const value_type &v)
47 {
48 c.push_back(v);
49 std::push_heap(c.begin(), c.end(), comp);
50 }
51
53 void pop()
54 {
55 std::pop_heap(c.begin(), c.end(), comp);
56 c.pop_back();
57 }
58
61 {
62 c.erase(i);
63 std::make_heap(c.begin(), c.end(), comp);
64 }
65
68 {
69 return c.begin();
70 }
71
74 {
75 return c.end();
76 }
77
80 {
81 c.swap(q.c);
82 }
83
85 void swap(Container &c2) noexcept
86 {
87 c.swap(c2);
88 std::make_heap(c.begin(), c.end(), comp);
89 }
90
92
94
96 bool empty() const
97 {
98 return c.empty();
99 }
100
103 {
104 return c.size();
105 }
106
108 const value_type &top() const
109 {
110 return c.front();
111 }
112
115 {
116 return c.begin();
117 }
118
121 {
122 return c.end();
123 }
124
126
127private:
128 Container c;
129 Compare comp;
130};
const value_type & top() const
Returns the head element.
Definition PriorityQueue.h:108
void pop()
Remove head element.
Definition PriorityQueue.h:53
std::vector< Timer >::size_type size_type
Definition PriorityQueue.h:30
void push(const value_type &v)
Add element.
Definition PriorityQueue.h:46
std::vector< Timer >::iterator iterator
Definition PriorityQueue.h:31
size_type size() const
Returns the number of elements.
Definition PriorityQueue.h:102
iterator end()
Get end iterator.
Definition PriorityQueue.h:73
std::vector< Timer >::const_iterator const_iterator
Definition PriorityQueue.h:32
std::vector< Timer > container_type
Definition PriorityQueue.h:33
~PriorityQueue()=default
std::vector< Timer >::value_type value_type
Definition PriorityQueue.h:29
const_iterator end() const
Get end iterator.
Definition PriorityQueue.h:120
const_iterator begin() const
Get start iterator.
Definition PriorityQueue.h:114
bool empty() const
Returns true if there are no elements.
Definition PriorityQueue.h:96
PriorityQueue()=default
PriorityQueue(Container &swappedIn)
Definition PriorityQueue.h:36
void swap(Container &c2) noexcept
Swap contents with another container.
Definition PriorityQueue.h:85
void erase(iterator i)
Erase element.
Definition PriorityQueue.h:60
iterator begin()
Get start iterator.
Definition PriorityQueue.h:67
void swap(PriorityQueue< T, Container, Compare > &q) noexcept
Swap contents with another priority queue.
Definition PriorityQueue.h:79