Tue, 28 Mar 2023 19:13:33 +0200
add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()
src/cx/array_list.h | file | annotate | diff | comparison | revisions | |
src/cx/hash_map.h | file | annotate | diff | comparison | revisions | |
src/cx/linked_list.h | file | annotate | diff | comparison | revisions | |
src/cx/list.h | file | annotate | diff | comparison | revisions | |
src/cx/map.h | file | annotate | diff | comparison | revisions | |
src/hash_map.c | file | annotate | diff | comparison | revisions | |
tests/test_map.cpp | file | annotate | diff | comparison | revisions |
--- a/src/cx/array_list.h Tue Mar 21 17:21:20 2023 +0100 +++ b/src/cx/array_list.h Tue Mar 28 19:13:33 2023 +0200 @@ -152,6 +152,9 @@ /** * Allocates an array list for storing elements with \p item_size bytes each. * + * If \p item_size is CX_STORE_POINTERS, the created list will be created as if + * cxListStorePointers() was called immediately after creation. + * * @param allocator the allocator for allocating the list memory * @param comparator the comparator for the elements * @param item_size the size of each element in bytes @@ -172,6 +175,9 @@ * If you want to call functions that need a compare function, you have to * set it immediately after creation or use cxArrayListCreate(). * + * If \p item_size is CX_STORE_POINTERS, the created list will be created as if + * cxListStorePointers() was called immediately after creation. + * * @param item_size the size of each element in bytes * @param initial_capacity the initial number of elements the array can store * @return the created list
--- a/src/cx/hash_map.h Tue Mar 21 17:21:20 2023 +0100 +++ b/src/cx/hash_map.h Tue Mar 28 19:13:33 2023 +0200 @@ -70,6 +70,9 @@ * * If \p buckets is zero, an implementation defined default will be used. * + * If \p itemsize is CX_STORE_POINTERS, the created map will be created as if + * cxMapStorePointers() was called immediately after creation. + * * @note Iterators provided by this hash map implementation provide the remove operation. * The index value of an iterator is the incremented when the iterator advanced without removal. * In other words, when the iterator is finished, \c index==size . @@ -87,27 +90,6 @@ ); /** - * Convenience function for creating a hash map that is storing pointers. - * - * If \p buckets is zero, an implementation defined default will be used. - * - * @param allocator the allocator to use - * @param buckets the initial number of buckets in this hash map - * @return a pointer to the new hash map - */ -__attribute__((__nonnull__, __warn_unused_result__)) -static inline CxMap *cxHashMapCreateForPointers( - CxAllocator *allocator, - size_t buckets -) { - CxMap *map = cxHashMapCreate(allocator, sizeof(void *), buckets); - if (map != NULL) { - map->store_pointers = true; - } - return map; -} - -/** * Increases the number of buckets, if necessary. * * The load threshold is \c 0.75*buckets. If the element count exceeds the load
--- a/src/cx/linked_list.h Tue Mar 21 17:21:20 2023 +0100 +++ b/src/cx/linked_list.h Tue Mar 28 19:13:33 2023 +0200 @@ -54,6 +54,9 @@ /** * Allocates a linked list for storing elements with \p item_size bytes each. * + * If \p item_size is CX_STORE_POINTERS, the created list will be created as if + * cxListStorePointers() was called immediately after creation. + * * @param allocator the allocator for allocating the list nodes * @param comparator the comparator for the elements * @param item_size the size of each element in bytes @@ -72,6 +75,9 @@ * to call functions that need a comparator, you must either set one immediately * after list creation or use cxLinkedListCreate(). * + * If \p item_size is CX_STORE_POINTERS, the created list will be created as if + * cxListStorePointers() was called immediately after creation. + * * @param item_size the size of each element in bytes * @return the created list */
--- a/src/cx/list.h Tue Mar 21 17:21:20 2023 +0100 +++ b/src/cx/list.h Tue Mar 28 19:13:33 2023 +0200 @@ -46,6 +46,9 @@ #endif #ifndef CX_STORE_POINTERS +/** + * Special constant used for creating collections that are storing pointers. + */ #define CX_STORE_POINTERS 0 #endif
--- a/src/cx/map.h Tue Mar 21 17:21:20 2023 +0100 +++ b/src/cx/map.h Tue Mar 28 19:13:33 2023 +0200 @@ -47,6 +47,9 @@ #endif #ifndef CX_STORE_POINTERS +/** + * Special constant used for creating collections that are storing pointers. + */ #define CX_STORE_POINTERS 0 #endif
--- a/src/hash_map.c Tue Mar 21 17:21:20 2023 +0100 +++ b/src/hash_map.c Tue Mar 28 19:13:33 2023 +0200 @@ -428,15 +428,16 @@ } // initialize base members - map->base.store_pointers = false; map->base.cl = &cx_hash_map_class; map->base.allocator = allocator; map->base.size = 0; if (itemsize > 0) { + map->base.store_pointers = false; map->base.itemsize = itemsize; } else { - cxMapStorePointers((CxMap *) map); + map->base.store_pointers = true; + map->base.itemsize = sizeof(void *); } return (CxMap *) map;
--- a/tests/test_map.cpp Tue Mar 21 17:21:20 2023 +0100 +++ b/tests/test_map.cpp Tue Mar 28 19:13:33 2023 +0200 @@ -155,7 +155,7 @@ TEST(CxHashMap, BasicOperations) { // create the map CxTestingAllocator allocator; - auto map = cxHashMapCreateForPointers(&allocator, 8); + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); // create a reference map std::unordered_map<std::string, std::string> refmap; @@ -199,7 +199,7 @@ TEST(CxHashMap, RemoveViaIterator) { CxTestingAllocator allocator; - auto map = cxHashMapCreateForPointers(&allocator, 4); + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 4); cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); @@ -228,7 +228,7 @@ TEST(CxHashMap, RehashNotRequired) { CxTestingAllocator allocator; - auto map = cxHashMapCreateForPointers(&allocator, 8); + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); @@ -248,7 +248,7 @@ TEST(CxHashMap, Rehash) { CxTestingAllocator allocator; - auto map = cxHashMapCreateForPointers(&allocator, 8); + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); @@ -277,7 +277,7 @@ TEST(CxHashMap, Clear) { CxTestingAllocator allocator; - auto map = cxHashMapCreateForPointers(&allocator, 0); + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");