Skip to content
Myr

stb_ds v0.3: fixes for compiling client code in C++

Before adoption: this commit came in with the history the project adopted, so no landing records it.

add missing _wrapper suffixes disable clang rvalue support in C++ disable unit tests in VC6 C++ other tweaks

authored by Sean Barrett · committed by Sean Barrett · parent 39c0559 · Browse files · 2 files+49 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+49 added−38 removed

  • other:+49 added−38 removed

2 files+49 added−38 removed

modified:README.md+2 added-2 removed
modified:stb_ds.h+47 added-36 removed
1/* stb_ds.h - v0.2 - public domain data structures - Sean Barrett 20191/* stb_ds.h - v0.3 - public domain data structures - Sean Barrett 20192 3 This is a single-header-file library that provides easy-to-use4 dynamic arrays and hash tables for C (also works in C++).5
395 unmodified lines
401402#if defined(__GNUC__) || defined(__clang__)403#define STBDS_HAS_TYPEOF404#ifdef __cplusplus405#define STBDS_HAS_LITERAL_ARRAY405//#define STBDS_HAS_LITERAL_ARRAY // this is currently broken for clang406#endif407#endif408409#if !defined(__cplusplus)
3 unmodified lines
413#endif414415// this macro takes the address of the argument, but on gcc/clang can accept rvalues416#if defined(STBDS_HAS_LITERAL_ARRAY) && defined(STBDS_HAS_TYPEOF)417 #if __clang__418 #define STBDS_ADDRESSOF(typevar, value) ((__typeof__(typevar)[1]){value}) // literal array decays to pointer to value419 #else417#define STBDS_ADDRESSOF(typevar, value) ((typeof(typevar)[1]){value}) // literal array decays to pointer to value420 #define STBDS_ADDRESSOF(typevar, value) ((typeof(typevar)[]){value}) // literal array decays to pointer to value421 #endif422#else423#define STBDS_ADDRESSOF(typevar, value) &(value)424#endif425
20 unmodified lines
446447#define stbds_arrmaybegrow(a,n) ((!(a) || stbds_header(a)->length + (n) > stbds_header(a)->capacity) \448 ? (stbds_arrgrow(a,n,0),0) : 0)449446#define stbds_arrgrow(a,b,c) ((a) = stbds_arrgrowf((a), sizeof *(a), (b), (c)))450#define stbds_arrgrow(a,b,c) ((a) = stbds_arrgrowf_wrapper((a), sizeof *(a), (b), (c)))451452#define stbds_hmput(t, k, v) \449 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, 0), \453 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, 0), \454 (t)[stbds_temp((t)-1)].key = (k), \455 (t)[stbds_temp((t)-1)].value = (v))456457#define stbds_hmputs(t, s) \458 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), &(s).key, sizeof (s).key, STBDS_HM_BINARY), \459 (t)[stbds_temp((t)-1)] = (s))460461#define stbds_hmgeti(t,k) \458 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \462 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \463 stbds_temp((t)-1))464465#define stbds_hmgetp(t, k) \466 ((void) stbds_hmgeti(t,k), &(t)[stbds_temp((t)-1)])467468#define stbds_hmdel(t,k) \465 (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_BINARY)),(t)?stbds_temp((t)-1):0)469 (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_BINARY)),(t)?stbds_temp((t)-1):0)470471#define stbds_hmdefault(t, v) \472 ((t) = stbds_hmput_default_wrapper((t), sizeof *(t)), (t)[-1].value = (v))473
8 unmodified lines
482#define stbds_hmlen(t) (stbds_arrlen((t)-1)-1)483#define stbds_hmlenu(t) (stbds_arrlenu((t)-1)-1)484485#define stbds_shput(t, k, v) \482 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (k), sizeof (t)->key, STBDS_HM_STRING), \486 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \487 (t)[stbds_temp(t-1)].value = (v))488489#define stbds_shputs(t, s) \486 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (s).key, sizeof (s).key, STBDS_HM_STRING), \490 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (s).key, sizeof (s).key, STBDS_HM_STRING), \491 (t)[stbds_temp(t-1)] = (s))492493#define stbds_shgeti(t,k) \490 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (k), sizeof (t)->key, STBDS_HM_STRING), \494 ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \495 stbds_temp(t))496497#define stbds_shgetp(t, k) \498 ((void) stbds_shgeti(t,k), &(t)[stbds_temp(t-1)])499500#define stbds_shdel(t,k) \497 (((t) = stbds_hmdel_key((t),sizeof *(t), (k), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_STRING)),(t)?stbds_temp((t)-1):0)501 (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_STRING)),(t)?stbds_temp((t)-1):0)502503#define stbds_sh_new_arena(t) \504 ((t) = stbds_shmode_func_wrapper(t, sizeof *(t), STBDS_SH_ARENA))505#define stbds_sh_new_strdup(t) \
62 unmodified lines
568template<class T> static T * stbds_hmdel_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode){569 return (T*)stbds_hmdel_key((void*)a, elemsize, key, keysize, keyoffset, mode);570}571template<class T> static T * stbds_shmode_func_wrapper(T *, size_t elemsize, int mode) {568 return stbds_shmode_func(elemsize, mode);572 return (T*)stbds_shmode_func(elemsize, mode);573}574#else575#define stbds_arrgrowf_wrapper stbds_arrgrowf576#define stbds_hmget_key_wrapper stbds_hmget_key
870 unmodified lines
1447}14481449void stbds_unit_tests(void)1450{1451#if defined(_MSC_VER) && _MSC_VER <= 1200 && defined(__cplusplus)1452 // VC6 C++ doesn't like the template<> trick on unnamed structures, so do nothing!1453 STBDS_ASSERT(0);1454#else1455 const int testsize = 100000;1456 int *arr=NULL;1457 struct { int key; int value; } *intmap = NULL;1458 struct { char *key; int value; } *strmap = NULL;
20 unmodified lines
14791480 for (i=0; i < 5; ++i) {1481 arrpush(arr,1); arrpush(arr,2); arrpush(arr,3); arrpush(arr,4);1482 stbds_arrins(arr,i,5);1475 assert(arr[i] == 5);1483 STBDS_ASSERT(arr[i] == 5);1484 if (i < 4)1477 assert(arr[4] == 4);1485 STBDS_ASSERT(arr[4] == 4);1486 arrfree(arr);1487 }14881489 hmdefault(intmap, -1);1482 i=1; assert(hmget(intmap, i) == -1);1490 i=1; STBDS_ASSERT(hmget(intmap, i) == -1);1491 for (i=0; i < testsize; i+=2)1492 hmput(intmap, i, i*5);1493 for (i=0; i < testsize; i+=1)1486 if (i & 1) assert(hmget(intmap, i) == -1 );1487 else assert(hmget(intmap, i) == i*5);1494 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -1 );1495 else STBDS_ASSERT(hmget(intmap, i) == i*5);1496 for (i=0; i < testsize; i+=2)1497 hmput(intmap, i, i*3);1498 for (i=0; i < testsize; i+=1)1491 if (i & 1) assert(hmget(intmap, i) == -1 );1492 else assert(hmget(intmap, i) == i*3);1499 if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -1 );1500 else STBDS_ASSERT(hmget(intmap, i) == i*3);1501 for (i=2; i < testsize; i+=4)1502 hmdel(intmap, i); // delete half the entries1503 for (i=0; i < testsize; i+=1)1496 if (i & 3) assert(hmget(intmap, i) == -1 );1497 else assert(hmget(intmap, i) == i*3);1504 if (i & 3) STBDS_ASSERT(hmget(intmap, i) == -1 );1505 else STBDS_ASSERT(hmget(intmap, i) == i*3);1506 for (i=0; i < testsize; i+=1)1507 hmdel(intmap, i); // delete the rest of the entries 1508 for (i=0; i < testsize; i+=1)1501 assert(hmget(intmap, i) == -1 );1509 STBDS_ASSERT(hmget(intmap, i) == -1 );1510 hmfree(intmap);1511 for (i=0; i < testsize; i+=2)1512 hmput(intmap, i, i*3);1513 hmfree(intmap);15141507 #ifdef __clang__1515 #if defined(__clang__) || defined(__GNUC__)1516 #ifndef __cplusplus1517 intmap = NULL;1518 hmput(intmap, 15, 7);1519 hmput(intmap, 11, 3);1520 hmput(intmap, 9, 5);1512 assert(hmget(intmap, 9) == 5);1513 assert(hmget(intmap, 11) == 3);1514 assert(hmget(intmap, 15) == 7);1521 STBDS_ASSERT(hmget(intmap, 9) == 5);1522 STBDS_ASSERT(hmget(intmap, 11) == 3);1523 STBDS_ASSERT(hmget(intmap, 15) == 7);1524 #endif1525 #endif15261527 for (i=0; i < testsize; ++i)1528 stralloc(&sa, strkey(i));1529 strreset(&sa);
6 unmodified lines
1536 shdefault(strmap, -1);1537 for (i=0; i < testsize; i+=2)1538 shput(strmap, strkey(i), i*3);1539 for (i=0; i < testsize; i+=1)1530 if (i & 1) assert(shget(strmap, strkey(i)) == -1 );1531 else assert(shget(strmap, strkey(i)) == i*3);1540 if (i & 1) STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1541 else STBDS_ASSERT(shget(strmap, strkey(i)) == i*3);1542 for (i=2; i < testsize; i+=4)1543 shdel(strmap, strkey(i)); // delete half the entries1544 for (i=0; i < testsize; i+=1)1535 if (i & 3) assert(shget(strmap, strkey(i)) == -1 );1536 else assert(shget(strmap, strkey(i)) == i*3);1545 if (i & 3) STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1546 else STBDS_ASSERT(shget(strmap, strkey(i)) == i*3);1547 for (i=0; i < testsize; i+=1)1548 shdel(strmap, strkey(i)); // delete the rest of the entries 1549 for (i=0; i < testsize; i+=1)1540 assert(shget(strmap, strkey(i)) == -1 );1550 STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1551 shfree(strmap);1552 }15531554 {
16 unmodified lines
15711572 for (i=0; i < testsize; i += 1) {1573 stbds_struct s = { i,i*2,i*3 ,i*4 };1574 stbds_struct t = { i,i*2,i*3+1,i*4 };1565 if (i & 1) assert(hmget(map, s) == 0);1566 else assert(hmget(map, s) == i*5);1567 assert(hmget(map, t) == 0);1575 if (i & 1) STBDS_ASSERT(hmget(map, s) == 0);1576 else STBDS_ASSERT(hmget(map, s) == i*5);1577 STBDS_ASSERT(hmget(map, t) == 0);1578 }15791580 for (i=0; i < testsize; i += 2) {1581 stbds_struct s = { i,i*2,i*3,i*4 };
3 unmodified lines
15851586 for (i=0; i < testsize; i += 1) {1587 stbds_struct s = { i,i*2,i*3,i*4 };1588 stbds_struct t = { i,i*2,i*3,i*4 };1579 if (i & 1) assert(hmgets(map2, s.key).d == 0);1580 else assert(hmgets(map2, s.key).d == i*4);1589 if (i & 1) STBDS_ASSERT(hmgets(map2, s.key).d == 0);1590 else STBDS_ASSERT(hmgets(map2, s.key).d == i*4);1591 }1592 hmfree(map2);1593#endif1594}1595#endif15961597
41 unmodified lines