/src/glib/glib/gbsearcharray.h
Line | Count | Source |
1 | | /* GBSearchArray - Binary Searchable Array implementation |
2 | | * Copyright (C) 2000-2003 Tim Janik |
3 | | * |
4 | | * This software is provided "as is"; redistribution and modification |
5 | | * is permitted, provided that the following disclaimer is retained. |
6 | | * |
7 | | * This software is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10 | | * In no event shall the authors or contributors be liable for any |
11 | | * direct, indirect, incidental, special, exemplary, or consequential |
12 | | * damages (including, but not limited to, procurement of substitute |
13 | | * goods or services; loss of use, data, or profits; or business |
14 | | * interruption) however caused and on any theory of liability, whether |
15 | | * in contract, strict liability, or tort (including negligence or |
16 | | * otherwise) arising in any way out of the use of this software, even |
17 | | * if advised of the possibility of such damage. |
18 | | */ |
19 | | #ifndef __G_BSEARCH_ARRAY_H__ |
20 | | #define __G_BSEARCH_ARRAY_H__ |
21 | | |
22 | | #include <glib.h> |
23 | | #include <string.h> |
24 | | |
25 | | |
26 | | G_BEGIN_DECLS /* c++ guards */ |
27 | | |
28 | | /* this implementation is intended to be usable in third-party code |
29 | | * simply by pasting the contents of this file. as such, the |
30 | | * implementation needs to be self-contained within this file. |
31 | | */ |
32 | | |
33 | | /* convenience macro to avoid signed overflow for value comparisons */ |
34 | 321k | #define G_BSEARCH_ARRAY_CMP(v1,v2) ((v1) > (v2) ? +1 : (v1) == (v2) ? 0 : -1) |
35 | | |
36 | | |
37 | | /* --- typedefs --- */ |
38 | | typedef gint (*GBSearchCompareFunc) (gconstpointer bsearch_node1, /* key */ |
39 | | gconstpointer bsearch_node2); |
40 | | typedef enum |
41 | | { |
42 | | G_BSEARCH_ARRAY_ALIGN_POWER2 = 1 << 0, /* align memory to power2 sizes */ |
43 | | G_BSEARCH_ARRAY_AUTO_SHRINK = 1 << 1 /* shrink array upon removal */ |
44 | | } GBSearchArrayFlags; |
45 | | |
46 | | |
47 | | /* --- structures --- */ |
48 | | typedef struct |
49 | | { |
50 | | guint sizeof_node; |
51 | | GBSearchCompareFunc cmp_nodes; |
52 | | guint flags; |
53 | | } GBSearchConfig; |
54 | | typedef union |
55 | | { |
56 | | guint n_nodes; |
57 | | /*< private >*/ |
58 | | gpointer alignment_dummy1; |
59 | | glong alignment_dummy2; |
60 | | gdouble alignment_dummy3; |
61 | | } GBSearchArray; |
62 | | |
63 | | |
64 | | /* --- public API --- */ |
65 | | static inline GBSearchArray* g_bsearch_array_create (const GBSearchConfig *bconfig); |
66 | | static inline gpointer g_bsearch_array_get_nth (GBSearchArray *barray, |
67 | | const GBSearchConfig *bconfig, |
68 | | guint nth); |
69 | | static inline guint g_bsearch_array_get_index (GBSearchArray *barray, |
70 | | const GBSearchConfig *bconfig, |
71 | | gconstpointer node_in_array); |
72 | | static inline GBSearchArray* g_bsearch_array_remove (GBSearchArray *barray, |
73 | | const GBSearchConfig *bconfig, |
74 | | guint index_); |
75 | | /* provide uninitialized space at index for node insertion */ |
76 | | static inline GBSearchArray* g_bsearch_array_grow (GBSearchArray *barray, |
77 | | const GBSearchConfig *bconfig, |
78 | | guint index); |
79 | | /* insert key_node into array if it does not exist, otherwise do nothing */ |
80 | | static inline GBSearchArray* g_bsearch_array_insert (GBSearchArray *barray, |
81 | | const GBSearchConfig *bconfig, |
82 | | gconstpointer key_node); |
83 | | /* insert key_node into array if it does not exist, |
84 | | * otherwise replace the existing node's contents with key_node |
85 | | */ |
86 | | static inline GBSearchArray* g_bsearch_array_replace (GBSearchArray *barray, |
87 | | const GBSearchConfig *bconfig, |
88 | | gconstpointer key_node); |
89 | | static inline void g_bsearch_array_free (GBSearchArray *barray, |
90 | | const GBSearchConfig *bconfig); |
91 | 1.48k | #define g_bsearch_array_get_n_nodes(barray) (((GBSearchArray*) (barray))->n_nodes) |
92 | | |
93 | | /* g_bsearch_array_lookup(): |
94 | | * return NULL or exact match node |
95 | | */ |
96 | | #define g_bsearch_array_lookup(barray, bconfig, key_node) \ |
97 | 52.7k | g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 0) |
98 | | |
99 | | /* g_bsearch_array_lookup_sibling(): |
100 | | * return NULL for barray->n_nodes==0, otherwise return the |
101 | | * exact match node, or, if there's no such node, return the |
102 | | * node last visited, which is pretty close to an exact match |
103 | | * (will be one off into either direction). |
104 | | */ |
105 | | #define g_bsearch_array_lookup_sibling(barray, bconfig, key_node) \ |
106 | | g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 1) |
107 | | |
108 | | /* g_bsearch_array_lookup_insertion(): |
109 | | * return NULL for barray->n_nodes==0 or exact match, otherwise |
110 | | * return the node where key_node should be inserted (may be one |
111 | | * after end, i.e. g_bsearch_array_get_index(result) <= barray->n_nodes). |
112 | | */ |
113 | | #define g_bsearch_array_lookup_insertion(barray, bconfig, key_node) \ |
114 | 18.7k | g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 2) |
115 | | |
116 | | |
117 | | /* --- implementation --- */ |
118 | | /* helper macro to cut down realloc()s */ |
119 | 30.8k | #define G_BSEARCH_UPPER_POWER2(n) ((n) ? 1 << g_bit_storage ((n) - 1) : 0) |
120 | 122k | #define G_BSEARCH_ARRAY_NODES(barray) (((guint8*) (barray)) + sizeof (GBSearchArray)) |
121 | | static inline GBSearchArray* |
122 | | g_bsearch_array_create (const GBSearchConfig *bconfig) |
123 | 1.42k | { |
124 | 1.42k | GBSearchArray *barray; |
125 | 1.42k | guint size; |
126 | | |
127 | 1.42k | g_return_val_if_fail (bconfig != NULL, NULL); |
128 | | |
129 | 1.42k | size = sizeof (GBSearchArray) + bconfig->sizeof_node; |
130 | 1.42k | if (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2) |
131 | 180 | size = G_BSEARCH_UPPER_POWER2 (size); |
132 | 1.42k | barray = (GBSearchArray *) g_malloc (size); |
133 | 1.42k | memset (barray, 0, sizeof (GBSearchArray)); |
134 | | |
135 | 1.42k | return barray; |
136 | 1.42k | } gsignal.c:g_bsearch_array_create Line | Count | Source | 123 | 1.33k | { | 124 | 1.33k | GBSearchArray *barray; | 125 | 1.33k | guint size; | 126 | | | 127 | 1.33k | g_return_val_if_fail (bconfig != NULL, NULL); | 128 | | | 129 | 1.33k | size = sizeof (GBSearchArray) + bconfig->sizeof_node; | 130 | 1.33k | if (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2) | 131 | 90 | size = G_BSEARCH_UPPER_POWER2 (size); | 132 | 1.33k | barray = (GBSearchArray *) g_malloc (size); | 133 | 1.33k | memset (barray, 0, sizeof (GBSearchArray)); | 134 | | | 135 | 1.33k | return barray; | 136 | 1.33k | } |
gvalue.c:g_bsearch_array_create Line | Count | Source | 123 | 90 | { | 124 | 90 | GBSearchArray *barray; | 125 | 90 | guint size; | 126 | | | 127 | 90 | g_return_val_if_fail (bconfig != NULL, NULL); | 128 | | | 129 | 90 | size = sizeof (GBSearchArray) + bconfig->sizeof_node; | 130 | 90 | if (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2) | 131 | 90 | size = G_BSEARCH_UPPER_POWER2 (size); | 132 | 90 | barray = (GBSearchArray *) g_malloc (size); | 133 | 90 | memset (barray, 0, sizeof (GBSearchArray)); | 134 | | | 135 | 90 | return barray; | 136 | 90 | } |
|
137 | | static inline gpointer |
138 | | g_bsearch_array_lookup_fuzzy (GBSearchArray *barray, |
139 | | const GBSearchConfig *bconfig, |
140 | | gconstpointer key_node, |
141 | | const guint sibling_or_after); |
142 | | static inline gpointer |
143 | | g_bsearch_array_lookup_fuzzy (GBSearchArray *barray, |
144 | | const GBSearchConfig *bconfig, |
145 | | gconstpointer key_node, |
146 | | const guint sibling_or_after) |
147 | 71.5k | { |
148 | 71.5k | GBSearchCompareFunc cmp_nodes = bconfig->cmp_nodes; |
149 | 71.5k | guint8 *check = NULL, *nodes = G_BSEARCH_ARRAY_NODES (barray); |
150 | 71.5k | guint n_nodes = barray->n_nodes, offs = 0; |
151 | 71.5k | guint sizeof_node = bconfig->sizeof_node; |
152 | 71.5k | gint cmp = 0; |
153 | | |
154 | 309k | while (offs < n_nodes) |
155 | 262k | { |
156 | 262k | guint i = (offs + n_nodes) >> 1; |
157 | | |
158 | 262k | check = nodes + i * sizeof_node; |
159 | 262k | cmp = cmp_nodes (key_node, check); |
160 | 262k | if (cmp == 0) |
161 | 24.3k | return sibling_or_after > 1 ? NULL : check; |
162 | 238k | else if (cmp < 0) |
163 | 74.9k | n_nodes = i; |
164 | 163k | else /* (cmp > 0) */ |
165 | 163k | offs = i + 1; |
166 | 262k | } |
167 | | |
168 | | /* check is last mismatch, cmp > 0 indicates greater key */ |
169 | 47.2k | return G_LIKELY (!sibling_or_after) ? NULL : (sibling_or_after > 1 && cmp > 0) ? check + sizeof_node : check; |
170 | 71.5k | } gsignal.c:g_bsearch_array_lookup_fuzzy Line | Count | Source | 147 | 41.0k | { | 148 | 41.0k | GBSearchCompareFunc cmp_nodes = bconfig->cmp_nodes; | 149 | 41.0k | guint8 *check = NULL, *nodes = G_BSEARCH_ARRAY_NODES (barray); | 150 | 41.0k | guint n_nodes = barray->n_nodes, offs = 0; | 151 | 41.0k | guint sizeof_node = bconfig->sizeof_node; | 152 | 41.0k | gint cmp = 0; | 153 | | | 154 | 98.1k | while (offs < n_nodes) | 155 | 81.4k | { | 156 | 81.4k | guint i = (offs + n_nodes) >> 1; | 157 | | | 158 | 81.4k | check = nodes + i * sizeof_node; | 159 | 81.4k | cmp = cmp_nodes (key_node, check); | 160 | 81.4k | if (cmp == 0) | 161 | 24.3k | return sibling_or_after > 1 ? NULL : check; | 162 | 57.1k | else if (cmp < 0) | 163 | 33.3k | n_nodes = i; | 164 | 23.8k | else /* (cmp > 0) */ | 165 | 23.8k | offs = i + 1; | 166 | 81.4k | } | 167 | | | 168 | | /* check is last mismatch, cmp > 0 indicates greater key */ | 169 | 16.7k | return G_LIKELY (!sibling_or_after) ? NULL : (sibling_or_after > 1 && cmp > 0) ? check + sizeof_node : check; | 170 | 41.0k | } |
gvalue.c:g_bsearch_array_lookup_fuzzy Line | Count | Source | 147 | 30.5k | { | 148 | 30.5k | GBSearchCompareFunc cmp_nodes = bconfig->cmp_nodes; | 149 | 30.5k | guint8 *check = NULL, *nodes = G_BSEARCH_ARRAY_NODES (barray); | 150 | 30.5k | guint n_nodes = barray->n_nodes, offs = 0; | 151 | 30.5k | guint sizeof_node = bconfig->sizeof_node; | 152 | 30.5k | gint cmp = 0; | 153 | | | 154 | 211k | while (offs < n_nodes) | 155 | 181k | { | 156 | 181k | guint i = (offs + n_nodes) >> 1; | 157 | | | 158 | 181k | check = nodes + i * sizeof_node; | 159 | 181k | cmp = cmp_nodes (key_node, check); | 160 | 181k | if (cmp == 0) | 161 | 0 | return sibling_or_after > 1 ? NULL : check; | 162 | 181k | else if (cmp < 0) | 163 | 41.5k | n_nodes = i; | 164 | 139k | else /* (cmp > 0) */ | 165 | 139k | offs = i + 1; | 166 | 181k | } | 167 | | | 168 | | /* check is last mismatch, cmp > 0 indicates greater key */ | 169 | 30.5k | return G_LIKELY (!sibling_or_after) ? NULL : (sibling_or_after > 1 && cmp > 0) ? check + sizeof_node : check; | 170 | 30.5k | } |
|
171 | | static inline gpointer |
172 | | g_bsearch_array_get_nth (GBSearchArray *barray, |
173 | | const GBSearchConfig *bconfig, |
174 | | guint nth) |
175 | 2.71k | { |
176 | 2.71k | return (G_LIKELY (nth < barray->n_nodes) ? |
177 | 2.71k | G_BSEARCH_ARRAY_NODES (barray) + nth * bconfig->sizeof_node : |
178 | 2.71k | NULL); |
179 | 2.71k | } gsignal.c:g_bsearch_array_get_nth Line | Count | Source | 175 | 2.71k | { | 176 | 2.71k | return (G_LIKELY (nth < barray->n_nodes) ? | 177 | 2.71k | G_BSEARCH_ARRAY_NODES (barray) + nth * bconfig->sizeof_node : | 178 | | NULL); | 179 | 2.71k | } |
Unexecuted instantiation: gvalue.c:g_bsearch_array_get_nth |
180 | | static inline guint |
181 | | g_bsearch_array_get_index (GBSearchArray *barray, |
182 | | const GBSearchConfig *bconfig, |
183 | | gconstpointer node_in_array) |
184 | 15.2k | { |
185 | 15.2k | guint distance = ((guint8*) node_in_array) - G_BSEARCH_ARRAY_NODES (barray); |
186 | | |
187 | 15.2k | g_return_val_if_fail (node_in_array != NULL, barray->n_nodes); |
188 | | |
189 | 15.2k | distance /= bconfig->sizeof_node; |
190 | | |
191 | 15.2k | return MIN (distance, barray->n_nodes + 1); /* may return one after end */ |
192 | 15.2k | } gsignal.c:g_bsearch_array_get_index Line | Count | Source | 184 | 8 | { | 185 | 8 | guint distance = ((guint8*) node_in_array) - G_BSEARCH_ARRAY_NODES (barray); | 186 | | | 187 | 8 | g_return_val_if_fail (node_in_array != NULL, barray->n_nodes); | 188 | | | 189 | 8 | distance /= bconfig->sizeof_node; | 190 | | | 191 | 8 | return MIN (distance, barray->n_nodes + 1); /* may return one after end */ | 192 | 8 | } |
gvalue.c:g_bsearch_array_get_index Line | Count | Source | 184 | 15.2k | { | 185 | 15.2k | guint distance = ((guint8*) node_in_array) - G_BSEARCH_ARRAY_NODES (barray); | 186 | | | 187 | 15.2k | g_return_val_if_fail (node_in_array != NULL, barray->n_nodes); | 188 | | | 189 | 15.2k | distance /= bconfig->sizeof_node; | 190 | | | 191 | 15.2k | return MIN (distance, barray->n_nodes + 1); /* may return one after end */ | 192 | 15.2k | } |
|
193 | | static inline GBSearchArray* |
194 | | g_bsearch_array_grow (GBSearchArray *barray, |
195 | | const GBSearchConfig *bconfig, |
196 | | guint index_) |
197 | 16.5k | { |
198 | 16.5k | guint old_size = barray->n_nodes * bconfig->sizeof_node; |
199 | 16.5k | guint new_size = old_size + bconfig->sizeof_node; |
200 | 16.5k | guint8 *node; |
201 | | |
202 | 16.5k | g_return_val_if_fail (index_ <= barray->n_nodes, NULL); |
203 | | |
204 | 16.5k | if (G_UNLIKELY (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2)) |
205 | 15.3k | { |
206 | 15.3k | new_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + new_size); |
207 | 15.3k | old_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + old_size); |
208 | 15.3k | if (old_size != new_size) |
209 | 769 | barray = (GBSearchArray *) g_realloc (barray, new_size); |
210 | 15.3k | } |
211 | 1.24k | else |
212 | 1.24k | barray = (GBSearchArray *) g_realloc (barray, sizeof (GBSearchArray) + new_size); |
213 | 16.5k | node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node; |
214 | 16.5k | memmove (node + bconfig->sizeof_node, node, (barray->n_nodes - index_) * bconfig->sizeof_node); |
215 | 16.5k | barray->n_nodes += 1; |
216 | 16.5k | return barray; |
217 | 16.5k | } gsignal.c:g_bsearch_array_grow Line | Count | Source | 197 | 1.29k | { | 198 | 1.29k | guint old_size = barray->n_nodes * bconfig->sizeof_node; | 199 | 1.29k | guint new_size = old_size + bconfig->sizeof_node; | 200 | 1.29k | guint8 *node; | 201 | | | 202 | 1.29k | g_return_val_if_fail (index_ <= barray->n_nodes, NULL); | 203 | | | 204 | 1.29k | if (G_UNLIKELY (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2)) | 205 | 53 | { | 206 | 53 | new_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + new_size); | 207 | 53 | old_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + old_size); | 208 | 53 | if (old_size != new_size) | 209 | 49 | barray = (GBSearchArray *) g_realloc (barray, new_size); | 210 | 53 | } | 211 | 1.24k | else | 212 | 1.24k | barray = (GBSearchArray *) g_realloc (barray, sizeof (GBSearchArray) + new_size); | 213 | 1.29k | node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node; | 214 | 1.29k | memmove (node + bconfig->sizeof_node, node, (barray->n_nodes - index_) * bconfig->sizeof_node); | 215 | 1.29k | barray->n_nodes += 1; | 216 | 1.29k | return barray; | 217 | 1.29k | } |
gvalue.c:g_bsearch_array_grow Line | Count | Source | 197 | 15.3k | { | 198 | 15.3k | guint old_size = barray->n_nodes * bconfig->sizeof_node; | 199 | 15.3k | guint new_size = old_size + bconfig->sizeof_node; | 200 | 15.3k | guint8 *node; | 201 | | | 202 | 15.3k | g_return_val_if_fail (index_ <= barray->n_nodes, NULL); | 203 | | | 204 | 15.3k | if (G_UNLIKELY (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2)) | 205 | 15.3k | { | 206 | 15.3k | new_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + new_size); | 207 | 15.3k | old_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + old_size); | 208 | 15.3k | if (old_size != new_size) | 209 | 720 | barray = (GBSearchArray *) g_realloc (barray, new_size); | 210 | 15.3k | } | 211 | 0 | else | 212 | 0 | barray = (GBSearchArray *) g_realloc (barray, sizeof (GBSearchArray) + new_size); | 213 | 15.3k | node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node; | 214 | 15.3k | memmove (node + bconfig->sizeof_node, node, (barray->n_nodes - index_) * bconfig->sizeof_node); | 215 | 15.3k | barray->n_nodes += 1; | 216 | 15.3k | return barray; | 217 | 15.3k | } |
|
218 | | static inline GBSearchArray* |
219 | | g_bsearch_array_insert (GBSearchArray *barray, |
220 | | const GBSearchConfig *bconfig, |
221 | | gconstpointer key_node) |
222 | 20.1k | { |
223 | 20.1k | guint8 *node; |
224 | | |
225 | 20.1k | if (G_UNLIKELY (!barray->n_nodes)) |
226 | 1.37k | { |
227 | 1.37k | barray = g_bsearch_array_grow (barray, bconfig, 0); |
228 | 1.37k | node = G_BSEARCH_ARRAY_NODES (barray); |
229 | 1.37k | } |
230 | 18.7k | else |
231 | 18.7k | { |
232 | 18.7k | node = (guint8 *) g_bsearch_array_lookup_insertion (barray, bconfig, key_node); |
233 | 18.7k | if (G_LIKELY (node)) |
234 | 15.2k | { |
235 | 15.2k | guint index_ = g_bsearch_array_get_index (barray, bconfig, node); |
236 | | |
237 | | /* grow and insert */ |
238 | 15.2k | barray = g_bsearch_array_grow (barray, bconfig, index_); |
239 | 15.2k | node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node; |
240 | 15.2k | } |
241 | 3.56k | else /* no insertion needed, node already there */ |
242 | 3.56k | return barray; |
243 | 18.7k | } |
244 | 16.5k | memcpy (node, key_node, bconfig->sizeof_node); |
245 | 16.5k | return barray; |
246 | 20.1k | } gsignal.c:g_bsearch_array_insert Line | Count | Source | 222 | 4.86k | { | 223 | 4.86k | guint8 *node; | 224 | | | 225 | 4.86k | if (G_UNLIKELY (!barray->n_nodes)) | 226 | 1.28k | { | 227 | 1.28k | barray = g_bsearch_array_grow (barray, bconfig, 0); | 228 | 1.28k | node = G_BSEARCH_ARRAY_NODES (barray); | 229 | 1.28k | } | 230 | 3.57k | else | 231 | 3.57k | { | 232 | 3.57k | node = (guint8 *) g_bsearch_array_lookup_insertion (barray, bconfig, key_node); | 233 | 3.57k | if (G_LIKELY (node)) | 234 | 8 | { | 235 | 8 | guint index_ = g_bsearch_array_get_index (barray, bconfig, node); | 236 | | | 237 | | /* grow and insert */ | 238 | 8 | barray = g_bsearch_array_grow (barray, bconfig, index_); | 239 | 8 | node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node; | 240 | 8 | } | 241 | 3.56k | else /* no insertion needed, node already there */ | 242 | 3.56k | return barray; | 243 | 3.57k | } | 244 | 1.29k | memcpy (node, key_node, bconfig->sizeof_node); | 245 | 1.29k | return barray; | 246 | 4.86k | } |
gvalue.c:g_bsearch_array_insert Line | Count | Source | 222 | 15.3k | { | 223 | 15.3k | guint8 *node; | 224 | | | 225 | 15.3k | if (G_UNLIKELY (!barray->n_nodes)) | 226 | 90 | { | 227 | 90 | barray = g_bsearch_array_grow (barray, bconfig, 0); | 228 | 90 | node = G_BSEARCH_ARRAY_NODES (barray); | 229 | 90 | } | 230 | 15.2k | else | 231 | 15.2k | { | 232 | 15.2k | node = (guint8 *) g_bsearch_array_lookup_insertion (barray, bconfig, key_node); | 233 | 15.2k | if (G_LIKELY (node)) | 234 | 15.2k | { | 235 | 15.2k | guint index_ = g_bsearch_array_get_index (barray, bconfig, node); | 236 | | | 237 | | /* grow and insert */ | 238 | 15.2k | barray = g_bsearch_array_grow (barray, bconfig, index_); | 239 | 15.2k | node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node; | 240 | 15.2k | } | 241 | 0 | else /* no insertion needed, node already there */ | 242 | 0 | return barray; | 243 | 15.2k | } | 244 | 15.3k | memcpy (node, key_node, bconfig->sizeof_node); | 245 | 15.3k | return barray; | 246 | 15.3k | } |
|
247 | | static inline GBSearchArray* |
248 | | g_bsearch_array_replace (GBSearchArray *barray, |
249 | | const GBSearchConfig *bconfig, |
250 | | gconstpointer key_node) |
251 | 15.3k | { |
252 | 15.3k | guint8 *node = (guint8 *) g_bsearch_array_lookup (barray, bconfig, key_node); |
253 | 15.3k | if (G_LIKELY (node)) /* expected path */ |
254 | 0 | memcpy (node, key_node, bconfig->sizeof_node); |
255 | 15.3k | else /* revert to insertion */ |
256 | 15.3k | barray = g_bsearch_array_insert (barray, bconfig, key_node); |
257 | 15.3k | return barray; |
258 | 15.3k | } Unexecuted instantiation: gsignal.c:g_bsearch_array_replace gvalue.c:g_bsearch_array_replace Line | Count | Source | 251 | 15.3k | { | 252 | 15.3k | guint8 *node = (guint8 *) g_bsearch_array_lookup (barray, bconfig, key_node); | 253 | 15.3k | if (G_LIKELY (node)) /* expected path */ | 254 | 0 | memcpy (node, key_node, bconfig->sizeof_node); | 255 | 15.3k | else /* revert to insertion */ | 256 | 15.3k | barray = g_bsearch_array_insert (barray, bconfig, key_node); | 257 | 15.3k | return barray; | 258 | 15.3k | } |
|
259 | | static inline GBSearchArray* |
260 | | g_bsearch_array_remove (GBSearchArray *barray, |
261 | | const GBSearchConfig *bconfig, |
262 | | guint index_) |
263 | 0 | { |
264 | 0 | guint8 *node; |
265 | 0 |
|
266 | 0 | g_return_val_if_fail (index_ < barray->n_nodes, NULL); |
267 | 0 |
|
268 | 0 | barray->n_nodes -= 1; |
269 | 0 | node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node; |
270 | 0 | memmove (node, node + bconfig->sizeof_node, (barray->n_nodes - index_) * bconfig->sizeof_node); |
271 | 0 | if (G_UNLIKELY (bconfig->flags & G_BSEARCH_ARRAY_AUTO_SHRINK)) |
272 | 0 | { |
273 | 0 | guint new_size = barray->n_nodes * bconfig->sizeof_node; |
274 | 0 | guint old_size = new_size + bconfig->sizeof_node; |
275 | 0 |
|
276 | 0 | if (G_UNLIKELY (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2)) |
277 | 0 | { |
278 | 0 | new_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + new_size); |
279 | 0 | old_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + old_size); |
280 | 0 | if (old_size != new_size) |
281 | 0 | barray = (GBSearchArray *) g_realloc (barray, new_size); |
282 | 0 | } |
283 | 0 | else |
284 | 0 | barray = (GBSearchArray *) g_realloc (barray, sizeof (GBSearchArray) + new_size); |
285 | 0 | } |
286 | 0 | return barray; |
287 | 0 | } Unexecuted instantiation: gsignal.c:g_bsearch_array_remove Unexecuted instantiation: gvalue.c:g_bsearch_array_remove |
288 | | static inline void |
289 | | g_bsearch_array_free (GBSearchArray *barray, |
290 | | const GBSearchConfig *bconfig) |
291 | 1.18k | { |
292 | 1.18k | g_return_if_fail (barray != NULL); |
293 | | |
294 | 1.18k | g_free (barray); |
295 | 1.18k | } gsignal.c:g_bsearch_array_free Line | Count | Source | 291 | 1.18k | { | 292 | 1.18k | g_return_if_fail (barray != NULL); | 293 | | | 294 | 1.18k | g_free (barray); | 295 | 1.18k | } |
Unexecuted instantiation: gvalue.c:g_bsearch_array_free |
296 | | |
297 | | G_END_DECLS /* c++ guards */ |
298 | | |
299 | | #endif /* !__G_BSEARCH_ARRAY_H__ */ |