Loading...
Searching...
No Matches
priority_queue.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2015 John Wellbelove, rlindeman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_PRIORITY_QUEUE_INCLUDED
#define ETL_PRIORITY_QUEUE_INCLUDED
#include "platform.h"
#include "algorithm.h"
#include "deque.h"
#include "error_handler.h"
#include "exception.h"
#include "functional.h"
#include "iterator.h"
#include "parameter_type.h"
#include "static_assert.h"
#include "type_traits.h"
#include "utility.h"
#include "vector.h"
#include <stddef.h>
//*****************************************************************************
//*****************************************************************************
namespace etl
{
//***************************************************************************
//***************************************************************************
class priority_queue_exception : public exception
{
public:
priority_queue_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class priority_queue_full : public etl::priority_queue_exception
{
public:
priority_queue_full(string_type file_name_, numeric_type line_number_)
: priority_queue_exception(ETL_ERROR_TEXT("priority_queue:full", ETL_PRIORITY_QUEUE_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class priority_queue_iterator : public etl::priority_queue_exception
{
public:
priority_queue_iterator(string_type file_name_, numeric_type line_number_)
: priority_queue_exception(ETL_ERROR_TEXT("priority_queue:iterator", ETL_PRIORITY_QUEUE_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class priority_queue_empty : public etl::priority_queue_exception
{
public:
priority_queue_empty(string_type file_name_, numeric_type line_number_)
: priority_queue_exception(ETL_ERROR_TEXT("priority_queue:empty", ETL_PRIORITY_QUEUE_FILE_ID"C"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
template <typename TContainer>
struct priority_queue_container_interface
{
ETL_STATIC_ASSERT(sizeof(TContainer) == 0, "Unsupported container type for etl::ipriority_queue");
};
template <typename T, const size_t N>
struct priority_queue_container_interface<etl::vector<T, N> >
{
typedef etl::ivector<T> type;
};
template <typename T, const size_t N>
struct priority_queue_container_interface<etl::deque<T, N> >
{
typedef etl::ideque<T> type;
};
//***************************************************************************
//***************************************************************************
template <typename T, typename TContainerBase = etl::ivector<T>, typename TCompare = etl::less<T> >
class ipriority_queue
{
public:
typedef T value_type;
typedef TCompare compare_type;
typedef TContainerBase container_base_type;
typedef T& reference;
typedef const T& const_reference;
#if ETL_USING_CPP11
typedef T&& rvalue_reference;
#endif
typedef typename TContainerBase::size_type size_type;
typedef typename TContainerBase::difference_type difference_type;
//*************************************************************************
//*************************************************************************
reference top()
{
ETL_ASSERT_CHECK_EXTRA(!empty(), ETL_ERROR(priority_queue_empty));
return container.front();
}
//*************************************************************************
//*************************************************************************
const_reference top() const
{
ETL_ASSERT_CHECK_EXTRA(!empty(), ETL_ERROR(priority_queue_empty));
return container.front();
}
//*************************************************************************
//*************************************************************************
void push(const_reference value)
{
ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
// Put element at end
container.push_back(value);
// Make elements in container into heap
etl::push_heap(container.begin(), container.end(), compare);
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
void push(rvalue_reference value)
{
ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
// Put element at end
container.push_back(etl::move(value));
// Make elements in container into heap
etl::push_heap(container.begin(), container.end(), compare);
}
#endif
#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_PRIORITY_QUEUE_FORCE_CPP03_IMPLEMENTATION)
//*************************************************************************
//*************************************************************************
template <typename... Args>
void emplace(Args&&... args)
{
ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
// Put element at end
container.emplace_back(etl::forward<Args>(args)...);
// Make elements in container into heap
etl::push_heap(container.begin(), container.end(), compare);
}
#else
//*************************************************************************
//*************************************************************************
void emplace()
{
ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
// Put element at end
container.emplace_back();
// Make elements in container into heap
etl::push_heap(container.begin(), container.end(), compare);
}
//*************************************************************************
//*************************************************************************
template <typename T1>
void emplace(const T1& value1)
{
ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
// Put element at end
container.emplace_back(value1);
// Make elements in container into heap
etl::push_heap(container.begin(), container.end(), compare);
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2>
void emplace(const T1& value1, const T2& value2)
{
ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
// Put element at end
container.emplace_back(value1, value2);
// Make elements in container into heap
etl::push_heap(container.begin(), container.end(), compare);
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2, typename T3>
void emplace(const T1& value1, const T2& value2, const T3& value3)
{
ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
// Put element at end
container.emplace_back(value1, value2, value3);
// Make elements in container into heap
etl::push_heap(container.begin(), container.end(), compare);
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2, typename T3, typename T4>
void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
{
ETL_ASSERT(!full(), ETL_ERROR(etl::priority_queue_full));
// Put element at end
container.emplace_back(value1, value2, value3, value4);
// Make elements in container into heap
etl::push_heap(container.begin(), container.end(), compare);
}
#endif
//*************************************************************************
//*************************************************************************
template <typename TIterator>
void assign(TIterator first, TIterator last)
{
#if ETL_IS_DEBUG_BUILD
difference_type d = etl::distance(first, last);
ETL_ASSERT(d >= 0, ETL_ERROR(etl::priority_queue_iterator));
ETL_ASSERT(static_cast<size_t>(d) <= max_size(), ETL_ERROR(etl::priority_queue_full));
#endif
clear();
container.assign(first, last);
etl::make_heap(container.begin(), container.end(), compare);
}
//*************************************************************************
//*************************************************************************
void pop()
{
ETL_ASSERT_CHECK_PUSH_POP_OR_RETURN(!empty(), ETL_ERROR(priority_queue_empty));
// Move largest element to end
etl::pop_heap(container.begin(), container.end(), compare);
// Actually remove largest element at end
container.pop_back();
}
//*************************************************************************
//*************************************************************************
void pop_into(reference destination)
{
destination = ETL_MOVE(top());
pop();
}
//*************************************************************************
//*************************************************************************
size_type size() const
{
return container.size();
}
//*************************************************************************
//*************************************************************************
size_type max_size() const
{
return container.max_size();
}
//*************************************************************************
//*************************************************************************
bool empty() const
{
return container.empty();
}
//*************************************************************************
//*************************************************************************
bool full() const
{
return container.size() == container.max_size();
}
//*************************************************************************
//*************************************************************************
size_type available() const
{
return container.max_size() - container.size();
}
//*************************************************************************
//*************************************************************************
void clear()
{
container.clear();
}
//*************************************************************************
//*************************************************************************
ipriority_queue& operator=(const ipriority_queue& rhs)
{
if (&rhs != this)
{
clone(rhs);
}
return *this;
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
ipriority_queue& operator=(ipriority_queue&& rhs)
{
if (&rhs != this)
{
clear();
move(etl::move(rhs));
}
return *this;
}
#endif
//*************************************************************************
//*************************************************************************
#if defined(ETL_POLYMORPHIC_PRIORITY_QUEUE) || defined(ETL_POLYMORPHIC_CONTAINERS)
public:
virtual ~ipriority_queue() {}
#else
protected:
~ipriority_queue() {}
#endif
protected:
//*************************************************************************
//*************************************************************************
void clone(const ipriority_queue& other)
{
#include "etl/private/diagnostic_uninitialized_push.h"
assign(other.container.cbegin(), other.container.cend());
#include "etl/private/diagnostic_pop.h"
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
void move(ipriority_queue&& other)
{
while (!other.empty())
{
push(etl::move(other.top()));
other.pop();
}
}
#endif
//*************************************************************************
//*************************************************************************
ipriority_queue(TContainerBase& container_)
: container(container_)
{
}
private:
// Disable copy construction.
ipriority_queue(const ipriority_queue&);
TContainerBase& container;
TCompare compare;
};
//***************************************************************************
//***************************************************************************
template <typename T, const size_t SIZE, typename TContainer = etl::vector<T, SIZE>,
typename TCompare = etl::less<typename TContainer::value_type> >
class priority_queue : public etl::ipriority_queue<T, typename etl::priority_queue_container_interface<TContainer>::type, TCompare>
{
private:
typedef etl::ipriority_queue<T, typename etl::priority_queue_container_interface<TContainer>::type, TCompare> base_t;
public:
typedef typename TContainer::size_type size_type;
typedef TContainer container_type;
static ETL_CONSTANT size_type MAX_SIZE = size_type(SIZE);
//*************************************************************************
//*************************************************************************
priority_queue()
: base_t(container)
{
}
//*************************************************************************
//*************************************************************************
priority_queue(const priority_queue& rhs)
: base_t(container)
{
base_t::clone(rhs);
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
priority_queue(priority_queue&& rhs)
: base_t(container)
{
base_t::move(etl::move(rhs));
}
#endif
//*************************************************************************
//*************************************************************************
template <typename TIterator>
priority_queue(TIterator first, TIterator last)
: base_t(container)
{
base_t::assign(first, last);
}
//*************************************************************************
//*************************************************************************
~priority_queue()
{
base_t::clear();
}
//*************************************************************************
//*************************************************************************
priority_queue& operator=(const priority_queue& rhs)
{
if (&rhs != this)
{
base_t::clone(rhs);
}
return *this;
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
priority_queue& operator=(priority_queue&& rhs)
{
if (&rhs != this)
{
base_t::clear();
base_t::move(etl::move(rhs));
}
return *this;
}
#endif
private:
TContainer container;
};
template <typename T, const size_t SIZE, typename TContainer, typename TCompare>
ETL_CONSTANT typename priority_queue<T, SIZE, TContainer, TCompare>::size_type priority_queue<T, SIZE, TContainer, TCompare>::MAX_SIZE;
} // namespace etl
#endif
algorithm.h
etl::ivector
Definition vector.h:65
etl::priority_queue
Definition priority_queue.h:527
etl::priority_queue::~priority_queue
~priority_queue()
Destructor.
Definition priority_queue.h:583
etl::priority_queue::priority_queue
priority_queue(const priority_queue &rhs)
Copy constructor.
Definition priority_queue.h:550
etl::priority_queue::operator=
priority_queue & operator=(const priority_queue &rhs)
Assignment operator.
Definition priority_queue.h:591
etl::priority_queue::priority_queue
priority_queue()
Default constructor.
Definition priority_queue.h:542
etl::priority_queue::priority_queue
priority_queue(TIterator first, TIterator last)
Definition priority_queue.h:574
deque.h
diagnostic_pop.h
diagnostic_uninitialized_push.h
Make this a clone of the supplied priority queue.
error_handler.h
exception.h
functional.h
etl::deque
Definition deque.h:2365
etl::ideque
Definition deque.h:222
ETL_ASSERT
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
etl::ipriority_queue::top
reference top()
Definition priority_queue.h:173
etl::ipriority_queue::ipriority_queue
ipriority_queue(TContainerBase &container_)
The constructor that is called from derived classes.
Definition priority_queue.h:501
etl::ipriority_queue::operator=
ipriority_queue & operator=(const ipriority_queue &rhs)
Assignment operator.
Definition priority_queue.h:431
etl::ipriority_queue::size_type
TContainerBase::size_type size_type
The type used for determining the size of the queue.
Definition priority_queue.h:164
etl::ipriority_queue::compare_type
TCompare compare_type
The comparison type.
Definition priority_queue.h:157
etl::ipriority_queue::emplace
void emplace()
Definition priority_queue.h:249
etl::ipriority_queue::container_base_type
TContainerBase container_base_type
The non-sized container base type.
Definition priority_queue.h:158
etl::ipriority_queue::top
const_reference top() const
Definition priority_queue.h:185
etl::ipriority_queue::reference
T & reference
A reference to the type used in the queue.
Definition priority_queue.h:159
etl::ipriority_queue::const_reference
const T & const_reference
A const reference to the type used in the queue.
Definition priority_queue.h:160
etl::ipriority_queue::full
bool full() const
Definition priority_queue.h:406
etl::ipriority_queue::push
void push(const_reference value)
Definition priority_queue.h:197
etl::ipriority_queue::~ipriority_queue
~ipriority_queue()
Destructor.
Definition priority_queue.h:469
etl::ipriority_queue::pop_into
void pop_into(reference destination)
Definition priority_queue.h:370
etl::ipriority_queue::max_size
size_type max_size() const
Returns the maximum number of items that can be queued.
Definition priority_queue.h:387
etl::ipriority_queue::clear
void clear()
Clears the queue to the empty state.
Definition priority_queue.h:423
etl::ipriority_queue::value_type
T value_type
The type stored in the queue.
Definition priority_queue.h:156
etl::ipriority_queue::emplace
void emplace(const T1 &value1, const T2 &value2)
Definition priority_queue.h:283
etl::ipriority_queue::emplace
void emplace(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition priority_queue.h:317
etl::ipriority_queue::emplace
void emplace(const T1 &value1)
Definition priority_queue.h:266
etl::ipriority_queue::available
size_type available() const
Definition priority_queue.h:415
etl::ipriority_queue::pop
void pop()
Definition priority_queue.h:357
etl::ipriority_queue::size
size_type size() const
Returns the current number of items in the priority queue.
Definition priority_queue.h:379
etl::ipriority_queue::empty
bool empty() const
Definition priority_queue.h:396
etl::ipriority_queue::assign
void assign(TIterator first, TIterator last)
Definition priority_queue.h:338
etl::ipriority_queue::emplace
void emplace(const T1 &value1, const T2 &value2, const T3 &value3)
Definition priority_queue.h:300
etl::ipriority_queue
This is the base for all priority queues that contain a particular type.
Definition priority_queue.h:153
etl::priority_queue_empty
Definition priority_queue.h:105
etl::priority_queue_exception
Definition priority_queue.h:63
etl::priority_queue_full
Definition priority_queue.h:77
etl::priority_queue_iterator
Definition priority_queue.h:91
etl::vector
Definition vector.h:1830
iterator.h
etl
Definition absolute.h:40
parameter_type.h
platform.h
etl::compare
Definition compare.h:51
etl::priority_queue_container_interface
Definition priority_queue.h:120
type_traits.h
utility.h
vector.h