// This file is part of cevo-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/Pile.h" #include #include namespace Cevo { void Pile::create(int size) { assert(size <= MaxSize); if (n_ != 0 || size > currentSize_) { std::fill_n(ix_, size, -1); n_ = 0; } currentSize_ = size; } void Pile::empty() { if (n_ != 0) { std::fill_n(ix_, currentSize_, -1); n_ = 0; } } // Parent(i) = (i-1)/2. bool Pile::put(int item, int value) // O(lg(n)) { assert(item < currentSize_); int i = ix_[item]; if (i >= 0) { if (bh_[i].value <= value) return false; } else { i = n_; ++n_; } while (i > 0) { const int j = (i - 1) >> 1; // Parent(i) = (i-1)/2 if (value >= bh_[j].value) break; bh_[i] = bh_[j]; ix_[bh_[i].item] = i; i = j; } // Insert the new item at the insertion point found. bh_[i].value = value; bh_[i].item = item; ix_[bh_[i].item] = i; return true; } bool Pile::testPut(int item, int value) const { assert(item < currentSize_); const int i = ix_[item]; return i < 0 || bh_[i].value > value; } // Left(i) = 2*i+1, Right(i) = Left(i)+1. bool Pile::get(int &item, int &value) // O(lg(n)) { if (n_ == 0) return false; item = bh_[0].item; value = bh_[0].value; ix_[item] = -1; --n_; if (n_ > 0) { const HeapItem last = bh_[n_]; int i = 0; int j = 1; while (j < n_) { if (j < n_ - 1 && bh_[j].value > bh_[j + 1].value) ++j; // Right(i) = Left(i)+1 if (last.value <= bh_[j].value) break; bh_[i] = bh_[j]; ix_[bh_[i].item] = i; i = j; j = (j << 1) + 1; // Left(j) = 2*j+1 } // Insert the root in the correct place in the heap. bh_[i] = last; ix_[last.item] = i; } return true; } } // namespace Cevo