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 |
1.1 --- a/src/cx/array_list.h Tue Mar 21 17:21:20 2023 +0100 1.2 +++ b/src/cx/array_list.h Tue Mar 28 19:13:33 2023 +0200 1.3 @@ -152,6 +152,9 @@ 1.4 /** 1.5 * Allocates an array list for storing elements with \p item_size bytes each. 1.6 * 1.7 + * If \p item_size is CX_STORE_POINTERS, the created list will be created as if 1.8 + * cxListStorePointers() was called immediately after creation. 1.9 + * 1.10 * @param allocator the allocator for allocating the list memory 1.11 * @param comparator the comparator for the elements 1.12 * @param item_size the size of each element in bytes 1.13 @@ -172,6 +175,9 @@ 1.14 * If you want to call functions that need a compare function, you have to 1.15 * set it immediately after creation or use cxArrayListCreate(). 1.16 * 1.17 + * If \p item_size is CX_STORE_POINTERS, the created list will be created as if 1.18 + * cxListStorePointers() was called immediately after creation. 1.19 + * 1.20 * @param item_size the size of each element in bytes 1.21 * @param initial_capacity the initial number of elements the array can store 1.22 * @return the created list
2.1 --- a/src/cx/hash_map.h Tue Mar 21 17:21:20 2023 +0100 2.2 +++ b/src/cx/hash_map.h Tue Mar 28 19:13:33 2023 +0200 2.3 @@ -70,6 +70,9 @@ 2.4 * 2.5 * If \p buckets is zero, an implementation defined default will be used. 2.6 * 2.7 + * If \p itemsize is CX_STORE_POINTERS, the created map will be created as if 2.8 + * cxMapStorePointers() was called immediately after creation. 2.9 + * 2.10 * @note Iterators provided by this hash map implementation provide the remove operation. 2.11 * The index value of an iterator is the incremented when the iterator advanced without removal. 2.12 * In other words, when the iterator is finished, \c index==size . 2.13 @@ -87,27 +90,6 @@ 2.14 ); 2.15 2.16 /** 2.17 - * Convenience function for creating a hash map that is storing pointers. 2.18 - * 2.19 - * If \p buckets is zero, an implementation defined default will be used. 2.20 - * 2.21 - * @param allocator the allocator to use 2.22 - * @param buckets the initial number of buckets in this hash map 2.23 - * @return a pointer to the new hash map 2.24 - */ 2.25 -__attribute__((__nonnull__, __warn_unused_result__)) 2.26 -static inline CxMap *cxHashMapCreateForPointers( 2.27 - CxAllocator *allocator, 2.28 - size_t buckets 2.29 -) { 2.30 - CxMap *map = cxHashMapCreate(allocator, sizeof(void *), buckets); 2.31 - if (map != NULL) { 2.32 - map->store_pointers = true; 2.33 - } 2.34 - return map; 2.35 -} 2.36 - 2.37 -/** 2.38 * Increases the number of buckets, if necessary. 2.39 * 2.40 * The load threshold is \c 0.75*buckets. If the element count exceeds the load
3.1 --- a/src/cx/linked_list.h Tue Mar 21 17:21:20 2023 +0100 3.2 +++ b/src/cx/linked_list.h Tue Mar 28 19:13:33 2023 +0200 3.3 @@ -54,6 +54,9 @@ 3.4 /** 3.5 * Allocates a linked list for storing elements with \p item_size bytes each. 3.6 * 3.7 + * If \p item_size is CX_STORE_POINTERS, the created list will be created as if 3.8 + * cxListStorePointers() was called immediately after creation. 3.9 + * 3.10 * @param allocator the allocator for allocating the list nodes 3.11 * @param comparator the comparator for the elements 3.12 * @param item_size the size of each element in bytes 3.13 @@ -72,6 +75,9 @@ 3.14 * to call functions that need a comparator, you must either set one immediately 3.15 * after list creation or use cxLinkedListCreate(). 3.16 * 3.17 + * If \p item_size is CX_STORE_POINTERS, the created list will be created as if 3.18 + * cxListStorePointers() was called immediately after creation. 3.19 + * 3.20 * @param item_size the size of each element in bytes 3.21 * @return the created list 3.22 */
4.1 --- a/src/cx/list.h Tue Mar 21 17:21:20 2023 +0100 4.2 +++ b/src/cx/list.h Tue Mar 28 19:13:33 2023 +0200 4.3 @@ -46,6 +46,9 @@ 4.4 #endif 4.5 4.6 #ifndef CX_STORE_POINTERS 4.7 +/** 4.8 + * Special constant used for creating collections that are storing pointers. 4.9 + */ 4.10 #define CX_STORE_POINTERS 0 4.11 #endif 4.12
5.1 --- a/src/cx/map.h Tue Mar 21 17:21:20 2023 +0100 5.2 +++ b/src/cx/map.h Tue Mar 28 19:13:33 2023 +0200 5.3 @@ -47,6 +47,9 @@ 5.4 #endif 5.5 5.6 #ifndef CX_STORE_POINTERS 5.7 +/** 5.8 + * Special constant used for creating collections that are storing pointers. 5.9 + */ 5.10 #define CX_STORE_POINTERS 0 5.11 #endif 5.12
6.1 --- a/src/hash_map.c Tue Mar 21 17:21:20 2023 +0100 6.2 +++ b/src/hash_map.c Tue Mar 28 19:13:33 2023 +0200 6.3 @@ -428,15 +428,16 @@ 6.4 } 6.5 6.6 // initialize base members 6.7 - map->base.store_pointers = false; 6.8 map->base.cl = &cx_hash_map_class; 6.9 map->base.allocator = allocator; 6.10 map->base.size = 0; 6.11 6.12 if (itemsize > 0) { 6.13 + map->base.store_pointers = false; 6.14 map->base.itemsize = itemsize; 6.15 } else { 6.16 - cxMapStorePointers((CxMap *) map); 6.17 + map->base.store_pointers = true; 6.18 + map->base.itemsize = sizeof(void *); 6.19 } 6.20 6.21 return (CxMap *) map;
7.1 --- a/tests/test_map.cpp Tue Mar 21 17:21:20 2023 +0100 7.2 +++ b/tests/test_map.cpp Tue Mar 28 19:13:33 2023 +0200 7.3 @@ -155,7 +155,7 @@ 7.4 TEST(CxHashMap, BasicOperations) { 7.5 // create the map 7.6 CxTestingAllocator allocator; 7.7 - auto map = cxHashMapCreateForPointers(&allocator, 8); 7.8 + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); 7.9 7.10 // create a reference map 7.11 std::unordered_map<std::string, std::string> refmap; 7.12 @@ -199,7 +199,7 @@ 7.13 7.14 TEST(CxHashMap, RemoveViaIterator) { 7.15 CxTestingAllocator allocator; 7.16 - auto map = cxHashMapCreateForPointers(&allocator, 4); 7.17 + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 4); 7.18 7.19 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 7.20 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); 7.21 @@ -228,7 +228,7 @@ 7.22 7.23 TEST(CxHashMap, RehashNotRequired) { 7.24 CxTestingAllocator allocator; 7.25 - auto map = cxHashMapCreateForPointers(&allocator, 8); 7.26 + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); 7.27 7.28 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 7.29 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); 7.30 @@ -248,7 +248,7 @@ 7.31 7.32 TEST(CxHashMap, Rehash) { 7.33 CxTestingAllocator allocator; 7.34 - auto map = cxHashMapCreateForPointers(&allocator, 8); 7.35 + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); 7.36 7.37 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 7.38 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); 7.39 @@ -277,7 +277,7 @@ 7.40 7.41 TEST(CxHashMap, Clear) { 7.42 CxTestingAllocator allocator; 7.43 - auto map = cxHashMapCreateForPointers(&allocator, 0); 7.44 + auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); 7.45 7.46 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 7.47 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");