Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file set.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Generic set structure and manipulation routines. |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #ifndef LY_SET_H_ |
| 16 | #define LY_SET_H_ |
| 17 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include "log.h" |
| 21 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| 26 | /** |
| 27 | * @defgroup lyset Generic sets |
| 28 | * |
| 29 | * Structure and functions to hold and manipulate with sets of nodes from schema or data trees. |
| 30 | * |
| 31 | * @{ |
| 32 | */ |
| 33 | |
| 34 | /** |
Radek Krejci | 59583bb | 2019-09-11 12:57:55 +0200 | [diff] [blame] | 35 | * @brief Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 36 | * ::lysp_node or ::lysc_node objects, but it is not limited to them. Caller is supposed to not mix the type of objects |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 37 | * added to the set and according to its knowledge about the set content, it can access objects via the members |
| 38 | * of the set union. |
| 39 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 40 | * Until ::ly_set_rm() or ::ly_set_rm_index() is used, the set keeps the order of the inserted items as they |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 41 | * were added into the set, so the first added item is on array index 0. |
| 42 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 43 | * To free the structure, use ::ly_set_free() function, to manipulate with the structure, use other |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 44 | * ly_set_* functions. |
| 45 | */ |
Michal Vasko | 87f1cf0 | 2021-06-08 14:02:47 +0200 | [diff] [blame] | 46 | struct ly_set { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 47 | uint32_t size; /**< allocated size of the set array */ |
| 48 | uint32_t count; /**< number of elements in (used size of) the set array */ |
Michal Vasko | 0c03f14 | 2020-08-11 10:36:13 +0200 | [diff] [blame] | 49 | union { |
| 50 | struct lyd_node **dnodes; /**< set array of data nodes */ |
| 51 | struct lysc_node **snodes; /**< set array of schema nodes */ |
| 52 | void **objs; /**< set array of generic object pointers */ |
| 53 | }; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 57 | * @brief Create and initiate new ::ly_set structure. |
| 58 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 59 | * @param[out] set_p Pointer to store the created ::ly_set structure. |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 60 | * @return LY_SUCCESS on success. |
| 61 | * @return LY_EINVAL in case of NULL @p set parameter. |
| 62 | * @return LY_EMEM in case of memory allocation failure. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 63 | */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 64 | LY_ERR ly_set_new(struct ly_set **set_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * @brief Duplicate the existing set. |
| 68 | * |
| 69 | * @param[in] set Original set to duplicate |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 70 | * @param[in] duplicator Optional pointer to function that duplicates the objects stored |
| 71 | * in the original set. If not provided, the new set points to the exact same objects as |
| 72 | * the original set. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 73 | * @param[out] newset_p Pointer to return the duplication of the original set. |
| 74 | * @return LY_SUCCESS in case the data were successfully duplicated. |
| 75 | * @return LY_EMEM in case of memory allocation failure. |
| 76 | * @return LY_EINVAL in case of invalid parameters. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 77 | */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 78 | LY_ERR ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj), struct ly_set **newset_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 79 | |
| 80 | /** |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 81 | * @brief Add an object into the set |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 82 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 83 | * @param[in] set Set where the \p object will be added. |
| 84 | * @param[in] object Object to be added into the \p set; |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 85 | * @param[in] list flag to handle set as a list (without checking for (ignoring) duplicit items) |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 86 | * @param[out] index_p Optional pointer to return index of the added @p object. Usually it is the last index (::ly_set::count - 1), |
| 87 | * but in case the duplicities are checked and the object is already in the set, the @p object is not added and index of the |
| 88 | * already present object is returned. |
| 89 | * @return LY_SUCCESS in case of success |
| 90 | * @return LY_EINVAL in case of invalid input parameters. |
| 91 | * @return LY_EMEM in case of memory allocation failure. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 92 | */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 93 | LY_ERR ly_set_add(struct ly_set *set, void *object, ly_bool list, uint32_t *index_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * @brief Add all objects from \p src to \p trg. |
| 97 | * |
| 98 | * Since it is a set, the function checks for duplicities. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 99 | * |
| 100 | * @param[in] trg Target (result) set. |
| 101 | * @param[in] src Source set. |
Radek Krejci | 52ca92d | 2020-10-12 16:51:22 +0200 | [diff] [blame] | 102 | * @param[in] list flag to handle set as a list (without checking for (ignoring) duplicit items) |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 103 | * @param[in] duplicator Optional pointer to function that duplicates the objects being added |
| 104 | * from \p src into \p trg set. If not provided, the \p trg set will point to the exact same |
| 105 | * objects as the \p src set. |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 106 | * @return LY_SUCCESS in case of success |
| 107 | * @return LY_EINVAL in case of invalid input parameters. |
| 108 | * @return LY_EMEM in case of memory allocation failure. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 109 | */ |
Michal Vasko | 50694be | 2021-02-04 12:09:09 +0100 | [diff] [blame] | 110 | LY_ERR ly_set_merge(struct ly_set *trg, const struct ly_set *src, ly_bool list, void *(*duplicator)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 111 | |
| 112 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 113 | * @brief Learn whether the set contains the specified object. |
| 114 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 115 | * @param[in] set Set to explore. |
| 116 | * @param[in] object Object to be found in the set. |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 117 | * @param[out] index_p Optional pointer to return index of the searched @p object. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 118 | * @return Boolean value whether the @p object was found in the @p set. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 119 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 120 | ly_bool ly_set_contains(const struct ly_set *set, void *object, uint32_t *index_p); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * @brief Remove all objects from the set, but keep the set container for further use. |
| 124 | * |
| 125 | * @param[in] set Set to clean. |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 126 | * @param[in] destructor Optional function to free the objects in the set. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 127 | */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 128 | void ly_set_clean(struct ly_set *set, void (*destructor)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * @brief Remove an object from the set. |
| 132 | * |
| 133 | * Note that after removing the object from a set, indexes of other objects in the set can change |
| 134 | * (the last object is placed instead of the removed object). |
| 135 | * |
| 136 | * @param[in] set Set from which the \p node will be removed. |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 137 | * @param[in] object The object to be removed from the \p set. |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 138 | * @param[in] destructor Optional function to free the objects being removed. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 139 | * @return LY_ERR return value. |
| 140 | */ |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 141 | LY_ERR ly_set_rm(struct ly_set *set, void *object, void (*destructor)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * @brief Remove an object on the specific set index. |
| 145 | * |
| 146 | * Note that after removing the object from a set, indexes of other nodes in the set can change |
| 147 | * (the last object is placed instead of the removed object). |
| 148 | * |
| 149 | * @param[in] set Set from which a node will be removed. |
| 150 | * @param[in] index Index of the object to remove in the \p set. |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 151 | * @param[in] destructor Optional function to free the objects being removed. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 152 | * @return LY_ERR return value. |
| 153 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 154 | LY_ERR ly_set_rm_index(struct ly_set *set, uint32_t index, void (*destructor)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 155 | |
| 156 | /** |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 157 | * @brief Free the ::ly_set data. If the destructor is not provided, it frees only the set structure |
| 158 | * content, not the referred data. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 159 | * |
| 160 | * @param[in] set The set to be freed. |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 161 | * @param[in] destructor Optional function to free the objects in the set. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 162 | */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 163 | void ly_set_free(struct ly_set *set, void (*destructor)(void *obj)); |
| 164 | |
| 165 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 166 | * @brief Alternative to the ::ly_set_free() for static ::ly_set objects - in contrast to ::ly_set_free() |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 167 | * it does not free the provided ::ly_set object. |
| 168 | * |
| 169 | * @param[in] set The set to be erased. |
| 170 | * @param[in] destructor Optional function to free the objects in the set. |
| 171 | */ |
| 172 | void ly_set_erase(struct ly_set *set, void (*destructor)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 173 | |
| 174 | /** @} lyset */ |
| 175 | |
| 176 | #ifdef __cplusplus |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | #endif /* LY_SET_H_ */ |