blob: 1a2269d552db65bc9e4b09faa9b0f1df06e89065 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
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 Vasko52927e22020-03-16 17:26:14 +010018#include <stdint.h>
19
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include "log.h"
21
Radek Krejci5aeea3a2018-09-05 13:29:36 +020022#ifdef __cplusplus
23extern "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 Krejci59583bb2019-09-11 12:57:55 +020035 * @brief Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node,
Radek Krejcie53a8dc2018-10-17 12:52:40 +020036 * ::lysp_node or ::lysc_node objects, but it is not limited to them. Caller is supposed to not mix the type of objects
Radek Krejci5aeea3a2018-09-05 13:29:36 +020037 * 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 */
46struct ly_set
47{
Michal Vasko52927e22020-03-16 17:26:14 +010048 uint32_t size; /**< allocated size of the set array */
49 uint32_t count; /**< number of elements in (used size of) the set array */
Michal Vasko0c03f142020-08-11 10:36:13 +020050 union {
51 struct lyd_node **dnodes; /**< set array of data nodes */
52 struct lysc_node **snodes; /**< set array of schema nodes */
53 void **objs; /**< set array of generic object pointers */
54 };
Radek Krejci5aeea3a2018-09-05 13:29:36 +020055};
56
57/**
58 * @brief Option for ly_set_add() to allow duplicities in the ly_set structure so the
59 * set is not used as a set, but as a list of (container for) items.
60 */
61#define LY_SET_OPT_USEASLIST 0x01
62
63/**
64 * @brief Create and initiate new ::ly_set structure.
65 *
Radek Krejciba03a5a2020-08-27 14:40:41 +020066 * @param[out] set Pointer to store the created ::ly_set structure.
67 * @return LY_SUCCESS on success.
68 * @return LY_EINVAL in case of NULL @p set parameter.
69 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020070 */
Radek Krejciba03a5a2020-08-27 14:40:41 +020071LY_ERR ly_set_new(struct ly_set **set_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020072
73/**
74 * @brief Duplicate the existing set.
75 *
76 * @param[in] set Original set to duplicate
Radek Krejci2f2bd902018-09-18 17:04:24 +020077 * @param[in] duplicator Optional pointer to function that duplicates the objects stored
78 * in the original set. If not provided, the new set points to the exact same objects as
79 * the original set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020080 * @return Duplication of the original set.
81 */
Radek Krejciba03a5a2020-08-27 14:40:41 +020082LY_ERR ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj), struct ly_set **newset_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020083
84/**
Radek Krejcie53a8dc2018-10-17 12:52:40 +020085 * @brief Add an object into the set
Radek Krejci5aeea3a2018-09-05 13:29:36 +020086 *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020087 * @param[in] set Set where the \p object will be added.
88 * @param[in] object Object to be added into the \p set;
89 * @param[in] options Options to change behavior of the function. Accepted options are:
90 * - #LY_SET_OPT_USEASLIST - do not check for duplicities
Radek Krejciba03a5a2020-08-27 14:40:41 +020091 * @param[out] index_p Optional pointer to return index of the added @p object. Usually it is the last index (::ly_set::count - 1),
92 * 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
93 * already present object is returned.
94 * @return LY_SUCCESS in case of success
95 * @return LY_EINVAL in case of invalid input parameters.
96 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020097 */
Radek Krejciba03a5a2020-08-27 14:40:41 +020098LY_ERR ly_set_add(struct ly_set *set, void *object, uint32_t options, uint32_t *index_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020099
100/**
101 * @brief Add all objects from \p src to \p trg.
102 *
103 * Since it is a set, the function checks for duplicities.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200104 *
105 * @param[in] trg Target (result) set.
106 * @param[in] src Source set.
107 * @param[in] options Options to change behavior of the function. Accepted options are:
Radek Krejci2f2bd902018-09-18 17:04:24 +0200108 * - #LY_SET_OPT_USEASLIST - add without checking for duplicities
109 * @param[in] duplicator Optional pointer to function that duplicates the objects being added
110 * from \p src into \p trg set. If not provided, the \p trg set will point to the exact same
111 * objects as the \p src set.
Radek Krejciba03a5a2020-08-27 14:40:41 +0200112 * @return LY_SUCCESS in case of success
113 * @return LY_EINVAL in case of invalid input parameters.
114 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200115 */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200116LY_ERR ly_set_merge(struct ly_set *trg, struct ly_set *src, uint32_t options, void *(*duplicator)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200117
118/**
Michal Vasko52927e22020-03-16 17:26:14 +0100119 * @brief Learn whether the set contains the specified object.
120 *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200121 * @param[in] set Set to explore.
122 * @param[in] object Object to be found in the set.
Radek Krejciba03a5a2020-08-27 14:40:41 +0200123 * @param[out] index_p Optional pointer to return index of the searched @p object.
Radek Krejci857189e2020-09-01 13:26:36 +0200124 * @return Boolean value whether the @p object was found in the @p set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200125 */
Radek Krejci857189e2020-09-01 13:26:36 +0200126ly_bool ly_set_contains(const struct ly_set *set, void *object, uint32_t *index_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200127
128/**
129 * @brief Remove all objects from the set, but keep the set container for further use.
130 *
131 * @param[in] set Set to clean.
Radek Krejcia40f21b2018-09-18 10:42:08 +0200132 * @param[in] destructor Optional function to free the objects in the set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200133 */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200134void ly_set_clean(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200135
136/**
137 * @brief Remove an object from the set.
138 *
139 * Note that after removing the object from a set, indexes of other objects in the set can change
140 * (the last object is placed instead of the removed object).
141 *
142 * @param[in] set Set from which the \p node will be removed.
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200143 * @param[in] object The object to be removed from the \p set.
Radek Krejci820d2262018-09-20 12:15:31 +0200144 * @param[in] destructor Optional function to free the objects being removed.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200145 * @return LY_ERR return value.
146 */
Radek Krejci820d2262018-09-20 12:15:31 +0200147LY_ERR ly_set_rm(struct ly_set *set, void *object, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200148
149/**
150 * @brief Remove an object on the specific set index.
151 *
152 * Note that after removing the object from a set, indexes of other nodes in the set can change
153 * (the last object is placed instead of the removed object).
154 *
155 * @param[in] set Set from which a node will be removed.
156 * @param[in] index Index of the object to remove in the \p set.
Radek Krejci820d2262018-09-20 12:15:31 +0200157 * @param[in] destructor Optional function to free the objects being removed.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200158 * @return LY_ERR return value.
159 */
Michal Vasko52927e22020-03-16 17:26:14 +0100160LY_ERR ly_set_rm_index(struct ly_set *set, uint32_t index, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200161
162/**
Radek Krejcia40f21b2018-09-18 10:42:08 +0200163 * @brief Free the ::ly_set data. If the destructor is not provided, it frees only the set structure
164 * content, not the referred data.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200165 *
166 * @param[in] set The set to be freed.
Radek Krejcia40f21b2018-09-18 10:42:08 +0200167 * @param[in] destructor Optional function to free the objects in the set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200168 */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200169void ly_set_free(struct ly_set *set, void (*destructor)(void *obj));
170
171/**
172 * @brief Alternative to the ly_set_free() for static ::ly_set objects - in contrast to ly_set_free()
173 * it does not free the provided ::ly_set object.
174 *
175 * @param[in] set The set to be erased.
176 * @param[in] destructor Optional function to free the objects in the set.
177 */
178void ly_set_erase(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200179
180/** @} lyset */
181
182#ifdef __cplusplus
183}
184#endif
185
186#endif /* LY_SET_H_ */