diff options
| -rw-r--r-- | examples/csmap_erase.c | 52 | ||||
| -rw-r--r-- | examples/csmap_find.c | 9 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 6 | ||||
| -rw-r--r-- | include/stc/cmap.h | 8 |
4 files changed, 34 insertions, 41 deletions
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c index 66b45185..993f1837 100644 --- a/examples/csmap_erase.c +++ b/examples/csmap_erase.c @@ -3,41 +3,41 @@ #include <stc/cstr.h> #include <stdio.h> -#define i_tag my #define i_key int #define i_val_str +#define i_cnt mymap #include <stc/csmap.h> -void printmap(csmap_my map) +void printmap(mymap m) { - c_foreach (e, csmap_my, map) - printf(" [%d, %s]", e.ref->first, e.ref->second.str); - printf("\nsize() == %zu\n\n", csmap_my_size(map)); + c_foreach (elem, mymap, m) + printf(" [%d, %s]", elem.ref->first, elem.ref->second.str); + printf("\nsize() == %zu\n\n", mymap_size(m)); } int main() { - c_auto (csmap_my, m1) + c_auto (mymap, m1) { // Fill in some data to test with, one at a time - csmap_my_insert(&m1, 1, cstr_lit("A")); - csmap_my_insert(&m1, 2, cstr_lit("B")); - csmap_my_insert(&m1, 3, cstr_lit("C")); - csmap_my_insert(&m1, 4, cstr_lit("D")); - csmap_my_insert(&m1, 5, cstr_lit("E")); + mymap_insert(&m1, 1, cstr_lit("A")); + mymap_insert(&m1, 2, cstr_lit("B")); + mymap_insert(&m1, 3, cstr_lit("C")); + mymap_insert(&m1, 4, cstr_lit("D")); + mymap_insert(&m1, 5, cstr_lit("E")); puts("Starting data of map m1 is:"); printmap(m1); // The 1st member function removes an element at a given position - csmap_my_erase_at(&m1, csmap_my_advance(csmap_my_begin(&m1), 1)); + mymap_erase_at(&m1, mymap_advance(mymap_begin(&m1), 1)); puts("After the 2nd element is deleted, the map m1 is:"); printmap(m1); } - c_auto (csmap_my, m2) + c_auto (mymap, m2) { // Fill in some data to test with, one at a time, using c_apply_pair() - c_apply_pair(csmap_my, emplace, &m2, { + c_apply_pair(mymap, emplace, &m2, { {10, "Bob"}, {11, "Rob"}, {12, "Robert"}, @@ -47,30 +47,30 @@ int main() puts("Starting data of map m2 is:"); printmap(m2); - csmap_my_iter_t it1 = csmap_my_advance(csmap_my_begin(&m2), 1); - csmap_my_iter_t it2 = csmap_my_find(&m2, csmap_my_back(&m2)->first); + mymap_iter_t it1 = mymap_advance(mymap_begin(&m2), 1); + mymap_iter_t it2 = mymap_find(&m2, mymap_back(&m2)->first); // The 2nd member function removes elements // in the range [First, Last) - csmap_my_erase_range(&m2, it1, it2); + mymap_erase_range(&m2, it1, it2); puts("After the middle elements are deleted, the map m2 is:"); printmap(m2); } - c_auto (csmap_my, m3) + c_auto (mymap, m3) { // Fill in some data to test with, one at a time, using emplace - csmap_my_emplace(&m3, 1, "red"); - csmap_my_emplace(&m3, 2, "yellow"); - csmap_my_emplace(&m3, 3, "blue"); - csmap_my_emplace(&m3, 4, "green"); - csmap_my_emplace(&m3, 5, "orange"); - csmap_my_emplace(&m3, 6, "purple"); - csmap_my_emplace(&m3, 7, "pink"); + mymap_emplace(&m3, 1, "red"); + mymap_emplace(&m3, 2, "yellow"); + mymap_emplace(&m3, 3, "blue"); + mymap_emplace(&m3, 4, "green"); + mymap_emplace(&m3, 5, "orange"); + mymap_emplace(&m3, 6, "purple"); + mymap_emplace(&m3, 7, "pink"); puts("Starting data of map m3 is:"); printmap(m3); // The 3rd member function removes elements with a given Key - size_t count = csmap_my_erase(&m3, 2); + size_t count = mymap_erase(&m3, 2); // The 3rd member function also returns the number of elements removed printf("The number of elements removed from m3 is: %zu\n", count); puts("After the element with a key of 2 is deleted, the map m3 is:"); diff --git a/examples/csmap_find.c b/examples/csmap_find.c index 1b3320c9..eca14c52 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -33,9 +33,9 @@ using_print_collection(cvec_istr) void findit(csmap_istr c, csmap_istr_key_t val) { printf("Trying find() on value %d\n", val); - csmap_istr_value_t* result = csmap_istr_get(&c, val); // easier with get than find. - if (result) { - printf("Element found: "); print_elem(csmap_istr_value_toraw(result)); puts(""); + csmap_istr_iter_t result = csmap_istr_find(&c, val); // prefer contains() or get() + if (result.ref != csmap_istr_end(&c).ref) { + printf("Element found: "); print_elem(csmap_istr_value_toraw(result.ref)); puts(""); } else { puts("Element not found."); } @@ -61,7 +61,8 @@ int main() puts("Inserting the following vector data into m1:"); print_collection_cvec_istr(v); - c_foreach (i, cvec_istr, v) csmap_istr_emplace(&m1, i.ref->first, i.ref->second); + c_foreach (i, cvec_istr, cvec_istr_begin(&v), cvec_istr_end(&v)) + csmap_istr_emplace(&m1, i.ref->first, i.ref->second); puts("The modified map m1 is (key, value):"); print_collection_csmap_istr(m1); diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index af56f43a..c9ca7ad0 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -168,17 +168,11 @@ STC_API uint64_t c_default_hash(const void *key, size_t len); for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \
CX##_##method(_c_cx, _c_arr[_c_i].first, _c_arr[_c_i].second); \
} while (0)
-
#define c_apply_n(CX, method, cx, arr, n) do { \
CX* _c_cx = cx; \
for (const CX##_rawvalue_t *_c_i = arr, *_c_end = _c_i+(n); _c_i != _c_end; ++_c_i) \
CX##_##method(_c_cx, *_c_i); \
} while (0)
-#define c_apply_pair_n(CX, method, cx, arr, n) do { \
- CX* _c_cx = cx; \
- for (const CX##_rawvalue_t *_c_i = arr, *_c_end = _c_i+(n); _c_i != _c_end; ++_c_i) \
- CX##_##method(_c_cx, _c_i->first, _c_i->second); \
-} while (0)
#define c_del(CX, ...) do { \
CX* _c_arr[] = {__VA_ARGS__}; \
diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 1cb61d00..203f0e79 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -162,11 +162,9 @@ cx_memb(_insert)(Self* self, i_key _key cx_MAP_ONLY(, i_val _mapped)) { STC_INLINE cx_iter_t
cx_memb(_find)(const Self* self, i_keyraw rkey) {
- cx_size_t idx = self->bucket_count;
- if (self->size) {
- chash_bucket_t b = cx_memb(_bucket_)(self, &rkey);
- if (self->_hashx[b.idx]) idx = b.idx;
- }
+ cx_size_t idx;
+ if (!(self->size && self->_hashx[idx = cx_memb(_bucket_)(self, &rkey).idx]))
+ idx = self->bucket_count;
return c_make(cx_iter_t){self->table+idx, self->_hashx+idx};
}
|
