GRASS 8 Programmer's Manual 8.6.0dev(2026)-56a9afeb9f
Loading...
Searching...
No Matches
graph_v1.c
Go to the documentation of this file.
1/* LIBDGL -- a Directed Graph Library implementation
2 * Copyright (C) 2002 Roberto Micarelli
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19/*
20 * best view with tabstop=4
21 */
22
23#include <stdio.h>
24#include <string.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <errno.h>
30
31#include "type.h"
32#include "tree.h"
33#include "graph.h"
34#include "graph_v1.h"
35#include "helpers.h"
36
37/* Template expansion
38 */
39#include "v1-defs.h"
40#include "sp-template.c"
41#include "nodemgmt-template.c"
42#include "edgemgmt-template.c"
43#include "misc-template.c"
44
45/* algorithms for TREE state
46 */
47#define DGL_DEFINE_TREE_PROCS 1
48#include "v1-defs.h"
49#include "sp-template.c"
50#include "span-template.c"
51#undef DGL_DEFINE_TREE_PROCS
52
53/* algorithms for FLAT state
54 */
55#define DGL_DEFINE_FLAT_PROCS 1
56#include "v1-defs.h"
57#include "sp-template.c"
58#include "span-template.c"
59#undef DGL_DEFINE_FLAT_PROCS
60
75
89
103
105{
106 if (pgraph->pNodeTree == NULL)
107 pgraph->pNodeTree =
109 if (pgraph->pNodeTree == NULL) {
111 return -pgraph->iErrno;
112 }
113 pgraph->pEdgeTree = NULL;
114 return 0;
115}
116
118{
119 pgraph->iErrno = 0;
120
121 if (pgraph->pNodeTree)
123 if (pgraph->pEdgeTree)
125 if (pgraph->pNodeBuffer)
126 free(pgraph->pNodeBuffer);
127 if (pgraph->pEdgeBuffer)
128 free(pgraph->pEdgeBuffer);
129 if (pgraph->edgePrioritizer.pvAVL)
130 avl_destroy(pgraph->edgePrioritizer.pvAVL, dglTreeEdgePri32Cancel);
131 if (pgraph->nodePrioritizer.pvAVL)
132 avl_destroy(pgraph->nodePrioritizer.pvAVL, dglTreeNodePri32Cancel);
133
134 return 0;
135}
136
138{
139 long nret, cnt, tot;
140
141 pgraph->iErrno = 0;
142
143 if (write(fd, &pgraph->Version, 1) != 1) {
144 pgraph->iErrno = DGL_ERR_Write;
145 return -pgraph->iErrno;
146 }
147
148 if (write(fd, &pgraph->Endian, 1) != 1) {
149 pgraph->iErrno = DGL_ERR_Write;
150 return -pgraph->iErrno;
151 }
152
153 if (write(fd, &pgraph->NodeAttrSize, sizeof(dglInt32_t)) !=
154 sizeof(dglInt32_t)) {
155 pgraph->iErrno = DGL_ERR_Write;
156 return -pgraph->iErrno;
157 }
158
159 if (write(fd, &pgraph->EdgeAttrSize, sizeof(dglInt32_t)) !=
160 sizeof(dglInt32_t)) {
161 pgraph->iErrno = DGL_ERR_Write;
162 return -pgraph->iErrno;
163 }
164
165 for (cnt = 0; cnt < 16; cnt++) {
166 if (write(fd, &pgraph->aOpaqueSet[cnt], sizeof(dglInt32_t)) !=
167 sizeof(dglInt32_t)) {
168 pgraph->iErrno = DGL_ERR_Write;
169 return -pgraph->iErrno;
170 }
171 }
172
173 if (write(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
174 pgraph->iErrno = DGL_ERR_Write;
175 return -pgraph->iErrno;
176 }
177
178 if (write(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
179 pgraph->iErrno = DGL_ERR_Write;
180 return -pgraph->iErrno;
181 }
182
183 if (write(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
184 pgraph->iErrno = DGL_ERR_Write;
185 return -pgraph->iErrno;
186 }
187
188 if (write(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
189 pgraph->iErrno = DGL_ERR_Write;
190 return -pgraph->iErrno;
191 }
192
193 if (write(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
194 pgraph->iErrno = DGL_ERR_Write;
195 return -pgraph->iErrno;
196 }
197
198 if (write(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
199 pgraph->iErrno = DGL_ERR_Write;
200 return -pgraph->iErrno;
201 }
202
203 if (write(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
204 sizeof(dglInt32_t)) {
205 pgraph->iErrno = DGL_ERR_Write;
206 return -pgraph->iErrno;
207 }
208
209 if (write(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
210 sizeof(dglInt32_t)) {
211 pgraph->iErrno = DGL_ERR_Write;
212 return -pgraph->iErrno;
213 }
214
215 for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
216 if ((nret = write(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
217 pgraph->iErrno = DGL_ERR_Write;
218 return -pgraph->iErrno;
219 }
220 }
221
222 for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
223 if ((nret = write(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
224 pgraph->iErrno = DGL_ERR_Write;
225 return -pgraph->iErrno;
226 }
227 }
228
229 return 0;
230}
231
233{
234 long nret, cnt, tot;
235 dglByte_t Endian;
236 dglInt32_t NodeAttrSize, EdgeAttrSize;
237 int i, cn, fSwap;
238 dglInt32_t *pn;
239
240 if (read(fd, &Endian, 1) != 1) {
241 pgraph->iErrno = DGL_ERR_Read;
242 return -pgraph->iErrno;
243 }
244
245 fSwap = 0;
246#ifdef DGL_ENDIAN_BIG
247 if (Endian == DGL_ENDIAN_LITTLE)
248 fSwap = 1;
249#else
250 if (Endian == DGL_ENDIAN_BIG)
251 fSwap = 1;
252#endif
253
254 if (read(fd, &NodeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
255 pgraph->iErrno = DGL_ERR_Read;
256 return -pgraph->iErrno;
257 }
258 if (fSwap)
259 dgl_swapInt32Bytes(&NodeAttrSize);
260
261 if (read(fd, &EdgeAttrSize, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
262 pgraph->iErrno = DGL_ERR_Read;
263 return -pgraph->iErrno;
264 }
265 if (fSwap)
266 dgl_swapInt32Bytes(&EdgeAttrSize);
267
268 if ((nret = dglInitialize(pgraph, 1, NodeAttrSize, EdgeAttrSize, NULL)) <
269 0) {
270 return nret;
271 }
272
273 for (cnt = 0; cnt < 16; cnt++) {
274 if ((nret = read(fd, &pgraph->aOpaqueSet[cnt], sizeof(dglInt32_t))) !=
275 sizeof(dglInt32_t)) {
276 pgraph->iErrno = DGL_ERR_Read;
277 return -pgraph->iErrno;
278 }
279 if (fSwap)
280 dgl_swapInt32Bytes(&pgraph->aOpaqueSet[cnt]);
281 }
282
283 if (read(fd, &pgraph->nnCost, sizeof(dglInt64_t)) != sizeof(dglInt64_t)) {
284 pgraph->iErrno = DGL_ERR_Read;
285 return -pgraph->iErrno;
286 }
287 if (fSwap)
288 dgl_swapInt64Bytes(&pgraph->nnCost);
289
290 if (read(fd, &pgraph->cNode, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
291 pgraph->iErrno = DGL_ERR_Read;
292 return -pgraph->iErrno;
293 }
294 if (fSwap)
295 dgl_swapInt32Bytes(&pgraph->cNode);
296
297 if (read(fd, &pgraph->cHead, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
298 pgraph->iErrno = DGL_ERR_Read;
299 return -pgraph->iErrno;
300 }
301 if (fSwap)
302 dgl_swapInt32Bytes(&pgraph->cHead);
303
304 if (read(fd, &pgraph->cTail, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
305 pgraph->iErrno = DGL_ERR_Read;
306 return -pgraph->iErrno;
307 }
308 if (fSwap)
309 dgl_swapInt32Bytes(&pgraph->cTail);
310
311 if (read(fd, &pgraph->cAlone, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
312 pgraph->iErrno = DGL_ERR_Read;
313 return -pgraph->iErrno;
314 }
315 if (fSwap)
316 dgl_swapInt32Bytes(&pgraph->cAlone);
317
318 if (read(fd, &pgraph->cEdge, sizeof(dglInt32_t)) != sizeof(dglInt32_t)) {
319 pgraph->iErrno = DGL_ERR_Read;
320 return -pgraph->iErrno;
321 }
322 if (fSwap)
323 dgl_swapInt32Bytes(&pgraph->cEdge);
324
325 if (read(fd, &pgraph->iNodeBuffer, sizeof(dglInt32_t)) !=
326 sizeof(dglInt32_t)) {
327 pgraph->iErrno = DGL_ERR_Read;
328 return -pgraph->iErrno;
329 }
330 if (fSwap)
331 dgl_swapInt32Bytes(&pgraph->iNodeBuffer);
332
333 if (read(fd, &pgraph->iEdgeBuffer, sizeof(dglInt32_t)) !=
334 sizeof(dglInt32_t)) {
335 pgraph->iErrno = DGL_ERR_Read;
336 return -pgraph->iErrno;
337 }
338 if (fSwap)
339 dgl_swapInt32Bytes(&pgraph->iEdgeBuffer);
340
341 if ((pgraph->pNodeBuffer = malloc(pgraph->iNodeBuffer)) == NULL) {
343 return -pgraph->iErrno;
344 }
345
346 if ((pgraph->pEdgeBuffer = malloc(pgraph->iEdgeBuffer)) == NULL) {
347 free(pgraph->pNodeBuffer);
349 return -pgraph->iErrno;
350 }
351
352 for (tot = 0, cnt = pgraph->iNodeBuffer; tot < cnt; tot += nret) {
353 if ((nret = read(fd, &pgraph->pNodeBuffer[tot], cnt - tot)) <= 0) {
354 free(pgraph->pNodeBuffer);
355 free(pgraph->pEdgeBuffer);
356 pgraph->iErrno = DGL_ERR_Read;
357 return -pgraph->iErrno;
358 }
359 }
360 if (fSwap) {
361 pn = (dglInt32_t *)pgraph->pNodeBuffer;
362 cn = pgraph->iNodeBuffer / sizeof(dglInt32_t);
363 for (i = 0; i < cn; i++) {
365 }
366 }
367
368 for (tot = 0, cnt = pgraph->iEdgeBuffer; tot < cnt; tot += nret) {
369 if ((nret = read(fd, &pgraph->pEdgeBuffer[tot], cnt - tot)) <= 0) {
370 free(pgraph->pNodeBuffer);
371 free(pgraph->pEdgeBuffer);
372 pgraph->iErrno = DGL_ERR_Read;
373 return -pgraph->iErrno;
374 }
375 }
376 if (fSwap) {
377 pn = (dglInt32_t *)pgraph->pEdgeBuffer;
378 cn = pgraph->iEdgeBuffer / sizeof(dglInt32_t);
379 for (i = 0; i < cn; i++) {
381 }
382 }
383
384 pgraph->Flags |= 0x1; /* flat-state */
385 return 0;
386}
#define NULL
Definition ccmath.h:32
int(* dglSPClip_fn)(dglGraph_s *, dglSPClipInput_s *, dglSPClipOutput_s *, void *)
Definition graph.h:179
#define DGL_ERR_Write
Definition graph.h:257
#define DGL_ENDIAN_LITTLE
Definition graph.h:72
#define DGL_ERR_MemoryExhausted
Definition graph.h:254
#define DGL_GS_FLAT
Definition graph.h:36
#define DGL_ERR_Read
Definition graph.h:258
#define DGL_ENDIAN_BIG
Definition graph.h:71
int(* dglSpanClip_fn)(dglGraph_s *, dglGraph_s *, dglSpanClipInput_s *, dglSpanClipOutput_s *, void *)
Definition graph.h:185
int dgl_initialize_V1(dglGraph_s *pgraph)
Definition graph_v1.c:104
int dgl_write_V1(dglGraph_s *pgraph, int fd)
Definition graph_v1.c:137
int dgl_read_V1(dglGraph_s *pgraph, int fd)
Definition graph_v1.c:232
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:76
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:61
int dgl_minimum_spanning_V1(dglGraph_s *pgraphIn, dglGraph_s *pgraphOut, dglInt32_t nVertex, dglSpanClip_fn fnClip, void *pvClipArg)
Definition graph_v1.c:90
int dgl_release_V1(dglGraph_s *pgraph)
Definition graph_v1.c:117
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:67
void dgl_swapInt32Bytes(dglInt32_t *pn)
Definition helpers.c:54
void * malloc(unsigned)
void free(void *)
void dglTreeEdgePri32Cancel(void *pvEdgePri32, void *pvParam)
Definition tree.c:352
void dglTreeEdgeCancel(void *pvEdge, void *pvParam)
Definition tree.c:152
int dglTreeNodeCompare(const void *pvNodeA, const void *pvNodeB, void *pvParam)
Definition tree.c:54
void * dglTreeGetAllocator(void)
Definition tree.c:406
void dglTreeNodePri32Cancel(void *pvNodePri32, void *pvParam)
Definition tree.c:302
void dglTreeNodeCancel(void *pvNode, void *pvParam)
Definition tree.c:45
#define avl_create
Definition tree.h:31
#define avl_destroy
Definition tree.h:33
long long dglInt64_t
Definition type.h:37
unsigned char dglByte_t
Definition type.h:35
long dglInt32_t
Definition type.h:36
#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)