• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 struct hash_map_t;
25 typedef struct hash_map_t hash_map_t;
26 
27 typedef struct hash_map_entry_t {
28   const void *key;
29   void *data;
30   const hash_map_t *hash_map;
31 } hash_map_entry_t;
32 
33 typedef size_t hash_index_t;
34 
35 // Takes a key structure and returns a hash value.
36 typedef hash_index_t (*hash_index_fn)(const void *key);
37 typedef bool (*hash_map_iter_cb)(hash_map_entry_t *hash_entry, void *context);
38 
39 typedef bool (*key_equality_fn)(const void *x, const void *y);
40 
41 typedef void (*key_free_fn)(void *data);
42 typedef void (*data_free_fn)(void *data);
43 
44 // Returns a new, empty hash_map. Returns NULL if not enough memory could be allocated
45 // for the hash_map structure. The returned hash_map must be freed with |hash_map_free|.
46 // The |num_bucket| specifies the number of hashable buckets for the map and must not
47 // be zero.  The |hash_fn| specifies a hash function to be used and must not be NULL.
48 // The |key_fn| and |data_fn| are called whenever a hash_map element is removed from
49 // the hash_map. They can be used to release resources held by the hash_map element,
50 // e.g.  memory or file descriptor.  |key_fn| and |data_fn| may be NULL if no cleanup
51 // is necessary on element removal. |equality_fn| is used to check for key equality.
52 // If |equality_fn| is NULL, default pointer equality is used.
53 hash_map_t *hash_map_new(
54     size_t size,
55     hash_index_fn hash_fn,
56     key_free_fn key_fn,
57     data_free_fn data_fn,
58     key_equality_fn equality_fn);
59 
60 // Frees the hash_map. This function accepts NULL as an argument, in which case it
61 // behaves like a no-op.
62 void hash_map_free(hash_map_t *hash_map);
63 
64 // Returns true if the hash_map is empty (has no elements), false otherwise.
65 // Note that a NULL |hash_map| is not the same as an empty |hash_map|. This function
66 // does not accept a NULL |hash_map|.
67 bool hash_map_is_empty(const hash_map_t *hash_map);
68 
69 // Returns the number of elements in the hash map.  This function does not accept a
70 // NULL |hash_map|.
71 size_t hash_map_size(const hash_map_t *hash_map);
72 
73 // Returns the number of buckets in the hash map.  This function does not accept a
74 // NULL |hash_map|.
75 size_t hash_map_num_buckets(const hash_map_t *hash_map);
76 
77 // Returns true if the hash_map has a valid entry for the presented key.
78 // This function does not accept a NULL |hash_map|.
79 bool hash_map_has_key(const hash_map_t *hash_map, const void *key);
80 
81 // Returns the element indexed by |key| in the hash_map without removing it. |hash_map|
82 // may not be NULL.  Returns NULL if no entry indexed by |key|.
83 void *hash_map_get(const hash_map_t *hash_map, const void *key);
84 
85 // Sets the value |data| indexed by |key| into the |hash_map|. Neither |data| nor
86 // |hash_map| may be NULL.  This function does not make copies of |data| nor |key|
87 // so the pointers must remain valid at least until the element is removed from the
88 // hash_map or the hash_map is freed.  Returns true if |data| could be set, false
89 // otherwise (e.g. out of memory).
90 bool hash_map_set(hash_map_t *hash_map, const void *key, void *data);
91 
92 // Removes data indexed by |key| from the hash_map. |hash_map| may not be NULL.
93 // If |key_fn| or |data_fn| functions were specified in |hash_map_new|, they
94 // will be called back with |key| or |data| respectively. This function returns true
95 // if |key| was found in the hash_map and removed, false otherwise.
96 bool hash_map_erase(hash_map_t *hash_map, const void *key);
97 
98 // Removes all elements in the hash_map. Calling this function will return the hash_map
99 // to the same state it was in after |hash_map_new|. |hash_map| may not be NULL.
100 void hash_map_clear(hash_map_t *hash_map);
101 
102 // Iterates through the entire |hash_map| and calls |callback| for each data
103 // element and passes through the |context| argument. If the hash_map is
104 // empty, |callback| will never be called. It is not safe to mutate the
105 // hash_map inside the callback. Neither |hash_map| nor |callback| may be NULL.
106 // If |callback| returns false, the iteration loop will immediately exit.
107 void hash_map_foreach(hash_map_t *hash_map, hash_map_iter_cb callback, void *context);
108