70 #define DG_DYNARR_IMPLEMENTATION 71 #include "DG_dynarr.h" 75 void printIntArr(MyIntArrType* arr,
const char*
name)
78 printf(
"%s = {",
name);
80 printf(
" %d", arr->p[0]);
82 printf(
", %d", arr->p[i]);
88 MyIntArrType
a1 = {0};
98 for(
size_t i=0; i<3; ++i)
99 addedElements[i] = i+5;
101 printIntArr(&
a1,
"a1");
110 printIntArr(&
a2,
"a2");
113 printIntArr(&
a2,
"a2");
116 printIntArr(&
a2,
"a2");
119 printIntArr(&
a1,
"a1");
122 printf(
"x = %d\n",
x);
123 printIntArr(&
a1,
"a1");
139 #ifndef DG_DYNARR_INDEX_CHECK_LEVEL 146 #define DG_DYNARR_INDEX_CHECK_LEVEL 3 153 #ifndef DG_DYNARR_ASSERT 155 #define DG_DYNARR_ASSERT(cond, msg) assert((cond) && msg) 190 #ifndef DG_DYNARR_DEF 192 #define DG_DYNARR_DEF 198 #ifndef DG_DYNARR_INLINE 199 #define DG_DYNARR_INLINE static INLINE 204 #ifndef DG_DYNARR_NO_SHORTNAMES 209 #define DA_TYPEDEF(TYPE, NewArrayTypeName) \ 210 DG_DYNARR_TYPEDEF(TYPE, NewArrayTypeName) 228 #define da_init_external(a, buf, buf_cap) \ 229 dg_dynarr_init_external(a, buf, buf_cap) 240 #define da_push(a, v) \ 246 #define da_add(a, v) \ 252 #define da_addn(a, vals, n) \ 253 dg_dynarr_addn(a, vals, n) 258 #define da_addn_zeroed(a, n) \ 259 dg_dynarr_addn_zeroed(a, n) 264 #define da_addn_uninit(a, n) \ 265 dg_dynarr_addn_uninit(a, n) 269 #define da_insert(a, idx, v) \ 270 dg_dynarr_insert(a, idx, v) 276 #define da_insertn(a, idx, vals, n) \ 277 dg_dynarr_insertn(a, idx, vals, n) 282 #define da_insertn_zeroed(a, idx, n) \ 283 dg_dynarr_insertn_zeroed(a, idx, n) 288 #define da_insertn_uninit(a, idx, n) \ 289 dg_dynarr_insertn_uninit(a, idx, n) 292 #define da_set(a, idx, v) \ 293 dg_dynarr_set(a, idx, v) 299 #define da_setn(a, idx, vals, n) \ 300 dg_dynarr_setn(a, idx, vals, n) 303 #define da_delete(a, idx) \ 304 dg_dynarr_delete(a, idx) 307 #define da_deleten(a, idx, n) \ 308 dg_dynarr_deleten(a, idx, n) 311 #define da_deletefast(a, idx) \ 312 dg_dynarr_deletefast(a, idx) 315 #define da_deletenfast(a, idx, n) \ 316 dg_dynarr_deletenfast(a, idx, n) 321 #define da_clear(a) \ 328 #define da_setcount(a, cnt) \ 329 dg_dynarr_setcount(a, cnt) 334 #define da_reserve(a, cap) \ 335 dg_dynarr_reserve(a, cap) 343 #define da_shrink_to_fit(a) \ 344 dg_dynarr_shrink_to_fit(a) 359 #define da_lastptr(a) \ 365 #define da_get(a, idx) \ 371 #define da_getptr(a, idx) \ 372 dg_dynarr_getptr(a, idx) 377 #define da_begin(a) \ 390 #define da_count(a) \ 394 #define da_capacity(a) \ 395 dg_dynarr_capacity(a) 398 #define da_empty(a) \ 413 #define da_sort(a, cmp) \ 414 dg_dynarr_sort(a, cmp) 422 #define DG_DYNARR_TYPEDEF(TYPE, NewArrayTypeName) \ 423 typedef struct { TYPE* p; dg__dynarr_md md; } NewArrayTypeName; 428 #define dg_dynarr_init(a) \ 429 dg__dynarr_init((void**)&(a).p, &(a).md, NULL, 0) 434 #define dg_dynarr_init_external(a, buf, buf_cap) \ 435 dg__dynarr_init((void**)&(a).p, &(a).md, (buf), (buf_cap)) 441 #define dg_dynarr_free(a) \ 442 dg__dynarr_free((void**)&(a).p, &(a).md) 446 #define dg_dynarr_push(a, v) \ 447 (dg__dynarr_maybegrowadd(dg__dynarr_unp(a), 1) ? (((a).p[(a).md.cnt++] = (v)),0) : 0) 452 #define dg_dynarr_add(a, v) \ 453 dg_dynarr_push((a), (v)) 458 #define dg_dynarr_addn(a, vals, n) do { \ 459 DG_DYNARR_ASSERT((vals)!=NULL, "Don't pass NULL als vals to dg_dynarr_addn!"); \ 460 if((vals)!=NULL && dg__dynarr_add(dg__dynarr_unp(a), n, 0)) { \ 461 size_t i_=(a).md.cnt-(n), v_=0; \ 462 while(i_<(a).md.cnt) (a).p[i_++]=(vals)[v_++]; \ 463 } } DG__DYNARR_WHILE0 468 #define dg_dynarr_addn_zeroed(a, n) \ 469 (dg__dynarr_add(dg__dynarr_unp(a), (n), 1) ? &(a).p[(a).md.cnt-(size_t)(n)] : NULL) 474 #define dg_dynarr_addn_uninit(a, n) \ 475 (dg__dynarr_add(dg__dynarr_unp(a), (n), 0) ? &(a).p[(a).md.cnt-(size_t)(n)] : NULL) 478 #define dg_dynarr_insert(a, idx, v) \ 479 (dg__dynarr_checkidxle((a),(idx)), \ 480 dg__dynarr_insert(dg__dynarr_unp(a), (idx), 1, 0), \ 481 (a).p[dg__dynarr_idx((a).md, (idx))] = (v)) 487 #define dg_dynarr_insertn(a, idx, vals, n) do { \ 488 DG_DYNARR_ASSERT((vals)!=NULL, "Don't pass NULL as vals to dg_dynarr_insertn!"); \ 489 dg__dynarr_checkidxle((a),(idx)); \ 490 if((vals)!=NULL && dg__dynarr_insert(dg__dynarr_unp(a), (idx), (n), 0)){ \ 491 size_t i_=(idx), v_=0, e_=(idx)+(n); \ 492 while(i_ < e_) (a).p[i_++] = (vals)[v_++]; \ 498 #define dg_dynarr_insertn_zeroed(a, idx, n) \ 499 (dg__dynarr_checkidxle((a),(idx)), \ 500 dg__dynarr_insert(dg__dynarr_unp(a), (idx), (n), 1) \ 501 ? &(a).p[dg__dynarr_idx((a).md, (idx))] : NULL) 506 #define dg_dynarr_insertn_uninit(a, idx, n) \ 507 (dg__dynarr_checkidxle((a),(idx)), \ 508 dg__dynarr_insert(dg__dynarr_unp(a), idx, n, 0) \ 509 ? &(a).p[dg__dynarr_idx((a).md, (idx))] : NULL) 512 #define dg_dynarr_set(a, idx, v) \ 513 (dg__dynarr_checkidx((a),(idx)), \ 514 (a).p[dg__dynarr_idx((a).md, (idx))] = (v)) 520 #define dg_dynarr_setn(a, idx, vals, n) do { \ 521 DG_DYNARR_ASSERT((vals)!=NULL, "Don't pass NULL as vals to dg_dynarr_setn!"); \ 522 size_t idx_=(idx); size_t end_=idx_+(size_t)n; \ 523 dg__dynarr_checkidx((a),idx_); dg__dynarr_checkidx((a),end_-1); \ 524 if((vals)!=NULL && idx_ < (a).md.cnt && end_ <= (a).md.cnt) { \ 526 while(idx_ < end_) (a).p[idx_++] = (vals)[v_++]; \ 530 #define dg_dynarr_delete(a, idx) \ 531 (dg__dynarr_checkidx((a),(idx)), dg__dynarr_delete(dg__dynarr_unp(a), (idx), 1)) 534 #define dg_dynarr_deleten(a, idx, n) \ 535 (dg__dynarr_checkidx((a),(idx)), dg__dynarr_delete(dg__dynarr_unp(a), (idx), (n))) 539 #define dg_dynarr_deletefast(a, idx) \ 540 (dg__dynarr_checkidx((a),(idx)), dg__dynarr_deletefast(dg__dynarr_unp(a), (idx), 1)) 543 #define dg_dynarr_deletenfast(a, idx, n) \ 544 (dg__dynarr_checkidx((a),(idx)), dg__dynarr_deletefast(dg__dynarr_unp(a), idx, n)) 550 #define dg_dynarr_clear(a) \ 557 #define dg_dynarr_setcount(a, n) \ 558 (dg__dynarr_maybegrow(dg__dynarr_unp(a), (n)) ? ((a).md.cnt = (n)) : 0) 563 #define dg_dynarr_reserve(a, cap) \ 564 dg__dynarr_maybegrow(dg__dynarr_unp(a), (cap)) 572 #define dg_dynarr_shrink_to_fit(a) \ 573 dg__dynarr_shrink_to_fit(dg__dynarr_unp(a)) 575 #if (DG_DYNARR_INDEX_CHECK_LEVEL == 1) || (DG_DYNARR_INDEX_CHECK_LEVEL == 3) 578 #define dg_dynarr_pop(a) \ 579 (dg__dynarr_check_notempty((a), "Don't pop an empty array!"), \ 580 (a).p[((a).md.cnt > 0) ? (--(a).md.cnt) : 0]) 583 #define dg_dynarr_last(a) \ 584 (dg__dynarr_check_notempty((a), "Don't call da_last() on an empty array!"), \ 585 (a).p[((a).md.cnt > 0) ? ((a).md.cnt-1) : 0]) 587 #elif (DG_DYNARR_INDEX_CHECK_LEVEL == 0) || (DG_DYNARR_INDEX_CHECK_LEVEL == 2) 590 #define dg_dynarr_pop(a) \ 591 (dg__dynarr_check_notempty((a), "Don't pop an empty array!"), \ 595 #define dg_dynarr_last(a) \ 596 (dg__dynarr_check_notempty((a), "Don't call da_last() on an empty array!"), \ 600 #error Invalid index check level DG_DYNARR_INDEX_CHECK_LEVEL (must be 0-3) ! 607 #define dg_dynarr_lastptr(a) \ 608 (((a).md.cnt > 0) ? ((a).p + (a).md.cnt - 1) : NULL) 613 #define dg_dynarr_get(a, idx) \ 614 (dg__dynarr_checkidx((a),(idx)), (a).p[dg__dynarr_idx((a).md, (idx))]) 620 #define dg_dynarr_getptr(a, idx) \ 621 (dg__dynarr_checkidx((a),(idx)), \ 622 ((size_t)(idx) < (a).md.cnt) ? ((a).p+(size_t)(idx)) : NULL) 627 #define dg_dynarr_begin(a) \ 635 #define dg_dynarr_end(a) \ 639 #define dg_dynarr_count(a) \ 643 #define dg_dynarr_capacity(a) \ 644 ((a).md.cap & DG__DYNARR_SIZE_T_ALL_BUT_MSB) 647 #define dg_dynarr_empty(a) \ 658 #define dg_dynarr_oom(a) \ 664 #define dg_dynarr_sort(a, cmp) \ 665 qsort((a).p, (a).md.cnt, sizeof((a).p[0]), (cmp)) 694 #define dg__dynarr_unp(a) \ 695 (void**)&(a).p, &(a).md, sizeof((a).p[0]) 701 #if _MSC_VER >= 1400 // MSVC 2005 and newer 707 #define DG__DYNARR_WHILE0 \ 708 __pragma(warning(push)) \ 709 __pragma(warning(disable:4127)) \ 711 __pragma(warning(pop)) 713 #define DG__DYNARR_WHILE0 while(0,0) 718 #define DG__DYNARR_WHILE0 while(0) 722 #if (DG_DYNARR_INDEX_CHECK_LEVEL == 2) || (DG_DYNARR_INDEX_CHECK_LEVEL == 3) 724 #define dg__dynarr_checkidx(a,i) \ 725 DG_DYNARR_ASSERT((size_t)i < a.md.cnt, "index out of bounds!") 728 #define dg__dynarr_checkidxle(a,i) \ 729 DG_DYNARR_ASSERT((size_t)i <= a.md.cnt, "index out of bounds!") 731 #define dg__dynarr_check_notempty(a, msg) \ 732 DG_DYNARR_ASSERT(a.md.cnt > 0, msg) 734 #elif (DG_DYNARR_INDEX_CHECK_LEVEL == 0) || (DG_DYNARR_INDEX_CHECK_LEVEL == 1) 737 #define dg__dynarr_checkidx(a,i) (void)0 738 #define dg__dynarr_checkidxle(a,i) (void)0 740 #define dg__dynarr_check_notempty(a, msg) (void)0 743 #error Invalid index check level DG_DYNARR_INDEX_CHECK_LEVEL (must be 0-3) ! 746 #if (DG_DYNARR_INDEX_CHECK_LEVEL == 1) || (DG_DYNARR_INDEX_CHECK_LEVEL == 3) 749 #define dg__dynarr_idx(md,i) \ 750 (((size_t)(i) < md.cnt) ? (size_t)(i) : 0) 752 #elif (DG_DYNARR_INDEX_CHECK_LEVEL == 0) || (DG_DYNARR_INDEX_CHECK_LEVEL == 2) 755 #define dg__dynarr_idx(md,i) (size_t)(i) 758 #error Invalid index check level DG_DYNARR_INDEX_CHECK_LEVEL (must be 0-3) ! 806 size_t min_needed =
md->cnt+num_add;
815 size_t oldCount =
md->cnt;
816 size_t newCount = oldCount+
n;
819 unsigned char*
p = (
unsigned char*)*arr;
821 if(
idx < oldCount) memmove(
p+(
idx+
n)*itemsize,
p+
idx*itemsize, itemsize*(oldCount -
idx));
835 size_t cnt =
md->cnt;
838 unsigned char*
p = (
unsigned char*)*arr;
840 if(init0)
memset(
p+cnt*itemsize, 0,
n*itemsize);
851 size_t cnt =
md->cnt;
857 unsigned char*
p = (
unsigned char*)*arr;
859 memmove(
p+itemsize*
idx,
p+itemsize*(
idx+
n), itemsize*(cnt - (
idx+
n)));
868 size_t cnt =
md->cnt;
874 unsigned char*
p = (
unsigned char*)*arr;
878 size_t numItemsAfterDeleted = cnt - (
idx+
n);
879 size_t m = (
n < numItemsAfterDeleted) ?
n : numItemsAfterDeleted;
894 #ifdef DG_DYNARR_IMPLEMENTATION 900 #ifndef DG_DYNARR_MALLOC 901 #define DG_DYNARR_MALLOC(elemSize, numElems) malloc(elemSize*numElems) 906 #define DG_DYNARR_REALLOC(ptr, elemSize, oldNumElems, newCapacity) \ 907 realloc(ptr, elemSize*newCapacity); 909 #define DG_DYNARR_FREE(ptr) free(ptr) 915 #ifndef DG_DYNARR_OUT_OF_MEMORY 916 #define DG_DYNARR_OUT_OF_MEMORY DG_DYNARR_ASSERT(0, "Out of Memory!"); 941 DG_DYNARR_ASSERT(min_needed >
cap,
"dg__dynarr_grow() should only be called if storage actually needs to grow!");
945 size_t newcap = (
cap > 4) ? (2*
cap) : 8;
948 if(min_needed > newcap) newcap = min_needed;
953 void*
p = DG_DYNARR_MALLOC(itemsize, newcap);
959 void*
p = DG_DYNARR_REALLOC(*arr, itemsize,
md->cnt, newcap);
960 if(
p ==
NULL) DG_DYNARR_FREE(*arr);
966 if(*arr)
md->cap = newcap;
972 DG_DYNARR_OUT_OF_MEMORY ;
988 size_t cnt =
md->cnt;
992 void*
p = DG_DYNARR_MALLOC(itemsize, cnt);
997 DG_DYNARR_FREE(*arr);
GLuint const GLchar * name
Definition: glext.h:6671
DG_DYNARR_INLINE int dg__dynarr_add(void **arr, dg__dynarr_md *md, size_t itemsize, size_t n, int init0)
Definition: dynarray.h:833
DG_DYNARR_INLINE int dg__dynarr_insert(void **arr, dg__dynarr_md *md, size_t itemsize, size_t idx, size_t n, int init0)
Definition: dynarray.h:812
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:8418
static const size_t DG__DYNARR_SIZE_T_MSB
Definition: dynarray.h:688
#define md
Definition: compat-1.3.h:1980
DG_DYNARR_INLINE int dg__dynarr_maybegrow(void **arr, dg__dynarr_md *md, size_t itemsize, size_t min_needed)
Definition: dynarray.h:797
#define da_free(a)
Definition: dynarray.h:235
#define da_pop(a)
Definition: dynarray.h:348
DG_DYNARR_DEF void dg__dynarr_free(void **p, dg__dynarr_md *md)
Definition: dynarray.h:676
#define NULL
Pointer to 0.
Definition: gctypes.h:65
#define DA_TYPEDEF(TYPE, NewArrayTypeName)
Definition: dynarray.h:209
DG_DYNARR_DEF void dg__dynarr_shrink_to_fit(void **arr, dg__dynarr_md *md, size_t itemsize)
#define DG_DYNARR_ASSERT(cond, msg)
Definition: dynarray.h:155
#define da_insert(a, idx, v)
Definition: dynarray.h:269
DG_DYNARR_DEF int dg__dynarr_grow(void **arr, dg__dynarr_md *md, size_t itemsize, size_t min_needed)
static const size_t DG__DYNARR_SIZE_T_ALL_BUT_MSB
Definition: dynarray.h:689
#define da_deletefast(a, idx)
Definition: dynarray.h:311
GLint GLint GLint GLint GLint x
Definition: glext.h:6295
f32 a2
Definition: gx_regdef.h:5096
DG_DYNARR_INLINE void dg__dynarr_init(void **p, dg__dynarr_md *md, void *buf, size_t buf_cap)
Definition: dynarray.h:788
size_t cap
Definition: dynarray.h:678
GLfloat GLfloat p
Definition: glext.h:9809
DG_DYNARR_INLINE int dg__dynarr_maybegrowadd(void **arr, dg__dynarr_md *md, size_t itemsize, size_t num_add)
Definition: dynarray.h:804
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 idx
Definition: pixman-arm-neon-asm.h:96
DG_DYNARR_INLINE void dg__dynarr_delete(void **arr, dg__dynarr_md *md, size_t itemsize, size_t idx, size_t n)
Definition: dynarray.h:849
f32 a1
Definition: gx_regdef.h:5095
GLenum cap
Definition: glext.h:10546
#define da_delete(a, idx)
Definition: dynarray.h:303
#define da_push(a, v)
Definition: dynarray.h:240
#define DG_DYNARR_INLINE
Definition: dynarray.h:199
DG_DYNARR_INLINE void dg__dynarr_deletefast(void **arr, dg__dynarr_md *md, size_t itemsize, size_t idx, size_t n)
Definition: dynarray.h:866
size_t cnt
Definition: dynarray.h:677
#define da_addn_uninit(a, n)
Definition: dynarray.h:264
#define da_addn(a, vals, n)
Definition: dynarray.h:252
void * memset(void *b, int c, size_t len)
Definition: string.c:7
#define da_count(a)
Definition: dynarray.h:390
GLdouble n
Definition: glext.h:8396
const GLfloat * m
Definition: glext.h:11755
#define DG_DYNARR_DEF
Definition: dynarray.h:192
#define da_init(a)
Definition: dynarray.h:215
void * memcpy(void *dst, const void *src, size_t len)
Definition: string.c:26