| 1 | #ifndef AWS_HTTP_RANDOM_ACCESS_SET_H |
| 2 | #define AWS_HTTP_RANDOM_ACCESS_SET_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/common/array_list.h> |
| 10 | #include <aws/common/hash_table.h> |
| 11 | #include <aws/http/http.h> |
| 12 | |
| 13 | /* TODO: someday, if you want to use it from other repo, move it to aws-c-common. */ |
| 14 | |
| 15 | struct aws_random_access_set_impl; |
| 16 | |
| 17 | struct aws_random_access_set { |
| 18 | struct aws_random_access_set_impl *impl; |
| 19 | }; |
| 20 | |
| 21 | AWS_EXTERN_C_BEGIN |
| 22 | |
| 23 | /** |
| 24 | * Initialize the set, which support constant time of insert, remove and get random element |
| 25 | * from the data structure. |
| 26 | * |
| 27 | * The underlying hash map will use hash_fn to compute the hash of each element. equals_fn to compute equality of two |
| 28 | * keys. |
| 29 | * |
| 30 | * @param set Pointer of structure to initialize with |
| 31 | * @param allocator Allocator |
| 32 | * @param hash_fn Compute the hash of each element |
| 33 | * @param equals_fn Compute equality of two elements |
| 34 | * @param destroy_element_fn Optional. Called when the element is removed |
| 35 | * @param initial_item_allocation The initial number of item to allocate. |
| 36 | * @return AWS_OP_ERR if any fails to initialize, AWS_OP_SUCCESS on success. |
| 37 | */ |
| 38 | AWS_HTTP_API |
| 39 | int aws_random_access_set_init( |
| 40 | struct aws_random_access_set *set, |
| 41 | struct aws_allocator *allocator, |
| 42 | aws_hash_fn *hash_fn, |
| 43 | aws_hash_callback_eq_fn *equals_fn, |
| 44 | aws_hash_callback_destroy_fn *destroy_element_fn, |
| 45 | size_t initial_item_allocation); |
| 46 | |
| 47 | AWS_HTTP_API |
| 48 | void aws_random_access_set_clean_up(struct aws_random_access_set *set); |
| 49 | |
| 50 | /** |
| 51 | * Insert the element to the end of the array list. A map from the element to the index of it to the hash table. |
| 52 | */ |
| 53 | AWS_HTTP_API |
| 54 | int aws_random_access_set_add(struct aws_random_access_set *set, const void *element, bool *added); |
| 55 | |
| 56 | /** |
| 57 | * Find and remove the element from the table. If the element does not exist, or the table is empty, nothing will |
| 58 | * happen. Switch the element with the end of the arraylist if needed. Remove the end of the arraylist |
| 59 | */ |
| 60 | AWS_HTTP_API |
| 61 | int aws_random_access_set_remove(struct aws_random_access_set *set, const void *element); |
| 62 | |
| 63 | /** |
| 64 | * Get the pointer to a random element from the data structure. Fails when the data structure is empty. |
| 65 | */ |
| 66 | AWS_HTTP_API |
| 67 | int aws_random_access_set_random_get_ptr(const struct aws_random_access_set *set, void **out); |
| 68 | |
| 69 | AWS_HTTP_API |
| 70 | size_t aws_random_access_set_get_size(const struct aws_random_access_set *set); |
| 71 | |
| 72 | /** |
| 73 | * Check the element exist in the data structure or not. |
| 74 | */ |
| 75 | AWS_HTTP_API |
| 76 | int aws_random_access_set_exist(const struct aws_random_access_set *set, const void *element, bool *exist); |
| 77 | |
| 78 | /** |
| 79 | * Get the pointer to an element that currently stored at that index. It may change if operations like remove and add |
| 80 | * happens. Helpful for debugging and iterating through the whole set. |
| 81 | */ |
| 82 | AWS_HTTP_API |
| 83 | int aws_random_access_set_random_get_ptr_index(const struct aws_random_access_set *set, void **out, size_t index); |
| 84 | |
| 85 | AWS_EXTERN_C_END |
| 86 | #endif /* AWS_HTTP_RANDOM_ACCESS_SET_H */ |