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 | * |
| 40 | * Until ly_set_rm() or ly_set_rm_index() is used, the set keeps the order of the inserted items as they |
| 41 | * were added into the set, so the first added item is on array index 0. |
| 42 | * |
| 43 | * To free the structure, use ly_set_free() function, to manipulate with the structure, use other |
| 44 | * ly_set_* functions. |
| 45 | */ |
| 46 | struct ly_set |
| 47 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 48 | uint32_t size; /**< allocated size of the set array */ |
| 49 | uint32_t count; /**< number of elements in (used size of) the set array */ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 50 | void **objs; /**< set array of generic object pointers */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * @brief Option for ly_set_add() to allow duplicities in the ly_set structure so the |
| 55 | * set is not used as a set, but as a list of (container for) items. |
| 56 | */ |
| 57 | #define LY_SET_OPT_USEASLIST 0x01 |
| 58 | |
| 59 | /** |
| 60 | * @brief Create and initiate new ::ly_set structure. |
| 61 | * |
| 62 | * @return Created ::ly_set structure or NULL in case of error. |
| 63 | */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 64 | struct ly_set *ly_set_new(void); |
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 | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 73 | * @return Duplication of the original set. |
| 74 | */ |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 75 | struct ly_set *ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 76 | |
| 77 | /** |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 78 | * @brief Add an object into the set |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 79 | * |
| 80 | * Since it is a set, the function checks for duplicity and if the |
| 81 | * node is already in the set, the index of the previously added |
| 82 | * node is returned. |
| 83 | * |
| 84 | * @param[in] set Set where the \p object will be added. |
| 85 | * @param[in] object Object to be added into the \p set; |
| 86 | * @param[in] options Options to change behavior of the function. Accepted options are: |
| 87 | * - #LY_SET_OPT_USEASLIST - do not check for duplicities |
| 88 | * @return -1 on failure, index of the \p node in the set on success |
| 89 | */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 90 | int ly_set_add(struct ly_set *set, void *object, int options); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * @brief Add all objects from \p src to \p trg. |
| 94 | * |
| 95 | * Since it is a set, the function checks for duplicities. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 96 | * |
| 97 | * @param[in] trg Target (result) set. |
| 98 | * @param[in] src Source set. |
| 99 | * @param[in] options Options to change behavior of the function. Accepted options are: |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 100 | * - #LY_SET_OPT_USEASLIST - add without checking for duplicities |
| 101 | * @param[in] duplicator Optional pointer to function that duplicates the objects being added |
| 102 | * from \p src into \p trg set. If not provided, the \p trg set will point to the exact same |
| 103 | * objects as the \p src set. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 104 | * @return -1 on failure, number of objects added into \p trg on success. |
| 105 | */ |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 106 | int ly_set_merge(struct ly_set *trg, struct ly_set *src, int options, void *(*duplicator)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 107 | |
| 108 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 109 | * @brief Learn whether the set contains the specified object. |
| 110 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 111 | * @param[in] set Set to explore. |
| 112 | * @param[in] object Object to be found in the set. |
| 113 | * @return Index of the object in the set or -1 if the object is not present in the set. |
| 114 | */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 115 | int ly_set_contains(const struct ly_set *set, void *object); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * @brief Remove all objects from the set, but keep the set container for further use. |
| 119 | * |
| 120 | * @param[in] set Set to clean. |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 121 | * @param[in] destructor Optional function to free the objects in the set. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 122 | */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 123 | void ly_set_clean(struct ly_set *set, void (*destructor)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * @brief Remove an object from the set. |
| 127 | * |
| 128 | * Note that after removing the object from a set, indexes of other objects in the set can change |
| 129 | * (the last object is placed instead of the removed object). |
| 130 | * |
| 131 | * @param[in] set Set from which the \p node will be removed. |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 132 | * @param[in] object The object to be removed from the \p set. |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 133 | * @param[in] destructor Optional function to free the objects being removed. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 134 | * @return LY_ERR return value. |
| 135 | */ |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 136 | 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] | 137 | |
| 138 | /** |
| 139 | * @brief Remove an object on the specific set index. |
| 140 | * |
| 141 | * Note that after removing the object from a set, indexes of other nodes in the set can change |
| 142 | * (the last object is placed instead of the removed object). |
| 143 | * |
| 144 | * @param[in] set Set from which a node will be removed. |
| 145 | * @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] | 146 | * @param[in] destructor Optional function to free the objects being removed. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 147 | * @return LY_ERR return value. |
| 148 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 149 | 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] | 150 | |
| 151 | /** |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 152 | * @brief Free the ::ly_set data. If the destructor is not provided, it frees only the set structure |
| 153 | * content, not the referred data. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 154 | * |
| 155 | * @param[in] set The set to be freed. |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 156 | * @param[in] destructor Optional function to free the objects in the set. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 157 | */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 158 | void ly_set_free(struct ly_set *set, void (*destructor)(void *obj)); |
| 159 | |
| 160 | /** |
| 161 | * @brief Alternative to the ly_set_free() for static ::ly_set objects - in contrast to ly_set_free() |
| 162 | * it does not free the provided ::ly_set object. |
| 163 | * |
| 164 | * @param[in] set The set to be erased. |
| 165 | * @param[in] destructor Optional function to free the objects in the set. |
| 166 | */ |
| 167 | void ly_set_erase(struct ly_set *set, void (*destructor)(void *obj)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 168 | |
| 169 | /** @} lyset */ |
| 170 | |
| 171 | #ifdef __cplusplus |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | #endif /* LY_SET_H_ */ |