forked from SanderMertens/flecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
352 lines (305 loc) · 9.18 KB
/
search.c
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
/**
* @file search.c
* @brief Search functions to find (component) ids in table types.
*
* Search functions are used to find the column index of a (component) id in a
* table. Additionally, search functions implement the logic for finding a
* component id by following a relationship upwards.
*/
#include "private_api.h"
static
int32_t flecs_type_search(
const ecs_table_t *table,
ecs_id_t search_id,
ecs_id_record_t *idr,
ecs_id_t *ids,
ecs_id_t *id_out,
ecs_table_record_t **tr_out)
{
ecs_table_record_t *tr = ecs_table_cache_get(&idr->cache, table);
if (tr) {
int32_t r = tr->column;
if (tr_out) tr_out[0] = tr;
if (id_out) {
if (ECS_PAIR_FIRST(search_id) == EcsUnion) {
id_out[0] = ids[r];
} else {
id_out[0] = flecs_to_public_id(ids[r]);
}
}
return r;
}
return -1;
}
static
int32_t flecs_type_offset_search(
int32_t offset,
ecs_id_t id,
ecs_id_t *ids,
int32_t count,
ecs_id_t *id_out)
{
ecs_assert(ids != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(count > 0, ECS_INTERNAL_ERROR, NULL);
ecs_assert(offset > 0, ECS_INTERNAL_ERROR, NULL);
ecs_assert(id != 0, ECS_INVALID_PARAMETER, NULL);
while (offset < count) {
ecs_id_t type_id = ids[offset ++];
if (ecs_id_match(type_id, id)) {
if (id_out) {
id_out[0] = flecs_to_public_id(type_id);
}
return offset - 1;
}
}
return -1;
}
static
bool flecs_type_can_inherit_id(
const ecs_world_t *world,
const ecs_table_t *table,
const ecs_id_record_t *idr,
ecs_id_t id)
{
ecs_assert(idr != NULL, ECS_INTERNAL_ERROR, NULL);
if (idr->flags & EcsIdDontInherit) {
return false;
}
if (idr->flags & EcsIdExclusive) {
if (ECS_HAS_ID_FLAG(id, PAIR)) {
ecs_entity_t er = ECS_PAIR_FIRST(id);
if (flecs_table_record_get(
world, table, ecs_pair(er, EcsWildcard)))
{
return false;
}
}
}
return true;
}
static
int32_t flecs_type_search_relation(
const ecs_world_t *world,
const ecs_table_t *table,
int32_t offset,
ecs_id_t id,
ecs_id_record_t *idr,
ecs_id_t rel,
ecs_id_record_t *idr_r,
bool self,
ecs_entity_t *subject_out,
ecs_id_t *id_out,
ecs_table_record_t **tr_out)
{
ecs_type_t type = table->type;
ecs_id_t *ids = type.array;
int32_t count = type.count;
if (self) {
if (offset) {
int32_t r = flecs_type_offset_search(offset, id, ids, count, id_out);
if (r != -1) {
return r;
}
} else {
int32_t r = flecs_type_search(table, id, idr, ids, id_out, tr_out);
if (r != -1) {
return r;
}
}
}
ecs_flags32_t flags = table->flags;
if ((flags & EcsTableHasPairs) && rel) {
bool is_a = rel == ecs_pair(EcsIsA, EcsWildcard);
if (is_a) {
if (!(flags & EcsTableHasIsA)) {
return -1;
}
idr_r = world->idr_isa_wildcard;
if (!flecs_type_can_inherit_id(world, table, idr, id)) {
return -1;
}
}
if (!idr_r) {
idr_r = flecs_id_record_get(world, rel);
if (!idr_r) {
return -1;
}
}
ecs_id_t id_r;
int32_t r, r_column;
if (offset) {
r_column = flecs_type_offset_search(offset, rel, ids, count, &id_r);
} else {
r_column = flecs_type_search(table, id, idr_r, ids, &id_r, 0);
}
while (r_column != -1) {
ecs_entity_t obj = ECS_PAIR_SECOND(id_r);
ecs_assert(obj != 0, ECS_INTERNAL_ERROR, NULL);
ecs_record_t *rec = flecs_entities_get_any(world, obj);
ecs_assert(rec != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_table_t *obj_table = rec->table;
if (obj_table) {
ecs_assert(obj_table != table, ECS_CYCLE_DETECTED, NULL);
r = flecs_type_search_relation(world, obj_table, 0, id, idr,
rel, idr_r, true, subject_out, id_out, tr_out);
if (r != -1) {
if (subject_out && !subject_out[0]) {
subject_out[0] = ecs_get_alive(world, obj);
}
return r_column;
}
if (!is_a) {
r = flecs_type_search_relation(world, obj_table, 0, id, idr,
ecs_pair(EcsIsA, EcsWildcard), world->idr_isa_wildcard,
true, subject_out, id_out, tr_out);
if (r != -1) {
if (subject_out && !subject_out[0]) {
subject_out[0] = ecs_get_alive(world, obj);
}
return r_column;
}
}
}
r_column = flecs_type_offset_search(
r_column + 1, rel, ids, count, &id_r);
}
}
return -1;
}
int32_t flecs_search_relation_w_idr(
const ecs_world_t *world,
const ecs_table_t *table,
int32_t offset,
ecs_id_t id,
ecs_entity_t rel,
ecs_flags32_t flags,
ecs_entity_t *subject_out,
ecs_id_t *id_out,
struct ecs_table_record_t **tr_out,
ecs_id_record_t *idr)
{
if (!table) return -1;
ecs_poly_assert(world, ecs_world_t);
ecs_assert(id != 0, ECS_INVALID_PARAMETER, NULL);
flags = flags ? flags : (EcsSelf|EcsUp);
if (!idr) {
idr = flecs_query_id_record_get(world, id);
if (!idr) {
return -1;
}
}
if (subject_out) subject_out[0] = 0;
if (!(flags & EcsUp)) {
if (offset) {
return ecs_search_offset(world, table, offset, id, id_out);
} else {
return flecs_type_search(
table, id, idr, table->type.array, id_out, tr_out);
}
}
int32_t result = flecs_type_search_relation(world, table, offset, id, idr,
ecs_pair(rel, EcsWildcard), NULL, flags & EcsSelf, subject_out,
id_out, tr_out);
return result;
}
int32_t ecs_search_relation(
const ecs_world_t *world,
const ecs_table_t *table,
int32_t offset,
ecs_id_t id,
ecs_entity_t rel,
ecs_flags32_t flags,
ecs_entity_t *subject_out,
ecs_id_t *id_out,
struct ecs_table_record_t **tr_out)
{
if (!table) return -1;
ecs_poly_assert(world, ecs_world_t);
ecs_assert(id != 0, ECS_INVALID_PARAMETER, NULL);
flags = flags ? flags : (EcsSelf|EcsUp);
if (subject_out) subject_out[0] = 0;
if (!(flags & EcsUp)) {
return ecs_search_offset(world, table, offset, id, id_out);
}
ecs_id_record_t *idr = flecs_query_id_record_get(world, id);
if (!idr) {
return -1;
}
int32_t result = flecs_type_search_relation(world, table, offset, id, idr,
ecs_pair(rel, EcsWildcard), NULL, flags & EcsSelf, subject_out,
id_out, tr_out);
return result;
}
int32_t ecs_search(
const ecs_world_t *world,
const ecs_table_t *table,
ecs_id_t id,
ecs_id_t *id_out)
{
if (!table) return -1;
ecs_poly_assert(world, ecs_world_t);
ecs_assert(id != 0, ECS_INVALID_PARAMETER, NULL);
ecs_id_record_t *idr = flecs_query_id_record_get(world, id);
if (!idr) {
return -1;
}
ecs_type_t type = table->type;
ecs_id_t *ids = type.array;
return flecs_type_search(table, id, idr, ids, id_out, 0);
}
int32_t ecs_search_offset(
const ecs_world_t *world,
const ecs_table_t *table,
int32_t offset,
ecs_id_t id,
ecs_id_t *id_out)
{
if (!offset) {
ecs_poly_assert(world, ecs_world_t);
return ecs_search(world, table, id, id_out);
}
if (!table) return -1;
ecs_type_t type = table->type;
ecs_id_t *ids = type.array;
int32_t count = type.count;
return flecs_type_offset_search(offset, id, ids, count, id_out);
}
static
int32_t flecs_relation_depth_walk(
const ecs_world_t *world,
const ecs_id_record_t *idr,
const ecs_table_t *first,
const ecs_table_t *table)
{
int32_t result = 0;
const ecs_table_record_t *tr = flecs_id_record_get_table(idr, table);
if (!tr) {
return 0;
}
int32_t i = tr->column, end = i + tr->count;
for (; i != end; i ++) {
ecs_entity_t o = ecs_pair_second(world, table->type.array[i]);
ecs_assert(o != 0, ECS_INTERNAL_ERROR, NULL);
ecs_table_t *ot = ecs_get_table(world, o);
if (!ot) {
continue;
}
ecs_assert(ot != first, ECS_CYCLE_DETECTED, NULL);
int32_t cur = flecs_relation_depth_walk(world, idr, first, ot);
if (cur > result) {
result = cur;
}
}
return result + 1;
}
int32_t flecs_relation_depth(
const ecs_world_t *world,
ecs_entity_t r,
const ecs_table_t *table)
{
ecs_id_record_t *idr = flecs_id_record_get(world, ecs_pair(r, EcsWildcard));
if (!idr) {
return 0;
}
return flecs_relation_depth_walk(world, idr, table, table);
}