src/cx/map.h

changeset 1426
3a89b31f0724
parent 1424
563033aa998c
child 1429
6e0c3a8a914a
equal deleted inserted replaced
1425:83284b289430 1426:3a89b31f0724
187 * Add or overwrite an element. 187 * Add or overwrite an element.
188 * If the @p value is @c NULL, the implementation 188 * If the @p value is @c NULL, the implementation
189 * shall only allocate memory instead of adding an existing value to the map. 189 * shall only allocate memory instead of adding an existing value to the map.
190 * Returns a pointer to the allocated memory or @c NULL if allocation fails. 190 * Returns a pointer to the allocated memory or @c NULL if allocation fails.
191 */ 191 */
192 void *(*put)( 192 void *(*put)(CxMap *map, CxHashKey key, void *value);
193 CxMap *map,
194 CxHashKey key,
195 void *value
196 );
197 193
198 /** 194 /**
199 * Returns an element. 195 * Returns an element.
200 */ 196 */
201 void *(*get)( 197 void *(*get)(const CxMap *map, CxHashKey key);
202 const CxMap *map,
203 CxHashKey key
204 );
205 198
206 /** 199 /**
207 * Removes an element. 200 * Removes an element.
208 * 201 *
209 * Implementations SHALL check if @p targetbuf is set and copy the elements 202 * Implementations SHALL check if @p targetbuf is set and copy the elements
211 * When @p targetbuf is not set, the destructors SHALL be invoked. 204 * When @p targetbuf is not set, the destructors SHALL be invoked.
212 * 205 *
213 * The function SHALL return zero when the @p key was found and 206 * The function SHALL return zero when the @p key was found and
214 * non-zero, otherwise. 207 * non-zero, otherwise.
215 */ 208 */
216 int (*remove)( 209 int (*remove)(CxMap *map, CxHashKey key, void *targetbuf);
217 CxMap *map,
218 CxHashKey key,
219 void *targetbuf
220 );
221 210
222 /** 211 /**
223 * Creates an iterator for this map. 212 * Creates an iterator for this map.
224 */ 213 */
225 CxMapIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); 214 CxMapIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type);
231 * Writing to that map is not allowed. 220 * Writing to that map is not allowed.
232 * 221 *
233 * You can use this as a placeholder for initializing CxMap pointers 222 * You can use this as a placeholder for initializing CxMap pointers
234 * for which you do not want to reserve memory right from the beginning. 223 * for which you do not want to reserve memory right from the beginning.
235 */ 224 */
236 cx_attr_export 225 CX_EXPORT extern CxMap *const cxEmptyMap;
237 extern CxMap *const cxEmptyMap;
238 226
239 /** 227 /**
240 * Deallocates the memory of the specified map. 228 * Deallocates the memory of the specified map.
241 * 229 *
242 * Also calls the content destructor functions for each element, if specified. 230 * Also calls the content destructor functions for each element, if specified.
243 * 231 *
244 * @param map the map to be freed 232 * @param map the map to be freed
245 */ 233 */
246 cx_attr_export 234 CX_EXPORT void cxMapFree(CxMap *map);
247 void cxMapFree(CxMap *map);
248 235
249 236
250 /** 237 /**
251 * Clears a map by removing all elements. 238 * Clears a map by removing all elements.
252 * 239 *
253 * Also calls the content destructor functions for each element, if specified. 240 * Also calls the content destructor functions for each element, if specified.
254 * 241 *
255 * @param map the map to be cleared 242 * @param map the map to be cleared
256 */ 243 */
257 cx_attr_nonnull 244 cx_attr_nonnull
258 static inline void cxMapClear(CxMap *map) { 245 CX_EXPORT void cxMapClear(CxMap *map);
259 map->cl->clear(map);
260 }
261 246
262 /** 247 /**
263 * Returns the number of elements in this map. 248 * Returns the number of elements in this map.
264 * 249 *
265 * @param map the map 250 * @param map the map
266 * @return the number of stored elements 251 * @return the number of stored elements
267 */ 252 */
268 cx_attr_nonnull 253 cx_attr_nonnull
269 static inline size_t cxMapSize(const CxMap *map) { 254 CX_EXPORT size_t cxMapSize(const CxMap *map);
270 return map->collection.size;
271 }
272 255
273 /** 256 /**
274 * Creates a value iterator for a map. 257 * Creates a value iterator for a map.
275 * 258 *
276 * When the map is storing pointers, those pointers are returned. 259 * When the map is storing pointers, those pointers are returned.
282 * 265 *
283 * @param map the map to create the iterator for (can be @c NULL) 266 * @param map the map to create the iterator for (can be @c NULL)
284 * @return an iterator for the currently stored values 267 * @return an iterator for the currently stored values
285 */ 268 */
286 cx_attr_nodiscard 269 cx_attr_nodiscard
287 static inline CxMapIterator cxMapIteratorValues(const CxMap *map) { 270 CX_EXPORT CxMapIterator cxMapIteratorValues(const CxMap *map);
288 if (map == NULL) map = cxEmptyMap;
289 return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
290 }
291 271
292 /** 272 /**
293 * Creates a key iterator for a map. 273 * Creates a key iterator for a map.
294 * 274 *
295 * The elements of the iterator are keys of type CxHashKey, and the pointer returned 275 * The elements of the iterator are keys of type CxHashKey, and the pointer returned
300 * 280 *
301 * @param map the map to create the iterator for (can be @c NULL) 281 * @param map the map to create the iterator for (can be @c NULL)
302 * @return an iterator for the currently stored keys 282 * @return an iterator for the currently stored keys
303 */ 283 */
304 cx_attr_nodiscard 284 cx_attr_nodiscard
305 static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) { 285 CX_EXPORT CxMapIterator cxMapIteratorKeys(const CxMap *map);
306 if (map == NULL) map = cxEmptyMap;
307 return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
308 }
309 286
310 /** 287 /**
311 * Creates an iterator for a map. 288 * Creates an iterator for a map.
312 * 289 *
313 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned 290 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned
320 * @return an iterator for the currently stored entries 297 * @return an iterator for the currently stored entries
321 * @see cxMapIteratorKeys() 298 * @see cxMapIteratorKeys()
322 * @see cxMapIteratorValues() 299 * @see cxMapIteratorValues()
323 */ 300 */
324 cx_attr_nodiscard 301 cx_attr_nodiscard
325 static inline CxMapIterator cxMapIterator(const CxMap *map) { 302 CX_EXPORT CxMapIterator cxMapIterator(const CxMap *map);
326 if (map == NULL) map = cxEmptyMap;
327 return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
328 }
329
330 303
331 /** 304 /**
332 * Creates a mutating iterator over the values of a map. 305 * Creates a mutating iterator over the values of a map.
333 * 306 *
334 * When the map is storing pointers, those pointers are returned. 307 * When the map is storing pointers, those pointers are returned.
340 * 313 *
341 * @param map the map to create the iterator for (can be @c NULL) 314 * @param map the map to create the iterator for (can be @c NULL)
342 * @return an iterator for the currently stored values 315 * @return an iterator for the currently stored values
343 */ 316 */
344 cx_attr_nodiscard 317 cx_attr_nodiscard
345 cx_attr_export 318 CX_EXPORT CxMapIterator cxMapMutIteratorValues(CxMap *map);
346 CxMapIterator cxMapMutIteratorValues(CxMap *map);
347 319
348 /** 320 /**
349 * Creates a mutating iterator over the keys of a map. 321 * Creates a mutating iterator over the keys of a map.
350 * 322 *
351 * The elements of the iterator are keys of type CxHashKey, and the pointer returned 323 * The elements of the iterator are keys of type CxHashKey, and the pointer returned
356 * 328 *
357 * @param map the map to create the iterator for (can be @c NULL) 329 * @param map the map to create the iterator for (can be @c NULL)
358 * @return an iterator for the currently stored keys 330 * @return an iterator for the currently stored keys
359 */ 331 */
360 cx_attr_nodiscard 332 cx_attr_nodiscard
361 cx_attr_export 333 CX_EXPORT CxMapIterator cxMapMutIteratorKeys(CxMap *map);
362 CxMapIterator cxMapMutIteratorKeys(CxMap *map);
363 334
364 /** 335 /**
365 * Creates a mutating iterator for a map. 336 * Creates a mutating iterator for a map.
366 * 337 *
367 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned 338 * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned
374 * @return an iterator for the currently stored entries 345 * @return an iterator for the currently stored entries
375 * @see cxMapMutIteratorKeys() 346 * @see cxMapMutIteratorKeys()
376 * @see cxMapMutIteratorValues() 347 * @see cxMapMutIteratorValues()
377 */ 348 */
378 cx_attr_nodiscard 349 cx_attr_nodiscard
379 cx_attr_export 350 CX_EXPORT CxMapIterator cxMapMutIterator(CxMap *map);
380 CxMapIterator cxMapMutIterator(CxMap *map);
381 351
382 /** 352 /**
383 * Puts a key/value-pair into the map. 353 * Puts a key/value-pair into the map.
384 * 354 *
385 * A possible existing value will be overwritten. 355 * A possible existing value will be overwritten.
398 * @retval zero success 368 * @retval zero success
399 * @retval non-zero value on memory allocation failure 369 * @retval non-zero value on memory allocation failure
400 * @see cxMapPut() 370 * @see cxMapPut()
401 */ 371 */
402 cx_attr_nonnull 372 cx_attr_nonnull
403 static inline int cx_map_put( 373 CX_EXPORT int cx_map_put(CxMap *map, CxHashKey key, void *value);
404 CxMap *map,
405 CxHashKey key,
406 void *value
407 ) {
408 return map->cl->put(map, key, value) == NULL;
409 }
410 374
411 /** 375 /**
412 * Puts a key/value-pair into the map. 376 * Puts a key/value-pair into the map.
413 * 377 *
414 * A possible existing value will be overwritten. 378 * A possible existing value will be overwritten.
448 * @retval zero success 412 * @retval zero success
449 * @retval non-zero value on memory allocation failure 413 * @retval non-zero value on memory allocation failure
450 * @see cxMapEmplace() 414 * @see cxMapEmplace()
451 */ 415 */
452 cx_attr_nonnull 416 cx_attr_nonnull
453 static inline void *cx_map_emplace( 417 CX_EXPORT void *cx_map_emplace(CxMap *map, CxHashKey key);
454 CxMap *map,
455 CxHashKey key
456 ) {
457 return map->cl->put(map, key, NULL);
458 }
459 418
460 /** 419 /**
461 * Allocates memory for a value in the map associated with the specified key. 420 * Allocates memory for a value in the map associated with the specified key.
462 * 421 *
463 * A possible existing value will be overwritten. 422 * A possible existing value will be overwritten.
488 * @param map the map 447 * @param map the map
489 * @param key the key 448 * @param key the key
490 * @return the value 449 * @return the value
491 * @see cxMapGet() 450 * @see cxMapGet()
492 */ 451 */
493 cx_attr_nonnull 452 cx_attr_nonnull cx_attr_nodiscard
494 cx_attr_nodiscard 453 CX_EXPORT void *cx_map_get(const CxMap *map, CxHashKey key);
495 static inline void *cx_map_get(
496 const CxMap *map,
497 CxHashKey key
498 ) {
499 return map->cl->get(map, key);
500 }
501 454
502 /** 455 /**
503 * Retrieves a value by using a key. 456 * Retrieves a value by using a key.
504 * 457 *
505 * If this map is storing pointers, the stored pointer is returned. 458 * If this map is storing pointers, the stored pointer is returned.
527 * 480 *
528 * @see cxMapRemove() 481 * @see cxMapRemove()
529 * @see cxMapRemoveAndGet() 482 * @see cxMapRemoveAndGet()
530 */ 483 */
531 cx_attr_nonnull_arg(1) 484 cx_attr_nonnull_arg(1)
532 static inline int cx_map_remove( 485 CX_EXPORT int cx_map_remove(CxMap *map, CxHashKey key, void *targetbuf);
533 CxMap *map,
534 CxHashKey key,
535 void *targetbuf
536 ) {
537 return map->cl->remove(map, key, targetbuf);
538 }
539 486
540 /** 487 /**
541 * Removes a key/value-pair from the map by using the key. 488 * Removes a key/value-pair from the map by using the key.
542 * 489 *
543 * Always invokes the destructor functions, if any, on the removed element. 490 * Always invokes the destructor functions, if any, on the removed element.

mercurial