Commit c3e9e78a authored by Benjamin REIS's avatar Benjamin REIS

Create bctbx_map_cchar_t and the corresponding functions

parent 7254fe94
......@@ -29,49 +29,77 @@ typedef struct _bctbx_map_t bctbx_map_t;
typedef struct _bctbx_pair_t bctbx_pair_t;
typedef struct _bctbx_iterator_t bctbx_iterator_t;
typedef struct _bctbx_mmap_ullong_t bctbx_mmap_ullong_t;
typedef struct _bctbx_mmap_cchar_t bctbx_mmap_cchar_t;
/*map*/
BCTBX_PUBLIC bctbx_map_t *bctbx_mmap_ullong_new(void);
BCTBX_PUBLIC bctbx_map_t *bctbx_mmap_cchar_new(void);
BCTBX_PUBLIC void bctbx_mmap_ullong_delete(bctbx_map_t *mmap);
BCTBX_PUBLIC void bctbx_map_insert(bctbx_map_t *map,const bctbx_pair_t *pair);
BCTBX_PUBLIC void bctbx_mmap_cchar_delete(bctbx_map_t *mmap);
#define bctbx_map_insert bctbx_map_ullong_insert
BCTBX_PUBLIC void bctbx_map_ullong_insert(bctbx_map_t *map,const bctbx_pair_t *pair);
BCTBX_PUBLIC void bctbx_map_cchar_insert(bctbx_map_t *map,const bctbx_pair_t *pair);
/*same as insert, but also deleting pair*/
BCTBX_PUBLIC void bctbx_map_insert_and_delete(bctbx_map_t *map,bctbx_pair_t *pair);
#define bctbx_map_insert_and_delete bctbx_map_ullong_insert_and_delete
BCTBX_PUBLIC void bctbx_map_ullong_insert_and_delete(bctbx_map_t *map,bctbx_pair_t *pair);
BCTBX_PUBLIC void bctbx_map_cchar_insert_and_delete(bctbx_map_t *map,bctbx_pair_t *pair);
/*same as insert and deleting pair with a newly allocated it returned */
BCTBX_PUBLIC bctbx_iterator_t * bctbx_map_insert_and_delete_with_returned_it(bctbx_map_t *map,bctbx_pair_t *pair);
#define bctbx_map_insert_and_delete_with_returned_it bctbx_map_ullong_insert_and_delete_with_returned_it
BCTBX_PUBLIC bctbx_iterator_t * bctbx_map_ullong_insert_and_delete_with_returned_it(bctbx_map_t *map,bctbx_pair_t *pair);
BCTBX_PUBLIC bctbx_iterator_t * bctbx_map_cchar_insert_and_delete_with_returned_it(bctbx_map_t *map,bctbx_pair_t *pair);
/*at return, it point to the next element*/
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_erase(bctbx_map_t *map,bctbx_iterator_t *it);
#define bctbx_map_erase bctbx_map_ullong_erase
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_ullong_erase(bctbx_map_t *map,bctbx_iterator_t *it);
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_cchar_erase(bctbx_map_t *map,bctbx_iterator_t *it);
/*return a new allocated iterator*/
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_begin(const bctbx_map_t *map);
#define bctbx_map_begin bctbx_map_ullong_begin
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_ullong_begin(const bctbx_map_t *map);
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_cchar_begin(const bctbx_map_t *map);
/*return a new allocated iterator*/
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_end(const bctbx_map_t *map);
#define bctbx_map_end bctbx_map_ullong_end
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_ullong_end(const bctbx_map_t *map);
BCTBX_PUBLIC bctbx_iterator_t *bctbx_map_cchar_end(const bctbx_map_t *map);
/*return a new allocated iterator or null*/
BCTBX_PUBLIC bctbx_iterator_t * bctbx_map_find_custom(bctbx_map_t *map, bctbx_compare_func compare_func, const void *user_data);
#define bctbx_map_find_custom bctbx_map_ullong_find_custom
BCTBX_PUBLIC bctbx_iterator_t * bctbx_map_ullong_find_custom(bctbx_map_t *map, bctbx_compare_func compare_func, const void *user_data);
BCTBX_PUBLIC bctbx_iterator_t * bctbx_map_cchar_find_custom(bctbx_map_t *map, bctbx_compare_func compare_func, const void *user_data);
/*return the iterator associated to the key in the map or Null*/
BCTBX_PUBLIC bctbx_iterator_t * bctbx_map_ullong_find_key(bctbx_map_t *map, unsigned long long key);
BCTBX_PUBLIC bctbx_iterator_t * bctbx_map_cchar_find_key(bctbx_map_t *map, const char * key);
/* return the size of the map*/
#define bctbx_map_size bctbx_map_ullong_size
BCTBX_PUBLIC size_t bctbx_map_ullong_size(const bctbx_map_t *map);
BCTBX_PUBLIC size_t bctbx_map_cchar_size(const bctbx_map_t *map);
BCTBX_PUBLIC size_t bctbx_map_size(const bctbx_map_t *map);
/*iterator*/
BCTBX_PUBLIC bctbx_pair_t *bctbx_iterator_get_pair(const bctbx_iterator_t *it);
#define bctbx_iterator_get_pair bctbx_iterator_ullong_get_pair
BCTBX_PUBLIC bctbx_pair_t *bctbx_iterator_ullong_get_pair(const bctbx_iterator_t *it);
BCTBX_PUBLIC bctbx_pair_t *bctbx_iterator_cchar_get_pair(const bctbx_iterator_t *it);
/*return same pointer but pointing to next*/
BCTBX_PUBLIC bctbx_iterator_t *bctbx_iterator_get_next(bctbx_iterator_t *it);
BCTBX_PUBLIC bool_t bctbx_iterator_equals(const bctbx_iterator_t *a,const bctbx_iterator_t *b);
#define bctbx_iterator_get_next bctbx_iterator_ullong_get_next
BCTBX_PUBLIC bctbx_iterator_t *bctbx_iterator_ullong_get_next(bctbx_iterator_t *it);
BCTBX_PUBLIC bctbx_iterator_t *bctbx_iterator_cchar_get_next(bctbx_iterator_t *it);
#define bctbx_iterator_equals bctbx_iterator_ullong_equals
BCTBX_PUBLIC bool_t bctbx_iterator_ullong_equals(const bctbx_iterator_t *a,const bctbx_iterator_t *b);
BCTBX_PUBLIC bool_t bctbx_iterator_cchar_equals(const bctbx_iterator_t *a,const bctbx_iterator_t *b);
#define bctbx_iterator_delete bctbx_iterator_ullong_delete
BCTBX_PUBLIC void bctbx_iterator_ullong_delete(bctbx_iterator_t *it);
BCTBX_PUBLIC void bctbx_iterator_cchar_delete(bctbx_iterator_t *it);
BCTBX_PUBLIC void bctbx_iterator_delete(bctbx_iterator_t *it);
/*pair*/
typedef struct _bctbx_pair_ullong_t bctbx_pair_ullong_t; /*inherite from bctbx_pair_t*/
BCTBX_PUBLIC bctbx_pair_ullong_t * bctbx_pair_ullong_new(unsigned long long key,void *value);
typedef struct _bctbx_pair_cchar_t bctbx_pair_cchar_t; /*inherite from bctbx_pair_t*/
BCTBX_PUBLIC bctbx_pair_cchar_t * bctbx_pair_cchar_new(const char * key,void *value);
BCTBX_PUBLIC void* bctbx_pair_get_second(const bctbx_pair_t * pair);
#define bctbx_pair_get_second bctbx_pair_ullong_get_second
BCTBX_PUBLIC void* bctbx_pair_ullong_get_second(const bctbx_pair_t * pair);
BCTBX_PUBLIC void* bctbx_pair_cchar_get_second(const bctbx_pair_t * pair);
BCTBX_PUBLIC unsigned long long bctbx_pair_ullong_get_first(const bctbx_pair_ullong_t * pair);
BCTBX_PUBLIC void bctbx_pair_delete(bctbx_pair_t * pair);
BCTBX_PUBLIC const char * bctbx_pair_cchar_get_first(const bctbx_pair_cchar_t * pair);
#define bctbx_pair_delete bctbx_pair_ullong_delete
BCTBX_PUBLIC void bctbx_pair_ullong_delete(bctbx_pair_t * pair);
BCTBX_PUBLIC void bctbx_pair_cchar_delete(bctbx_pair_t * pair);
#ifdef __cplusplus
}
......
This diff is collapsed.
......@@ -31,33 +31,33 @@ static void multimap_insert(void) {
for(i=0;i<N;i++) {
bctbx_pair_t* pair = (bctbx_pair_t*)bctbx_pair_ullong_new(N-i-1, (void*)((long)N-i-1));
ref = bctbx_list_append(ref, (void*)i);
bctbx_map_insert(mmap, pair);
bctbx_pair_delete(pair);
bctbx_map_ullong_insert(mmap, pair);
bctbx_pair_ullong_delete(pair);
}
BC_ASSERT_EQUAL(bctbx_map_size(mmap),N, int, "%i");
BC_ASSERT_EQUAL(bctbx_map_ullong_size(mmap),N, int, "%i");
it = bctbx_map_begin(mmap);
it = bctbx_map_ullong_begin(mmap);
head = bctbx_list_first_elem(ref);
for(ref = bctbx_list_first_elem(ref);ref!=NULL;ref=bctbx_list_next(ref)) {
BC_ASSERT_EQUAL( (long)bctbx_pair_get_second(bctbx_iterator_get_pair(it))
BC_ASSERT_EQUAL( (long)bctbx_pair_ullong_get_second(bctbx_iterator_ullong_get_pair(it))
, (long)bctbx_list_get_data(ref)
, long, "%lu");
it = bctbx_iterator_get_next(it);
it = bctbx_iterator_ullong_get_next(it);
}
bctbx_iterator_delete(it);
bctbx_iterator_ullong_delete(it);
ref = head;
end = bctbx_map_end(mmap);
end = bctbx_map_ullong_end(mmap);
for(it = bctbx_map_begin(mmap);!bctbx_iterator_equals(it,end);it = bctbx_iterator_get_next(it)) {
for(it = bctbx_map_ullong_begin(mmap);!bctbx_iterator_ullong_equals(it,end);it = bctbx_iterator_ullong_get_next(it)) {
BC_ASSERT_PTR_NOT_NULL(ref);
BC_ASSERT_EQUAL( (long)bctbx_pair_get_second(bctbx_iterator_get_pair(it))
BC_ASSERT_EQUAL( (long)bctbx_pair_ullong_get_second(bctbx_iterator_ullong_get_pair(it))
, (long)bctbx_list_get_data(ref)
, long, "%lu");
ref=bctbx_list_next(ref);
}
bctbx_iterator_delete(it);
bctbx_iterator_delete(end);
bctbx_iterator_ullong_delete(it);
bctbx_iterator_ullong_delete(end);
bctbx_list_free(head);
bctbx_mmap_ullong_delete(mmap);
}
......@@ -70,26 +70,26 @@ static void multimap_erase(void) {
int N = 100;
for(i=0;i<N;i++) {
bctbx_map_insert_and_delete(mmap, (bctbx_pair_t*)bctbx_pair_ullong_new(i, (void*)((long)i)));
bctbx_map_ullong_insert_and_delete(mmap, (bctbx_pair_t*)bctbx_pair_ullong_new(i, (void*)((long)i)));
}
end= bctbx_map_end(mmap);
end= bctbx_map_ullong_end(mmap);
for(it = bctbx_map_begin(mmap);!bctbx_iterator_equals(it,end);) {
long value = (long)bctbx_pair_get_second(bctbx_iterator_get_pair(it));
for(it = bctbx_map_ullong_begin(mmap);!bctbx_iterator_ullong_equals(it,end);) {
long value = (long)bctbx_pair_ullong_get_second(bctbx_iterator_ullong_get_pair(it));
if (value < N/2) {
it = bctbx_map_erase(mmap, it);
it = bctbx_map_ullong_erase(mmap, it);
} else {
break;
}
}
it = bctbx_map_begin(mmap);
BC_ASSERT_EQUAL((long)bctbx_pair_get_second(bctbx_iterator_get_pair(it))
it = bctbx_map_ullong_begin(mmap);
BC_ASSERT_EQUAL((long)bctbx_pair_ullong_get_second(bctbx_iterator_ullong_get_pair(it))
, N/2
,long, "%lu");
bctbx_mmap_ullong_delete(mmap);
bctbx_iterator_delete(end);
bctbx_iterator_ullong_delete(end);
}
static int compare_func(const void *a, const void*b) {
......@@ -102,14 +102,98 @@ static void multimap_find_custom(void) {
for(i=0;i<N;i++) {
bctbx_pair_t* pair = (bctbx_pair_t*)bctbx_pair_ullong_new(i, (void*)((long)i));
bctbx_map_insert_and_delete(mmap, pair);
bctbx_map_ullong_insert_and_delete(mmap, pair);
}
bctbx_iterator_t * it = bctbx_map_find_custom(mmap, compare_func, (void*)10l);
BC_ASSERT_EQUAL((long)bctbx_pair_get_second(bctbx_iterator_get_pair(it))
bctbx_iterator_t * it = bctbx_map_ullong_find_custom(mmap, compare_func, (void*)10l);
BC_ASSERT_EQUAL((long)bctbx_pair_ullong_get_second(bctbx_iterator_ullong_get_pair(it))
, 0
,long, "%lu");
bctbx_mmap_ullong_delete(mmap);
bctbx_iterator_delete(it);
bctbx_iterator_ullong_delete(it);
}
static void multimap_insert_cchar(void) {
bctbx_map_t *mmap = bctbx_mmap_cchar_new(); bctbx_iterator_t *it,*end;
long i=0;
int N = 100;
for(i=0;i<N;i++) {
char str[32];
std::snprintf(str, sizeof(str), "%ld", N-i-1);
bctbx_pair_t* pair = (bctbx_pair_t*)bctbx_pair_cchar_new((const char *)str, (void*)((long)N-i-1));
bctbx_map_cchar_insert(mmap, pair);
bctbx_pair_cchar_delete(pair);
}
BC_ASSERT_EQUAL(bctbx_map_cchar_size(mmap),N, int, "%i");
for(i = 0;i < N;i++) {
char str[32];
std::snprintf(str, sizeof(str), "%ld", i);
it = bctbx_map_cchar_find_key(mmap, (const char *)str);
BC_ASSERT_EQUAL( (long)bctbx_pair_cchar_get_second(bctbx_iterator_cchar_get_pair(it))
, (long)i
, long, "%lu");
}
end = bctbx_map_cchar_end(mmap);
bctbx_iterator_cchar_delete(it);
bctbx_iterator_cchar_delete(end);
bctbx_mmap_cchar_delete(mmap);
}
static void multimap_erase_cchar(void) {
bctbx_map_t *mmap = bctbx_mmap_cchar_new();
bctbx_iterator_t *it;
bctbx_iterator_t *end;
long i=0;
int N = 100;
for(i=0;i<N;i++) {
char str[32];
std::snprintf(str, sizeof(str),"%ld", i);
bctbx_map_cchar_insert_and_delete(mmap, (bctbx_pair_t*)bctbx_pair_cchar_new((const char *)str, (void*)((long)i)));
}
end= bctbx_map_cchar_end(mmap);
for(it = bctbx_map_cchar_begin(mmap);!bctbx_iterator_cchar_equals(it,end);) {
long value = (long)bctbx_pair_cchar_get_second(bctbx_iterator_cchar_get_pair(it));
if (value < N/2) {
it = bctbx_map_cchar_erase(mmap, it);
} else {
break;
}
}
it = bctbx_map_cchar_begin(mmap);
BC_ASSERT_EQUAL((long)bctbx_pair_cchar_get_second(bctbx_iterator_cchar_get_pair(it))
, N/2
,long, "%lu");
bctbx_mmap_cchar_delete(mmap);
bctbx_iterator_cchar_delete(end);
}
static int compare_func_cchar(const void *a, const void*b) {
return (long)a == (long)b;
}
static void multimap_find_custom_cchar(void) {
bctbx_map_t *mmap = bctbx_mmap_cchar_new();
long i=0;
int N = 100;
for(i=0;i<N;i++) {
char str[32];
std::snprintf(str, sizeof(str), "%ld", i);
bctbx_pair_t* pair = (bctbx_pair_t*)bctbx_pair_cchar_new((const char *)str, (void*)((long)i));
bctbx_map_cchar_insert_and_delete(mmap, pair);
}
bctbx_iterator_t * it = bctbx_map_cchar_find_custom(mmap, compare_func, (void*)10l);
BC_ASSERT_EQUAL((long)bctbx_pair_cchar_get_second(bctbx_iterator_cchar_get_pair(it))
, 0
,long, "%lu");
bctbx_mmap_cchar_delete(mmap);
bctbx_iterator_cchar_delete(it);
}
......@@ -117,6 +201,9 @@ static test_t container_tests[] = {
TEST_NO_TAG("mmap insert", multimap_insert),
TEST_NO_TAG("mmap erase", multimap_erase),
TEST_NO_TAG("mmap find custom", multimap_find_custom),
TEST_NO_TAG("mmap insert cchar", multimap_insert_cchar),
TEST_NO_TAG("mmap erase cchar", multimap_erase_cchar),
TEST_NO_TAG("mmap find custom cchar", multimap_find_custom_cchar),
};
test_suite_t containers_test_suite = {"Containers", NULL, NULL, NULL, NULL,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment