GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
graph_v2.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 <stdio.h>
12#include <string.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <unistd.h>
16#include <stdlib.h>
17#include <errno.h>
18
19#include "type.h"
20#include "tree.h"
21#include "graph.h"
22#include "graph_v2.h"
23#include "helpers.h"
24
25/* Template expansion
26 */
27#include "v2-defs.h"
28#include "sp-template.c"
29#include "nodemgmt-template.c"
30#include "edgemgmt-template.c"
31#include "misc-template.c"
32
33/* algorithms for TREE state
34 */
35#define DGL_DEFINE_TREE_PROCS 1
36#include "v2-defs.h"
37#include "sp-template.c"
38#include "span-template.c"
39#undef DGL_DEFINE_TREE_PROCS
40
41/* algorithms for FLAT state
42 */
43#define DGL_DEFINE_FLAT_PROCS 1
44#include "v2-defs.h"
45#include "sp-template.c"
46#include "span-template.c"
47#undef DGL_DEFINE_FLAT_PROCS
48
63
77
91
93{
94 if (pgraph->pNodeTree == NULL)
95 pgraph->pNodeTree =
97 if (pgraph->pNodeTree == NULL) {
99 return -pgraph->iErrno;
100 }
101 if (pgraph->pEdgeTree == NULL)
102 pgraph->pEdgeTree =
104 if (pgraph->pEdgeTree == NULL) {
106 return -pgraph->iErrno;
107 }
108 return 0;
109}
110
112{
113 pgraph->iErrno = 0;
114
115 if (pgraph->pNodeTree)
117 if (pgraph->pEdgeTree)
119 if (pgraph->pNodeBuffer)
120 free(pgraph->pNodeBuffer);
121 if (pgraph->pEdgeBuffer)
122 free(pgraph->pEdgeBuffer);
123 if (pgraph->edgePrioritizer.pvAVL)
124 avl_destroy(pgraph->edgePrioritizer.pvAVL, dglTreeEdgePri32Cancel);
125 if (pgraph->nodePrioritizer.pvAVL)
126 avl_destroy(pgraph->nodePrioritizer.pvAVL, dglTreeNodePri32Cancel);
127
128 return 0;
129}
130
132{
133 long nret, cnt, tot;
134
135 pgraph->iErrno = 0;
136
137 if (write(fd, &pgraph->Version, 1) != 1) {
138 pgraph->iErrno = DGL_ERR_Write;
139 return -pgraph->iErrno;
140 }
141
142 if (write(fd, &pgraph->Endian, 1) != 1) {
143 pgraph->iErrno = DGL_ERR_Write;
144 return -pgraph->iErrno;
145 }
146
147 if (write(fd, &pgraph->NodeAttrSize, sizeof(dglInt32_t)) !=
148 sizeof(dglInt32_t)) {
149 pgraph->iErrno = DGL_ERR_Write;
150 return -pgraph->iErrno;
151 }
152
153 if (write(fd, &pgraph->EdgeAttrSize, sizeof(dglInt32_t)) !=
154 sizeof(dglInt32_t)) {
155 pgraph->iErrno = DGL_ERR_Write;
156 return -pgraph->iErrno;
157 }
158
159 for (cnt = 0; cnt < 16; cnt++) {
160 if (write(fd, &pgraph->aOpaqueSet[cnt], sizeof(dglInt32_t)) !=
161 sizeof(dglInt32_t)) {
162 pgraph->iErrno = DGL_ERR_Write;
163 return -pgraph->iErrno;
164 }
165 }
166
167 if (write(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
168 pgraph->iErrno = DGL_ERR_Write;
169 return -pgraph->iErrno;
170 }
171
172 if (write(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
173 pgraph->iErrno = DGL_ERR_Write;
174 return -pgraph->iErrno;
175 }
176
177 if (write(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
178 pgraph->iErrno = DGL_ERR_Write;
179 return -pgraph->iErrno;
180 }
181
182 if (write(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
183 pgraph->iErrno = DGL_ERR_Write;
184 return -pgraph->iErrno;
185 }
186
187 if (write(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
188 pgraph->iErrno = DGL_ERR_Write;
189 return -pgraph->iErrno;
190 }
191
192 if (write(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
193 pgraph->iErrno = DGL_ERR_Write;
194 return -pgraph->iErrno;
195 }
196
197 if (write(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
198 sizeof(dglInt32_t)) {
199 pgraph->iErrno = DGL_ERR_Write;
200 return -pgraph->iErrno;
201 }
202
203 if (write(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
204 sizeof(dglInt32_t)) {
205 pgraph->iErrno = DGL_ERR_Write;
206 return -pgraph->iErrno;
207 }
208
209 for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
210 if ((nret = write(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
211 pgraph->iErrno = DGL_ERR_Write;
212 return -pgraph->iErrno;
213 }
214 }
215
216 for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
217 if ((nret = write(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
218 pgraph->iErrno = DGL_ERR_Write;
219 return -pgraph->iErrno;
220 }
221 }
222
223 return 0;
224}
225
226int dgl_read_V2(dglGraph_s *pgraph, int fd, int version)
227{
228 long nret, cnt, tot;
229 dglByte_t Endian;
230 dglInt32_t NodeAttrSize, EdgeAttrSize;
231 int i, cn, fSwap;
232 dglInt32_t *pn;
233
234 if (read(fd, &Endian, 1) != 1) {
235 pgraph->iErrno = DGL_ERR_Read;
236 return -pgraph->iErrno;
237 }
238
239 fSwap = 0;
240#ifdef DGL_ENDIAN_BIG
241 if (Endian == DGL_ENDIAN_LITTLE)
242 fSwap = 1;
243#else
244 if (Endian == DGL_ENDIAN_BIG)
245 fSwap = 1;
246#endif
247
248 if (read(fd, &NodeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
249 pgraph->iErrno = DGL_ERR_Read;
250 return -pgraph->iErrno;
251 }
252 if (fSwap)
253 dgl_swapInt32Bytes(&NodeAttrSize);
254
255 if (read(fd, &EdgeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
256 pgraph->iErrno = DGL_ERR_Read;
257 return -pgraph->iErrno;
258 }
259 if (fSwap)
260 dgl_swapInt32Bytes(&EdgeAttrSize);
261
262 if ((nret = dglInitialize(pgraph, version, NodeAttrSize, EdgeAttrSize,
263 NULL)) < 0) {
264 return nret;
265 }
266
267 for (cnt = 0; cnt < 16; cnt++) {
268 if ((nret = read(fd, &pgraph->aOpaqueSet[cnt], sizeof(dglInt32_t))) !=
269 sizeof(dglInt32_t)) {
270 pgraph->iErrno = DGL_ERR_Read;
271 return -pgraph->iErrno;
272 }
273 if (fSwap)
274 dgl_swapInt32Bytes(&pgraph->aOpaqueSet[cnt]);
275 }
276
277 if (read(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
278 pgraph->iErrno = DGL_ERR_Read;
279 return -pgraph->iErrno;
280 }
281 if (fSwap)
282 dgl_swapInt64Bytes(&pgraph->nnCost);
283
284 if (read(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
285 pgraph->iErrno = DGL_ERR_Read;
286 return -pgraph->iErrno;
287 }
288 if (fSwap)
289 dgl_swapInt32Bytes(&pgraph->cNode);
290
291 if (read(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
292 pgraph->iErrno = DGL_ERR_Read;
293 return -pgraph->iErrno;
294 }
295 if (fSwap)
296 dgl_swapInt32Bytes(&pgraph->cHead);
297
298 if (read(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
299 pgraph->iErrno = DGL_ERR_Read;
300 return -pgraph->iErrno;
301 }
302 if (fSwap)
303 dgl_swapInt32Bytes(&pgraph->cTail);
304
305 if (read(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
306 pgraph->iErrno = DGL_ERR_Read;
307 return -pgraph->iErrno;
308 }
309 if (fSwap)
310 dgl_swapInt32Bytes(&pgraph->cAlone);
311
312 if (read(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
313 pgraph->iErrno = DGL_ERR_Read;
314 return -pgraph->iErrno;
315 }
316 if (fSwap)
317 dgl_swapInt32Bytes(&pgraph->cEdge);
318
319 if (read(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
320 sizeof(dglInt32_t)) {
321 pgraph->iErrno = DGL_ERR_Read;
322 return -pgraph->iErrno;
323 }
324 if (fSwap)
325 dgl_swapInt32Bytes(&pgraph->iNodeBuffer);
326
327 if (read(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
328 sizeof(dglInt32_t)) {
329 pgraph->iErrno = DGL_ERR_Read;
330 return -pgraph->iErrno;
331 }
332 if (fSwap)
333 dgl_swapInt32Bytes(&pgraph->iEdgeBuffer);
334
335 if ((pgraph->pNodeBuffer = malloc(pgraph->iNodeBuffer)) == NULL) {
337 return -pgraph->iErrno;
338 }
339
340 if ((pgraph->pEdgeBuffer = malloc(pgraph->iEdgeBuffer)) == NULL) {
341 free(pgraph->pNodeBuffer);
343 return -pgraph->iErrno;
344 }
345
346 for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
347 if ((nret = read(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
348 free(pgraph->pNodeBuffer);
349 free(pgraph->pEdgeBuffer);
350 pgraph->iErrno = DGL_ERR_Read;
351 return -pgraph->iErrno;
352 }
353 }
354 if (fSwap) {
355 pn = (dglInt32_t *)pgraph->pNodeBuffer;
356 cn = pgraph->iNodeBuffer / sizeof(dglInt32_t);
357 for (i = 0; i < cn; i++) {
359 }
360 }
361
362 for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
363 if ((nret = read(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
364 free(pgraph->pNodeBuffer);
365 free(pgraph->pEdgeBuffer);
366 pgraph->iErrno = DGL_ERR_Read;
367 return -pgraph->iErrno;
368 }
369 }
370 if (fSwap) {
371 pn = (dglInt32_t *)pgraph->pEdgeBuffer;
372 cn = pgraph->iEdgeBuffer / sizeof(dglInt32_t);
373 for (i = 0; i < cn; i++) {
375 }
376 }
377
378 pgraph->Flags |= 0x1; /* flat-state */
379 return 0;
380}
#define NULL
Definition ccmath.h:32
int(* dglSPClip_fn)(dglGraph_s *, dglSPClipInput_s *, dglSPClipOutput_s *, void *)
Definition graph.h:167
#define DGL_ERR_Write
Definition graph.h:245
#define DGL_ENDIAN_LITTLE
Definition graph.h:60
#define DGL_ERR_MemoryExhausted
Definition graph.h:242
#define DGL_GS_FLAT
Definition graph.h:24
#define DGL_ERR_Read
Definition graph.h:246
#define DGL_ENDIAN_BIG
Definition graph.h:59
int(* dglSpanClip_fn)(dglGraph_s *, dglGraph_s *, dglSpanClipInput_s *, dglSpanClipOutput_s *, void *)
Definition graph.h:173
int dgl_dijkstra_V2(dglGraph_s *pgraph, dglSPReport_s **ppReport, dglInt32_t *pDistance, dglInt32_t nStart, dglInt32_t nDestination, dglSPClip_fn fnClip, void *pvClipArg, dglSPCache_s *pCache)
Definition graph_v2.c:49
int dgl_write_V2(dglGraph_s *pgraph, int fd)
Definition graph_v2.c:131
int dgl_release_V2(dglGraph_s *pgraph)
Definition graph_v2.c:111
int dgl_depthfirst_spanning_V2(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, void *pvVisited, dglSpanClip_fn fnClip, void *pvClipArg)
Definition graph_v2.c:64
int dgl_initialize_V2(dglGraph_s *pgraph)
Definition graph_v2.c:92
int dgl_read_V2(dglGraph_s *pgraph, int fd, int version)
Definition graph_v2.c:226
int dgl_minimum_spanning_V2(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, dglSpanClip_fn fnClip, void *pvClipArg)
Definition graph_v2.c:78
int dgl_span_minimum_spanning_V2_TREE(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, dglSpanClip_fn fnClip, void *pvClipArg)
int dgl_dijkstra_V2_FLAT(dglGraph_s *pgraph, dglSPReport_s **ppReport, dglInt32_t *pDistance, dglInt32_t nStart, dglInt32_t nDestination, dglSPClip_fn fnClip, void *pvClipArg, dglSPCache_s *pCache)
int dgl_dijkstra_V2_TREE(dglGraph_s *pgraph, dglSPReport_s **ppReport, dglInt32_t *pDistance, dglInt32_t nStart, dglInt32_t nDestination, dglSPClip_fn fnClip, void *pvClipArg, dglSPCache_s *pCache)
int dgl_span_minimum_spanning_V2_FLAT(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, dglSpanClip_fn fnClip, void *pvClipArg)
int dgl_span_depthfirst_spanning_V2_FLAT(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, void *pvVisited, dglSpanClip_fn fnClip, void *pvClipArg)
int dgl_span_depthfirst_spanning_V2_TREE(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, void *pvVisited, dglSpanClip_fn fnClip, void *pvClipArg)
void dgl_swapInt64Bytes(dglInt64_t *pn)
Definition helpers.c:55
void dgl_swapInt32Bytes(dglInt32_t *pn)
Definition helpers.c:42
void * malloc(unsigned)
void free(void *)
void dglTreeEdgePri32Cancel(void *pvEdgePri32, void *pvParam)
Definition tree.c:340
void dglTreeEdgeCancel(void *pvEdge, void *pvParam)
Definition tree.c:140
int dglTreeNode2Compare(const void *pvNode2A, const void *pvNode2B, void *pvParam)
Definition tree.c:96
void * dglTreeGetAllocator(void)
Definition tree.c:394
void dglTreeNodePri32Cancel(void *pvNodePri32, void *pvParam)
Definition tree.c:290
void dglTreeNodeCancel(void *pvNode, void *pvParam)
Definition tree.c:33
int dglTreeEdgeCompare(const void *pvEdgeA, const void *pvEdgeB, void *pvParam)
Definition tree.c:147
#define avl_create
Definition tree.h:19
#define avl_destroy
Definition tree.h:21
long long dglInt64_t
Definition type.h:25
unsigned char dglByte_t
Definition type.h:23
long dglInt32_t
Definition type.h:24
#define read
Definition unistd.h:5
#define write
Definition unistd.h:6
int dglInitialize(dglGraph_s *pGraph, dglByte_t Version, dglInt32_t NodeAttrSize, dglInt32_t EdgeAttrSize, dglInt32_t *pOpaqueSet)