GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
graph_v1.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_v1.h"
23#include "helpers.h"
24
25/* Template expansion
26 */
27#include "v1-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 "v1-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 "v1-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 pgraph->pEdgeTree = NULL;
102 return 0;
103}
104
106{
107 pgraph->iErrno = 0;
108
109 if (pgraph->pNodeTree)
111 if (pgraph->pEdgeTree)
113 if (pgraph->pNodeBuffer)
114 free(pgraph->pNodeBuffer);
115 if (pgraph->pEdgeBuffer)
116 free(pgraph->pEdgeBuffer);
117 if (pgraph->edgePrioritizer.pvAVL)
118 avl_destroy(pgraph->edgePrioritizer.pvAVL, dglTreeEdgePri32Cancel);
119 if (pgraph->nodePrioritizer.pvAVL)
120 avl_destroy(pgraph->nodePrioritizer.pvAVL, dglTreeNodePri32Cancel);
121
122 return 0;
123}
124
126{
127 long nret, cnt, tot;
128
129 pgraph->iErrno = 0;
130
131 if (write(fd, &pgraph->Version, 1) != 1) {
132 pgraph->iErrno = DGL_ERR_Write;
133 return -pgraph->iErrno;
134 }
135
136 if (write(fd, &pgraph->Endian, 1) != 1) {
137 pgraph->iErrno = DGL_ERR_Write;
138 return -pgraph->iErrno;
139 }
140
141 if (write(fd, &pgraph->NodeAttrSize, sizeof(dglInt32_t)) !=
142 sizeof(dglInt32_t)) {
143 pgraph->iErrno = DGL_ERR_Write;
144 return -pgraph->iErrno;
145 }
146
147 if (write(fd, &pgraph->EdgeAttrSize, sizeof(dglInt32_t)) !=
148 sizeof(dglInt32_t)) {
149 pgraph->iErrno = DGL_ERR_Write;
150 return -pgraph->iErrno;
151 }
152
153 for (cnt = 0; cnt < 16; cnt++) {
154 if (write(fd, &pgraph->aOpaqueSet[cnt], sizeof(dglInt32_t)) !=
155 sizeof(dglInt32_t)) {
156 pgraph->iErrno = DGL_ERR_Write;
157 return -pgraph->iErrno;
158 }
159 }
160
161 if (write(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
162 pgraph->iErrno = DGL_ERR_Write;
163 return -pgraph->iErrno;
164 }
165
166 if (write(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
167 pgraph->iErrno = DGL_ERR_Write;
168 return -pgraph->iErrno;
169 }
170
171 if (write(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
172 pgraph->iErrno = DGL_ERR_Write;
173 return -pgraph->iErrno;
174 }
175
176 if (write(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
177 pgraph->iErrno = DGL_ERR_Write;
178 return -pgraph->iErrno;
179 }
180
181 if (write(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
182 pgraph->iErrno = DGL_ERR_Write;
183 return -pgraph->iErrno;
184 }
185
186 if (write(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
187 pgraph->iErrno = DGL_ERR_Write;
188 return -pgraph->iErrno;
189 }
190
191 if (write(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
192 sizeof(dglInt32_t)) {
193 pgraph->iErrno = DGL_ERR_Write;
194 return -pgraph->iErrno;
195 }
196
197 if (write(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
198 sizeof(dglInt32_t)) {
199 pgraph->iErrno = DGL_ERR_Write;
200 return -pgraph->iErrno;
201 }
202
203 for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
204 if ((nret = write(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
205 pgraph->iErrno = DGL_ERR_Write;
206 return -pgraph->iErrno;
207 }
208 }
209
210 for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
211 if ((nret = write(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
212 pgraph->iErrno = DGL_ERR_Write;
213 return -pgraph->iErrno;
214 }
215 }
216
217 return 0;
218}
219
221{
222 long nret, cnt, tot;
223 dglByte_t Endian;
224 dglInt32_t NodeAttrSize, EdgeAttrSize;
225 int i, cn, fSwap;
226 dglInt32_t *pn;
227
228 if (read(fd, &Endian, 1) != 1) {
229 pgraph->iErrno = DGL_ERR_Read;
230 return -pgraph->iErrno;
231 }
232
233 fSwap = 0;
234#ifdef DGL_ENDIAN_BIG
235 if (Endian == DGL_ENDIAN_LITTLE)
236 fSwap = 1;
237#else
238 if (Endian == DGL_ENDIAN_BIG)
239 fSwap = 1;
240#endif
241
242 if (read(fd, &NodeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
243 pgraph->iErrno = DGL_ERR_Read;
244 return -pgraph->iErrno;
245 }
246 if (fSwap)
247 dgl_swapInt32Bytes(&NodeAttrSize);
248
249 if (read(fd, &EdgeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
250 pgraph->iErrno = DGL_ERR_Read;
251 return -pgraph->iErrno;
252 }
253 if (fSwap)
254 dgl_swapInt32Bytes(&EdgeAttrSize);
255
256 if ((nret = dglInitialize(pgraph, 1, NodeAttrSize, EdgeAttrSize, NULL)) <
257 0) {
258 return nret;
259 }
260
261 for (cnt = 0; cnt < 16; cnt++) {
262 if ((nret = read(fd, &pgraph->aOpaqueSet[cnt], sizeof(dglInt32_t))) !=
263 sizeof(dglInt32_t)) {
264 pgraph->iErrno = DGL_ERR_Read;
265 return -pgraph->iErrno;
266 }
267 if (fSwap)
268 dgl_swapInt32Bytes(&pgraph->aOpaqueSet[cnt]);
269 }
270
271 if (read(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
272 pgraph->iErrno = DGL_ERR_Read;
273 return -pgraph->iErrno;
274 }
275 if (fSwap)
276 dgl_swapInt64Bytes(&pgraph->nnCost);
277
278 if (read(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
279 pgraph->iErrno = DGL_ERR_Read;
280 return -pgraph->iErrno;
281 }
282 if (fSwap)
283 dgl_swapInt32Bytes(&pgraph->cNode);
284
285 if (read(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
286 pgraph->iErrno = DGL_ERR_Read;
287 return -pgraph->iErrno;
288 }
289 if (fSwap)
290 dgl_swapInt32Bytes(&pgraph->cHead);
291
292 if (read(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
293 pgraph->iErrno = DGL_ERR_Read;
294 return -pgraph->iErrno;
295 }
296 if (fSwap)
297 dgl_swapInt32Bytes(&pgraph->cTail);
298
299 if (read(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
300 pgraph->iErrno = DGL_ERR_Read;
301 return -pgraph->iErrno;
302 }
303 if (fSwap)
304 dgl_swapInt32Bytes(&pgraph->cAlone);
305
306 if (read(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
307 pgraph->iErrno = DGL_ERR_Read;
308 return -pgraph->iErrno;
309 }
310 if (fSwap)
311 dgl_swapInt32Bytes(&pgraph->cEdge);
312
313 if (read(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
314 sizeof(dglInt32_t)) {
315 pgraph->iErrno = DGL_ERR_Read;
316 return -pgraph->iErrno;
317 }
318 if (fSwap)
319 dgl_swapInt32Bytes(&pgraph->iNodeBuffer);
320
321 if (read(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
322 sizeof(dglInt32_t)) {
323 pgraph->iErrno = DGL_ERR_Read;
324 return -pgraph->iErrno;
325 }
326 if (fSwap)
327 dgl_swapInt32Bytes(&pgraph->iEdgeBuffer);
328
329 if ((pgraph->pNodeBuffer = malloc(pgraph->iNodeBuffer)) == NULL) {
331 return -pgraph->iErrno;
332 }
333
334 if ((pgraph->pEdgeBuffer = malloc(pgraph->iEdgeBuffer)) == NULL) {
335 free(pgraph->pNodeBuffer);
337 return -pgraph->iErrno;
338 }
339
340 for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
341 if ((nret = read(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
342 free(pgraph->pNodeBuffer);
343 free(pgraph->pEdgeBuffer);
344 pgraph->iErrno = DGL_ERR_Read;
345 return -pgraph->iErrno;
346 }
347 }
348 if (fSwap) {
349 pn = (dglInt32_t *)pgraph->pNodeBuffer;
350 cn = pgraph->iNodeBuffer / sizeof(dglInt32_t);
351 for (i = 0; i < cn; i++) {
353 }
354 }
355
356 for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
357 if ((nret = read(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
358 free(pgraph->pNodeBuffer);
359 free(pgraph->pEdgeBuffer);
360 pgraph->iErrno = DGL_ERR_Read;
361 return -pgraph->iErrno;
362 }
363 }
364 if (fSwap) {
365 pn = (dglInt32_t *)pgraph->pEdgeBuffer;
366 cn = pgraph->iEdgeBuffer / sizeof(dglInt32_t);
367 for (i = 0; i < cn; i++) {
369 }
370 }
371
372 pgraph->Flags |= 0x1; /* flat-state */
373 return 0;
374}
#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_initialize_V1(dglGraph_s *pgraph)
Definition graph_v1.c:92
int dgl_write_V1(dglGraph_s *pgraph, int fd)
Definition graph_v1.c:125
int dgl_read_V1(dglGraph_s *pgraph, int fd)
Definition graph_v1.c:220
int dgl_depthfirst_spanning_V1(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, void *pvVisited, dglSpanClip_fn fnClip, void *pvClipArg)
Definition graph_v1.c:64
int dgl_dijkstra_V1(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_v1.c:49
int dgl_minimum_spanning_V1(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, dglSpanClip_fn fnClip, void *pvClipArg)
Definition graph_v1.c:78
int dgl_release_V1(dglGraph_s *pgraph)
Definition graph_v1.c:105
int dgl_dijkstra_V1_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_V1_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_depthfirst_spanning_V1_TREE(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, void *pvVisited, dglSpanClip_fn fnClip, void *pvClipArg)
int dgl_span_depthfirst_spanning_V1_FLAT(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, void *pvVisited, dglSpanClip_fn fnClip, void *pvClipArg)
int dgl_span_minimum_spanning_V1_FLAT(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, dglSpanClip_fn fnClip, void *pvClipArg)
int dgl_span_minimum_spanning_V1_TREE(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, 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 dglTreeNodeCompare(const void *pvNodeA, const void *pvNodeB, void *pvParam)
Definition tree.c:42
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
#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)