de65914f43
- modifiche per integrare vroni con le nostre librerie - spostamento header in Extern per evitare duplicati.
240 lines
5.5 KiB
C++
240 lines
5.5 KiB
C++
/*****************************************************************************/
|
|
/* */
|
|
/* Copyright (C) 2003--2023 M. Held */
|
|
/* */
|
|
/* This code is not in the public domain. All rights reserved! Please make */
|
|
/* sure to read the full copyright statement contained in "README" or in the */
|
|
/* "main" file of this code, such as "main.cc". */
|
|
/* */
|
|
/*****************************************************************************/
|
|
/* */
|
|
/* Written by: Martin Held */
|
|
/* */
|
|
/* E-Mail: held@cs.sbg.ac.at */
|
|
/* Fax Mail: (+4 662) 8044-611 */
|
|
/* Voice Mail: (+4 662) 8044-6304 */
|
|
/* Snail Mail: Martin Held */
|
|
/* FB Informatik */
|
|
/* Universitaet Salzburg */
|
|
/* A-5020 Salzburg, Austria */
|
|
/* */
|
|
/*****************************************************************************/
|
|
/* */
|
|
/* this file contains the subroutines and functions used for manipulating */
|
|
/* a heap-based priority queue. */
|
|
/* */
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
/* get my header files */
|
|
/* */
|
|
#include "vroni_object.h"
|
|
|
|
|
|
void vroniObject::StoreHeapData(int idx, double s_key, int s_ref)
|
|
{
|
|
assert(InHeapInsert(idx));
|
|
heap[idx].key = s_key;
|
|
heap[idx].ref = s_ref;
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
void vroniObject::GetHeapData(int idx, double *s_key, int *s_ref)
|
|
{
|
|
assert(InHeap(idx));
|
|
*s_key = heap[idx].key;
|
|
*s_ref = heap[idx].ref;
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
vr_bool vroniObject::InHeapInsert(int idx)
|
|
{
|
|
return ((idx >= 0) && (idx < max_num_heap));
|
|
}
|
|
|
|
|
|
vr_bool vroniObject::InHeap(int idx)
|
|
{
|
|
return ((idx >= 0) && (idx < num_heap) && (num_heap < max_num_heap));
|
|
}
|
|
|
|
|
|
|
|
void vroniObject::FreeHeap(void)
|
|
{
|
|
heap.clear();
|
|
|
|
max_num_heap = 0;
|
|
num_heap = 0;
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
void vroniObject::DumpOnHeap(double_arg key, int ref)
|
|
{
|
|
if (num_heap >= max_num_heap) {
|
|
max_num_heap += HEAP_BLOCK_SIZE;
|
|
gentlyResizeSTLVector(heap, max_num_heap, "heap:heap");
|
|
}
|
|
|
|
StoreHeapData(num_heap, key, ref);
|
|
++num_heap;
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
|
|
void vroniObject::UpdateHeap(double_arg key, int ref)
|
|
{
|
|
int i, j, j1, j2;
|
|
|
|
i = 0;
|
|
j1 = 2 * i + 1;
|
|
j2 = j1 + 1;
|
|
|
|
while (j2 < num_heap) {
|
|
if (heap[j1].key < heap[j2].key) j = j1;
|
|
else j = j2;
|
|
if (heap[j].key < key) {
|
|
heap[i] = heap[j];
|
|
i = j;
|
|
j1 = 2 * i + 1;
|
|
j2 = j1 + 1;
|
|
}
|
|
else {
|
|
j1 = j2 = num_heap;
|
|
}
|
|
}
|
|
|
|
if (j1 < num_heap) {
|
|
if (heap[j1].key < key) {
|
|
heap[i] = heap[j1];
|
|
StoreHeapData(j1, key, ref);
|
|
}
|
|
else {
|
|
StoreHeapData(i, key, ref);
|
|
}
|
|
}
|
|
else {
|
|
StoreHeapData(i, key, ref);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
|
|
void vroniObject::ExtendHeap(double_arg key, int ref)
|
|
{
|
|
int i, j;
|
|
|
|
j = num_heap;
|
|
++num_heap;
|
|
|
|
if (num_heap >= max_num_heap) {
|
|
max_num_heap += HEAP_BLOCK_SIZE;
|
|
gentlyResizeSTLVector(heap, max_num_heap, "heap:heap");
|
|
}
|
|
|
|
while (j > 0) {
|
|
i = (j - 1) / 2;
|
|
if (heap[i].key > key) {
|
|
heap[j] = heap[i];
|
|
j = i;
|
|
}
|
|
else {
|
|
StoreHeapData(j, key, ref);
|
|
return;
|
|
}
|
|
}
|
|
|
|
StoreHeapData(0, key, ref);
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
void vroniObject::InsertIntoHeap(double_arg key, int ref)
|
|
{
|
|
if (deleted) {
|
|
UpdateHeap(key, ref);
|
|
}
|
|
else {
|
|
ExtendHeap(key, ref);
|
|
}
|
|
deleted = false;
|
|
not_updated = false;
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
|
|
vr_bool vroniObject::DeleteFromHeap(double *key, int *ref)
|
|
{
|
|
if (deleted && not_updated) {
|
|
if (num_heap <= 1) {
|
|
num_heap = 0;
|
|
return false;
|
|
}
|
|
--num_heap;
|
|
UpdateHeap(heap[num_heap].key, heap[num_heap].ref);
|
|
not_updated = false;
|
|
deleted = false;
|
|
}
|
|
else if (num_heap <= 0) {
|
|
return false;
|
|
}
|
|
|
|
GetHeapData(0, key, ref);
|
|
|
|
not_updated = true;
|
|
deleted = true;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
void vroniObject::InitHeap(void)
|
|
{
|
|
if(heap.size() == 0) {
|
|
max_num_heap = 0;
|
|
}
|
|
if (num_pnts > max_num_heap) {
|
|
max_num_heap = num_pnts;
|
|
gentlyResizeSTLVector(heap, max_num_heap, "heap:heap");
|
|
}
|
|
num_heap = 0;
|
|
deleted = false;
|
|
not_updated = false;
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
void vroniObject::ResetHeap(void)
|
|
{
|
|
num_heap = 0;
|
|
deleted = false;
|
|
not_updated = false;
|
|
|
|
return;
|
|
}
|
|
|