Commit 33853246 authored by jehan's avatar jehan

add map_find_custom

add link dependency on librt
parent 7e057311
......@@ -194,7 +194,7 @@ else()
endif()
add_compile_options(${STRICT_OPTIONS_CPP} ${STRICT_OPTIONS_C})
check_library_exists("rt" "clock_gettime" "" HAVE_LIBRT)
add_subdirectory(include)
add_subdirectory(src)
......
......@@ -25,3 +25,5 @@
#cmakedefine HAVE_CU_GET_SUITE
#cmakedefine HAVE_CU_CURSES
#cmakedefine HAVE_CU_SET_TRACE_HANDLER
#cmakedefine HAVE_LIBRT
......@@ -18,7 +18,7 @@
#ifndef BCTOOLBOX_MMAP_H_
#define BCTOOLBOX_MMAP_H_
#include "bctoolbox/list.h"
#include "bctoolbox/port.h"
#ifdef __cplusplus
......@@ -38,18 +38,21 @@ 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);
BCTOOLBOX_PUBLIC void bctoolbox_map_erase(bctoolbox_map_t *map,bctoolbox_iterator_t *it);
/*invalidate and delete in iterator and return new allocated next*/
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);
/*return a new allocated iterator*/
BCTOOLBOX_PUBLIC bctoolbox_iterator_t *bctoolbox_map_end(const bctoolbox_map_t *map);
/*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);
/*iterator*/
BCTOOLBOX_PUBLIC bctoolbox_pair_t *bctoolbox_iterator_get_pair(const bctoolbox_iterator_t *it);
/*return a new allocated iterator*/
BCTOOLBOX_PUBLIC bctoolbox_iterator_t *bctoolbox_iterator_get_next(const bctoolbox_iterator_t *it);
/* return a new allocated iterator and delete previous*/
BCTOOLBOX_PUBLIC bctoolbox_iterator_t *bctoolbox_iterator_get_next_and_delete(bctoolbox_iterator_t *it);
/*return same pointer but pointing to next*/
BCTOOLBOX_PUBLIC bctoolbox_iterator_t *bctoolbox_iterator_get_next(bctoolbox_iterator_t *it);
BCTOOLBOX_PUBLIC bool_t bctoolbox_iterator_equals(const bctoolbox_iterator_t *a,const bctoolbox_iterator_t *b);
......
......@@ -45,6 +45,9 @@ if(ENABLE_STATIC)
add_library(bctoolbox-tester-static STATIC ${BCTOOLBOX_TESTER_SOURCE_FILES})
set_target_properties(bctoolbox-tester-static PROPERTIES OUTPUT_NAME bctoolbox-tester)
endif()
if(HAVE_LIBRT)
target_link_libraries(bctoolbox-static rt)
endif()
endif()
if(ENABLE_SHARED)
add_library(bctoolbox SHARED ${BCTOOLBOX_SOURCE_FILES})
......@@ -82,6 +85,9 @@ if(ENABLE_SHARED)
endif()
endif()
endif()
if(HAVE_LIBRT)
target_link_libraries(bctoolbox rt)
endif()
endif()
......
......@@ -43,8 +43,12 @@ extern "C" void bctoolbox_map_insert_and_delete(bctoolbox_map_t *map, bctoolbox_
bctoolbox_map_insert(map,pair);
bctoolbox_pair_delete(pair);
}
extern "C" void bctoolbox_map_erase(bctoolbox_map_t *map,bctoolbox_iterator_t *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;
}
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());
......@@ -56,10 +60,9 @@ extern "C" bctoolbox_iterator_t * bctoolbox_map_end(const bctoolbox_map_t *map)
extern "C" bctoolbox_pair_t *bctoolbox_iterator_get_pair(const bctoolbox_iterator_t *it) {
return (bctoolbox_pair_t *)&(**((mmap_long_t::iterator*)it));
}
extern "C" bctoolbox_iterator_t *bctoolbox_iterator_get_next(const bctoolbox_iterator_t *it) {
mmap_long_t::iterator *next = new mmap_long_t::iterator(*(mmap_long_t::iterator*)it);
next->operator++();
return (bctoolbox_iterator_t *)next;
extern "C" bctoolbox_iterator_t *bctoolbox_iterator_get_next(bctoolbox_iterator_t *it) {
((mmap_long_t::iterator*)it)->operator++();
return it;
}
extern "C" bctoolbox_iterator_t *bctoolbox_iterator_get_next_and_delete(bctoolbox_iterator_t *it) {
bctoolbox_iterator_t * next = bctoolbox_iterator_get_next(it);
......@@ -88,4 +91,17 @@ extern "C" void bctoolbox_pair_delete(bctoolbox_pair_t * pair) {
delete ((pair_long_t*)pair);
}
extern "C" bctoolbox_iterator_t * bctoolbox_map_find_custom(bctoolbox_map_t *map, bctoolbox_compare_func compare_func, const void *user_data) {
bctoolbox_iterator_t * end = bctoolbox_map_end(map);
for(bctoolbox_iterator_t * it = bctoolbox_map_begin(map);!bctoolbox_iterator_equals(it,end);) {
if (compare_func(bctoolbox_pair_get_second(bctoolbox_iterator_get_pair(it)),user_data)==0) {
bctoolbox_iterator_delete(end);
return it;
}
}
bctoolbox_iterator_delete(end);
return NULL;
}
......@@ -21,7 +21,7 @@
#include "bctoolbox/map.h"
#include "bctoolbox/list.h"
void multimap_insert(void) {
static void multimap_insert(void) {
bctoolbox_map_t *mmap = bctoolbox_mmap_long_new();
bctoolbox_list_t *ref = NULL;
bctoolbox_iterator_t *it;
......@@ -41,12 +41,12 @@ void multimap_insert(void) {
BC_ASSERT_EQUAL((long)bctoolbox_list_get_data(ref)
,(long)bctoolbox_pair_get_second(bctoolbox_iterator_get_pair(it))
,long, "%lu");
it = bctoolbox_iterator_get_next_and_delete(it);
it = bctoolbox_iterator_get_next(it);
}
bctoolbox_mmap_long_delete(mmap);
}
void multimap_erase(void) {
static void multimap_erase(void) {
bctoolbox_map_t *mmap = bctoolbox_mmap_long_new();
bctoolbox_iterator_t *it;
bctoolbox_iterator_t *end;
......@@ -62,10 +62,7 @@ void multimap_erase(void) {
for(it = bctoolbox_map_begin(mmap);!bctoolbox_iterator_equals(it,end);) {
long value = (long)bctoolbox_pair_get_second(bctoolbox_iterator_get_pair(it));
if (value < N/2) {
bctoolbox_iterator_t *cur = it;
it=bctoolbox_iterator_get_next(it);
bctoolbox_map_erase(mmap, cur);
bctoolbox_iterator_delete(cur);
it = bctoolbox_map_erase(mmap, it);
} else {
break;
}
......@@ -79,9 +76,31 @@ void multimap_erase(void) {
bctoolbox_iterator_delete(end);
}
static int compare_func(const void *a, const void*b) {
return (long)a == (long)b;
}
static void multimap_find_custom(void) {
bctoolbox_map_t *mmap = bctoolbox_mmap_long_new();
long i=0;
int N = 100;
for(i=0;i<N;i++) {
bctoolbox_pair_t* pair = (bctoolbox_pair_t*)bctoolbox_pair_long_new(i, (void*)((long)i));
bctoolbox_map_insert_and_delete(mmap, pair);
}
bctoolbox_iterator_t * it = bctoolbox_map_find_custom(mmap, compare_func, (void*)10l);
BC_ASSERT_EQUAL((long)bctoolbox_pair_get_second(bctoolbox_iterator_get_pair(it))
, 0
,long, "%lu");
bctoolbox_mmap_long_delete(mmap);
bctoolbox_iterator_delete(it);
}
static test_t container_tests[] = {
TEST_NO_TAG("mmap", multimap_insert),
TEST_NO_TAG("mmap", multimap_erase),
TEST_NO_TAG("mmap insert", multimap_insert),
TEST_NO_TAG("mmap erase", multimap_erase),
TEST_NO_TAG("mmap find custom", multimap_find_custom),
};
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