2 months ago
fix wrong selection in list onactivate event, when the item was pre-selected
174 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | /** | |
440 | 29 | * @file hash_map.h |
30 | * @brief Hash map implementation. | |
31 | * @author Mike Becker | |
32 | * @author Olaf Wintermann | |
33 | * @copyright 2-Clause BSD License | |
174 | 34 | */ |
35 | ||
36 | #ifndef UCX_HASH_MAP_H | |
37 | #define UCX_HASH_MAP_H | |
38 | ||
39 | #include "map.h" | |
40 | ||
41 | #ifdef __cplusplus | |
42 | extern "C" { | |
43 | #endif | |
44 | ||
45 | /** Internal structure for an element of a hash map. */ | |
46 | struct cx_hash_map_element_s; | |
47 | ||
48 | /** | |
49 | * Internal structure for a hash map. | |
50 | */ | |
51 | struct cx_hash_map_s { | |
52 | /** | |
53 | * Base structure for maps. | |
54 | */ | |
55 | struct cx_map_s base; | |
56 | /** | |
57 | * The buckets of this map, each containing a linked list of elements. | |
58 | */ | |
59 | struct cx_hash_map_element_s **buckets; | |
60 | /** | |
61 | * The number of buckets. | |
62 | */ | |
63 | size_t bucket_count; | |
64 | }; | |
65 | ||
66 | ||
67 | /** | |
68 | * Creates a new hash map with the specified number of buckets. | |
69 | * | |
440 | 70 | * If @p buckets is zero, an implementation defined default will be used. |
174 | 71 | * |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
72 | * If @p elem_size is #CX_STORE_POINTERS, the created map stores pointers instead of |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
73 | * copies of the added elements. |
174 | 74 | * |
75 | * @note Iterators provided by this hash map implementation provide the remove operation. | |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
76 | * The index value of an iterator is incremented when the iterator advanced without removal. |
440 | 77 | * In other words, when the iterator is finished, @c index==size . |
174 | 78 | * |
79 | * @param allocator the allocator to use | |
440 | 80 | * (if @c NULL, a default stdlib allocator will be used) |
174 | 81 | * @param itemsize the size of one element |
82 | * @param buckets the initial number of buckets in this hash map | |
83 | * @return a pointer to the new hash map | |
84 | */ | |
440 | 85 | cx_attr_nodiscard |
86 | cx_attr_malloc | |
87 | cx_attr_dealloc(cxMapFree, 1) | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
88 | cx_attr_export |
174 | 89 | CxMap *cxHashMapCreate( |
324 | 90 | const CxAllocator *allocator, |
174 | 91 | size_t itemsize, |
92 | size_t buckets | |
93 | ); | |
94 | ||
95 | /** | |
96 | * Creates a new hash map with a default number of buckets. | |
97 | * | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
98 | * If @p elem_size is #CX_STORE_POINTERS, the created map stores pointers instead of |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
99 | * copies of the added elements. |
174 | 100 | * |
101 | * @note Iterators provided by this hash map implementation provide the remove operation. | |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
102 | * The index value of an iterator is incremented when the iterator advanced without removal. |
440 | 103 | * In other words, when the iterator is finished, @c index==size . |
174 | 104 | * |
440 | 105 | * @param itemsize (@c size_t) the size of one element |
106 | * @return (@c CxMap*) a pointer to the new hash map | |
174 | 107 | */ |
440 | 108 | #define cxHashMapCreateSimple(itemsize) cxHashMapCreate(NULL, itemsize, 0) |
174 | 109 | |
110 | /** | |
111 | * Increases the number of buckets, if necessary. | |
112 | * | |
440 | 113 | * The load threshold is @c 0.75*buckets. If the element count exceeds the load |
174 | 114 | * threshold, the map will be rehashed. Otherwise, no action is performed and |
115 | * this function simply returns 0. | |
116 | * | |
117 | * The rehashing process ensures, that the number of buckets is at least | |
118 | * 2.5 times the element count. So there is enough room for additional | |
119 | * elements without the need of another soon rehashing. | |
120 | * | |
121 | * You can use this function after filling a map to increase access performance. | |
122 | * | |
123 | * @note If the specified map is not a hash map, the behavior is undefined. | |
124 | * | |
125 | * @param map the map to rehash | |
440 | 126 | * @retval zero success |
127 | * @retval non-zero if a memory allocation error occurred | |
174 | 128 | */ |
440 | 129 | cx_attr_nonnull |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
130 | cx_attr_export |
174 | 131 | int cxMapRehash(CxMap *map); |
132 | ||
133 | ||
134 | #ifdef __cplusplus | |
135 | } // extern "C" | |
136 | #endif | |
137 | ||
138 | #endif // UCX_HASH_MAP_H |