forked from SanderMertens/flecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflecs.h
13138 lines (11257 loc) · 395 KB
/
flecs.h
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define flecs_STATIC
/**
* @file flecs.h
* @brief Flecs public API.
*
* This file contains the public API for Flecs.
*/
#ifndef FLECS_H
#define FLECS_H
/* FLECS_LEGACY should be defined when building for C89 */
// #define FLECS_LEGACY
/* FLECS_NO_CPP should be defined when building for C++ without the C++ API */
// #define FLECS_NO_CPP
/* FLECS_CUSTOM_BUILD should be defined when manually selecting features */
// #define FLECS_CUSTOM_BUILD
#ifndef FLECS_CUSTOM_BUILD
/* Modules */
#define FLECS_SYSTEM
#define FLECS_PIPELINE
#define FLECS_TIMER
/* Addons */
#define FLECS_BULK
#define FLECS_DBG
#define FLECS_MODULE
#define FLECS_QUEUE
#define FLECS_READER_WRITER
#define FLECS_SNAPSHOT
#define FLECS_DIRECT_ACCESS
#define FLECS_STATS
#endif
/* Set to double or int to increase accuracy of time keeping. Note that when
* using an integer type, an application has to provide the delta_time values
* to the progress() function, as the code that measures time requires a
* floating point type. */
#ifndef FLECS_FLOAT
#define FLECS_FLOAT float
#endif
/**
* @file api_defines.h
* @brief Supporting defines for the public API.
*
* This file contains constants / macro's that are typically not used by an
* application but support the public API, and therefore must be exposed. This
* header should not be included by itself.
*/
#ifndef FLECS_API_DEFINES_H
#define FLECS_API_DEFINES_H
/* Standard library dependencies */
#include <time.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
/* Non-standard but required. If not provided by platform, add manually. */
#include <stdint.h>
/* Contains macro's for importing / exporting symbols */
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef FLECS_BAKE_CONFIG_H
#define FLECS_BAKE_CONFIG_H
/* Headers of public dependencies */
/* No dependencies */
/* Convenience macro for exporting symbols */
#ifndef flecs_STATIC
#if flecs_EXPORTS && (defined(_MSC_VER) || defined(__MINGW32__))
#define FLECS_API __declspec(dllexport)
#elif flecs_EXPORTS
#define FLECS_API __attribute__((__visibility__("default")))
#elif defined _MSC_VER
#define FLECS_API __declspec(dllimport)
#else
#define FLECS_API
#endif
#else
#define FLECS_API
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __BAKE_LEGACY__
#define FLECS_LEGACY
#endif
////////////////////////////////////////////////////////////////////////////////
//// Language support defines
////////////////////////////////////////////////////////////////////////////////
#ifndef FLECS_LEGACY
#include <stdbool.h>
#endif
/* The API uses the native bool type in C++, or a custom one in C */
#if !defined(__cplusplus) && !defined(__bool_true_false_are_defined)
#undef bool
#undef true
#undef false
typedef char bool;
#define false 0
#define true !false
#endif
typedef uint32_t ecs_flags32_t;
typedef uint64_t ecs_flags64_t;
/* Keep unsigned integers out of the codebase as they do more harm than good */
typedef int32_t ecs_size_t;
#define ECS_SIZEOF(T) (ecs_size_t)sizeof(T)
/* Use alignof in C++, or a trick in C. */
#ifdef __cplusplus
#define ECS_ALIGNOF(T) (int64_t)alignof(T)
#elif defined(_MSC_VER)
#define ECS_ALIGNOF(T) (int64_t)__alignof(T)
#elif defined(__GNUC__)
#define ECS_ALIGNOF(T) (int64_t)__alignof__(T)
#else
#define ECS_ALIGNOF(T) ((int64_t)&((struct { char c; T d; } *)0)->d)
#endif
#if defined(__GNUC__)
#define ECS_UNUSED __attribute__((unused))
#else
#define ECS_UNUSED
#endif
#define ECS_ALIGN(size, alignment) (ecs_size_t)((((((size_t)size) - 1) / ((size_t)alignment)) + 1) * ((size_t)alignment))
/* Simple utility for determining the max of two values */
#define ECS_MAX(a, b) ((a > b) ? a : b)
////////////////////////////////////////////////////////////////////////////////
//// Reserved component ids
////////////////////////////////////////////////////////////////////////////////
/** Builtin component ids */
#define FLECS__EEcsComponent (1)
#define FLECS__EEcsComponentLifecycle (2)
#define FLECS__EEcsType (3)
#define FLECS__EEcsName (6)
/** System module component ids */
#define FLECS__EEcsTrigger (4)
#define FLECS__EEcsSystem (5)
#define FLECS__EEcsTickSource (7)
#define FLECS__EEcsSignatureExpr (8)
#define FLECS__EEcsSignature (9)
#define FLECS__EEcsQuery (10)
#define FLECS__EEcsIterAction (11)
#define FLECS__EEcsContext (12)
/** Pipeline module component ids */
#define FLECS__EEcsPipelineQuery (13)
/** Timer module component ids */
#define FLECS__EEcsTimer (14)
#define FLECS__EEcsRateFilter (15)
////////////////////////////////////////////////////////////////////////////////
//// Entity id macro's
////////////////////////////////////////////////////////////////////////////////
#define ECS_ROLE_MASK ((ecs_entity_t)0xFF << 56)
#define ECS_ENTITY_MASK ((uint64_t)0xFFFFFFFF)
#define ECS_GENERATION_MASK ((uint64_t)0xFFFF << 32)
#define ECS_GENERATION(e) ((e & ECS_GENERATION_MASK) >> 32)
#define ECS_GENERATION_INC(e) ((e & ~ECS_GENERATION_MASK) | ((ECS_GENERATION(e) + 1) << 32))
#define ECS_COMPONENT_MASK ((ecs_entity_t)~ECS_ROLE_MASK)
#define ECS_TYPE_ROLE_START ECS_CHILDOF
#define ECS_HAS_ROLE(e, role) ((e & ECS_ROLE_MASK) == ECS_##role)
////////////////////////////////////////////////////////////////////////////////
//// Convert between C typenames and variables
////////////////////////////////////////////////////////////////////////////////
/** Translate C type to ecs_type_t variable. */
#define ecs_type(T) FLECS__T##T
/** Translate C type to entity id. */
#define ecs_typeid(T) FLECS__E##T
/* DEPRECATED: old way to get entity id from type */
#define ecs_entity(T) ecs_typeid(T)
/** Translate C type to module struct. */
#define ecs_module(T) FLECS__M##T
/** Translate C type to module struct. */
#define ecs_module_ptr(T) FLECS__M##T##_ptr
/** Translate C type to module struct. */
#define ecs_iter_action(T) FLECS__F##T
#ifndef FLECS_LEGACY
////////////////////////////////////////////////////////////////////////////////
//// Utilities for working with trait identifiers
////////////////////////////////////////////////////////////////////////////////
#define ecs_entity_t_lo(value) ((uint32_t)(value))
#define ecs_entity_t_hi(value) ((uint32_t)((value) >> 32))
#define ecs_entity_t_comb(v1, v2) (((uint64_t)(v2) << 32) + (uint32_t)(v1))
#define ecs_trait(comp, trait) ECS_TRAIT | ecs_entity_t_comb(comp, trait)
////////////////////////////////////////////////////////////////////////////////
//// Convenience macro's for ctor, dtor, move and copy
////////////////////////////////////////////////////////////////////////////////
/* Constructor / destructor convenience macro */
#define ECS_XTOR_IMPL(type, postfix, var, ...)\
void type##_##postfix(\
ecs_world_t *world,\
ecs_entity_t component,\
const ecs_entity_t *entity_ptr,\
void *_ptr,\
size_t _size,\
int32_t _count,\
void *ctx)\
{\
(void)world;\
(void)component;\
(void)entity_ptr;\
(void)_ptr;\
(void)_size;\
(void)_count;\
(void)ctx;\
for (int32_t i = 0; i < _count; i ++) {\
ecs_entity_t entity = entity_ptr[i];\
type *var = &((type*)_ptr)[i];\
(void)entity;\
(void)var;\
__VA_ARGS__\
}\
}
/* Copy convenience macro */
#define ECS_COPY_IMPL(type, dst_var, src_var, ...)\
void type##_##copy(\
ecs_world_t *world,\
ecs_entity_t component,\
const ecs_entity_t *dst_entities,\
const ecs_entity_t *src_entities,\
void *_dst_ptr,\
const void *_src_ptr,\
size_t _size,\
int32_t _count,\
void *ctx)\
{\
(void)world;\
(void)component;\
(void)dst_entities;\
(void)src_entities;\
(void)_dst_ptr;\
(void)_src_ptr;\
(void)_size;\
(void)_count;\
(void)ctx;\
for (int32_t i = 0; i < _count; i ++) {\
ecs_entity_t dst_entity = dst_entities[i];\
ecs_entity_t src_entity = src_entities[i];\
type *dst_var = &((type*)_dst_ptr)[i];\
type *src_var = &((type*)_src_ptr)[i];\
(void)dst_entity;\
(void)src_entity;\
(void)dst_var;\
(void)src_var;\
__VA_ARGS__\
}\
}
/* Move convenience macro */
#define ECS_MOVE_IMPL(type, dst_var, src_var, ...)\
void type##_##move(\
ecs_world_t *world,\
ecs_entity_t component,\
const ecs_entity_t *dst_entities,\
const ecs_entity_t *src_entities,\
void *_dst_ptr,\
void *_src_ptr,\
size_t _size,\
int32_t _count,\
void *ctx)\
{\
(void)world;\
(void)component;\
(void)dst_entities;\
(void)src_entities;\
(void)_dst_ptr;\
(void)_src_ptr;\
(void)_size;\
(void)_count;\
(void)ctx;\
for (int32_t i = 0; i < _count; i ++) {\
ecs_entity_t dst_entity = dst_entities[i];\
ecs_entity_t src_entity = src_entities[i];\
type *dst_var = &((type*)_dst_ptr)[i];\
type *src_var = &((type*)_src_ptr)[i];\
(void)dst_entity;\
(void)src_entity;\
(void)dst_var;\
(void)src_var;\
__VA_ARGS__\
}\
}
#endif
#ifdef __cplusplus
}
#endif
#endif
/**
* @file vector.h
* @brief Vector datastructure.
*
* This is an implementation of a simple vector type. The vector is allocated in
* a single block of memory, with the element count, and allocated number of
* elements encoded in the block. As this vector is used for user-types it has
* been designed to support alignments higher than 8 bytes. This makes the size
* of the vector header variable in size. To reduce the overhead associated with
* retrieving or computing this size, the functions are wrapped in macro calls
* that compute the header size at compile time.
*
* The API provides a number of _t macro's, which accept a size and alignment.
* These macro's are used when no compile-time type is available.
*
* The vector guarantees contiguous access to its elements. When an element is
* removed from the vector, the last element is copied to the removed element.
*
* The API requires passing in the type of the vector. This type is used to test
* whether the size of the provided type equals the size of the type with which
* the vector was created. In release mode this check is not performed.
*
* When elements are added to the vector, it will automatically resize to the
* next power of two. This can change the pointer of the vector, which is why
* operations that can increase the vector size, accept a double pointer to the
* vector.
*/
#ifndef FLECS_VECTOR_H
#define FLECS_VECTOR_H
#ifdef __cplusplus
extern "C" {
#endif
/* Public, so we can do compile-time header size calculation */
struct ecs_vector_t {
int32_t count;
int32_t size;
#ifndef NDEBUG
int64_t elem_size;
#endif
};
/* Compute the header size of the vector from size & alignment */
#define ECS_VECTOR_U(size, alignment) size, ECS_MAX(ECS_SIZEOF(ecs_vector_t), alignment)
/* Compute the header size of the vector from a provided compile-time type */
#define ECS_VECTOR_T(T) ECS_VECTOR_U(ECS_SIZEOF(T), ECS_ALIGNOF(T))
/* Utility macro's for creating vector on stack */
#ifndef NDEBUG
#define ECS_VECTOR_VALUE(T, elem_count)\
{\
.elem_size = (int32_t)(ECS_SIZEOF(T)),\
.count = elem_count,\
.size = elem_count\
}
#else
#define ECS_VECTOR_VALUE(T, elem_count)\
{\
.count = elem_count,\
.size = elem_count\
}
#endif
#define ECS_VECTOR_DECL(name, T, elem_count)\
struct {\
union {\
ecs_vector_t vector;\
uint64_t align;\
} header;\
T array[elem_count];\
} __##name##_value = {\
.header.vector = ECS_VECTOR_VALUE(T, elem_count)\
};\
const ecs_vector_t *name = (ecs_vector_t*)&__##name##_value
#define ECS_VECTOR_IMPL(name, T, elems, elem_count)\
ecs_os_memcpy(__##name##_value.array, elems, sizeof(T) * elem_count)
#define ECS_VECTOR_STACK(name, T, elems, elem_count)\
ECS_VECTOR_DECL(name, T, elem_count);\
ECS_VECTOR_IMPL(name, T, elems, elem_count)
typedef struct ecs_vector_t ecs_vector_t;
typedef int (*ecs_comparator_t)(
const void* p1,
const void *p2);
/** Create new vector. */
FLECS_API
ecs_vector_t* _ecs_vector_new(
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_new(T, elem_count) \
_ecs_vector_new(ECS_VECTOR_T(T), elem_count)
#define ecs_vector_new_t(size, alignment, elem_count) \
_ecs_vector_new(ECS_VECTOR_U(size, alignment), elem_count)
/* Create new vector, initialize it with provided array */
FLECS_API
ecs_vector_t* _ecs_vector_from_array(
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count,
void *array);
#define ecs_vector_from_array(T, elem_count, array)\
_ecs_vector_from_array(ECS_VECTOR_T(T), elem_count, array)
/* Initialize vector with zero's */
FLECS_API
void _ecs_vector_zero(
ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset);
#define ecs_vector_zero(vector, T) \
_ecs_vector_zero(vector, ECS_VECTOR_T(T))
/** Free vector */
FLECS_API
void ecs_vector_free(
ecs_vector_t *vector);
/** Clear values in vector */
FLECS_API
void ecs_vector_clear(
ecs_vector_t *vector);
/** Assert when the provided size does not match the vector type. */
FLECS_API
void ecs_vector_assert_size(
ecs_vector_t* vector_inout,
ecs_size_t elem_size);
/** Assert when the provided alignment does not match the vector type. */
FLECS_API
void ecs_vector_assert_alignment(
ecs_vector_t* vector,
ecs_size_t elem_alignment);
/** Add element to vector. */
FLECS_API
void* _ecs_vector_add(
ecs_vector_t **array_inout,
ecs_size_t elem_size,
int16_t offset);
#define ecs_vector_add(vector, T) \
((T*)_ecs_vector_add(vector, ECS_VECTOR_T(T)))
#define ecs_vector_add_t(vector, size, alignment) \
_ecs_vector_add(vector, ECS_VECTOR_U(size, alignment))
/** Add n elements to the vector. */
FLECS_API
void* _ecs_vector_addn(
ecs_vector_t **array_inout,
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_addn(vector, T, elem_count) \
((T*)_ecs_vector_addn(vector, ECS_VECTOR_T(T), elem_count))
#define ecs_vector_addn_t(vector, size, alignment, elem_count) \
_ecs_vector_addn(vector, ECS_VECTOR_U(size, alignment), elem_count)
/** Get element from vector. */
FLECS_API
void* _ecs_vector_get(
const ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset,
int32_t index);
#define ecs_vector_get(vector, T, index) \
((T*)_ecs_vector_get(vector, ECS_VECTOR_T(T), index))
#define ecs_vector_get_t(vector, size, alignment, index) \
_ecs_vector_get(vector, ECS_VECTOR_U(size, alignment), index)
/** Get last element from vector. */
FLECS_API
void* _ecs_vector_last(
const ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset);
#define ecs_vector_last(vector, T) \
(T*)_ecs_vector_last(vector, ECS_VECTOR_T(T))
/** Set minimum size for vector. If the current size of the vector is larger,
* the function will have no side effects. */
FLECS_API
int32_t _ecs_vector_set_min_size(
ecs_vector_t **array_inout,
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_set_min_size(vector, T, size) \
_ecs_vector_set_min_size(vector, ECS_VECTOR_T(T), size)
/** Set minimum count for vector. If the current count of the vector is larger,
* the function will have no side effects. */
FLECS_API
int32_t _ecs_vector_set_min_count(
ecs_vector_t **vector_inout,
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_set_min_count(vector, T, size) \
_ecs_vector_set_min_count(vector, ECS_VECTOR_T(T), size)
/** Remove last element. This operation requires no swapping of values. */
FLECS_API
void ecs_vector_remove_last(
ecs_vector_t *vector);
/** Remove last value, store last element in provided value. */
FLECS_API
bool _ecs_vector_pop(
ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset,
void *value);
#define ecs_vector_pop(vector, T, value) \
_ecs_vector_pop(vector, ECS_VECTOR_T(T), value)
/** Append element at specified index to another vector. */
FLECS_API
int32_t _ecs_vector_move_index(
ecs_vector_t **dst,
ecs_vector_t *src,
ecs_size_t elem_size,
int16_t offset,
int32_t index);
#define ecs_vector_move_index(dst, src, T, index) \
_ecs_vector_move_index(dst, src, ECS_VECTOR_T(T), index)
/** Remove element at specified index. Moves the last value to the index. */
FLECS_API
int32_t _ecs_vector_remove_index(
ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset,
int32_t index);
#define ecs_vector_remove_index(vector, T, index) \
_ecs_vector_remove_index(vector, ECS_VECTOR_T(T), index)
#define ecs_vector_remove_index_t(vector, size, alignment, index) \
_ecs_vector_remove_index(vector, ECS_VECTOR_U(size, alignment), index)
/** Shrink vector to make the size match the count. */
FLECS_API
void _ecs_vector_reclaim(
ecs_vector_t **vector,
ecs_size_t elem_size,
int16_t offset);
#define ecs_vector_reclaim(vector, T)\
_ecs_vector_reclaim(vector, ECS_VECTOR_T(T))
/** Grow size of vector with provided number of elements. */
FLECS_API
int32_t _ecs_vector_grow(
ecs_vector_t **vector,
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_grow(vector, T, size) \
_ecs_vector_grow(vector, ECS_VECTOR_T(T), size)
/** Set allocation size of vector. */
FLECS_API
int32_t _ecs_vector_set_size(
ecs_vector_t **vector,
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_set_size(vector, T, elem_count) \
_ecs_vector_set_size(vector, ECS_VECTOR_T(T), elem_count)
#define ecs_vector_set_size_t(vector, size, alignment, elem_count) \
_ecs_vector_set_size(vector, ECS_VECTOR_U(size, alignment), elem_count)
/** Set count of vector. If the size of the vector is smaller than the provided
* count, the vector is resized. */
FLECS_API
int32_t _ecs_vector_set_count(
ecs_vector_t **vector,
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_set_count(vector, T, elem_count) \
_ecs_vector_set_count(vector, ECS_VECTOR_T(T), elem_count)
#define ecs_vector_set_count_t(vector, size, alignment, elem_count) \
_ecs_vector_set_count(vector, ECS_VECTOR_U(size, alignment), elem_count)
/** Return number of elements in vector. */
FLECS_API
int32_t ecs_vector_count(
const ecs_vector_t *vector);
/** Return size of vector. */
FLECS_API
int32_t ecs_vector_size(
const ecs_vector_t *vector);
/** Return first element of vector. */
FLECS_API
void* _ecs_vector_first(
const ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset);
#define ecs_vector_first(vector, T) \
((T*)_ecs_vector_first(vector, ECS_VECTOR_T(T)))
#define ecs_vector_first_t(vector, size, alignment) \
_ecs_vector_first(vector, ECS_VECTOR_U(size, alignment))
/** Sort elements in vector. */
FLECS_API
void _ecs_vector_sort(
ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset,
ecs_comparator_t compare_action);
#define ecs_vector_sort(vector, T, compare_action) \
_ecs_vector_sort(vector, ECS_VECTOR_T(T), compare_action)
/** Return memory occupied by vector. */
FLECS_API
void _ecs_vector_memory(
const ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset,
int32_t *allocd,
int32_t *used);
#define ecs_vector_memory(vector, T, allocd, used) \
_ecs_vector_memory(vector, ECS_VECTOR_T(T), allocd, used)
#define ecs_vector_memory_t(vector, size, alignment, allocd, used) \
_ecs_vector_memory(vector, ECS_VECTOR_U(size, alignment), allocd, used)
/** Copy vectors */
FLECS_API
ecs_vector_t* _ecs_vector_copy(
const ecs_vector_t *src,
ecs_size_t elem_size,
int16_t offset);
#define ecs_vector_copy(src, T) \
_ecs_vector_copy(src, ECS_VECTOR_T(T))
#define ecs_vector_copy_t(src, size, alignment) \
_ecs_vector_copy(src, ECS_VECTOR_U(size, alignment))
#ifndef FLECS_LEGACY
#define ecs_vector_each(vector, T, var, ...)\
{\
int var##_i, var##_count = ecs_vector_count(vector);\
T* var##_array = ecs_vector_first(vector, T);\
for (var##_i = 0; var##_i < var##_count; var##_i ++) {\
T* var = &var##_array[var##_i];\
__VA_ARGS__\
}\
}
#endif
#ifdef __cplusplus
}
#endif
/** C++ wrapper for vector class. */
#ifdef __cplusplus
#ifndef FLECS_NO_CPP
#include <iostream>
namespace flecs {
template <typename T>
class vector_iterator
{
public:
explicit vector_iterator(T* value, int index) {
m_value = value;
m_index = index;
}
bool operator!=(vector_iterator const& other) const
{
return m_index != other.m_index;
}
T const& operator*() const
{
return m_value[m_index];
}
vector_iterator& operator++()
{
++m_index;
return *this;
}
private:
T* m_value;
int m_index;
};
template <typename T>
class vector {
public:
explicit vector(ecs_vector_t *v) : m_vector( v ) { }
vector(int32_t count = 0) : m_vector( nullptr ) {
if (count) {
init(count);
}
}
vector(std::initializer_list<T> elems) : m_vector( nullptr) {
init(elems.size());
*this = elems;
}
void operator=(std::initializer_list<T> elems) {
for (auto elem : elems) {
this->add(elem);
}
}
T& operator[](size_t index) {
return ecs_vector_get(m_vector, T, index)[0];
}
vector_iterator<T> begin() {
return vector_iterator<T>(
ecs_vector_first(m_vector, T), 0);
}
vector_iterator<T> end() {
return vector_iterator<T>(
ecs_vector_last(m_vector, T),
ecs_vector_count(m_vector));
}
void clear() {
ecs_vector_clear(m_vector);
}
void add(T& value) {
T* elem = ecs_vector_add(&m_vector, T);
*elem = value;
}
void add(T&& value) {
T* elem = ecs_vector_add(&m_vector, T);
*elem = value;
}
T& get(int32_t index) {
return ecs_vector_get(m_vector, T, index);
}
T& first() {
return ecs_vector_first(m_vector, T);
}
T& last() {
return ecs_vector_last(m_vector, T);
}
int32_t count() {
return ecs_vector_count(m_vector);
}
int32_t size() {
return ecs_vector_size(m_vector);
}
ecs_vector_t *ptr() {
return m_vector;
}
void ptr(ecs_vector_t *ptr) {
m_vector = ptr;
}
private:
void init(int32_t count) {
m_vector = ecs_vector_new(T, count);
}
ecs_vector_t *m_vector;
};
}
#endif
#endif
#endif
/**
* @file sparse.h
* @brief Sparse set datastructure.
*
* This is an implementation of a paged sparse set that stores the payload in
* the sparse array.
*
* A sparse set has a dense and a sparse array. The sparse array is directly
* indexed by a 64 bit identifier. The sparse element is linked with a dense
* element, which allows for liveliness checking. The liveliness check itself
* can be performed by doing (psuedo code):
* dense[sparse[sparse_id].dense] == sparse_id
*
* To ensure that the sparse array doesn't have to grow to a large size when
* using large sparse_id's, the sparse set uses paging. This cuts up the array
* into several pages of 4096 elements. When an element is set, the sparse set
* ensures that the corresponding page is created. The page associated with an
* id is determined by shifting a bit 12 bits to the right.
*
* The sparse set keeps track of a generation count per id, which is increased
* each time an id is deleted. The generation is encoded in the returned id.
*
* This sparse set implementation stores payload in the sparse array, which is
* not typical. The reason for this is to guarantee that (in combination with
* paging) the returned payload pointers are stable. This allows for various
* optimizations in the parts of the framework that uses the sparse set.
*
* The sparse set has been designed so that new ids can be generated in bulk, in
* an O(1) operation. The way this works is that once a dense-sparse pair is
* created, it is never unpaired. Instead it is moved to the end of the dense
* array, and the sparse set stores an additional count to keep track of the
* last alive id in the sparse set. To generate new ids in bulk, the sparse set
* only needs to increase this count by the number of requested ids.
*/
#ifndef FLECS_SPARSE_H
#define FLECS_SPARSE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ecs_sparse_t ecs_sparse_t;
/** Create new sparse set */
FLECS_API ecs_sparse_t* _ecs_sparse_new(
ecs_size_t elem_size);
#define ecs_sparse_new(type)\
_ecs_sparse_new(sizeof(type))
/** Set id source. This allows the sparse set to use an external variable for
* issuing and increasing new ids. */
FLECS_API void ecs_sparse_set_id_source(
ecs_sparse_t *sparse,
uint64_t *id_source);
/** Free sparse set */
FLECS_API void ecs_sparse_free(
ecs_sparse_t *sparse);
/** Remove all elements from sparse set */
FLECS_API void ecs_sparse_clear(
ecs_sparse_t *sparse);
/** Add element to sparse set, this generates or recycles an id */
FLECS_API void* _ecs_sparse_add(
ecs_sparse_t *sparse,
ecs_size_t elem_size);
#define ecs_sparse_add(sparse, type)\
((type*)_ecs_sparse_add(sparse, sizeof(type)))
/** Get last issued id. */
FLECS_API uint64_t ecs_sparse_last_id(
ecs_sparse_t *sparse);
/** Generate or recycle a new id. */
FLECS_API uint64_t ecs_sparse_new_id(
ecs_sparse_t *sparse);
/** Generate or recycle new ids in bulk. The returned pointer points directly to
* the internal dense array vector with sparse ids. Operations on the sparse set
* can (and likely will) modify the contents of the buffer. */
FLECS_API const uint64_t* ecs_sparse_new_ids(
ecs_sparse_t *sparse,
int32_t count);
/** Remove an element */
FLECS_API void ecs_sparse_remove(
ecs_sparse_t *sparse,
uint64_t index);
/** Remove an element, return pointer to the value in the sparse array */
FLECS_API void* _ecs_sparse_remove_get(
ecs_sparse_t *sparse,
ecs_size_t elem_size,
uint64_t index);
#define ecs_sparse_remove_get(sparse, type, index)\
((type*)_ecs_sparse_remove_get(sparse, sizeof(type), index))
/** Override the generation count for a specific id */
FLECS_API void ecs_sparse_set_generation(
ecs_sparse_t *sparse,
uint64_t index);
/** Check whether an id has ever been issued. */
FLECS_API bool ecs_sparse_exists(
ecs_sparse_t *sparse,
uint64_t index);
/** Test if id is alive, which requires the generation count tp match. */
FLECS_API bool ecs_sparse_is_alive(
const ecs_sparse_t *sparse,
uint64_t index);
/** Get value from sparse set by dense id. This function is useful in
* combination with ecs_sparse_count for iterating all values in the set. */
FLECS_API void* _ecs_sparse_get(
const ecs_sparse_t *sparse,
ecs_size_t elem_size,