From 9c358ba618485bc1113708ab2634f10a5124aa97 Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Mon, 24 Aug 2015 12:21:23 +0200 Subject: [PATCH] Basic Priority Queue with modifykey implementation with std::map --- lib/graph/priority_queue.hpp | 7 +- test/graph/test_priority_queue.cpp | 105 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 test/graph/test_priority_queue.cpp diff --git a/lib/graph/priority_queue.hpp b/lib/graph/priority_queue.hpp index 970507d..bf864f3 100644 --- a/lib/graph/priority_queue.hpp +++ b/lib/graph/priority_queue.hpp @@ -38,12 +38,17 @@ public: void pop() { m_map.erase(m_map.begin()); } void push(const Key& key, const T& value) { m_map.emplace(key, value); } void modifyKey(const T& value, const Key& diff) { - auto it = std::find_if(m_map.begin(), m_map.end(), [&value](const std::pair & p) { return p.second == value; } ); + auto it = std::find_if(m_map.begin(), m_map.end(), [&value](const std::pair & p) { return p.second == value; } ); Key key = it->first; // take a copy m_map.erase(it); m_map.emplace(key + diff, value); } + bool contains(const T& value) const { + auto it = std::find_if(m_map.begin(), m_map.end(), [&value](const std::pair & p) { return p.second == value; } ); + return it != m_map.end(); + } + private: std::map m_map; }; diff --git a/test/graph/test_priority_queue.cpp b/test/graph/test_priority_queue.cpp new file mode 100644 index 0000000..9e0c408 --- /dev/null +++ b/test/graph/test_priority_queue.cpp @@ -0,0 +1,105 @@ +#include + +#include "../catch.hpp" + +#include "fixture.hpp" + + + +TEST_CASE("Priority queue", "[priority_queue][data_structure]" ) { + +typedef float W; +typedef float2 V; +typedef std::pair P; + + SECTION("empty") { + PriorityQueue pq; + + REQUIRE( pq.size() == 0 ); + REQUIRE( pq.empty() == true ); + } + + SECTION("One element") { + PriorityQueue pq; + const V f2(1.0f, 1.0f); + pq.push(1.0f, f2); + + REQUIRE( pq.size() == 1 ); + REQUIRE( pq.empty() == false ); + REQUIRE( pq.contains(f2) == true ); + + P p = pq.top(); + REQUIRE( p.second == f2 ); + + pq.pop(); + REQUIRE( pq.size() == 0 ); + REQUIRE( pq.empty() == true ); + } + + SECTION("Multiple elements") { + PriorityQueue pq; + const V f2_1(1.0f, 1.0f); + const V f2_2(2.0f, 2.0f); + const V f2_3(3.0f, 3.0f); + + pq.push(3.0f, f2_3); + pq.push(1.0f, f2_1); + pq.push(2.0f, f2_2); + + REQUIRE( pq.size() == 3 ); + REQUIRE( pq.empty() == false ); + REQUIRE( pq.contains(f2_1) == true ); + REQUIRE( pq.contains(f2_2) == true ); + REQUIRE( pq.contains(f2_3) == true ); + + P p1 = pq.top(); + pq.pop(); + REQUIRE( p1.first == 1.0f ); + REQUIRE( p1.second == f2_1 ); + REQUIRE( pq.size() == 2 ); + + P p2 = pq.top(); + pq.pop(); + REQUIRE( p2.first == 2.0f ); + REQUIRE( p2.second == f2_2 ); + REQUIRE( pq.size() == 1 ); + + P p3 = pq.top(); + pq.pop(); + REQUIRE( p3.first == 3.0f ); + REQUIRE( p3.second == f2_3 ); + + REQUIRE( pq.size() == 0 ); + REQUIRE( pq.empty() == true ); + } + + SECTION("Modify key") { + PriorityQueue pq; + const V f2_1(1.0f, 1.0f); + const V f2_2(2.0f, 2.0f); + const V f2_3(3.0f, 3.0f); + + pq.push(3.0f, f2_3); + pq.push(1.0f, f2_1); + pq.push(2.0f, f2_2); + + pq.modifyKey(f2_1, +3.0f); + pq.modifyKey(f2_3, -2.0f); + + P p1 = pq.top(); + pq.pop(); + REQUIRE( p1.first == 1.0f ); + REQUIRE( p1.second == f2_3 ); + + P p2 = pq.top(); + pq.pop(); + REQUIRE( p2.first == 2.0f ); + REQUIRE( p2.second == f2_2 ); + + P p3 = pq.top(); + pq.pop(); + REQUIRE( p3.first == 4.0f ); + REQUIRE( p3.second == f2_1 ); + } + +}