Macaulay2 Engine
Loading...
Searching...
No Matches

◆ remove()

s_pair * s_pair_heap::remove ( )

Definition at line 225 of file spair.cpp.

226{
227 // Find a non-zero element
228 if (nelems == 0) return nullptr;
229 int i, first;
230 for (first = 0; first <= top_of_heap; first++)
231 if (n_in_heap[first] > 0) break;
232
233 s_pair *smallest = heap[first];
234 // Now find the smallest one
235 for (i = first + 1; i <= top_of_heap; i++)
236 {
237 if (heap[i] == nullptr) continue;
238 int cmp = compare(smallest, heap[i]);
239 if (cmp > 0)
240 {
241 first = i;
242 smallest = heap[first];
243 }
244 }
245
246 // Now remove this element and return it:
247 heap[first] = smallest->next;
248 smallest->next = nullptr;
249 nelems--;
250
251 n_in_heap[first]--;
252 if (n_in_heap[top_of_heap] == 0)
253 for (i = top_of_heap - 1; i >= 0; i--)
254 if (n_in_heap[i] > 0)
255 {
256 top_of_heap = i;
257 break;
258 }
259
260 return smallest;
261}
s_pair * heap[NHEAP]
Definition spair.hpp:108
int n_in_heap[NHEAP]
Definition spair.hpp:109
int compare(s_pair *f, s_pair *g) const
Definition spair.cpp:66
int top_of_heap
Definition spair.hpp:110
s_pair * next
Definition spair.hpp:90

References compare(), heap, n_in_heap, nelems, s_pair::next, and top_of_heap.