Debian Codegraph @

crt/aws-c-common/include/aws/common/linked_hash_table.h

raw symbols SCIP
1 #ifndef AWS_COMMON_LINKED_HASH_TABLE_H
2 #define AWS_COMMON_LINKED_HASH_TABLE_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/common/hash_table.h>
8 #include <aws/common/linked_list.h>
9
10 AWS_PUSH_SANE_WARNING_LEVEL
11
12 /**
13 * Simple linked hash table. Preserves insertion order, and can be iterated in insertion order.
14 *
15 * You can also change the order safely without altering the shape of the underlying hash table.
16 */
17 struct aws_linked_hash_table {
18 struct aws_allocator *allocator;
19 struct aws_linked_list list;
20 struct aws_hash_table table;
21 aws_hash_callback_destroy_fn *user_on_value_destroy;
22 aws_hash_callback_destroy_fn *user_on_key_destroy;
23 };
24
25 /**
26 * Linked-List node stored in the table. This is the node type that will be returned in
27 * aws_linked_hash_table_get_iteration_list().
28 */
29 struct aws_linked_hash_table_node {
30 struct aws_linked_list_node node;
31 struct aws_linked_hash_table *table;
32 const void *key;
33 void *value;
34 };
35
36 AWS_EXTERN_C_BEGIN
37
38 /**
39 * Initializes the table. Sets up the underlying hash table and linked list.
40 * For the other parameters, see aws/common/hash_table.h. Hash table
41 * semantics of these arguments are preserved.
42 */
43 AWS_COMMON_API
44 int aws_linked_hash_table_init(
45 struct aws_linked_hash_table *table,
46 struct aws_allocator *allocator,
47 aws_hash_fn *hash_fn,
48 aws_hash_callback_eq_fn *equals_fn,
49 aws_hash_callback_destroy_fn *destroy_key_fn,
50 aws_hash_callback_destroy_fn *destroy_value_fn,
51 size_t initial_item_count);
52
53 /**
54 * Cleans up the table. Elements in the table will be evicted and cleanup
55 * callbacks will be invoked.
56 */
57 AWS_COMMON_API
58 void aws_linked_hash_table_clean_up(struct aws_linked_hash_table *table);
59
60 /**
61 * Finds element in the table by key. If found, AWS_OP_SUCCESS will be
62 * returned. If not found, AWS_OP_SUCCESS will be returned and *p_value will be
63 * NULL.
64 *
65 * If any errors occur AWS_OP_ERR will be returned.
66 */
67 AWS_COMMON_API
68 int aws_linked_hash_table_find(struct aws_linked_hash_table *table, const void *key, void **p_value);
69
70 /**
71 * Finds element in the table by key. If found, AWS_OP_SUCCESS will be returned and the item will be moved to the back
72 * of the list.
73 * If not found, AWS_OP_SUCCESS will be returned and *p_value will be NULL.
74 *
75 * Note: this will change the order of elements
76 */
77 AWS_COMMON_API
78 int aws_linked_hash_table_find_and_move_to_back(struct aws_linked_hash_table *table, const void *key, void **p_value);
79
80 /**
81 * Puts `p_value` at `key`. If an element is already stored at `key` it will be replaced.
82 */
83 AWS_COMMON_API
84 int aws_linked_hash_table_put(struct aws_linked_hash_table *table, const void *key, void *p_value);
85
86 /**
87 * Removes item at `key` from the table.
88 */
89 AWS_COMMON_API
90 int aws_linked_hash_table_remove(struct aws_linked_hash_table *table, const void *key);
91
92 /**
93 * Clears all items from the table.
94 */
95 AWS_COMMON_API
96 void aws_linked_hash_table_clear(struct aws_linked_hash_table *table);
97
98 /**
99 * returns number of elements in the table.
100 */
101 AWS_COMMON_API
102 size_t aws_linked_hash_table_get_element_count(const struct aws_linked_hash_table *table);
103
104 /**
105 * Move the aws_linked_hash_table_node to the end of the list.
106 *
107 * Note: this will change the order of elements
108 */
109 AWS_COMMON_API
110 void aws_linked_hash_table_move_node_to_end_of_list(
111 struct aws_linked_hash_table *table,
112 struct aws_linked_hash_table_node *node);
113
114 /**
115 * returns the underlying linked list for iteration.
116 *
117 * The returned list has nodes of the type: aws_linked_hash_table_node. Use AWS_CONTAINER_OF for access to the element.
118 */
119 AWS_COMMON_API
120 const struct aws_linked_list *aws_linked_hash_table_get_iteration_list(const struct aws_linked_hash_table *table);
121
122 AWS_EXTERN_C_END
123 AWS_POP_SANE_WARNING_LEVEL
124
125 #endif /* AWS_COMMON_LINKED_HASH_TABLE_H */