src/cx/hash_map.h

changeset 658
56c62780582e
parent 563
69a83fad8a35
child 669
dce9b8450656
equal deleted inserted replaced
657:3eeadf666d6b 658:56c62780582e
42 #ifdef __cplusplus 42 #ifdef __cplusplus
43 extern "C" { 43 extern "C" {
44 #endif 44 #endif
45 45
46 /** Internal structure for an element of a hash map. */ 46 /** Internal structure for an element of a hash map. */
47 struct cx_hash_map_element_s { 47 struct cx_hash_map_element_s;
48 /** The value data. */
49 void *data;
50
51 /** A pointer to the next element in the current bucket. */
52 struct cx_hash_map_element_s *next;
53
54 /** The corresponding key. */
55 CxHashKey key;
56 };
57 48
58 /** 49 /**
59 * Internal structure for a hash map. 50 * Internal structure for a hash map.
60 */ 51 */
61 struct cx_hash_map_s { 52 struct cx_hash_map_s {
82 * @note Iterators provided by this hash map implementation provide the remove operation. 73 * @note Iterators provided by this hash map implementation provide the remove operation.
83 * The index value of an iterator is the incremented when the iterator advanced without removal. 74 * The index value of an iterator is the incremented when the iterator advanced without removal.
84 * In other words, when the iterator is finished, \c index==size . 75 * In other words, when the iterator is finished, \c index==size .
85 * 76 *
86 * @param allocator the allocator to use 77 * @param allocator the allocator to use
78 * @param itemsize the size of one element
87 * @param buckets the initial number of buckets in this hash map 79 * @param buckets the initial number of buckets in this hash map
88 * @return a pointer to the new hash map 80 * @return a pointer to the new hash map
89 */ 81 */
90 __attribute__((__nonnull__, __warn_unused_result__)) 82 __attribute__((__nonnull__, __warn_unused_result__))
91 CxMap *cxHashMapCreate( 83 CxMap *cxHashMapCreate(
92 CxAllocator *allocator, 84 CxAllocator *allocator,
85 size_t itemsize,
93 size_t buckets 86 size_t buckets
94 ); 87 );
88
89 /**
90 * Convenience function for creating a hash map that is storing pointers.
91 *
92 * If \p buckets is zero, an implementation defined default will be used.
93 *
94 * @param allocator the allocator to use
95 * @param buckets the initial number of buckets in this hash map
96 * @return a pointer to the new hash map
97 */
98 __attribute__((__nonnull__, __warn_unused_result__))
99 static inline CxMap *cxHashMapCreateForPointers(
100 CxAllocator *allocator,
101 size_t buckets
102 ) {
103 CxMap *map = cxHashMapCreate(allocator, sizeof(void *), buckets);
104 if (map != NULL) {
105 map->store_pointers = true;
106 }
107 return map;
108 }
95 109
96 /** 110 /**
97 * Increases the number of buckets, if necessary. 111 * Increases the number of buckets, if necessary.
98 * 112 *
99 * The load threshold is \c 0.75*buckets. If the element count exceeds the load 113 * The load threshold is \c 0.75*buckets. If the element count exceeds the load

mercurial