GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
edgemgmt-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
13/*
14 * Add edge can be performed on TREE state graph. If the state is FLAT
15 * return BadOnFlatGraph error.
16 */
20{
27
28#if defined(_DGL_V2)
31 /* dglTreeEdge_s findEdge; */
32#endif
33
34 if (pgraph->Flags & DGL_GS_FLAT) {
36 return -pgraph->iErrno;
37 }
38
39#ifdef DGL_STATS
40 {
41 clock_t clk = clock();
42#endif
43
44 if ((pHeadNodeItem = DGL_T_NODEITEM_Add(pgraph->pNodeTree, nHead)) ==
45 NULL ||
47 NULL) {
49 return -pgraph->iErrno;
50 }
51
52#ifdef DGL_STATS
53 pgraph->clkNodeTree += clock() - clk;
54 pgraph->cNodeTree++;
55 pgraph->cNodeTree++;
56 }
57#endif
58
60 if ((pHead = DGL_NODE_ALLOC(pgraph->NodeAttrSize)) == NULL) {
62 return -1;
63 }
66 pgraph->cNode++;
67 pgraph->cHead++;
68 }
69 else {
72 pgraph->cHead++;
73 }
74
76 if ((pTail = DGL_NODE_ALLOC(pgraph->NodeAttrSize)) == NULL) {
78 return -pgraph->iErrno;
79 }
82 pgraph->cNode++;
83 pgraph->cTail++;
84 }
85 else {
88 pgraph->cTail++;
89 }
90
93
95 DGL_NODE_STATUS(pHead) &= ~DGL_NS_ALONE;
96 pgraph->cAlone--;
97 }
98
100 DGL_NODE_STATUS(pTail) &= ~DGL_NS_ALONE;
101 pgraph->cAlone--;
102 }
103
106
109
110 if (pvHeadAttr && pgraph->NodeAttrSize) {
112 }
113
114 if (pvTailAttr && pgraph->NodeAttrSize) {
116 }
117
118 /*
119 if ( DGL_T_NODEITEM_OutEdgesetPTR(pTailNodeItem) == NULL )
120 {
121 pEdgeset = DGL_EDGESET_ALLOC( 0 , pgraph->EdgeAttrSize );
122 if ( pEdgeset == NULL ) {
123 pgraph->iErrno = DGL_ERR_MemoryExhausted;
124 return -pgraph->iErrno;
125 }
126 DGL_EDGESET_EDGECOUNT(pEdgeset) = 0;
127 DGL_T_NODEITEM_Set_OutEdgesetPTR(pTailNodeItem,pEdgeset);
128 }
129 */
130
132 pEdgeset = DGL_EDGESET_ALLOC(1, pgraph->EdgeAttrSize);
133 if (pEdgeset == NULL) {
135 return -pgraph->iErrno;
136 }
139 }
140 else {
141 pEdgeset =
143 pgraph->EdgeAttrSize);
144
145 if (pEdgeset == NULL) {
147 return -pgraph->iErrno;
148 }
150 }
151
152#if defined(_DGL_V2)
153 /*
154 if ( DGL_T_NODEITEM_InEdgesetPTR(pHeadNodeItem) == NULL )
155 {
156 pinEdgeset = DGL_EDGESET_ALLOC( 0 , pgraph->EdgeAttrSize );
157 if ( pinEdgeset == NULL ) {
158 pgraph->iErrno = DGL_ERR_MemoryExhausted;
159 return -pgraph->iErrno;
160 }
161 DGL_EDGESET_EDGECOUNT(pinEdgeset) = 0;
162 DGL_T_NODEITEM_Set_InEdgesetPTR(pHeadNodeItem,pinEdgeset);
163 }
164 */
165
167 pinEdgeset = DGL_EDGESET_ALLOC(1, pgraph->EdgeAttrSize);
168 if (pinEdgeset == NULL) {
170 return -pgraph->iErrno;
171 }
174 }
175 else {
178 pgraph->EdgeAttrSize);
179
180 if (pinEdgeset == NULL) {
182 return -pgraph->iErrno;
183 }
185 }
186
187 /*
188 * Set the edge-tree
189 */
190 /* findEdge.nKey = nEdge; */
191
192 if ((pEdgeItem = dglTreeEdgeAdd(pgraph->pEdgeTree, nEdge)) == NULL) {
194 return -pgraph->iErrno;
195 }
196 if (pEdgeItem->pv) {
198 return -pgraph->iErrno;
199 }
200 if ((pEdgeItem->pv = DGL_EDGE_ALLOC(pgraph->EdgeAttrSize)) == NULL) {
202 return -pgraph->iErrno;
203 }
204
205 /*
206 * assign edge id
207 */
212
213 /*
214 printf( "add edge: node %ld(%ld,%ld) -> %ld(%ld,%ld)\n",
215 DGL_NODE_ID(pHead), DGL_EDGESET_EDGECOUNT(pEdgeset),0,
216 DGL_NODE_ID(pTail), 0,DGL_EDGESET_EDGECOUNT(pinEdgeset));
217 */
218
219 pEdge = pEdgeItem->pv;
220#endif
221
222#if defined(_DGL_V1)
224 pgraph->EdgeAttrSize);
226#endif
227
229 nHead; /* will be an offset after flattening */
231 nTail; /* will be an offset after flattening */
232 DGL_EDGE_COST(pEdge) = nCost;
234
235#if !defined(_DGL_V1)
238 else
240#endif
241
242 pgraph->cEdge++;
243 pgraph->nnCost += (dglInt64_t)nCost;
244
245 if (pvEdgeAttr && pgraph->EdgeAttrSize) {
247 }
248
249 /*
250 * If requested add a cost-weighted entry into the edge prioritizer
251 */
252#if !defined(_DGL_V1)
253 if (pgraph->nOptions & DGL_GO_EdgePrioritize_COST) {
255 DGL_EDGE_COST(pEdge)) < 0) {
256 return -pgraph->iErrno;
257 }
258 }
259#endif
260
265 }
266
267 return 0;
268}
269
271{
272#if defined(_DGL_V1)
274 return -pgraph->iErrno;
275#else
278
279 if (pgraph->Flags & DGL_GS_FLAT) {
281 return -pgraph->iErrno;
282 }
283
284 if (pgraph->pEdgeTree == NULL) {
286 return -pgraph->iErrno;
287 }
288
289 findEdgeItem.nKey = nEdge;
290 if ((pEdgeItem = avl_find(pgraph->pEdgeTree, &findEdgeItem)) == NULL) {
292 return -pgraph->iErrno;
293 }
294
295 pEdge = pEdgeItem->pv;
296
298 DGL_EDGE_ID(pEdge)) < 0) {
299 return -pgraph->iErrno;
300 }
301
303 DGL_EDGE_ID(pEdge)) < 0) {
304 return -pgraph->iErrno;
305 }
306
307 /* prioritizer sync
308 */
309 if (pgraph->nOptions & DGL_GO_EdgePrioritize_COST) {
311 DGL_EDGE_COST(pEdge)) < 0) {
312 return -pgraph->iErrno;
313 }
314 }
315 /*
316 */
317 pgraph->cEdge--;
319
320 avl_delete(pgraph->pEdgeTree, pEdgeItem);
322 return 0;
323#endif
324}
325
327{
328#if defined(_DGL_V1)
330 return NULL;
331#else
332 register dglInt32_t top; /* top of table */
333 register dglInt32_t pos; /* current position to compare */
334 register dglInt32_t bot; /* bottom of table */
335 register dglInt32_t *pref;
336 register int cwords; /* size of a edge in words of 32 bit */
337 register dglTreeEdge_s *ptreeEdge;
339 dglInt32_t id;
340
341 pgraph->iErrno = 0;
342 if (pgraph->Flags & DGL_GS_FLAT) {
343 cwords = DGL_EDGE_WSIZE(pgraph->EdgeAttrSize);
344 /*bot = pgraph->iEdgeBuffer / DGL_EDGE_SIZEOF(pgraph->EdgeAttrSize);
345 */
346 bot = pgraph->cEdge;
347 top = 0;
348 pos = 0;
349 pref = (dglInt32_t *)pgraph->pEdgeBuffer;
350
351 /* perform a binary search
352 */
353 while (top != bot) {
354 pos = top + (bot - top) / 2;
355 id = DGL_EDGE_ID(&pref[pos * cwords]);
356 if (id == nEdge) {
357 break;
358 }
359 else if (nEdge < id) {
360 bot = pos;
361 }
362 else if (nEdge > id) {
363 top = pos + 1;
364 }
365 }
366 if (top == bot) {
367 return NULL;
368 }
369 return &pref[pos * cwords];
370 }
371 else {
372 findEdge.nKey = nEdge;
373 ptreeEdge = avl_find(pgraph->pEdgeTree, &findEdge);
374 if (ptreeEdge && ptreeEdge->pv) {
375 return ptreeEdge->pv;
376 }
377 return NULL;
378 }
379#endif
380}
#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_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_EdgeAlreadyExist
Definition graph.h:261
#define DGL_ERR_NotSupported
Definition graph.h:247
#define DGL_GS_FLAT
Definition graph.h:24
#define DGL_STRONGCONNECT
Definition graph.h:66
#define DGL_ERR_UnexpectedNullPointer
Definition graph.h:256
#define DGL_ERR_EdgeNotFound
Definition graph.h:258
#define DGL_ES_DIRECTED
Definition graph.h:54
int dgl_edge_prioritizer_del(dglGraph_s *pG, dglInt32_t nId, dglInt32_t nPriId)
Definition helpers.c:79
int dgl_edge_prioritizer_add(dglGraph_s *pG, dglInt32_t nId, dglInt32_t nPriId)
Definition helpers.c:120
void dglTreeEdgeCancel(void *pvEdge, void *pvParam)
Definition tree.c:140
dglTreeEdge_s * dglTreeEdgeAdd(void *pavl, dglInt32_t nKey)
Definition tree.c:159
#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_EDGE_PTR
Definition v1-defs.h:137
#define DGL_T_NODEITEM_InEdgesetPTR(p)
Definition v1-defs.h:161
#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_EDGESET_ALLOC
Definition v1-defs.h:138
#define DGL_NODE_ID
Definition v1-defs.h:115
#define DGL_EDGE_WSIZE
Definition v1-defs.h:123
#define DGL_NODE_EDGESET_OFFSET
Definition v1-defs.h:117
#define DGL_NODE_ALLOC
Definition v1-defs.h:111
#define DGL_DEL_EDGE_FUNC
Definition v1-defs.h:86
#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_ALLOC
Definition v1-defs.h:121
#define DGL_EDGE_TAILNODE_OFFSET
Definition v1-defs.h:129
#define DGL_EDGESET_EDGECOUNT
Definition v1-defs.h:136
#define DGL_EDGE_ATTR_PTR
Definition v1-defs.h:127
#define DGL_T_NODEITEM_Set_OutEdgesetPTR(p, ptr)
Definition v1-defs.h:160
#define DGL_EDGE_STATUS(p)
Definition v1-defs.h:124
#define DGL_GET_EDGE_FUNC
Definition v1-defs.h:85
#define DGL_T_NODEITEM_NodePTR(p)
Definition v1-defs.h:157
#define DGL_NODE_ATTR_PTR
Definition v1-defs.h:116
#define DGL_EDGE_ID
Definition v1-defs.h:126
#define DGL_EDGE_HEADNODE_OFFSET
Definition v1-defs.h:128
#define DGL_ADD_EDGE_FUNC
Definition v1-defs.h:84
#define DGL_EDGESET_REALLOC
Definition v1-defs.h:139
#define DGL_EDGE_COST
Definition v1-defs.h:125
#define DGL_DEL_NODE_INEDGE_FUNC
Definition v2-defs.h:80
#define DGL_DEL_NODE_OUTEDGE_FUNC
Definition v2-defs.h:79