rcutils  master
C API providing common utilities and data structures.
hash_map.h
Go to the documentation of this file.
1 // Copyright 2018-2019 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #ifndef RCUTILS__TYPES__HASH_MAP_H_
18 #define RCUTILS__TYPES__HASH_MAP_H_
19 
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 
25 #include <string.h>
26 
27 #include "rcutils/allocator.h"
29 #include "rcutils/macros.h"
31 
32 struct rcutils_hash_map_impl_t;
33 
36 {
38  struct rcutils_hash_map_impl_t * impl;
40 
42 
46 typedef size_t (* rcutils_hash_map_key_hasher_t)(
47  const void * // key to hash
48 );
49 
51 
59  const void *, // val1
60  const void * // val2
61 );
62 
69 #define HASH_MAP_VALIDATE_HASH_MAP(map) \
70  RCUTILS_CHECK_ARGUMENT_FOR_NULL(map, RCUTILS_RET_INVALID_ARGUMENT); \
71  if (NULL == map->impl) { \
72  RCUTILS_SET_ERROR_MSG("map is not initialized"); \
73  return RCUTILS_RET_NOT_INITIALIZED; \
74  }
75 
77 
82 size_t
83 rcutils_hash_map_string_hash_func(const void * key_str);
84 
86 
91 int
92 rcutils_hash_map_string_cmp_func(const void * val1, const void * val2);
93 
95 
123 
125 
175  rcutils_hash_map_t * hash_map,
176  size_t initial_capacity,
177  size_t key_size,
178  size_t data_size,
179  rcutils_hash_map_key_hasher_t key_hashing_func,
180  rcutils_hash_map_key_cmp_t key_cmp_func,
181  const rcutils_allocator_t * allocator);
182 
184 
205 
207 
233 rcutils_hash_map_get_capacity(const rcutils_hash_map_t * hash_map, size_t * capacity);
234 
236 
259 rcutils_hash_map_get_size(const rcutils_hash_map_t * hash_map, size_t * size);
260 
262 
287 rcutils_hash_map_set(rcutils_hash_map_t * hash_map, const void * key, const void * value);
288 
290 
314 rcutils_hash_map_unset(rcutils_hash_map_t * hash_map, const void * key);
315 
317 
338 bool
339 rcutils_hash_map_key_exists(const rcutils_hash_map_t * hash_map, const void * key);
340 
342 
366 rcutils_hash_map_get(const rcutils_hash_map_t * hash_map, const void * key, void * data);
367 
369 
416  const rcutils_hash_map_t * hash_map,
417  const void * previous_key,
418  void * key,
419  void * data);
420 
421 
422 #ifdef __cplusplus
423 }
424 #endif
425 
426 #endif // RCUTILS__TYPES__HASH_MAP_H_
rcutils_hash_map_get_capacity
rcutils_ret_t rcutils_hash_map_get_capacity(const rcutils_hash_map_t *hash_map, size_t *capacity)
Get the current capacity of the hash_map.
rcutils_hash_map_t
struct rcutils_hash_map_t rcutils_hash_map_t
The structure holding the metadata for a hash map.
rcutils_hash_map_get_size
rcutils_ret_t rcutils_hash_map_get_size(const rcutils_hash_map_t *hash_map, size_t *size)
Get the current size of the hash_map.
rcutils_hash_map_key_exists
bool rcutils_hash_map_key_exists(const rcutils_hash_map_t *hash_map, const void *key)
Get whether or not a key exists.
rcutils_hash_map_set
rcutils_ret_t rcutils_hash_map_set(rcutils_hash_map_t *hash_map, const void *key, const void *value)
Set a key value pair in the hash_map, increasing capacity if necessary.
macros.h
rcutils_hash_map_key_cmp_t
int(* rcutils_hash_map_key_cmp_t)(const void *, const void *)
The function signature for a key comparison function.
Definition: hash_map.h:58
rcutils_hash_map_get
rcutils_ret_t rcutils_hash_map_get(const rcutils_hash_map_t *hash_map, const void *key, void *data)
Get value given a key.
rcutils_get_zero_initialized_hash_map
rcutils_hash_map_t rcutils_get_zero_initialized_hash_map()
Return an empty hash_map struct.
RCUTILS_PUBLIC_TYPE
#define RCUTILS_PUBLIC_TYPE
Definition: visibility_control.h:29
rcutils_hash_map_get_next_key_and_data
rcutils_ret_t rcutils_hash_map_get_next_key_and_data(const rcutils_hash_map_t *hash_map, const void *previous_key, void *key, void *data)
Get the next key in the hash_map, unless NULL is given, then get the first key.
rcutils_ret_t
int rcutils_ret_t
The type that holds a return value for an rcutils operation.
Definition: rcutils_ret.h:26
RCUTILS_WARN_UNUSED
#define RCUTILS_WARN_UNUSED
A macro to make the compiler warn when the return value of a function is not used.
Definition: macros.h:27
rcutils_ret.h
visibility_control.h
rcutils_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:47
rcutils_hash_map_string_hash_func
size_t rcutils_hash_map_string_hash_func(const void *key_str)
A hashing function for a null terminated c string.
allocator.h
RCUTILS_PUBLIC
#define RCUTILS_PUBLIC
Definition: visibility_control.h:23
rcutils_hash_map_t::impl
struct rcutils_hash_map_impl_t * impl
A pointer to the PIMPL implementation type.
Definition: hash_map.h:38
rcutils_hash_map_fini
rcutils_ret_t rcutils_hash_map_fini(rcutils_hash_map_t *hash_map)
Finalize the previously initialized hash_map struct.
rcutils_hash_map_key_hasher_t
size_t(* rcutils_hash_map_key_hasher_t)(const void *)
The function signature for a key hashing function.
Definition: hash_map.h:46
rcutils_hash_map_string_cmp_func
int rcutils_hash_map_string_cmp_func(const void *val1, const void *val2)
A comparison function for a null terminated c string.
rcutils_hash_map_init
rcutils_ret_t rcutils_hash_map_init(rcutils_hash_map_t *hash_map, size_t initial_capacity, size_t key_size, size_t data_size, rcutils_hash_map_key_hasher_t key_hashing_func, rcutils_hash_map_key_cmp_t key_cmp_func, const rcutils_allocator_t *allocator)
Initialize a rcutils_hash_map_t, allocating space for given capacity.
rcutils_hash_map_unset
rcutils_ret_t rcutils_hash_map_unset(rcutils_hash_map_t *hash_map, const void *key)
Unset a key value pair in the hash_map.
rcutils_hash_map_t
The structure holding the metadata for a hash map.
Definition: hash_map.h:35