PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tester/MinHeap.cpp
Go to the documentation of this file.
1 /*PGR-MIT*****************************************************************
2 
3 * $Id$
4 *
5 * Project: pgRouting bdsp and bdastar algorithms
6 * Purpose:
7 * Author: Razequl Islam <ziboncsedu@gmail.com>
8 *
9 
10 ------
11 MIT/X license
12 
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19 
20 
21 The above copyright notice and this permission notice shall be included in
22 all copies or substantial portions of the Software.
23 
24 
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 THE SOFTWARE.
32 
33 ********************************************************************PGR-MIT*/
34 
35 #include "MinHeap.h"
36 
38 {
39  init(maxNode);
40 }
41 
43 {
44  reset();
45 }
46 
47 bool MinHeap::reset(void)
48 {
49  if(m_HeapTree != NULL)
50  {
51  delete [] m_HeapTree;
52  m_HeapTree = NULL;
53  }
54  if(m_Index != NULL)
55  {
56  delete [] m_Index;
57  m_Index = NULL;
58  }
59  return true;
60 }
61 
62 bool MinHeap::init(int maxNode)
63 {
64  m_HeapTree = new PDI[maxNode + 1];
65  m_Index = new int[maxNode + 1];
66  memset(m_Index, -1, sizeof(int) * (maxNode + 1));
68  m_CurrentSize = 0;
69  return true;
70 }
71 
72 void MinHeap::push(PDI node)
73 {
74  int nid = node.second;
75  double ncost = node.first;
76  if(m_Index[nid] != -1)
77  {
78  int pos = m_Index[nid];
79  if(ncost < m_HeapTree[pos].first)
80  {
81  m_HeapTree[pos].first = ncost;
82  shift_up(pos);
83  }
84  else
85  {
86  return;
87  }
88  }
89  else
90  {
91  m_CurrentSize++;
92  m_HeapTree[m_CurrentSize] = node;
93  m_Index[nid] = m_CurrentSize;
95  }
96 }
97 
98 PDI MinHeap::top(void)
99 {
100  return(m_HeapTree[1]);
101 }
102 
103 bool MinHeap::empty(void)
104 {
105  if(m_CurrentSize == 0)
106  return true;
107  return false;
108 }
109 
110 void MinHeap::pop(void)
111 {
112  if(m_CurrentSize == 0)
113  return;
114  int nid = m_HeapTree[1].second;
115  m_Index[nid] = -1;
117  m_Index[m_HeapTree[1].second] = 1;
118  m_CurrentSize--;
119  shift_down(1);
120 }
121 
122 void MinHeap::shift_up(int node)
123 {
124  while(node > 1)
125  {
126  int par_node = node / 2;
127  if(m_HeapTree[par_node].first <= m_HeapTree[node].first)
128  {
129  return;
130  }
131  PDI temp = m_HeapTree[par_node];
132  m_HeapTree[par_node] = m_HeapTree[node];
133  m_HeapTree[node] = temp;
134  m_Index[m_HeapTree[node].second] = node;
135  m_Index[m_HeapTree[par_node].second] = par_node;
136  node = par_node;
137  }
138  return;
139 }
140 
141 void MinHeap::shift_down(int node)
142 {
143  while(node < m_CurrentSize)
144  {
145  int left_child = node * 2;
146  int right_child = node * 2 + 1;
147  if(left_child > m_CurrentSize)
148  {
149  return;
150  }
151  int smallest = node;
152  if(m_HeapTree[left_child] < m_HeapTree[smallest])
153  {
154  smallest = left_child;
155  }
156  if(right_child <= m_CurrentSize)
157  {
158  if(m_HeapTree[right_child] < m_HeapTree[smallest])
159  {
160  smallest = right_child;
161  }
162  }
163  if(node == smallest)
164  return;
165  PDI temp = m_HeapTree[node];
166  m_HeapTree[node] = m_HeapTree[smallest];
167  m_HeapTree[smallest] = temp;
168  m_Index[m_HeapTree[node].second] = node;
169  m_Index[m_HeapTree[smallest].second] = smallest;
170  node = smallest;
171  }
172  return;
173 }
PDI top()
Definition: src/MinHeap.cpp:88
MinHeap(int maxNode)
Definition: src/MinHeap.cpp:40
PDI * m_HeapTree
Definition: src/MinHeap.h:60
void pop()
Definition: src/MinHeap.cpp:98
void shift_up(int node)
std::pair< double, int > PDI
int m_CurrentSize
Definition: src/MinHeap.h:63
bool reset(void)
Definition: src/MinHeap.cpp:48
bool init(int maxNode)
Definition: src/MinHeap.cpp:60
int * m_Index
Definition: src/MinHeap.h:61
int maxNode
Definition: BDATester.cpp:47
bool empty()
Definition: src/MinHeap.cpp:92
void push(PDI node)
Definition: src/MinHeap.cpp:69
~MinHeap(void)
Definition: src/MinHeap.cpp:44
int m_MaxNodeID
Definition: src/MinHeap.h:62
void shift_down(int node)