Basic Priority Queue with modifykey implementation with std::map

master
dmatetelki 10 years ago
parent 951fe8a67d
commit 9c358ba618

@ -38,12 +38,17 @@ public:
void pop() { m_map.erase(m_map.begin()); } void pop() { m_map.erase(m_map.begin()); }
void push(const Key& key, const T& value) { m_map.emplace(key, value); } void push(const Key& key, const T& value) { m_map.emplace(key, value); }
void modifyKey(const T& value, const Key& diff) { void modifyKey(const T& value, const Key& diff) {
auto it = std::find_if(m_map.begin(), m_map.end(), [&value](const std::pair<const int, std::string> & p) { return p.second == value; } ); auto it = std::find_if(m_map.begin(), m_map.end(), [&value](const std::pair<const Key, T> & p) { return p.second == value; } );
Key key = it->first; // take a copy Key key = it->first; // take a copy
m_map.erase(it); m_map.erase(it);
m_map.emplace(key + diff, value); 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<const Key, T> & p) { return p.second == value; } );
return it != m_map.end();
}
private: private:
std::map<Key, T, Compare, Allocator> m_map; std::map<Key, T, Compare, Allocator> m_map;
}; };

@ -0,0 +1,105 @@
#include <graph/priority_queue.hpp>
#include "../catch.hpp"
#include "fixture.hpp"
TEST_CASE("Priority queue", "[priority_queue][data_structure]" ) {
typedef float W;
typedef float2 V;
typedef std::pair<W, V> P;
SECTION("empty") {
PriorityQueue<W, V> pq;
REQUIRE( pq.size() == 0 );
REQUIRE( pq.empty() == true );
}
SECTION("One element") {
PriorityQueue<W, V> 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<W, V> 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<W, V> 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 );
}
}
Loading…
Cancel
Save