GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
helpers.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 <stdlib.h>
12#include <string.h>
13
14#include "type.h"
15#include "tree.h"
16#include "graph.h"
17#include "helpers.h"
18
19/*
20 * helpers for parametric stack
21 */
22unsigned char *dgl_mempush(unsigned char *pstack, long *istack, long size,
23 void *pv)
24{
25 if (*istack == 0)
26 pstack = NULL;
27 pstack = realloc(pstack, size * (1 + *istack));
28 if (pstack == NULL)
29 return NULL;
30 memcpy(&pstack[(*istack) * size], pv, size);
31 (*istack)++;
32 return pstack;
33}
34
35unsigned char *dgl_mempop(unsigned char *pstack, long *istack, long size)
36{
37 if (*istack == 0)
38 return NULL;
39 return &pstack[size * (--(*istack))];
40}
41
43{
44 unsigned char *pb = (unsigned char *)pn;
45
46 pb[0] ^= pb[3];
47 pb[3] ^= pb[0];
48 pb[0] ^= pb[3];
49
50 pb[1] ^= pb[2];
51 pb[2] ^= pb[1];
52 pb[1] ^= pb[2];
53}
54
56{
57 unsigned char *pb = (unsigned char *)pn;
58
59 pb[0] ^= pb[7];
60 pb[7] ^= pb[0];
61 pb[0] ^= pb[7];
62
63 pb[1] ^= pb[6];
64 pb[6] ^= pb[1];
65 pb[1] ^= pb[6];
66
67 pb[2] ^= pb[5];
68 pb[5] ^= pb[2];
69 pb[2] ^= pb[5];
70
71 pb[3] ^= pb[4];
72 pb[4] ^= pb[3];
73 pb[3] ^= pb[4];
74}
75
76/*
77 * Keep the edge cost prioritizer in sync
78 */
80{
82 register int iEdge1, iEdge2;
84
85 if (pG->edgePrioritizer.pvAVL) {
86
87 findPriItem.nKey = nPriId;
89
90 if (pPriItem && pPriItem->pnData) {
91
92 pnNew = malloc(sizeof(dglInt32_t) * pPriItem->cnData);
93
94 if (pnNew == NULL) {
96 return -pG->iErrno;
97 }
98
99 for (iEdge1 = 0, iEdge2 = 0; iEdge2 < pPriItem->cnData; iEdge2++) {
100 if (pPriItem->pnData[iEdge2] != nId) {
101 pnNew[iEdge1++] = pPriItem->pnData[iEdge2];
102 }
103 }
104
105 free(pPriItem->pnData);
106 if (iEdge1 == 0) {
107 free(pnNew);
108 pPriItem->pnData = NULL;
109 pPriItem->cnData = 0;
110 }
111 else {
112 pPriItem->pnData = pnNew;
113 pPriItem->cnData = iEdge1;
114 }
115 }
116 }
117 return 0;
118}
119
121{
123
124 if (pG->edgePrioritizer.pvAVL == NULL) {
127 if (pG->edgePrioritizer.pvAVL == NULL) {
129 return -pG->iErrno;
130 }
131 }
133 if (pPriItem == NULL) {
135 return -pG->iErrno;
136 }
137 if (pPriItem->cnData == 0) {
138 pPriItem->pnData = (dglInt32_t *)malloc(sizeof(dglInt32_t));
139 }
140 else {
141 pPriItem->pnData = (dglInt32_t *)realloc(
142 pPriItem->pnData, sizeof(dglInt32_t) * (pPriItem->cnData + 1));
143 }
144 if (pPriItem->pnData == NULL) {
146 return -pG->iErrno;
147 }
148 pPriItem->pnData[pPriItem->cnData] = nId;
149 pPriItem->cnData++;
150 return 0;
151}
#define NULL
Definition ccmath.h:32
#define DGL_ERR_MemoryExhausted
Definition graph.h:242
int dgl_edge_prioritizer_del(dglGraph_s *pG, dglInt32_t nId, dglInt32_t nPriId)
Definition helpers.c:79
void dgl_swapInt64Bytes(dglInt64_t *pn)
Definition helpers.c:55
unsigned char * dgl_mempop(unsigned char *pstack, long *istack, long size)
Definition helpers.c:35
unsigned char * dgl_mempush(unsigned char *pstack, long *istack, long size, void *pv)
Definition helpers.c:22
void dgl_swapInt32Bytes(dglInt32_t *pn)
Definition helpers.c:42
int dgl_edge_prioritizer_add(dglGraph_s *pG, dglInt32_t nId, dglInt32_t nPriId)
Definition helpers.c:120
void * malloc(unsigned)
void free(void *)
dglEdgePrioritizer_s edgePrioritizer
Definition graph.h:152
int iErrno
Definition graph.h:126
dglTreeEdgePri32_s * dglTreeEdgePri32Add(void *pavl, dglInt32_t nKey)
Definition tree.c:361
void * dglTreeGetAllocator(void)
Definition tree.c:394
int dglTreeEdgePri32Compare(const void *pvEdgePri32A, const void *pvEdgePri32B, void *pvParam)
Definition tree.c:348
#define avl_find
Definition tree.h:26
#define avl_create
Definition tree.h:19
long long dglInt64_t
Definition type.h:25
long dglInt32_t
Definition type.h:24