GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
nodemgmt-template.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/*
8 * best view with tabstop=4
9 */
10
11#include <grass/gis.h>
12
15{
17 dglInt32_t *pnode;
18
19 if (pgraph->Flags & DGL_GS_FLAT) {
21 return -pgraph->iErrno;
22 }
23
24 if ((pNodeItem = DGL_T_NODEITEM_Add(pgraph->pNodeTree, nId)) == NULL) {
26 return -pgraph->iErrno;
27 }
28
30 if ((pnode = DGL_NODE_ALLOC(pgraph->NodeAttrSize)) == NULL) {
32 return -pgraph->iErrno;
33 }
34 memset(pnode, 0, DGL_NODE_SIZEOF(pgraph->NodeAttrSize));
35 DGL_NODE_ID(pnode) = nId;
38 pgraph->cNode++;
39 pgraph->cAlone++;
40 }
41 else {
42 /* node already exists */
44 return -pgraph->iErrno;
45 }
46 return 0;
47}
48
49#if !defined(_DGL_V1)
50/*
51 * Delete the link from the node's out-edgeset
52 */
55{
57 dglInt32_t *pnEdgeset, *pnEdge, *pnNode;
59
60 findNodeItem.nKey = nNode;
61
62 if ((pNodeItem = avl_find(pgraph->pNodeTree, &findNodeItem)) != NULL) {
64 if (DGL_NODE_STATUS(pnNode) == DGL_NS_ALONE) {
65 return 0;
66 }
67 if ((pnEdgeset = DGL_T_NODEITEM_OutEdgesetPTR(pNodeItem)) != NULL) {
68 if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraph, &t, pnEdgeset) >= 0) {
69 for (pnEdge = DGL_EDGESET_T_FIRST_FUNC(&t); pnEdge;
70 pnEdge = DGL_EDGESET_T_NEXT_FUNC(&t)) {
71 if (DGL_EDGE_ID(pnEdge) == nEdge) {
72 register dglInt32_t *pnSet;
73 register int i1, i2, c;
74
75 c = pnEdgeset[0];
76
77 if ((pnSet = malloc(sizeof(dglInt32_t) * (c + 1))) ==
78 NULL) {
80 return -pgraph->iErrno;
81 }
82
83 for (i1 = 0, i2 = 0; i2 < c; i2++) {
84 if (pnEdgeset[1 + i2] != nEdge) {
85 pnSet[1 + i1++] = pnEdgeset[1 + i2];
86 }
87 }
88 pnSet[0] = i1;
89
90 free(pnEdgeset);
92 break;
93 }
94 }
95 }
96 }
97 { /* check alone status */
99
103 if ((pOut == NULL || DGL_EDGESET_EDGECOUNT(pOut) == 0) &&
104 (pIn == NULL || DGL_EDGESET_EDGECOUNT(pIn) == 0)) {
106 pgraph->cHead--;
108 pgraph->cTail--;
110 pgraph->cAlone++;
111 }
112 }
113 }
114 return 0;
115}
116
119{
121 dglInt32_t *pnEdgeset, *pnEdge, *pnNode;
123
124 findNodeItem.nKey = nNode;
125
126 if ((pNodeItem = avl_find(pgraph->pNodeTree, &findNodeItem)) != NULL) {
128 if (DGL_NODE_STATUS(pnNode) == DGL_NS_ALONE) {
129 return 0;
130 }
131 if ((pnEdgeset = DGL_T_NODEITEM_InEdgesetPTR(pNodeItem)) != NULL) {
132 if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraph, &t, pnEdgeset) >= 0) {
133 for (pnEdge = DGL_EDGESET_T_FIRST_FUNC(&t); pnEdge;
134 pnEdge = DGL_EDGESET_T_NEXT_FUNC(&t)) {
135 if (DGL_EDGE_ID(pnEdge) == nEdge) {
136 register dglInt32_t *pnSet;
137 register int i1, i2, c;
138
139 c = pnEdgeset[0];
140
141 if ((pnSet = malloc(sizeof(dglInt32_t) * (c + 1))) ==
142 NULL) {
144 return -pgraph->iErrno;
145 }
146
147 for (i1 = 0, i2 = 0; i2 < c; i2++) {
148 if (pnEdgeset[1 + i2] != nEdge) {
149 pnSet[1 + i1++] = pnEdgeset[1 + i2];
150 }
151 }
152 pnSet[0] = i1;
153
154 free(pnEdgeset);
156 break;
157 }
158 }
159 }
160 }
161 { /* check alone status */
163
167 if ((pOut == NULL || DGL_EDGESET_EDGECOUNT(pOut) == 0) &&
168 (pIn == NULL || DGL_EDGESET_EDGECOUNT(pIn) == 0)) {
170 pgraph->cHead--;
172 pgraph->cTail--;
174 pgraph->cAlone++;
175 }
176 }
177 }
178 return 0;
179}
180#endif
181
183{
184#if defined(_DGL_V1)
186 return -pgraph->iErrno;
187#else
190 dglInt32_t *pnode;
193
195
196 if (pgraph->Flags & DGL_GS_FLAT) {
198 return -pgraph->iErrno;
199 }
200
201 if (pgraph->pNodeTree == NULL) {
203 return -pgraph->iErrno;
204 }
205
206 findNodeItem.nKey = nNodeId;
207 if ((pNodeItem = avl_find(pgraph->pNodeTree, &findNodeItem)) == NULL) {
209 return -pgraph->iErrno;
210 }
211
213
214 if (DGL_NODE_STATUS(pnode) & DGL_NS_ALONE)
215 goto node_is_alone;
216
218
220 return -pgraph->iErrno;
226 DGL_EDGE_ID(pEdge)) < 0) {
227 return -pgraph->iErrno;
228 }
229 }
230 if ((pEdgeItem = trav.pvCurrentItem) != NULL) {
231 /* prioritizer sync
232 */
233 if (pgraph->nOptions & DGL_GO_EdgePrioritize_COST) {
235 DGL_EDGE_COST(pEdge)) < 0) {
236 return -pgraph->iErrno;
237 }
238 }
239 /*
240 */
241 pgraph->cEdge--;
243
244 avl_delete(pgraph->pEdgeTree, pEdgeItem);
246 }
247 }
249
251
253 return -pgraph->iErrno;
259 DGL_EDGE_ID(pEdge)) < 0) {
260 return -pgraph->iErrno;
261 }
262 }
263 if ((pEdgeItem = trav.pvCurrentItem) != NULL) {
264 /* prioritizer sync
265 */
266 if (pgraph->nOptions & DGL_GO_EdgePrioritize_COST) {
268 DGL_EDGE_COST(pEdge)) < 0) {
269 return -pgraph->iErrno;
270 }
271 }
272 /*
273 */
274 pgraph->cEdge--;
276
277 avl_delete(pgraph->pEdgeTree, pEdgeItem);
279 }
280 }
282
283 if (DGL_NODE_STATUS(pnode) & DGL_NS_HEAD)
284 pgraph->cHead--;
285 if (DGL_NODE_STATUS(pnode) & DGL_NS_TAIL)
286 pgraph->cTail--;
287
289 if (DGL_NODE_STATUS(pnode) & DGL_NS_ALONE)
290 pgraph->cAlone--;
291 pgraph->cNode--;
292
293 avl_delete(pgraph->pNodeTree, pNodeItem);
295
296 return 0;
297#endif
298}
299
301{
302 register dglInt32_t top; /* top of table */
303 register dglInt32_t pos; /* current position to compare */
304 register dglInt32_t bot; /* bottom of table */
305 register dglInt32_t *pref;
306 register int cwords; /* size of a node in words of 32 bit */
307 register dglTreeNode_s *ptreenode;
309 dglInt32_t id;
310
311 pgraph->iErrno = 0;
312 if (pgraph->Flags & DGL_GS_FLAT) {
313 cwords = DGL_NODE_WSIZE(pgraph->NodeAttrSize);
314 /*bot = pgraph->iNodeBuffer / DGL_NODE_SIZEOF(pgraph->NodeAttrSize);
315 */
316 bot = pgraph->cNode;
317 top = 0;
318 pos = 0;
319 pref = (dglInt32_t *)pgraph->pNodeBuffer;
320
321 /* perform a binary search
322 */
323 while (top != bot) {
324 pos = top + (bot - top) / 2;
325 id = DGL_NODE_ID(&pref[pos * cwords]);
326 if (id == nodeid) {
327 break;
328 }
329 else if (nodeid < id) {
330 bot = pos;
331 }
332 else if (nodeid > id) {
333 top = pos + 1;
334 }
335 }
336 if (top == bot) {
337 return NULL;
338 }
339 return &pref[pos * cwords];
340 }
341 else {
342 findnode.nKey = nodeid;
343 ptreenode = avl_find(pgraph->pNodeTree, &findnode);
344 if (ptreenode && ptreenode->pv) {
345 return ptreenode->pv;
346 }
347 return NULL;
348 }
349}
350
351/*
352 * if graph is FLAT retrieve the edge area from the pEdgeBuffer
353 * if graph is TREE retrieve the node from the pNodeTree avl and return pv field
354 */
356{
359
360 pgraph->iErrno = 0;
361
362 if (pnode == NULL) {
364 return NULL;
365 }
366
367 if (DGL_NODE_STATUS(pnode) & DGL_NS_ALONE) {
369 return NULL;
370 }
371
372 if (pgraph->Flags & DGL_GS_FLAT) {
374 return pEdgeset;
375 }
376 else {
377 findnode.nKey = DGL_NODE_ID(pnode);
378 ptreenode = avl_find(pgraph->pNodeTree, &findnode);
381 }
382 return NULL;
383 }
384}
385
387 dglInt32_t *pnode G_UNUSED)
388{
389#if defined(_DGL_V1)
391 return NULL;
392#endif
393
394#if defined(_DGL_V2)
397
398 pgraph->iErrno = 0;
399
400 if (pnode == NULL) {
402 return NULL;
403 }
404
405 if (DGL_NODE_STATUS(pnode) & DGL_NS_ALONE) {
407 return NULL;
408 }
409
410 if (pgraph->Flags & DGL_GS_FLAT) {
413 pgraph->EdgeAttrSize);
414 return pEdgeset;
415 }
416 else {
417 findnode.nKey = DGL_NODE_ID(pnode);
418 ptreenode = avl_find(pgraph->pNodeTree, &findnode);
421 }
422 return NULL;
423 }
424#endif
425}
#define NULL
Definition ccmath.h:32
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
#define DGL_NS_TAIL
Definition graph.h:48
#define DGL_ERR_NodeIsAComponent
Definition graph.h:260
#define DGL_NS_ALONE
Definition graph.h:49
#define DGL_GO_EdgePrioritize_COST
Definition graph.h:40
#define DGL_ERR_MemoryExhausted
Definition graph.h:242
#define DGL_ERR_BadOnFlatGraph
Definition graph.h:252
#define DGL_NS_HEAD
Definition graph.h:47
#define DGL_ERR_NotSupported
Definition graph.h:247
#define DGL_GS_FLAT
Definition graph.h:24
#define DGL_ERR_UnexpectedNullPointer
Definition graph.h:256
#define DGL_ERR_NodeAlreadyExist
Definition graph.h:259
#define DGL_ERR_NodeNotFound
Definition graph.h:254
int dgl_edge_prioritizer_del(dglGraph_s *pG, dglInt32_t nId, dglInt32_t nPriId)
Definition helpers.c:79
double t
Definition r_raster.c:37
void * malloc(unsigned)
void free(void *)
void dglTreeEdgeCancel(void *pvEdge, void *pvParam)
Definition tree.c:140
#define avl_find
Definition tree.h:26
#define avl_delete
Definition tree.h:25
long long dglInt64_t
Definition type.h:25
long dglInt32_t
Definition type.h:24
#define DGL_EDGESET_T_FIRST_FUNC
Definition v1-defs.h:100
#define DGL_T_NODEITEM_InEdgesetPTR(p)
Definition v1-defs.h:161
#define DGL_DEL_NODE_FUNC
Definition v1-defs.h:79
#define DGL_T_NODEITEM_TYPE
Definition v1-defs.h:156
#define DGL_NODE_STATUS
Definition v1-defs.h:114
#define DGL_T_NODEITEM_Add
Definition v1-defs.h:165
#define DGL_ADD_NODE_FUNC
Definition v1-defs.h:78
#define DGL_NODE_ID
Definition v1-defs.h:115
#define DGL_NODE_EDGESET_OFFSET
Definition v1-defs.h:117
#define DGL_NODE_ALLOC
Definition v1-defs.h:111
#define DGL_T_NODEITEM_Set_InEdgesetPTR(p, ptr)
Definition v1-defs.h:162
#define DGL_T_NODEITEM_OutEdgesetPTR(p)
Definition v1-defs.h:159
#define DGL_T_NODEITEM_Set_NodePTR(p, ptr)
Definition v1-defs.h:158
#define DGL_EDGE_TAILNODE_OFFSET
Definition v1-defs.h:129
#define DGL_GET_NODE_FUNC
Definition v1-defs.h:80
#define DGL_EDGEBUFFER_SHIFT
Definition v1-defs.h:147
#define DGL_NODE_WSIZE
Definition v1-defs.h:113
#define DGL_EDGESET_EDGECOUNT
Definition v1-defs.h:136
#define DGL_T_NODEITEM_Set_OutEdgesetPTR(p, ptr)
Definition v1-defs.h:160
#define DGL_EDGESET_T_RELEASE_FUNC
Definition v1-defs.h:99
#define DGL_EDGESET_T_INITIALIZE_FUNC
Definition v1-defs.h:98
#define DGL_NODE_SIZEOF
Definition v1-defs.h:112
#define DGL_T_NODEITEM_Cancel
Definition v1-defs.h:164
#define DGL_T_NODEITEM_NodePTR(p)
Definition v1-defs.h:157
#define DGL_EDGE_ID
Definition v1-defs.h:126
#define DGL_EDGE_HEADNODE_OFFSET
Definition v1-defs.h:128
#define DGL_EDGESET_T_NEXT_FUNC
Definition v1-defs.h:101
#define DGL_EDGESET_WSIZE
Definition v1-defs.h:141
#define DGL_EDGE_COST
Definition v1-defs.h:125
#define DGL_GET_NODE_OUTEDGESET_FUNC
Definition v1-defs.h:81
#define DGL_DEL_NODE_INEDGE_FUNC
Definition v2-defs.h:80
#define DGL_DEL_NODE_OUTEDGE_FUNC
Definition v2-defs.h:79
#define DGL_GET_NODE_INEDGESET_FUNC
Definition v2-defs.h:84