Sturk 1.0.2
Publish-subscribe C implementation.
Loading...
Searching...
No Matches
vertegs.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_H
42#define VERTEGS_H
43
44#include <stddef.h>
45
46#ifndef VX_EXCEPT
47
53#define VX_EXCEPT(reason, file, line)
54
55#endif /* VX_EXCEPT */
56
64#define VX_ENSURE_MEM(ptr) \
65 do { \
66 if ((ptr) == NULL) { \
67 VX_EXCEPT("Null param.", __FILE__, __LINE__); \
68 return NULL; \
69 } \
70 } while (0)
71
77struct Vertegs {
83 struct Vertegs* nbor[1];
84};
85
95static inline struct Vertegs* vx_4nbor(struct Vertegs** nbor)
96{
97 return (struct Vertegs*)nbor;
98}
99
111static inline struct Vertegs* vx_walk(struct Vertegs* v, size_t edge, int len)
112{
113 struct Vertegs* p = NULL;
114
115 VX_ENSURE_MEM(v);
116 while (len-- && (p = v->nbor[edge]))
117 v = p;
118 return v;
119}
120
132static inline struct Vertegs*
133vx_inslist(struct Vertegs* list, struct Vertegs* entry, int pos)
134{
135 const size_t next = 0;
136 struct Vertegs* nbor[] = {list};
137 struct Vertegs* v = vx_walk(vx_4nbor(nbor), next, pos);
138
139 VX_ENSURE_MEM(entry);
140 entry->nbor[next] = v->nbor[next];
141 v->nbor[next] = entry;
142 return nbor[next];
143}
144
155static inline struct Vertegs* vx_remlist(struct Vertegs** listp, int pos)
156{
157 const size_t next = 0;
158 struct Vertegs* ret = NULL;
159 struct Vertegs* v = vx_walk(vx_4nbor(listp), next, pos);
160
161 ret = v->nbor[next];
162 v->nbor[next] = ret->nbor[next];
163 return ret;
164}
165
177static inline struct Vertegs*
178vx_inscirq(struct Vertegs* cirq, struct Vertegs* entry, int pos)
179{
180 const size_t next = 0;
181 const size_t prev = 1;
182 struct Vertegs* v = NULL;
183
184 VX_ENSURE_MEM(entry);
185 if (!cirq) {
186 entry->nbor[next] = entry;
187 entry->nbor[prev] = entry;
188 return entry;
189 }
190
191 if (pos > 0)
192 v = vx_walk(cirq, next, pos);
193 else if (pos < -1)
194 v = vx_walk(cirq, prev, -(pos + 1));
195 else
196 v = cirq;
197 entry->nbor[next] = v;
198 entry->nbor[prev] = v->nbor[prev];
199 v->nbor[prev] = entry;
200 entry->nbor[prev]->nbor[next] = entry;
201 return pos ? cirq : entry;
202}
203
214static inline struct Vertegs* vx_remcirq(struct Vertegs** cirqp, int pos)
215{
216 const size_t next = 0;
217 const size_t prev = 1;
218 struct Vertegs* ret = NULL;
219
220 VX_ENSURE_MEM(cirqp);
221 ret = *cirqp;
222 if (pos > 0)
223 ret = vx_walk(ret, next, pos);
224 else if (pos < 0)
225 ret = vx_walk(ret, prev, -pos);
226 ret->nbor[next]->nbor[prev] = ret->nbor[prev];
227 ret->nbor[prev]->nbor[next] = ret->nbor[next];
228 if (ret == ret->nbor[next])
229 *cirqp = NULL;
230 else if (ret == *cirqp)
231 *cirqp = ret->nbor[next];
232 return ret;
233}
234
235#endif /* VERTEGS_H */
The data type for a graph vertex.
Definition vertegs.h:77
struct Vertegs * nbor[1]
The neighbourhood of the vertex - an array of neighbours.
Definition vertegs.h:83
#define VX_ENSURE_MEM(ptr)
Raise an exception and return NULL if the pointer is NULL.
Definition vertegs.h:64
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 vertegs.h:111
static struct Vertegs * vx_inslist(struct Vertegs *list, struct Vertegs *entry, int pos)
Insert, at a given position, an entry into a list.
Definition vertegs.h:133
static struct Vertegs * vx_4nbor(struct Vertegs **nbor)
Cast to vertex from its neighbourhood.
Definition vertegs.h:95
static struct Vertegs * vx_remlist(struct Vertegs **listp, int pos)
Remove, at a given position, an entry from a list.
Definition vertegs.h:155
static struct Vertegs * vx_inscirq(struct Vertegs *cirq, struct Vertegs *entry, int pos)
Insert, at a given position, an entry into a cirq.
Definition vertegs.h:178
static struct Vertegs * vx_remcirq(struct Vertegs **cirqp, int pos)
Remove, at a given position, an entry from a cirq.
Definition vertegs.h:214