File: span-template.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (424 lines) | stat: -rw-r--r-- 11,822 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* LIBDGL -- a Directed Graph Library implementation
 * Copyright (C) 2002 Roberto Micarelli
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * best view with tabstop=4
 */

/*
 * Build the depth-first spanning tree of 'pgraphIn' into 'pgraphOut'
 * - pgraphOut must have been previously initialized by the caller and is returned in TREE state
 * - I prefer using a iterative approach with a stack for 'waiting edges' instead of recursion: it's
 *   cheaper to stack 8 bytes for each edge than the whole function stack
 * - The visited network is passed by the caller because this function can be used for two purposes:
 *   1. generate a single spanning tree (dglDepthSpanning)
 *   2. part of a loop for generating connected-components of the graph (dglDepthComponents)
 */
int DGL_SPAN_DEPTHFIRST_SPANNING_FUNC(dglGraph_s * pgraphIn,
				      dglGraph_s * pgraphOut,
				      dglInt32_t nVertex,
				      void *pvVisited,
				      dglSpanClip_fn fnClip, void *pvClipArg)
{
    struct _stackItem
    {
	dglInt32_t *pnHead;
	dglInt32_t *pnEdge;
	int iWay;
    };

    struct _stackItem stackItem;
    struct _stackItem *pStackItem;

    dglInt32_t *pHead;
    dglInt32_t *pTail;
    dglInt32_t *pEdgeset;
    dglInt32_t *pEdge;
    long istack = 0;
    unsigned char *pstack = NULL;
    int nret;
    dglSpanClipInput_s clipInput;
    dglTreeNode_s findVisited;
    dglEdgesetTraverser_s laT;


    if ((pHead = dglGetNode(pgraphIn, nVertex)) == NULL) {
	pgraphIn->iErrno = DGL_ERR_HeadNodeNotFound;
	goto dfs_error;
    }

    /*
     * the simplest case is when vertex node is alone or has no outgoing edges, the result
     * of the spanning is a graph having only one node
     */
    if (DGL_NODE_STATUS(pHead) & DGL_NS_ALONE ||
	(!(DGL_NODE_STATUS(pHead) & DGL_NS_HEAD) &&
	 DGL_NODE_STATUS(pHead) & DGL_NS_TAIL)) {
	nret =
	    DGL_ADD_NODE_FUNC(pgraphOut, DGL_NODE_ID(pHead),
			      DGL_NODE_ATTR_PTR(pHead), 0);
	if (nret < 0) {
	    goto dfs_error;
	}
	return 0;
    }

    if ((DGL_NODE_STATUS(pHead) & DGL_NS_HEAD) || pgraphIn->Version == 3) {

	pEdgeset = _DGL_OUTEDGESET(pgraphIn, pHead);

	if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraphIn, &laT, pEdgeset) < 0) {
	    goto dfs_error;
	}
	for (pEdge = DGL_EDGESET_T_FIRST_FUNC(&laT);
	     pEdge; pEdge = DGL_EDGESET_T_NEXT_FUNC(&laT)
	    ) {
	    stackItem.pnHead = pHead;
	    stackItem.pnEdge = pEdge;
	    stackItem.iWay = 0;
	    if ((pstack =
		 dgl_mempush(pstack, &istack, sizeof(stackItem),
			     &stackItem)) == NULL) {
		pgraphIn->iErrno = DGL_ERR_MemoryExhausted;
		goto dfs_error;
	    }
	}
	DGL_EDGESET_T_RELEASE_FUNC(&laT);

	if (pgraphIn->Version == 3) {
	    pEdgeset = _DGL_INEDGESET(pgraphIn, pHead);

	    if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraphIn, &laT, pEdgeset) < 0) {
		goto dfs_error;
	    }
	    for (pEdge = DGL_EDGESET_T_FIRST_FUNC(&laT);
		 pEdge; pEdge = DGL_EDGESET_T_NEXT_FUNC(&laT)
		) {
		if (DGL_EDGE_STATUS(pEdge) & DGL_ES_DIRECTED)
		    continue;
		stackItem.pnHead = pHead;
		stackItem.pnEdge = pEdge;
		stackItem.iWay = 1;
		if ((pstack =
		     dgl_mempush(pstack, &istack, sizeof(stackItem),
				 &stackItem)) == NULL) {
		    pgraphIn->iErrno = DGL_ERR_MemoryExhausted;
		    goto dfs_error;
		}
	    }
	    DGL_EDGESET_T_RELEASE_FUNC(&laT);
	}

	if (dglTreeNodeAdd(pvVisited, DGL_NODE_ID(pHead)) == NULL) {
	    pgraphIn->iErrno = DGL_ERR_MemoryExhausted;
	    goto dfs_error;
	}
    }

    while ((pStackItem =
	    (struct _stackItem *)dgl_mempop(pstack, &istack,
					    sizeof(stackItem))) != NULL) {
	pHead = pStackItem->pnHead;
	pEdge = pStackItem->pnEdge;

	if (pStackItem->iWay == 0)
	    pTail = _DGL_EDGE_TAILNODE(pgraphIn, pEdge);
	else
	    pTail = _DGL_EDGE_HEADNODE(pgraphIn, pEdge);

	findVisited.nKey = DGL_NODE_ID(pTail);
	if (avl_find(pvVisited, &findVisited)) {	/* already visited */
	    continue;
	}

	if (fnClip) {
	    clipInput.pnNodeFrom = pHead;
	    clipInput.pnEdge = pEdge;
	    clipInput.pnNodeTo = pTail;
	    if (fnClip(pgraphIn, pgraphOut, &clipInput, NULL, pvClipArg))
		continue;
	}

	if (dglTreeNodeAdd(pvVisited, DGL_NODE_ID(pTail)) == NULL) {
	    pgraphIn->iErrno = DGL_ERR_MemoryExhausted;
	    goto dfs_error;
	}

	/* add this edge */
	nret = DGL_ADD_EDGE_FUNC(pgraphOut,
				 DGL_NODE_ID(pHead),
				 DGL_NODE_ID(pTail),
				 DGL_EDGE_COST(pEdge),
				 DGL_EDGE_ID(pEdge),
				 DGL_NODE_ATTR_PTR(pHead),
				 DGL_NODE_ATTR_PTR(pTail),
				 DGL_EDGE_ATTR_PTR(pEdge), 0);

	if (nret < 0) {
	    goto dfs_error;
	}

	if ((DGL_NODE_STATUS(pHead) & DGL_NS_HEAD) || pgraphIn->Version == 3) {

	    pEdgeset = _DGL_OUTEDGESET(pgraphIn, pTail);
	    if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraphIn, &laT, pEdgeset) < 0) {
		goto dfs_error;
	    }
	    for (pEdge = DGL_EDGESET_T_FIRST_FUNC(&laT);
		 pEdge; pEdge = DGL_EDGESET_T_NEXT_FUNC(&laT)
		) {
		stackItem.pnHead = pTail;
		stackItem.pnEdge = pEdge;
		stackItem.iWay = 0;
		if ((pstack =
		     dgl_mempush(pstack, &istack, sizeof(stackItem),
				 &stackItem)) == NULL) {
		    pgraphIn->iErrno = DGL_ERR_MemoryExhausted;
		    goto dfs_error;
		}
	    }
	    DGL_EDGESET_T_RELEASE_FUNC(&laT);

	    if (pgraphIn->Version == 3) {
		pEdgeset = _DGL_INEDGESET(pgraphIn, pTail);
		if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraphIn, &laT, pEdgeset) <
		    0) {
		    goto dfs_error;
		}
		for (pEdge = DGL_EDGESET_T_FIRST_FUNC(&laT);
		     pEdge; pEdge = DGL_EDGESET_T_NEXT_FUNC(&laT)
		    ) {
		    if (DGL_EDGE_STATUS(pEdge) & DGL_ES_DIRECTED)
			continue;
		    stackItem.pnHead = pTail;
		    stackItem.pnEdge = pEdge;
		    stackItem.iWay = 1;
		    if ((pstack =
			 dgl_mempush(pstack, &istack, sizeof(stackItem),
				     &stackItem)) == NULL) {
			pgraphIn->iErrno = DGL_ERR_MemoryExhausted;
			goto dfs_error;
		    }
		}
		DGL_EDGESET_T_RELEASE_FUNC(&laT);
	    }
	}
    }

    if (pstack)
	free(pstack);
    return 0;

  dfs_error:
    if (pstack)
	free(pstack);
    return -pgraphIn->iErrno;
}

/*
 * Use a edge prioritized, tree growing scheme (aka Prim algorithm) in order to
 * be appliable to both undirected graphs (minimum spanning tree - MST) and
 * digraphs (minimum arborescense tree - MAT)
 * The vertex argument is ignored in MST (when algorithm is applied to a
 * version 3 undirected graph).
 */
int DGL_SPAN_MINIMUM_SPANNING_FUNC(dglGraph_s * pgraphIn,
				   dglGraph_s * pgraphOut,
				   dglInt32_t nVertex,
				   dglSpanClip_fn fnClip, void *pvClipArg)
{
    dglInt32_t *pHead, *pTail, *pEdgeset, *pEdge;
    dglHeap_s FrontEdgeHeap;
    dglHeapData_u HeapData;
    dglHeapNode_s HeapItem;
    dglTreeNode_s *pPredistItem, findItem;
    dglEdgesetTraverser_s laT;
    int nret;

    dglHeapInit(&FrontEdgeHeap);

    if (pgraphIn->Version == 3) {	/* undirected: pick up the first node */
	dglNodeTraverser_s pT;

	DGL_NODE_T_INITIALIZE_FUNC(pgraphIn, &pT);
	pHead = DGL_NODE_T_FIRST_FUNC(&pT);
	DGL_NODE_T_RELEASE_FUNC(&pT);
    }
    else {			/* directed: pick up the arborescense origin */
	pHead = DGL_GET_NODE_FUNC(pgraphIn, nVertex);
    }

    if (pHead == NULL) {
	pgraphIn->iErrno = DGL_ERR_HeadNodeNotFound;
	goto mst_error;
    }

    if (DGL_NODE_STATUS(pHead) & DGL_NS_HEAD ||
	DGL_NODE_STATUS(pHead) & DGL_NS_ALONE) {

	if ((DGL_NODE_STATUS(pHead) & DGL_NS_HEAD) ||
	    (DGL_NODE_STATUS(pHead) & DGL_NS_ALONE) ||
	    pgraphIn->Version == 3) {
	    if (DGL_ADD_NODE_FUNC
		(pgraphOut, DGL_NODE_ID(pHead), DGL_NODE_ATTR_PTR(pHead),
		 0) < 0) {
		goto mst_error;
	    }

	    if (DGL_NODE_STATUS(pHead) & DGL_NS_ALONE) {
		dglHeapFree(&FrontEdgeHeap, NULL);
		return 0;
	    }

	    pEdgeset = _DGL_OUTEDGESET(pgraphIn, pHead);
	    if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraphIn, &laT, pEdgeset) < 0) {
		goto mst_error;
	    }
	    for (pEdge = DGL_EDGESET_T_FIRST_FUNC(&laT);
		 pEdge; pEdge = DGL_EDGESET_T_NEXT_FUNC(&laT)
		) {
		HeapData.pv = pEdge;
		if (dglHeapInsertMin
		    (&FrontEdgeHeap, DGL_EDGE_COST(pEdge), 0, HeapData) < 0) {
		    pgraphIn->iErrno = DGL_ERR_HeapError;
		    goto mst_error;
		}
	    }
	    DGL_EDGESET_T_RELEASE_FUNC(&laT);
	    if (pgraphIn->Version == 3) {
		pEdgeset = _DGL_INEDGESET(pgraphIn, pHead);
		if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraphIn, &laT, pEdgeset) <
		    0) {
		    goto mst_error;
		}
		for (pEdge = DGL_EDGESET_T_FIRST_FUNC(&laT);
		     pEdge; pEdge = DGL_EDGESET_T_NEXT_FUNC(&laT)
		    ) {
		    if (DGL_EDGE_STATUS(pEdge) & DGL_ES_DIRECTED)
			continue;
		    HeapData.pv = pEdge;
		    if (dglHeapInsertMin
			(&FrontEdgeHeap, DGL_EDGE_COST(pEdge), 1,
			 HeapData) < 0) {
			pgraphIn->iErrno = DGL_ERR_HeapError;
			goto mst_error;
		    }
		}
		DGL_EDGESET_T_RELEASE_FUNC(&laT);
	    }
	}
    }
    else {
	pgraphIn->iErrno = DGL_ERR_BadEdge;
	goto mst_error;
    }

    while (dglHeapExtractMin(&FrontEdgeHeap, &HeapItem) == 1) {
	pEdge = HeapItem.value.pv;

	if (HeapItem.flags == 0) {
	    if ((pHead = _DGL_EDGE_HEADNODE(pgraphIn, pEdge)) == NULL) {
		pgraphIn->iErrno = DGL_ERR_UnexpectedNullPointer;
		goto mst_error;
	    }
	    if ((pTail = _DGL_EDGE_TAILNODE(pgraphIn, pEdge)) == NULL) {
		pgraphIn->iErrno = DGL_ERR_UnexpectedNullPointer;
		goto mst_error;
	    }
	}
	else if (pgraphIn->Version == 3) {
	    if ((pTail = _DGL_EDGE_HEADNODE(pgraphIn, pEdge)) == NULL) {
		pgraphIn->iErrno = DGL_ERR_UnexpectedNullPointer;
		goto mst_error;
	    }
	    if ((pHead = _DGL_EDGE_TAILNODE(pgraphIn, pEdge)) == NULL) {
		pgraphIn->iErrno = DGL_ERR_UnexpectedNullPointer;
		goto mst_error;
	    }
	}
	else
	    continue;

	findItem.nKey = DGL_NODE_ID(pTail);

	if ((pPredistItem =
	     avl_find(pgraphOut->pNodeTree, &findItem)) != NULL) {
	    continue;
	}

	nret = DGL_ADD_EDGE_FUNC(pgraphOut,
				 DGL_NODE_ID(pHead),
				 DGL_NODE_ID(pTail),
				 DGL_EDGE_COST(pEdge),
				 DGL_EDGE_ID(pEdge),
				 DGL_NODE_ATTR_PTR(pHead),
				 DGL_NODE_ATTR_PTR(pTail),
				 DGL_EDGE_ATTR_PTR(pEdge), 0);

	if (nret < 0) {
	    goto mst_error;
	}

	pHead = pTail;

	if ((DGL_NODE_STATUS(pHead) & DGL_NS_HEAD) || pgraphIn->Version == 3) {
	    pEdgeset = _DGL_OUTEDGESET(pgraphIn, pHead);
	    if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraphIn, &laT, pEdgeset) < 0) {
		goto mst_error;
	    }
	    for (pEdge = DGL_EDGESET_T_FIRST_FUNC(&laT);
		 pEdge; pEdge = DGL_EDGESET_T_NEXT_FUNC(&laT)
		) {
		HeapData.pv = pEdge;
		if (dglHeapInsertMin
		    (&FrontEdgeHeap, DGL_EDGE_COST(pEdge), 0, HeapData) < 0) {
		    pgraphIn->iErrno = DGL_ERR_HeapError;
		    goto mst_error;
		}
	    }
	    if (pgraphIn->Version == 3) {
		DGL_EDGESET_T_RELEASE_FUNC(&laT);
		pEdgeset = _DGL_OUTEDGESET(pgraphIn, pHead);
		if (DGL_EDGESET_T_INITIALIZE_FUNC(pgraphIn, &laT, pEdgeset) <
		    0) {
		    goto mst_error;
		}
		for (pEdge = DGL_EDGESET_T_FIRST_FUNC(&laT);
		     pEdge; pEdge = DGL_EDGESET_T_NEXT_FUNC(&laT)
		    ) {
		    if (DGL_EDGE_STATUS(pEdge) & DGL_ES_DIRECTED)
			continue;
		    HeapData.pv = pEdge;
		    if (dglHeapInsertMin
			(&FrontEdgeHeap, DGL_EDGE_COST(pEdge), 1,
			 HeapData) < 0) {
			pgraphIn->iErrno = DGL_ERR_HeapError;
			goto mst_error;
		    }
		}
		DGL_EDGESET_T_RELEASE_FUNC(&laT);
	    }
	}
    }
    dglHeapFree(&FrontEdgeHeap, NULL);
    return 0;

  mst_error:
    dglHeapFree(&FrontEdgeHeap, NULL);
    return -pgraphIn->iErrno;
}