Files
c-evo-evo/game/src/IPQ.cpp
T

116 lines
3.3 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#include "Cevo/IPQ.h"
#include <algorithm>
namespace Cevo {
IPQ::IPQ(int max) : m_heap(static_cast<std::size_t>(max)), m_index(static_cast<std::size_t>(max), -1)
{
}
void IPQ::clear()
{
if (m_size != 0) {
std::fill(m_index.begin(), m_index.end(), -1);
m_size = 0;
}
}
// Parent(i) = (i-1)/2.
bool IPQ::put(int item, int value)
{
int i = m_index[item];
if (i >= 0) {
if (m_heap[i].value <= value)
return false;
} else {
i = m_size;
++m_size;
}
while (i > 0) {
const int j = (i - 1) >> 1; // Parent(i) = (i-1)/2
if (value >= m_heap[j].value)
break;
m_heap[i] = m_heap[j];
m_index[m_heap[i].item] = i;
i = j;
}
// Insert the new item at the insertion point found.
m_heap[i].value = value;
m_heap[i].item = item;
m_index[item] = i;
return true;
}
bool IPQ::testPut(int item, int value) const
{
const int i = m_index[item];
return i < 0 || m_heap[i].value > value;
}
// Left(i) = 2*i+1, Right(i) = Left(i)+1.
bool IPQ::get(int &item, int &value)
{
if (m_size == 0)
return false;
item = m_heap[0].item;
value = m_heap[0].value;
m_index[item] = -1;
--m_size;
if (m_size > 0) {
const HeapItem last = m_heap[m_size];
int i = 0;
int j = 1;
while (j < m_size) {
// Right(i) = Left(i)+1
if (j < m_size - 1 && m_heap[j].value > m_heap[j + 1].value)
++j;
if (last.value <= m_heap[j].value)
break;
m_heap[i] = m_heap[j];
m_index[m_heap[i].item] = i;
i = j;
j = (j << 1) + 1; // Left(j) = 2*j+1
}
// Insert the former last element in the correct place in the heap.
m_heap[i] = last;
m_index[last.item] = i;
}
return true;
}
} // namespace Cevo