PriorityQueue does not have contains fn anymore

master
dmatetelki 10 years ago
parent 79b6b1b24d
commit 64e8db157d

@ -28,25 +28,23 @@ public:
PriorityQueue() : m_map() {} PriorityQueue() : m_map() {}
// capacity // capacity
size_t size() const { return m_map.size(); } size_t size() const noexcept { return m_map.size(); }
bool empty() const { return m_map.empty(); } bool empty() const noexcept { return m_map.empty(); }
// lookup // lookup
std::pair<Key, T> top() const { return *m_map.begin(); } std::pair<Key, T> top() const noexcept { return *m_map.begin(); }
// modifiers // modifiers
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 Key& key, const T& value, const Key& new_key) { bool modifyKey(const Key& key, const T& value, const Key& new_key) {
auto it = std::find_if(m_map.begin(), m_map.end(), [&key, &value](const std::pair<const Key, T> & p) { return p.first == key && p.second == value; } ); auto it = std::find_if(m_map.begin(), m_map.end(),
[&key, &value](const std::pair<const Key, T> & p) { return p.first == key && p.second == value; } );
if (it == m_map.end()) return false;
T v = it->second; // take a copy T v = it->second; // take a copy
m_map.erase(it); m_map.erase(it);
m_map.emplace(new_key, v); m_map.emplace(new_key, v);
} return true;
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:

@ -26,7 +26,6 @@ typedef std::pair<W, V> P;
REQUIRE( pq.size() == 1 ); REQUIRE( pq.size() == 1 );
REQUIRE( pq.empty() == false ); REQUIRE( pq.empty() == false );
REQUIRE( pq.contains(f2) == true );
P p = pq.top(); P p = pq.top();
REQUIRE( p.second == f2 ); REQUIRE( p.second == f2 );
@ -48,9 +47,6 @@ typedef std::pair<W, V> P;
REQUIRE( pq.size() == 3 ); REQUIRE( pq.size() == 3 );
REQUIRE( pq.empty() == false ); 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(); P p1 = pq.top();
pq.pop(); pq.pop();
@ -83,8 +79,8 @@ typedef std::pair<W, V> P;
pq.push(1.0f, f2_1); pq.push(1.0f, f2_1);
pq.push(2.0f, f2_2); pq.push(2.0f, f2_2);
pq.modifyKey(f2_1, +3.0f); pq.modifyKey(1.0f, f2_1, 1.0f + 3.0f);
pq.modifyKey(f2_3, -2.0f); pq.modifyKey(3.0f, f2_3, 3.0f - 2.0f);
P p1 = pq.top(); P p1 = pq.top();
pq.pop(); pq.pop();

Loading…
Cancel
Save