mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
add priority_queue implementation
This commit is contained in:
parent
426bbab41c
commit
f3ec3995cb
220
ipriority_queue.h
Normal file
220
ipriority_queue.h
Normal file
@ -0,0 +1,220 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
|
||||
Copyright(c) 2015 jwellbelove, 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_IPRIORITY_QUEUE__
|
||||
#define __ETL_IPRIORITY_QUEUE__
|
||||
#define __ETL_IN_IPRIORITY_QUEUE_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "priority_queue_base.h"
|
||||
#include "type_traits.h"
|
||||
#include "parameter_type.h"
|
||||
|
||||
#ifndef ETL_THROW_EXCEPTIONS
|
||||
#include "error_handler.h"
|
||||
#endif
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup queue
|
||||
///\brief This is the base for all priority queues that contain a particular type.
|
||||
///\details Normally a reference to this type will be taken from a derived queue.
|
||||
/// The TContainer specified must provide the front, push_back, pop_back, and
|
||||
/// assign methods to work correctly with priority_queue.
|
||||
///\code
|
||||
/// etl::priority_queue<int, 10> myPriorityQueue;
|
||||
/// etl::ipriority_queue<int>& iQueue = myPriorityQueue;
|
||||
///\endcode
|
||||
/// \warning This priority queue cannot be used for concurrent access from
|
||||
/// multiple threads.
|
||||
/// \tparam T The type of value that the queue holds.
|
||||
/// \tparam TContainer to hold the T queue values
|
||||
/// \tparam TCompare to use in comparing T values
|
||||
//***************************************************************************
|
||||
template <typename T, typename TContainer, typename TCompare>
|
||||
class ipriority_queue : public priority_queue_base
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T value_type; ///< The type stored in the queue.
|
||||
typedef TContainer container_type; ///< The container type used for priority queue.
|
||||
typedef T& reference; ///< A reference to the type used in the queue.
|
||||
typedef const T& const_reference; ///< A const reference to the type used in the queue.
|
||||
typedef priority_queue_base::size_type size_type; ///< The type used for determining the size of the queue.
|
||||
typedef typename std::iterator_traits<typename TContainer::iterator>::difference_type difference_type;
|
||||
|
||||
private:
|
||||
|
||||
typedef typename parameter_type<T>::type parameter_t;
|
||||
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets a reference to the highest priority value in the priority queue.<br>
|
||||
/// \return A reference to the highest priority value in the priority queue.
|
||||
//*************************************************************************
|
||||
reference top()
|
||||
{
|
||||
return container.front();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets a const reference to the highest priority value in the priority queue.<br>
|
||||
/// \return A const reference to the highest priority value in the priority queue.
|
||||
//*************************************************************************
|
||||
const_reference top() const
|
||||
{
|
||||
return container.front();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Adds a value to the queue.
|
||||
/// If ETL_THROW_EXCEPTIONS is defined, throws an etl::priority_queue_full
|
||||
/// is the priority queue is already full, otherwise does nothing if full.
|
||||
///\param value The value to push to the queue.
|
||||
//*************************************************************************
|
||||
void push(parameter_t value)
|
||||
{
|
||||
if (!full())
|
||||
{
|
||||
// Put element at end
|
||||
container.push_back(value);
|
||||
// Pre-increment size
|
||||
++current_size;
|
||||
// Make elements in container into heap
|
||||
std::push_heap(container.begin(), container.end(), TCompare());
|
||||
}
|
||||
else
|
||||
#ifdef ETL_THROW_EXCEPTIONS
|
||||
{
|
||||
throw priority_queue_full();
|
||||
}
|
||||
#else
|
||||
{
|
||||
error_handler::error(priority_queue_full());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Clears the queue to the empty state.
|
||||
//*************************************************************************
|
||||
void clear()
|
||||
{
|
||||
container.clear();
|
||||
current_size = 0;
|
||||
}
|
||||
|
||||
//*********************************************************************
|
||||
/// Assigns values to the priority queue.
|
||||
/// If ETL_THROW_EXCEPTIONS is defined, emits priority_queue_full if
|
||||
/// priority queue does not have enough free space.
|
||||
/// If ETL_THROW_EXCEPTIONS is defined, emits priority_iterator if the
|
||||
/// iterators are reversed.
|
||||
///\param first The iterator to the first element.
|
||||
///\param last The iterator to the last element + 1.
|
||||
//*********************************************************************
|
||||
template <typename TIterator>
|
||||
void assign(TIterator first, TIterator last)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
difference_type count = std::distance(first, last);
|
||||
|
||||
if (count < 0)
|
||||
{
|
||||
#ifdef ETL_THROW_EXCEPTIONS
|
||||
throw priority_queue_iterator();
|
||||
#else
|
||||
error_handler::error(priority_queue_iterator());
|
||||
#endif
|
||||
}
|
||||
|
||||
if (static_cast<size_t>(count) > MAX_SIZE)
|
||||
{
|
||||
#ifdef ETL_THROW_EXCEPTIONS
|
||||
throw priority_queue_full();
|
||||
#else
|
||||
error_handler::error(priority_queue_full());
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
clear();
|
||||
container.assign(first, last);
|
||||
std::make_heap(container.begin(), container.end(), TCompare());
|
||||
current_size = container.size();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Removes the oldest value from the back of the priority queue.
|
||||
/// Does nothing if the priority queue is already empty.
|
||||
//*************************************************************************
|
||||
void pop()
|
||||
{
|
||||
if (!empty())
|
||||
{
|
||||
// Move largest element to end
|
||||
std::pop_heap(container.begin(), container.end(), TCompare());
|
||||
// Actually remove largest element at end
|
||||
container.pop_back();
|
||||
// Decrement size
|
||||
--current_size;
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// Make this a clone of the supplied priority queue
|
||||
//*************************************************************************
|
||||
void clone(const ipriority_queue& other)
|
||||
{
|
||||
assign(other.container.cbegin(), other.container.cend());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// The constructor that is called from derived classes.
|
||||
//*************************************************************************
|
||||
ipriority_queue(size_type max_size)
|
||||
: priority_queue_base(max_size)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The container specified at instantiation of the priority_queue
|
||||
TContainer container;
|
||||
};
|
||||
}
|
||||
|
||||
#undef __ETL_IN_IPRIORITY_QUEUE_H__
|
||||
#endif
|
||||
2
iqueue.h
2
iqueue.h
@ -63,7 +63,7 @@ namespace etl
|
||||
typedef T& reference; ///< A reference to the type used in the queue.
|
||||
typedef const T& const_reference; ///< A const reference to the type used in the queue.
|
||||
typedef T* pointer; ///< A pointer to the type used in the queue.
|
||||
typedef const T* const_pointer; ///< A const pointer to the type used in the qu
|
||||
typedef const T* const_pointer; ///< A const pointer to the type used in the queue.
|
||||
typedef queue_base::size_type size_type; ///< The type used for determining the size of the queue.
|
||||
|
||||
private:
|
||||
|
||||
114
priority_queue.h
Normal file
114
priority_queue.h
Normal file
@ -0,0 +1,114 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
|
||||
Copyright(c) 2015 jwellbelove, 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__
|
||||
#define __ETL_PRIORITY_QUEUE__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <functional>
|
||||
|
||||
#include "ipriority_queue.h"
|
||||
#include "container.h"
|
||||
#include "vector.h"
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup queue queue
|
||||
/// A priority queue with the capacity defined at compile time,
|
||||
/// written in the STL style.
|
||||
///\ingroup containers
|
||||
//*****************************************************************************
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup queue
|
||||
/// A fixed capacity queue.
|
||||
/// This queue does not support concurrent access by different threads.
|
||||
/// \tparam T The type this queue should support.
|
||||
/// \tparam SIZE The maximum capacity of the queue.
|
||||
//***************************************************************************
|
||||
template <typename T, const size_t SIZE, typename TContainer = etl::vector<T, SIZE>, typename TCompare = std::less<typename TContainer::value_type> >
|
||||
class priority_queue : public ipriority_queue<T, TContainer, TCompare>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
priority_queue()
|
||||
: ipriority_queue<T, TContainer, TCompare>(SIZE)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor
|
||||
//*************************************************************************
|
||||
priority_queue(const priority_queue& rhs)
|
||||
: ipriority_queue<T, TContainer, TCompare>(SIZE)
|
||||
{
|
||||
ipriority_queue<T, TContainer, TCompare>::clone(rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor, from an iterator range.
|
||||
///\tparam TIterator The iterator type.
|
||||
///\param first The iterator to the first element.
|
||||
///\param last The iterator to the last element + 1.
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
priority_queue(TIterator first, TIterator last)
|
||||
: ipriority_queue<T, TContainer, TCompare>(SIZE)
|
||||
{
|
||||
ipriority_queue<T, TContainer, TCompare>::assign(first, last);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destructor.
|
||||
//*************************************************************************
|
||||
~priority_queue()
|
||||
{
|
||||
ipriority_queue<T, TContainer, TCompare>::clear();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assignment operator.
|
||||
//*************************************************************************
|
||||
priority_queue& operator = (const priority_queue& rhs)
|
||||
{
|
||||
if (&rhs != this)
|
||||
{
|
||||
ipriority_queue<T, TContainer, TCompare>::clone(rhs);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
154
priority_queue_base.h
Normal file
154
priority_queue_base.h
Normal file
@ -0,0 +1,154 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
|
||||
Copyright(c) 2015 jwellbelove, 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_IN_IPRIORITY_QUEUE_H__
|
||||
#error This header is a private element of etl::priority_queue & etl::ipriority_queue
|
||||
#endif
|
||||
|
||||
#ifndef __ETL_PRIORITY_QUEUE_BASE__
|
||||
#define __ETL_PRIORITY_QUEUE_BASE__
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "exception.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// The base class for priority_queue exceptions.
|
||||
///\ingroup queue
|
||||
//***************************************************************************
|
||||
class priority_queue_exception : public exception
|
||||
{
|
||||
public:
|
||||
|
||||
priority_queue_exception(const char* what)
|
||||
: exception(what)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// The exception thrown when the queue is full.
|
||||
///\ingroup queue
|
||||
//***************************************************************************
|
||||
class priority_queue_full : public priority_queue_exception
|
||||
{
|
||||
public:
|
||||
|
||||
priority_queue_full()
|
||||
: priority_queue_exception("priority_queue: full")
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// The priority queue iterator exception on reversed iterators
|
||||
///\ingroup queue
|
||||
//***************************************************************************
|
||||
class priority_queue_iterator : public priority_queue_exception
|
||||
{
|
||||
public:
|
||||
|
||||
priority_queue_iterator()
|
||||
: priority_queue_exception("priority_queue: iterator error")
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// The base class for all priority queues.
|
||||
///\ingroup queue
|
||||
//***************************************************************************
|
||||
class priority_queue_base
|
||||
{
|
||||
public:
|
||||
|
||||
typedef size_t size_type; ///< The type used for determining the size of queue.
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the current number of items in the priority queue.
|
||||
//*************************************************************************
|
||||
size_type size() const
|
||||
{
|
||||
return current_size;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum number of items that can be queued.
|
||||
//*************************************************************************
|
||||
size_type max_size() const
|
||||
{
|
||||
return MAX_SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Checks to see if the priority queue is empty.
|
||||
/// \return <b>true</b> if the queue is empty, otherwise <b>false</b>
|
||||
//*************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return current_size == 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Checks to see if the priority queue is full.
|
||||
/// \return <b>true</b> if the priority queue is full, otherwise <b>false</b>
|
||||
//*************************************************************************
|
||||
bool full() const
|
||||
{
|
||||
return current_size == MAX_SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the remaining capacity.
|
||||
///\return The remaining capacity.
|
||||
//*************************************************************************
|
||||
size_t available() const
|
||||
{
|
||||
return max_size() - size();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// The constructor that is called from derived classes.
|
||||
//*************************************************************************
|
||||
priority_queue_base(size_type max_size)
|
||||
: current_size(0),
|
||||
MAX_SIZE(max_size)
|
||||
{
|
||||
}
|
||||
|
||||
size_type current_size; ///< The number of items in the priority queue.
|
||||
const size_type MAX_SIZE; ///< The maximum number of items in the priority queue.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
2
queue.h
2
queue.h
@ -99,7 +99,7 @@ namespace etl
|
||||
|
||||
private:
|
||||
|
||||
/// The unititialised buffer of T used in the stack.
|
||||
/// The uninitialised buffer of T used in the stack.
|
||||
typename etl::aligned_storage<sizeof(T), etl::alignment_of<T>::value>::type buffer[SIZE];
|
||||
};
|
||||
}
|
||||
|
||||
353
test/test_priority_queue.cpp
Normal file
353
test/test_priority_queue.cpp
Normal file
@ -0,0 +1,353 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
|
||||
Copyright(c) 2015 jwellbelove, 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <queue>
|
||||
|
||||
#include "../priority_queue.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
SUITE(test_priority_queue)
|
||||
{
|
||||
static const size_t SIZE = 4;
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_default_constructor)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
CHECK_EQUAL(priority_queue.size(), size_t(0));
|
||||
CHECK_EQUAL(priority_queue.available(), SIZE);
|
||||
CHECK_EQUAL(priority_queue.max_size(), SIZE);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_copy_constructor)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
priority_queue.push(3);
|
||||
priority_queue.push(1);
|
||||
priority_queue.push(4);
|
||||
priority_queue.push(2);
|
||||
|
||||
etl::priority_queue<int, SIZE> priority_queue2(priority_queue);
|
||||
|
||||
CHECK(priority_queue.size() == priority_queue2.size());
|
||||
|
||||
while (!priority_queue.empty())
|
||||
{
|
||||
CHECK_EQUAL(priority_queue.top(), priority_queue2.top());
|
||||
priority_queue.pop();
|
||||
priority_queue2.pop();
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_range)
|
||||
{
|
||||
int n[] = { 3, 4, 1, 2 };
|
||||
etl::priority_queue<int, SIZE> priority_queue(std::begin(n), std::end(n));
|
||||
std::priority_queue<int> compare_priority_queue(std::begin(n), std::end(n));
|
||||
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK(!priority_queue.empty());
|
||||
|
||||
while (!priority_queue.empty())
|
||||
{
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
compare_priority_queue.pop();
|
||||
priority_queue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_size)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
priority_queue.push(1);
|
||||
priority_queue.push(2);
|
||||
priority_queue.push(3);
|
||||
|
||||
CHECK_EQUAL(3, priority_queue.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_clear)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
priority_queue.push(1);
|
||||
priority_queue.push(2);
|
||||
priority_queue.clear();
|
||||
CHECK_EQUAL(0, priority_queue.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_assign_range)
|
||||
{
|
||||
int n[] = { 3, 4, 1, 2 };
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
std::priority_queue<int> compare_priority_queue(std::begin(n), std::end(n));
|
||||
|
||||
priority_queue.assign(std::begin(n), std::end(n));
|
||||
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK(!priority_queue.empty());
|
||||
|
||||
while (!priority_queue.empty())
|
||||
{
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
compare_priority_queue.pop();
|
||||
priority_queue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_empty)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
CHECK(priority_queue.empty());
|
||||
|
||||
priority_queue.push(1);
|
||||
|
||||
CHECK(!priority_queue.empty());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_full)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
CHECK(!priority_queue.full());
|
||||
|
||||
priority_queue.push(1);
|
||||
priority_queue.push(2);
|
||||
priority_queue.push(3);
|
||||
priority_queue.push(4);
|
||||
|
||||
CHECK(priority_queue.full());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_top)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
std::priority_queue<int> compare_priority_queue;
|
||||
|
||||
priority_queue.push(1);
|
||||
compare_priority_queue.push(1);
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.push(3);
|
||||
compare_priority_queue.push(3);
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.push(2);
|
||||
compare_priority_queue.push(2);
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.push(4);
|
||||
compare_priority_queue.push(4);
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_top_const)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
const etl::priority_queue<int, SIZE>& constQueue = priority_queue;
|
||||
|
||||
priority_queue.push(1);
|
||||
CHECK_EQUAL(1, constQueue.top());
|
||||
priority_queue.push(3);
|
||||
CHECK_EQUAL(3, constQueue.top());
|
||||
priority_queue.push(2);
|
||||
CHECK_EQUAL(3, constQueue.top());
|
||||
priority_queue.push(4);
|
||||
CHECK_EQUAL(4, constQueue.top());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_push)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
std::priority_queue<int> compare_priority_queue;
|
||||
|
||||
priority_queue.push(1);
|
||||
compare_priority_queue.push(1);
|
||||
|
||||
priority_queue.push(2);
|
||||
compare_priority_queue.push(2);
|
||||
|
||||
priority_queue.push(3);
|
||||
compare_priority_queue.push(3);
|
||||
|
||||
priority_queue.push(4);
|
||||
compare_priority_queue.push(4);
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_push_excess)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
for (size_t i = 0; i < priority_queue.max_size(); ++i)
|
||||
{
|
||||
priority_queue.push(1);
|
||||
}
|
||||
|
||||
CHECK_THROW(priority_queue.push(1), etl::priority_queue_full);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_pop)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
std::priority_queue<int> compare_priority_queue;
|
||||
|
||||
priority_queue.push(1);
|
||||
compare_priority_queue.push(1);
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.pop();
|
||||
compare_priority_queue.pop();
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
|
||||
priority_queue.push(3);
|
||||
compare_priority_queue.push(3);
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.push(1);
|
||||
compare_priority_queue.push(1);
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.pop();
|
||||
compare_priority_queue.pop();
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.push(1);
|
||||
compare_priority_queue.push(1);
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.push(2);
|
||||
compare_priority_queue.push(2);
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.push(1);
|
||||
compare_priority_queue.push(1);
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.pop();
|
||||
compare_priority_queue.pop();
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.pop();
|
||||
compare_priority_queue.pop();
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.pop();
|
||||
compare_priority_queue.pop();
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
CHECK_EQUAL(compare_priority_queue.top(), priority_queue.top());
|
||||
|
||||
priority_queue.pop();
|
||||
compare_priority_queue.pop();
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
|
||||
// Go one beyond (which we handle without throwing)
|
||||
priority_queue.pop();
|
||||
CHECK_EQUAL(compare_priority_queue.size(), priority_queue.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_assignment)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
priority_queue.push(1);
|
||||
priority_queue.push(4);
|
||||
priority_queue.push(3);
|
||||
priority_queue.push(2);
|
||||
|
||||
etl::priority_queue<int, SIZE> priority_queue2;
|
||||
|
||||
priority_queue2 = priority_queue;
|
||||
|
||||
CHECK(priority_queue.size() == priority_queue2.size());
|
||||
|
||||
while (!priority_queue.empty())
|
||||
{
|
||||
CHECK_EQUAL(priority_queue.top(), priority_queue2.top());
|
||||
priority_queue.pop();
|
||||
priority_queue2.pop();
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_self_assignment)
|
||||
{
|
||||
etl::priority_queue<int, SIZE> priority_queue;
|
||||
|
||||
priority_queue.push(2);
|
||||
priority_queue.push(1);
|
||||
priority_queue.push(4);
|
||||
priority_queue.push(3);
|
||||
|
||||
priority_queue = priority_queue;
|
||||
|
||||
CHECK(priority_queue.max_size() == priority_queue.size());
|
||||
|
||||
CHECK_EQUAL(4, priority_queue.top());
|
||||
priority_queue.pop();
|
||||
|
||||
CHECK_EQUAL(3, priority_queue.top());
|
||||
priority_queue.pop();
|
||||
|
||||
CHECK_EQUAL(2, priority_queue.top());
|
||||
priority_queue.pop();
|
||||
|
||||
CHECK_EQUAL(1, priority_queue.top());
|
||||
priority_queue.pop();
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -159,6 +159,7 @@
|
||||
<ClInclude Include="..\..\integral_limits.h" />
|
||||
<ClInclude Include="..\..\io_port.h" />
|
||||
<ClInclude Include="..\..\ipool.h" />
|
||||
<ClInclude Include="..\..\ipriority_queue.h" />
|
||||
<ClInclude Include="..\..\iqueue.h" />
|
||||
<ClInclude Include="..\..\iset.h" />
|
||||
<ClInclude Include="..\..\istack.h" />
|
||||
@ -183,6 +184,8 @@
|
||||
<ClInclude Include="..\..\pool.h" />
|
||||
<ClInclude Include="..\..\pool_base.h" />
|
||||
<ClInclude Include="..\..\power.h" />
|
||||
<ClInclude Include="..\..\priority_queue.h" />
|
||||
<ClInclude Include="..\..\priority_queue_base.h" />
|
||||
<ClInclude Include="..\..\queue.h" />
|
||||
<ClInclude Include="..\..\queue_base.h" />
|
||||
<ClInclude Include="..\..\radix.h" />
|
||||
@ -278,6 +281,7 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_optional.cpp" />
|
||||
<ClCompile Include="..\test_pool.cpp" />
|
||||
<ClCompile Include="..\test_priority_queue.cpp" />
|
||||
<ClCompile Include="..\test_queue.cpp" />
|
||||
<ClCompile Include="..\test_set.cpp" />
|
||||
<ClCompile Include="..\test_smallest.cpp" />
|
||||
|
||||
@ -387,6 +387,15 @@
|
||||
<ClInclude Include="..\..\multiset.h">
|
||||
<Filter>ETL\Containers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\ipriority_queue.h">
|
||||
<Filter>ETL\Containers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\priority_queue.h">
|
||||
<Filter>ETL\Containers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\priority_queue_base.h">
|
||||
<Filter>ETL\Containers</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\..\unittest-cpp\UnitTest++\AssertException.cpp">
|
||||
@ -602,6 +611,9 @@
|
||||
<ClCompile Include="..\test_multiset.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_priority_queue.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\Doxyfile">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user