Sturk 1.1.0
Publish-subscribe C implementation.
Loading...
Searching...
No Matches
vertex.h
Go to the documentation of this file.
1/*
2BSD 3-Clause License
3
4Copyright (c) 2025, Szymon Turno
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
91. Redistributions of source code must retain the above copyright notice, this
10 list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*/
31
41#ifndef VERTEGS_VERTEX_H
42#define VERTEGS_VERTEX_H
43
44#include <stddef.h>
45
46#ifndef VX_ASSERT
47
48#include <assert.h>
49
57#define VX_ASSERT(cond) assert(cond)
58
59#endif /* VX_ASSERT */
60
68#define VX_ENSURE_MEM(ptr) \
69 do { \
70 VX_ASSERT(ptr); \
71 if ((ptr) == NULL) \
72 return NULL; \
73 } while (0)
74
80struct Vertegs {
86 struct Vertegs* nbor[1];
87};
88
98static inline struct Vertegs* vx_4nbor(struct Vertegs** nbor)
99{
100 return (struct Vertegs*)nbor;
101}
102
114static inline struct Vertegs* vx_walk(struct Vertegs* v, size_t edge, int len)
115{
116 VX_ENSURE_MEM(v);
117 while (len-- && v->nbor[edge])
118 v = v->nbor[edge];
119 return v;
120}
121
133static inline struct Vertegs*
134vx_inslist(struct Vertegs* list, struct Vertegs* entry, int pos)
135{
136 const size_t next = 0;
137 struct Vertegs* nbor[] = {list};
138 struct Vertegs* v = vx_walk(vx_4nbor(nbor), next, pos);
139
140 VX_ENSURE_MEM(entry);
141 entry->nbor[next] = v->nbor[next];
142 v->nbor[next] = entry;
143 return nbor[next];
144}
145
156static inline struct Vertegs* vx_remlist(struct Vertegs** listp, int pos)
157{
158 const size_t next = 0;
159 struct Vertegs* ret = NULL;
160 struct Vertegs* v = vx_walk(vx_4nbor(listp), next, pos);
161
162 ret = v->nbor[next];
163 v->nbor[next] = ret->nbor[next];
164 return ret;
165}
166
178static inline struct Vertegs*
179vx_inscirq(struct Vertegs* cirq, struct Vertegs* entry, int pos)
180{
181 const size_t next = 0;
182 const size_t prev = 1;
183 struct Vertegs* v = NULL;
184
185 VX_ENSURE_MEM(entry);
186 if (!cirq) {
187 entry->nbor[next] = entry;
188 entry->nbor[prev] = entry;
189 return entry;
190 }
191
192 if (pos > 0)
193 v = vx_walk(cirq, next, pos);
194 else if (pos < -1)
195 v = vx_walk(cirq, prev, -(pos + 1));
196 else
197 v = cirq;
198 entry->nbor[next] = v;
199 entry->nbor[prev] = v->nbor[prev];
200 v->nbor[prev] = entry;
201 entry->nbor[prev]->nbor[next] = entry;
202 return pos ? cirq : entry;
203}
204
215static inline struct Vertegs* vx_remcirq(struct Vertegs** cirqp, int pos)
216{
217 const size_t next = 0;
218 const size_t prev = 1;
219 struct Vertegs* ret = NULL;
220
221 VX_ENSURE_MEM(cirqp);
222 ret = *cirqp;
223 if (pos > 0)
224 ret = vx_walk(ret, next, pos);
225 else if (pos < 0)
226 ret = vx_walk(ret, prev, -pos);
227 ret->nbor[next]->nbor[prev] = ret->nbor[prev];
228 ret->nbor[prev]->nbor[next] = ret->nbor[next];
229 if (ret == ret->nbor[next])
230 *cirqp = NULL;
231 else if (ret == *cirqp)
232 *cirqp = ret->nbor[next];
233 return ret;
234}
235
236#endif /* VERTEGS_VERTEX_H */
The data type for a graph vertex.
Definition vertex.h:80
struct Vertegs * nbor[1]
The neighbourhood of the vertex - an array of neighbours.
Definition vertex.h:86
#define VX_ENSURE_MEM(ptr)
Raise an exception and return NULL if the pointer is NULL.
Definition vertex.h:68
static struct Vertegs * vx_walk(struct Vertegs *v, size_t edge, int len)
For a chain, in which all edges share the same index, find the other end.
Definition vertex.h:114
static struct Vertegs * vx_inslist(struct Vertegs *list, struct Vertegs *entry, int pos)
Insert, at a given position, an entry into a list.
Definition vertex.h:134
static struct Vertegs * vx_4nbor(struct Vertegs **nbor)
Cast to vertex from its neighbourhood.
Definition vertex.h:98
static struct Vertegs * vx_remlist(struct Vertegs **listp, int pos)
Remove, at a given position, an entry from a list.
Definition vertex.h:156
static struct Vertegs * vx_inscirq(struct Vertegs *cirq, struct Vertegs *entry, int pos)
Insert, at a given position, an entry into a cirq.
Definition vertex.h:179
static struct Vertegs * vx_remcirq(struct Vertegs **cirqp, int pos)
Remove, at a given position, an entry from a cirq.
Definition vertex.h:215