GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
heap.c
Go to the documentation of this file.
1/* LIBDGL -- a Directed Graph Library implementation
2 * SPDX-FileCopyrightText: 2002 Roberto Micarelli
3 * SPDX-FileCopyrightText: GRASS Development Team
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7/* best view tabstop=4
8 */
9#include <stdio.h>
10#include <stdlib.h>
11
12#include "type.h"
13#include "heap.h"
14
16{
17 pheap->index = 0;
18 pheap->count = 0;
19 pheap->block = 256;
20 pheap->pnode = NULL;
21}
22
24{
25 int iItem;
26
27 if (pheap->pnode) {
28 if (pfnCancelItem) {
29 for (iItem = 0; iItem <= pheap->index; iItem++) {
30 pfnCancelItem(pheap, &pheap->pnode[iItem]);
31 }
32 }
33 free(pheap->pnode);
34 }
35 pheap->pnode = NULL;
36}
37
38int dglHeapInsertMin(dglHeap_s *pheap, long key, unsigned char flags,
39 dglHeapData_u value)
40{
41 long i;
42
43 if (pheap->index >= pheap->count - 1) {
44 pheap->count += pheap->block;
45 if ((pheap->pnode = realloc(pheap->pnode, sizeof(dglHeapNode_s) *
46 pheap->count)) == NULL)
47 return -1;
48 }
49
50 i = ++pheap->index;
51
52 while (i != 1 && key < pheap->pnode[i / 2].key) {
53 pheap->pnode[i] = pheap->pnode[i / 2];
54 i /= 2;
55 }
56
57 pheap->pnode[i].key = key;
58 pheap->pnode[i].flags = flags;
59 pheap->pnode[i].value = value;
60
61 return i;
62}
63
65{
67 long iparent, ichild;
68
69 if (pheap->index == 0)
70 return 0; /* empty heap */
71
72 *pnoderet = pheap->pnode[1];
73
74 temp = pheap->pnode[pheap->index--];
75
76 iparent = 1;
77 ichild = 2;
78
79 while (ichild <= pheap->index) {
80 if (ichild < pheap->index &&
81 pheap->pnode[ichild].key > pheap->pnode[ichild + 1].key) {
82 ichild++;
83 }
84 if (temp.key <= pheap->pnode[ichild].key)
85 break;
86
87 pheap->pnode[iparent] = pheap->pnode[ichild];
89 ichild *= 2;
90 }
91 pheap->pnode[iparent] = temp;
92
93 return 1;
94}
95
96int dglHeapInsertMax(dglHeap_s *pheap, long key, unsigned char flags,
97 dglHeapData_u value)
98{
99 long i;
100
101 if (pheap->index >= pheap->count - 1) {
102 pheap->count += pheap->block;
103 if ((pheap->pnode = realloc(pheap->pnode, sizeof(dglHeapNode_s) *
104 pheap->count)) == NULL)
105 return -1;
106 }
107
108 i = ++pheap->index;
109
110 while (i != 1 && key > pheap->pnode[i / 2].key) {
111 pheap->pnode[i] = pheap->pnode[i / 2];
112 i /= 2;
113 }
114
115 pheap->pnode[i].key = key;
116 pheap->pnode[i].flags = flags;
117 pheap->pnode[i].value = value;
118
119 return i;
120}
121
123{
125 long iparent, ichild;
126
127 if (pheap->index == 0)
128 return 0; /* empty heap */
129
130 *pnoderet = pheap->pnode[1];
131
132 temp = pheap->pnode[pheap->index--];
133
134 iparent = 1;
135 ichild = 2;
136
137 while (ichild <= pheap->index) {
138 if (ichild < pheap->index &&
139 pheap->pnode[ichild].key < pheap->pnode[ichild + 1].key) {
140 ichild++;
141 }
142 if (temp.key >= pheap->pnode[ichild].key)
143 break;
144
145 pheap->pnode[iparent] = pheap->pnode[ichild];
146 iparent = ichild;
147 ichild *= 2;
148 }
149 pheap->pnode[iparent] = temp;
150
151 return 1;
152}
#define NULL
Definition ccmath.h:32
void dglHeapInit(dglHeap_s *pheap)
Definition heap.c:15
int dglHeapInsertMin(dglHeap_s *pheap, long key, unsigned char flags, dglHeapData_u value)
Definition heap.c:38
void dglHeapFree(dglHeap_s *pheap, dglHeapCancelItem_fn pfnCancelItem)
Definition heap.c:23
int dglHeapExtractMax(dglHeap_s *pheap, dglHeapNode_s *pnoderet)
Definition heap.c:122
int dglHeapInsertMax(dglHeap_s *pheap, long key, unsigned char flags, dglHeapData_u value)
Definition heap.c:96
int dglHeapExtractMin(dglHeap_s *pheap, dglHeapNode_s *pnoderet)
Definition heap.c:64
void(* dglHeapCancelItem_fn)(dglHeap_s *pheap, dglHeapNode_s *pitem)
Definition heap.h:41
void free(void *)