]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
take3: format 903 files
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / iter.qh
1 #pragma once
2
3 #if 1
4 #define ITER_CONST const
5 #else
6 #define ITER_CONST
7 #endif
8
9 #define FOREACH_ARRAY(arr, start, end, cond, body) \
10         MACRO_BEGIN \
11                 for (int _i = start; _i < end; ++_i) { \
12                         const noref int i = _i; \
13                         ITER_CONST noref entity it = arr[i]; \
14                         if (cond) { LAMBDA(body); } \
15                 } \
16         MACRO_END
17
18 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
19
20 #define FOREACH_LIST(list, next, cond, body) \
21         MACRO_BEGIN \
22                 int _i = 0; \
23                 for (entity _it = list##_first, _next = NULL; _it; (_it = _next, ++_i)) { \
24                         const noref int i = _i; \
25                         ITER_CONST noref entity it = _it; \
26                         _next = _it.next; \
27                         if (cond) { LAMBDA(body); } \
28                 } \
29         MACRO_END
30
31 #define FOREACH_WORD(words, cond, body) \
32         MACRO_BEGIN \
33                 string _words = words; \
34                 int _i = 0; \
35                 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) { \
36                         const noref int i = _i; \
37                         const noref string it = _it; \
38                         if (cond) { LAMBDA(body); } \
39                 } \
40         MACRO_END
41
42 #define STRING_ITERATOR(this, s, i) \
43         string this##_s = s; \
44         int this##_i = i
45
46 #define STRING_ITERATOR_SET(this, s, i) \
47         MACRO_BEGIN \
48                 this##_s = s; \
49                 this##_i = i; \
50         MACRO_END
51
52 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
53 #define STRING_ITERATOR_PEEK(this) str2chr(this##_s, this##_i)
54 #define STRING_ITERATOR_NEXT(this) MACRO_BEGIN { ++this##_i; } MACRO_END
55 #define STRING_ITERATOR_UNGET(this) MACRO_BEGIN { --this##_i; } MACRO_END
56 #define STRING_ITERATOR_SAVE(this) this##_i
57 #define STRING_ITERATOR_LOAD(this, n) MACRO_BEGIN this##_i = n; MACRO_END
58
59 #define FOREACH_CHAR(s, cond, body) \
60         MACRO_BEGIN \
61                 STRING_ITERATOR(iter, s, 0); \
62                 int _it; \
63                 while ((_it = STRING_ITERATOR_GET(iter)) > 0) { \
64                         const noref int it = _it; \
65                         if (cond) { LAMBDA(body); } \
66                 } \
67         MACRO_END
68
69 #if defined(CSQC)
70 entity(entity start, .string fld, string match) _findstring = #18;
71 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
72
73 entity(entity start, .entity fld, entity match) _findentity = #98;
74 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
75
76 entity(entity start, .float fld, float match) _findfloat = #98;
77 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
78
79 entity(entity start, .float fld, float match) _findflags = #449;
80 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
81 #elif defined(SVQC)
82 entity(entity start, .string fld, string match) _findstring = #18;
83 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
84
85 entity(entity start, .entity fld, entity match) _findentity = #98;
86 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
87
88 entity(entity start, .float fld, float match) _findfloat = #98;
89 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
90
91 entity(entity start, .float fld, float match) _findflags = #449;
92 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
93 #elif defined(MENUQC)
94 entity(entity start, .string fld, string match) _findstring = #24;
95 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
96
97 entity(entity start, .entity fld, entity match) _findentity = #25;
98 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #27;
99
100 entity(entity start, .float fld, float match) _findfloat = #25;
101 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
102
103 entity(entity start, .float fld, float match) _findflags = #87;
104 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
105 #endif
106
107 #define ORDERED(F) F##_UNORDERED
108 #define _FOREACH_ENTITY_FIND_ORDERED(T, fld, match, cond, body) \
109         MACRO_BEGIN \
110                 int _i = 0; \
111                 for (entity _it = NULL; (_it = _find##T(_it, fld, match)); ++_i) { \
112                         const noref int i = _i; \
113                         ITER_CONST noref entity it = _it; \
114                         if (cond) { LAMBDA(body); } \
115                 } \
116         MACRO_END
117 #define MUTEX_LOCK(this) \
118         MACRO_BEGIN \
119                 if (this) { LOG_SEVEREF("Loop mutex held by %s", this); } \
120                 this = __FUNC__; \
121         MACRO_END
122 #define MUTEX_UNLOCK(this) \
123         MACRO_BEGIN \
124                 this = string_null; \
125         MACRO_END
126 #define _FOREACH_ENTITY_FIND_UNORDERED(id, T, fld, match, cond, body) \
127         MACRO_BEGIN \
128                 MUTEX_LOCK(_FOREACH_ENTITY_FIND_##T##_##id##mutex); \
129                 entity _foundchain_first = _findchain##T##_tofield(fld, match, _FOREACH_ENTITY_FIND_##T##_next##id); \
130                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FIND_##T##_next##id, cond, body); \
131                 MUTEX_UNLOCK(_FOREACH_ENTITY_FIND_##T##_##id##mutex); \
132         MACRO_END
133
134 #define FOREACH_ENTITY(cond, body) ORDERED(FOREACH_ENTITY)(cond, body)
135 #define FOREACH_ENTITY_ORDERED(cond, body) \
136         MACRO_BEGIN \
137                 int _i = 0; \
138                 for (entity _it = NULL; (_it = nextent(_it)); ++_i) { \
139                         const noref int i = _i; \
140                         ITER_CONST noref entity it = _it; \
141                         if (cond) { LAMBDA(body); } \
142                 } \
143         MACRO_END
144 /** marker field, always NULL */
145 .entity _FOREACH_ENTITY_fld;
146 .entity _FOREACH_ENTITY_FIND_entity_nextall;
147 noref string _FOREACH_ENTITY_FIND_entity_allmutex;
148 #define FOREACH_ENTITY_UNORDERED(cond, body) _FOREACH_ENTITY_FIND_UNORDERED(all, entity, _FOREACH_ENTITY_fld, NULL, cond, body)
149
150 #define FOREACH_ENTITY_FLAGS(fld, match, body) ORDERED(FOREACH_ENTITY_FLAGS)(fld, match, body)
151 #define FOREACH_ENTITY_FLAGS_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(flags, fld, match, true, body)
152 .entity _FOREACH_ENTITY_FIND_flags_next;
153 noref string _FOREACH_ENTITY_FIND_flags_mutex;
154 #define FOREACH_ENTITY_FLAGS_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, flags, fld, match, true, body)
155
156 #ifdef GAMEQC
157 entity(vector org, float rad, .entity tofield) _findchainradius_tofield = #22;
158 #define FOREACH_ENTITY_RADIUS(org, dist, cond, body) ORDERED(FOREACH_ENTITY_RADIUS)(org, dist, cond, body)
159 .entity _FOREACH_ENTITY_FIND_radius_next;
160 noref string _FOREACH_ENTITY_FIND_radius_mutex;
161 #define FOREACH_ENTITY_RADIUS_UNORDERED(org, dist, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(, radius, org, dist, cond, body)
162 .entity _FOREACH_ENTITY_FIND_radius_nexttmp;
163 noref string _FOREACH_ENTITY_FIND_radius_tmpmutex;
164 #define FOREACH_ENTITY_RADIUS_ORDERED(org, dist, cond, body) \
165         MACRO_BEGIN \
166                 entity _rev_first = NULL; \
167                 _FOREACH_ENTITY_FIND_UNORDERED(tmp, radius, org, dist, cond, (it._FOREACH_ENTITY_FIND_radius_nexttmp = _rev_first, _rev_first = it)); \
168                 MUTEX_LOCK(_FOREACH_ENTITY_FIND_radius_tmpmutex); \
169                 FOREACH_LIST(_rev, _FOREACH_ENTITY_FIND_radius_nexttmp, true, body); \
170                 MUTEX_UNLOCK(_FOREACH_ENTITY_FIND_radius_tmpmutex); \
171         MACRO_END
172 #endif
173
174 #define FOREACH_ENTITY_FLOAT(fld, match, body) ORDERED(FOREACH_ENTITY_FLOAT)(fld, match, body)
175 #define FOREACH_ENTITY_FLOAT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(float, fld, match, true, body)
176 .entity _FOREACH_ENTITY_FIND_float_next;
177 noref string _FOREACH_ENTITY_FIND_float_mutex;
178 #define FOREACH_ENTITY_FLOAT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, float, fld, match, true, body)
179
180 #define FOREACH_ENTITY_ENT(fld, match, body) ORDERED(FOREACH_ENTITY_ENT)(fld, match, body)
181 #define FOREACH_ENTITY_ENT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(entity, fld, match, true, body)
182 .entity _FOREACH_ENTITY_FIND_entity_next;
183 noref string _FOREACH_ENTITY_FIND_entity_mutex;
184 #define FOREACH_ENTITY_ENT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, entity, fld, match, true, body)
185
186 #define FOREACH_ENTITY_STRING(fld, match, body) ORDERED(FOREACH_ENTITY_STRING)(fld, match, body)
187 #define FOREACH_ENTITY_STRING_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(string, fld, match, true, body)
188 .entity _FOREACH_ENTITY_FIND_string_next;
189 noref string _FOREACH_ENTITY_FIND_string_mutex;
190 #define FOREACH_ENTITY_STRING_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, string, fld, match, true, body)
191
192 #define FOREACH_ENTITY_CLASS(class, cond, body) ORDERED(FOREACH_ENTITY_CLASS)(class, cond, body)
193 #define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body) _FOREACH_ENTITY_FIND_ORDERED(string, classname, class, cond, body)
194 .entity _FOREACH_ENTITY_FIND_string_nextclazz;
195 noref string _FOREACH_ENTITY_FIND_string_clazzmutex;
196 #define FOREACH_ENTITY_CLASS_UNORDERED(class, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(clazz, string, classname, class, cond, body)