[go: nahoru, domu]

Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/srdjanstipic/stb into wor…
Browse files Browse the repository at this point in the history
…king
  • Loading branch information
Sean Barrett committed Jul 13, 2020
2 parents 580fc1a + 1e400e2 commit ef23148
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions stb_ds.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ extern void * stbds_shmode_func(size_t elemsize, int mode);

#define stbds_header(t) ((stbds_array_header *) (t) - 1)
#define stbds_temp(t) stbds_header(t)->temp
#define stbds_temp_key(t) stbds_header(t)->temp_key

#define stbds_arrsetcap(a,n) (stbds_arrgrow(a,0,n))
#define stbds_arrsetlen(a,n) ((stbds_arrcap(a) < (size_t) (n) ? stbds_arrsetcap((a),(size_t)(n)),0 : 0), (a) ? stbds_header(a)->length = (size_t) (n) : 0)
Expand Down Expand Up @@ -605,7 +606,8 @@ extern void * stbds_shmode_func(size_t elemsize, int mode);

#define stbds_shputs(t, s) \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (s).key, sizeof (s).key, STBDS_HM_STRING), \
(t)[stbds_temp((t)-1)] = (s))
(t)[stbds_temp((t)-1)] = (s), \
(t)[stbds_temp((t)-1)].key = stbds_temp_key((t)-1))

#define stbds_pshput(t, p) \
((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (p)->key, sizeof (p)->key, STBDS_HM_PTR_TO_STRING), \
Expand Down Expand Up @@ -652,6 +654,7 @@ typedef struct
size_t capacity;
void * hash_table;
ptrdiff_t temp;
char * temp_key;
} stbds_array_header;

typedef struct stbds_string_block
Expand Down Expand Up @@ -1429,9 +1432,9 @@ void *stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int m
stbds_temp(a) = i-1;

switch (table->string.mode) {
case STBDS_SH_STRDUP: *(char **) ((char *) a + elemsize*i) = stbds_strdup((char*) key); break;
case STBDS_SH_ARENA: *(char **) ((char *) a + elemsize*i) = stbds_stralloc(&table->string, (char*)key); break;
case STBDS_SH_DEFAULT: *(char **) ((char *) a + elemsize*i) = (char *) key; break;
case STBDS_SH_STRDUP: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = stbds_strdup((char*) key); break;
case STBDS_SH_ARENA: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = stbds_stralloc(&table->string, (char*)key); break;
case STBDS_SH_DEFAULT: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = (char *) key; break;
default: memcpy((char *) a + elemsize*i, key, keysize); break;
}
}
Expand Down Expand Up @@ -1636,7 +1639,7 @@ void stbds_unit_tests(void)
const int testsize2 = testsize/20;
int *arr=NULL;
struct { int key; int value; } *intmap = NULL;
struct { char *key; int value; } *strmap = NULL;
struct { char *key; int value; } *strmap = NULL, s;
struct { stbds_struct key; int value; } *map = NULL;
stbds_struct *map2 = NULL;
stbds_struct2 *map3 = NULL;
Expand Down Expand Up @@ -1719,6 +1722,35 @@ void stbds_unit_tests(void)
stralloc(&sa, strkey(i));
strreset(&sa);

{
s.key = "a", s.value = 1;
shputs(strmap, s);
STBDS_ASSERT(*strmap[0].key == 'a');
STBDS_ASSERT(strmap[0].key == s.key);
STBDS_ASSERT(strmap[0].value == s.value);
shfree(strmap);
}

{
s.key = "a", s.value = 1;
sh_new_strdup(strmap);
shputs(strmap, s);
STBDS_ASSERT(*strmap[0].key == 'a');
STBDS_ASSERT(strmap[0].key != s.key);
STBDS_ASSERT(strmap[0].value == s.value);
shfree(strmap);
}

{
s.key = "a", s.value = 1;
sh_new_arena(strmap);
shputs(strmap, s);
STBDS_ASSERT(*strmap[0].key == 'a');
STBDS_ASSERT(strmap[0].key != s.key);
STBDS_ASSERT(strmap[0].value == s.value);
shfree(strmap);
}

for (j=0; j < 2; ++j) {
STBDS_ASSERT(shgeti(strmap,"foo") == -1);
if (j == 0)
Expand Down

0 comments on commit ef23148

Please sign in to comment.