NAMD
Public Member Functions | List of all members
maxHeap Class Reference

#include <heap.h>

Public Member Functions

 maxHeap (int size)
 
 ~maxHeap ()
 
int numElements ()
 
int insert (InfoRecord *)
 
InfoRecorddeleteMax ()
 
InfoRecorditerator (heapIterator *)
 
InfoRecordnext (heapIterator *)
 

Detailed Description

Definition at line 43 of file heap.h.

Constructor & Destructor Documentation

maxHeap::maxHeap ( int  size)

Definition at line 110 of file heap.C.

111 {
112  this->size = size;
113  h = new heapRecord[size];
114  count = 0;
115 }
Definition: heap.h:9
maxHeap::~maxHeap ( )

Definition at line 117 of file heap.C.

117  {
118  delete [] h;
119 }

Member Function Documentation

InfoRecord * maxHeap::deleteMax ( )

Definition at line 152 of file heap.C.

References msm::swap().

Referenced by RefineTorusLB::newRefine(), and Rebalancer::refine().

153 {
154  if (count == 0) return 0;
155  InfoRecord *tmp = h[0].info;
156  int best;
157 
158  h[0] = h[count-1];
159  count--;
160 
161  int current = 0;
162  int c1 = 1;
163  int c2 = 2;
164  while (c1 < count)
165  {
166  if (c2 >= count)
167  best = c1;
168  else
169  {
170  if (h[c1].info->load > h[c2].info->load)
171  best = c1;
172  else
173  best = c2;
174  }
175  if (h[best].info->load > h[current].info->load)
176  {
177  swap(best, current);
178  current = best;
179  c1 = 2*current + 1;
180  c2 = c1 + 1;
181  }
182  else
183  break;
184  }
185  return tmp;
186 }
InfoRecord * info
Definition: heap.h:12
int maxHeap::insert ( InfoRecord x)

Definition at line 126 of file heap.C.

References msm::swap(), and x.

Referenced by Rebalancer::makeHeaps(), Rebalancer::makeTwoHeaps(), RefineTorusLB::newRefine(), and Rebalancer::refine().

127 {
128  h[count].info = x;
129  h[count].deleted = 0;
130  int current = count;
131  count++;
132 
133  if (count >= size) {
134  std::cout << "maxHeap overflow. \n" ;
135  return -1;}
136 
137  int parent = (current - 1)/2;
138  while (current != 0)
139  {
140  if (h[current].info->load > h[parent].info->load)
141  {
142  swap(current, parent);
143  current = parent;
144  parent = (current-1)/2;
145  }
146  else
147  break;
148  }
149  return 0;
150 }
short deleted
Definition: heap.h:11
gridSize x
InfoRecord * info
Definition: heap.h:12
InfoRecord * maxHeap::iterator ( heapIterator iter)

Definition at line 189 of file heap.C.

References heapIterator::next.

189  {
190  iter->next = 1;
191  if (count == 0)
192  return 0;
193  return h[0].info;
194 }
int next
Definition: heap.h:17
InfoRecord * info
Definition: heap.h:12
InfoRecord * maxHeap::next ( heapIterator iter)

Definition at line 196 of file heap.C.

References heapIterator::next.

196  {
197  if (iter->next >= count)
198  return 0;
199  iter->next += 1;
200  return h[iter->next - 1].info;
201 }
int next
Definition: heap.h:17
InfoRecord * info
Definition: heap.h:12
int maxHeap::numElements ( )

Definition at line 121 of file heap.C.

122 {
123  return count;
124 }

The documentation for this class was generated from the following files: