NAMD
heap.C
Go to the documentation of this file.
1 
7 class manheap;
8 class maxHeap;
9 #include <iostream>
10 using namespace std;
11 #include "heap.h"
12 // Heap of pointers. The field to be compared is:
13 
15 {
16  this->size = size;
17  h = new heapRecord[size];
18  count = 0;
19 }
20 
22  delete [] h;
23 }
24 
26 {
27  return count;
28 }
29 
31 {
32  h[count].info = x;
33  h[count].deleted = 0;
34 
35  int current = count;
36  count++;
37 
38  if (count >= size) {
39  std::cout << "minHeap overflow. \n" ;
40  return -1;}
41 
42  int parent = (current - 1)/2;
43  while (current != 0)
44  {
45  if (h[current].info->load < h[parent].info->load)
46  {
47  swap(current, parent);
48  current = parent;
49  parent = (current-1)/2;
50  }
51  else
52  break;
53  }
54  return 0;
55 }
56 
58 {
59  if (count == 0) return 0;
60 
61  InfoRecord *tmp = h[0].info;
62  int best;
63 
64  h[0] = h[count-1];
65  count--;
66 
67  int current = 0;
68  int c1 = 1;
69  int c2 = 2;
70  while (c1 < count)
71  {
72  if (c2 >= count)
73  best = c1;
74  else
75  {
76  if (h[c1].info->load < h[c2].info->load)
77  best = c1;
78  else
79  best = c2;
80  }
81  if (h[best].info->load < h[current].info->load)
82  {
83  swap(best, current);
84  current = best;
85  c1 = 2*current + 1;
86  c2 = c1 + 1;
87  }
88  else
89  break;
90  }
91  return tmp;
92 }
93 
95  iter->next = 1;
96  if (count == 0)
97  return 0;
98  return h[0].info;
99 }
101  if (iter->next >= count)
102  return 0;
103  iter->next += 1;
104  return h[iter->next - 1].info;
105 }
106 
107 //*****************
108 
109 
111 {
112  this->size = size;
113  h = new heapRecord[size];
114  count = 0;
115 }
116 
118  delete [] h;
119 }
120 
122 {
123  return count;
124 }
125 
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 }
151 
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 }
187 
188 
190  iter->next = 1;
191  if (count == 0)
192  return 0;
193  return h[0].info;
194 }
195 
197  if (iter->next >= count)
198  return 0;
199  iter->next += 1;
200  return h[iter->next - 1].info;
201 }
~minHeap()
Definition: heap.C:21
int insert(InfoRecord *)
Definition: heap.C:30
int numElements()
Definition: heap.C:121
maxHeap(int size)
Definition: heap.C:110
void swap(Array< T > &s, Array< T > &t)
Definition: MsmMap.h:319
InfoRecord * next(heapIterator *)
Definition: heap.C:196
InfoRecord * next(heapIterator *)
Definition: heap.C:100
Definition: heap.h:9
InfoRecord * deleteMax()
Definition: heap.C:152
Definition: heap.h:43
~maxHeap()
Definition: heap.C:117
int next
Definition: heap.h:17
int insert(InfoRecord *)
Definition: heap.C:126
InfoRecord * iterator(heapIterator *)
Definition: heap.C:94
int numElements()
Definition: heap.C:25
minHeap(int size)
Definition: heap.C:14
gridSize x
InfoRecord * deleteMin()
Definition: heap.C:57
InfoRecord * iterator(heapIterator *)
Definition: heap.C:189