2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2003 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <algorithm>
|
2025-03-20 22:42:02 +00:00
|
|
|
#include <vector>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//! A priority queue with an iterator
|
|
|
|
|
/*!
|
|
|
|
|
This priority queue is the same as a standard priority queue except:
|
|
|
|
|
it sorts by std::greater, it has a forward iterator through the elements
|
|
|
|
|
(which can appear in any order), and its contents can be swapped.
|
|
|
|
|
*/
|
2024-07-16 13:36:44 +00:00
|
|
|
template <
|
|
|
|
|
class T, class Container = std::vector<T>,
|
2012-06-10 16:50:54 +00:00
|
|
|
#if defined(_MSC_VER)
|
2024-07-16 13:36:44 +00:00
|
|
|
class Compare = std::greater<Container::value_type>>
|
2012-06-10 16:50:54 +00:00
|
|
|
#else
|
2024-07-16 13:36:44 +00:00
|
|
|
class Compare = std::greater<typename Container::value_type>>
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
2024-10-17 18:04:35 +00:00
|
|
|
class PriorityQueue
|
|
|
|
|
{
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2024-11-27 16:51:48 +00:00
|
|
|
using value_type = Container::value_type;
|
|
|
|
|
using size_type = Container::size_type;
|
|
|
|
|
using iterator = Container::iterator;
|
|
|
|
|
using const_iterator = Container::const_iterator;
|
|
|
|
|
using container_type = Container;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
PriorityQueue()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
PriorityQueue(Container &swappedIn)
|
|
|
|
|
{
|
|
|
|
|
swap(swappedIn);
|
|
|
|
|
}
|
|
|
|
|
~PriorityQueue()
|
|
|
|
|
{
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! @name manipulators
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//! Add element
|
2024-10-17 18:04:35 +00:00
|
|
|
void push(const value_type &v)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
c.push_back(v);
|
|
|
|
|
std::push_heap(c.begin(), c.end(), comp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Remove head element
|
2024-10-17 18:04:35 +00:00
|
|
|
void pop()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
std::pop_heap(c.begin(), c.end(), comp);
|
|
|
|
|
c.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Erase element
|
2024-10-17 18:04:35 +00:00
|
|
|
void erase(iterator i)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
c.erase(i);
|
|
|
|
|
std::make_heap(c.begin(), c.end(), comp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Get start iterator
|
2024-10-17 18:04:35 +00:00
|
|
|
iterator begin()
|
|
|
|
|
{
|
|
|
|
|
return c.begin();
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Get end iterator
|
2024-10-17 18:04:35 +00:00
|
|
|
iterator end()
|
|
|
|
|
{
|
|
|
|
|
return c.end();
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Swap contents with another priority queue
|
2024-10-17 18:04:35 +00:00
|
|
|
void swap(PriorityQueue<T, Container, Compare> &q)
|
|
|
|
|
{
|
|
|
|
|
c.swap(q.c);
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Swap contents with another container
|
2024-10-17 18:04:35 +00:00
|
|
|
void swap(Container &c2)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
c.swap(c2);
|
|
|
|
|
std::make_heap(c.begin(), c.end(), comp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
//! @name accessors
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//! Returns true if there are no elements
|
2024-10-17 18:04:35 +00:00
|
|
|
bool empty() const
|
|
|
|
|
{
|
|
|
|
|
return c.empty();
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Returns the number of elements
|
2024-10-17 18:04:35 +00:00
|
|
|
size_type size() const
|
|
|
|
|
{
|
|
|
|
|
return c.size();
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Returns the head element
|
2024-10-17 18:04:35 +00:00
|
|
|
const value_type &top() const
|
|
|
|
|
{
|
|
|
|
|
return c.front();
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Get start iterator
|
2024-10-17 18:04:35 +00:00
|
|
|
const_iterator begin() const
|
|
|
|
|
{
|
|
|
|
|
return c.begin();
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Get end iterator
|
2024-10-17 18:04:35 +00:00
|
|
|
const_iterator end() const
|
|
|
|
|
{
|
|
|
|
|
return c.end();
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//@}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
Container c;
|
|
|
|
|
Compare comp;
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|