Commit 29e865b5 authored by jehan's avatar jehan

improve map implementation

parent 0b1a467d
......@@ -38,7 +38,10 @@ BCTOOLBOX_PUBLIC void bctoolbox_mmap_long_delete(bctoolbox_map_t *mmap);
BCTOOLBOX_PUBLIC void bctoolbox_map_insert(bctoolbox_map_t *map,const bctoolbox_pair_t *pair);
/*same as insert, but also deleting pair*/
BCTOOLBOX_PUBLIC void bctoolbox_map_insert_and_delete(bctoolbox_map_t *map,bctoolbox_pair_t *pair);
/*invalidate and delete in iterator and return new allocated next*/
/*same as insert and deleting pair with a newly allocated it returned */
BCTOOLBOX_PUBLIC bctoolbox_iterator_t * bctoolbox_map_insert_and_delete_with_returned_it(bctoolbox_map_t *map,bctoolbox_pair_t *pair);
/*at return, it point to the next element*/
BCTOOLBOX_PUBLIC bctoolbox_iterator_t *bctoolbox_map_erase(bctoolbox_map_t *map,bctoolbox_iterator_t *it);
/*return a new allocated iterator*/
BCTOOLBOX_PUBLIC bctoolbox_iterator_t *bctoolbox_map_begin(const bctoolbox_map_t *map);
......@@ -47,6 +50,8 @@ BCTOOLBOX_PUBLIC bctoolbox_iterator_t *bctoolbox_map_end(const bctoolbox_map_t *
/*return a new allocated iterator or null*/
BCTOOLBOX_PUBLIC bctoolbox_iterator_t * bctoolbox_map_find_custom(bctoolbox_map_t *map, bctoolbox_compare_func compare_func, const void *user_data);
BCTOOLBOX_PUBLIC size_t bctoolbox_map_size(const bctoolbox_map_t *map);
/*iterator*/
......
......@@ -314,8 +314,8 @@ void bctoolbox_set_memory_functions(BctoolboxMemoryFunctions *functions);
#define bctoolbox_new(type,count) (type*)bctoolbox_malloc(sizeof(type)*(count))
#define bctoolbox_new0(type,count) (type*)bctoolbox_malloc0(sizeof(type)*(count))
BCTOOLBOX_PUBLIC int close_socket(bctoolbox_socket_t sock);
BCTOOLBOX_PUBLIC int set_non_blocking_socket(bctoolbox_socket_t sock);
BCTOOLBOX_PUBLIC int bctoolbox_socket_close(bctoolbox_socket_t sock);
BCTOOLBOX_PUBLIC int bctoolbox_socket_set_non_blocking(bctoolbox_socket_t sock);
BCTOOLBOX_PUBLIC char *bctoolbox_strndup(const char *str,int n);
BCTOOLBOX_PUBLIC char *bctoolbox_strdup_printf(const char *fmt,...);
......
......@@ -32,23 +32,41 @@ extern "C" bctoolbox_map_t *bctoolbox_mmap_long_new(void) {
extern "C" void bctoolbox_mmap_long_delete(bctoolbox_map_t *mmap) {
delete (mmap_long_t *)mmap;
}
extern "C" void bctoolbox_map_insert(bctoolbox_map_t *map,const bctoolbox_pair_t *pair) {
static bctoolbox_iterator_t * bctoolbox_map_insert_base(bctoolbox_map_t *map,const bctoolbox_pair_t *pair,bool_t returns_it) {
mmap_long_t::iterator it;
if (typeid((pair_long_t*)pair) != typeid(pair_long_t*)) {
BCTOOLBOX_SLOGE(LOG_DOMAIN) << "Cannot insert pair ["<< pair << "] on map [" << map <<"] , wrong type";
return NULL;
} else {
((mmap_long_t *)map)->insert(*((pair_long_t*)pair));
it = ((mmap_long_t *)map)->insert(*((pair_long_t*)pair));
}
if (returns_it) {
return (bctoolbox_iterator_t *) new mmap_long_t::iterator(it);
} else
return NULL;
}
extern "C" void bctoolbox_map_insert(bctoolbox_map_t *map,const bctoolbox_pair_t *pair) {
bctoolbox_map_insert_base(map,pair,FALSE);
}
extern "C" void bctoolbox_map_insert_and_delete(bctoolbox_map_t *map, bctoolbox_pair_t *pair) {
bctoolbox_map_insert(map,pair);
bctoolbox_pair_delete(pair);
}
extern "C" bctoolbox_iterator_t * bctoolbox_map_insert_and_delete_with_returned_it(bctoolbox_map_t *map, bctoolbox_pair_t *pair) {
bctoolbox_iterator_t * it = bctoolbox_map_insert_base(map,pair,TRUE);
bctoolbox_pair_delete(pair);
return it;
}
extern "C" bctoolbox_iterator_t *bctoolbox_map_erase(bctoolbox_map_t *map,bctoolbox_iterator_t *it) {
bctoolbox_iterator_t * next = (bctoolbox_iterator_t *) new mmap_long_t::iterator((*(mmap_long_t::iterator*)it));
next = bctoolbox_iterator_get_next(next);
((mmap_long_t *)map)->erase((*(mmap_long_t::iterator*)it));
bctoolbox_iterator_delete(it);
return next;
//bctoolbox_iterator_t * next = (bctoolbox_iterator_t *) new mmap_long_t::iterator((*(mmap_long_t::iterator*)it));
//next = bctoolbox_iterator_get_next(next);
((mmap_long_t *)map)->erase((*(mmap_long_t::iterator*)it)++);
//bctoolbox_iterator_delete(it);
return it;
}
extern "C" bctoolbox_iterator_t *bctoolbox_map_begin(const bctoolbox_map_t *map) {
return (bctoolbox_iterator_t *) new mmap_long_t::iterator(((mmap_long_t *)map)->begin());
......@@ -70,7 +88,7 @@ extern "C" bctoolbox_iterator_t *bctoolbox_iterator_get_next_and_delete(bctoolb
return next;
}
extern "C" bool_t bctoolbox_iterator_equals(const bctoolbox_iterator_t *a,const bctoolbox_iterator_t *b) {
return (mmap_long_t::iterator*)a == (mmap_long_t::iterator*)b;
return *(mmap_long_t::iterator*)a == *(mmap_long_t::iterator*)b;
}
extern "C" void bctoolbox_iterator_delete(bctoolbox_iterator_t *it) {
delete ((mmap_long_t::iterator*)it);
......@@ -105,3 +123,6 @@ extern "C" bctoolbox_iterator_t * bctoolbox_map_find_custom(bctoolbox_map_t *map
return NULL;
}
extern "C" size_t bctoolbox_map_size(const bctoolbox_map_t *map) {
return ((mmap_long_t *)map)->size();
}
......@@ -98,7 +98,7 @@ char * bctoolbox_strdup(const char *tmp){
* ioctlsocket on Win32.
* int retrun the result of the system method
*/
int set_non_blocking_socket (bctoolbox_socket_t sock){
int bctoolbox_socket_set_non_blocking(bctoolbox_socket_t sock){
#if !defined(_WIN32) && !defined(_WIN32_WCE)
return fcntl (sock, F_SETFL, O_NONBLOCK);
#else
......@@ -113,7 +113,7 @@ int set_non_blocking_socket (bctoolbox_socket_t sock){
* closesocket on Win32.
* int retrun the result of the system method
*/
int close_socket(bctoolbox_socket_t sock){
int bctoolbox_socket_close(bctoolbox_socket_t sock){
#if !defined(_WIN32) && !defined(_WIN32_WCE)
return close (sock);
#else
......
......@@ -23,8 +23,8 @@
static void multimap_insert(void) {
bctoolbox_map_t *mmap = bctoolbox_mmap_long_new();
bctoolbox_list_t *ref = NULL;
bctoolbox_iterator_t *it;
bctoolbox_list_t *head = NULL, *ref = NULL;
bctoolbox_iterator_t *it,*end;
long i=0;
int N = 100;
......@@ -34,15 +34,31 @@ static void multimap_insert(void) {
bctoolbox_map_insert(mmap, pair);
bctoolbox_pair_delete(pair);
}
BC_ASSERT_EQUAL(bctoolbox_map_size(mmap),N, int, "%i");
it = bctoolbox_map_begin(mmap);
head = bctoolbox_list_first_elem(ref);
for(ref = bctoolbox_list_first_elem(ref);ref!=NULL;ref=bctoolbox_list_next(ref)) {
BC_ASSERT_EQUAL((long)bctoolbox_list_get_data(ref)
,(long)bctoolbox_pair_get_second(bctoolbox_iterator_get_pair(it))
,long, "%lu");
BC_ASSERT_EQUAL( (long)bctoolbox_pair_get_second(bctoolbox_iterator_get_pair(it))
, (long)bctoolbox_list_get_data(ref)
, long, "%lu");
it = bctoolbox_iterator_get_next(it);
}
bctoolbox_iterator_delete(it);
ref = head;
end = bctoolbox_map_end(mmap);
for(it = bctoolbox_map_begin(mmap);!bctoolbox_iterator_equals(it,end);it = bctoolbox_iterator_get_next(it)) {
BC_ASSERT_PTR_NOT_NULL(ref);
BC_ASSERT_EQUAL( (long)bctoolbox_pair_get_second(bctoolbox_iterator_get_pair(it))
, (long)bctoolbox_list_get_data(ref)
, long, "%lu");
ref=bctoolbox_list_next(ref);
}
bctoolbox_iterator_delete(it);
bctoolbox_iterator_delete(end);
bctoolbox_list_free(head);
bctoolbox_mmap_long_delete(mmap);
}
......
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