blob: 636d3d0dc8c382388997f483d5af516838e941f3 [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 *
Radek Krejci8678fa42020-08-18 16:07:28 +020040 * Until ::ly_set_rm() or ::ly_set_rm_index() is used, the set keeps the order of the inserted items as they
Radek Krejci5aeea3a2018-09-05 13:29:36 +020041 * were added into the set, so the first added item is on array index 0.
42 *
Radek Krejci8678fa42020-08-18 16:07:28 +020043 * To free the structure, use ::ly_set_free() function, to manipulate with the structure, use other
Radek Krejci5aeea3a2018-09-05 13:29:36 +020044 * ly_set_* functions.
45 */
Michal Vasko87f1cf02021-06-08 14:02:47 +020046struct ly_set {
Michal Vasko52927e22020-03-16 17:26:14 +010047 uint32_t size; /**< allocated size of the set array */
48 uint32_t count; /**< number of elements in (used size of) the set array */
Michal Vasko26bbb272022-08-02 14:54:33 +020049
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/**
Radek Krejci5aeea3a2018-09-05 13:29:36 +020058 * @brief Create and initiate new ::ly_set structure.
59 *
Radek Krejci8678fa42020-08-18 16:07:28 +020060 * @param[out] set_p Pointer to store the created ::ly_set structure.
Radek Krejciba03a5a2020-08-27 14:40:41 +020061 * @return LY_SUCCESS on success.
62 * @return LY_EINVAL in case of NULL @p set parameter.
63 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020064 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010065LIBYANG_API_DECL LY_ERR ly_set_new(struct ly_set **set_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020066
67/**
68 * @brief Duplicate the existing set.
69 *
70 * @param[in] set Original set to duplicate
Radek Krejci2f2bd902018-09-18 17:04:24 +020071 * @param[in] duplicator Optional pointer to function that duplicates the objects stored
72 * in the original set. If not provided, the new set points to the exact same objects as
73 * the original set.
Radek Krejci8678fa42020-08-18 16:07:28 +020074 * @param[out] newset_p Pointer to return the duplication of the original set.
75 * @return LY_SUCCESS in case the data were successfully duplicated.
76 * @return LY_EMEM in case of memory allocation failure.
77 * @return LY_EINVAL in case of invalid parameters.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020078 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010079LIBYANG_API_DECL LY_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 +020080
81/**
Radek Krejcie53a8dc2018-10-17 12:52:40 +020082 * @brief Add an object into the set
Radek Krejci5aeea3a2018-09-05 13:29:36 +020083 *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020084 * @param[in] set Set where the \p object will be added.
85 * @param[in] object Object to be added into the \p set;
Radek Krejci3d92e442020-10-12 12:48:13 +020086 * @param[in] list flag to handle set as a list (without checking for (ignoring) duplicit items)
Radek Krejciba03a5a2020-08-27 14:40:41 +020087 * @param[out] index_p Optional pointer to return index of the added @p object. Usually it is the last index (::ly_set::count - 1),
88 * 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
89 * already present object is returned.
90 * @return LY_SUCCESS in case of success
91 * @return LY_EINVAL in case of invalid input parameters.
92 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020093 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010094LIBYANG_API_DECL LY_ERR ly_set_add(struct ly_set *set, void *object, ly_bool list, uint32_t *index_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020095
96/**
97 * @brief Add all objects from \p src to \p trg.
98 *
99 * Since it is a set, the function checks for duplicities.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200100 *
101 * @param[in] trg Target (result) set.
102 * @param[in] src Source set.
Radek Krejci52ca92d2020-10-12 16:51:22 +0200103 * @param[in] list flag to handle set as a list (without checking for (ignoring) duplicit items)
Radek Krejci2f2bd902018-09-18 17:04:24 +0200104 * @param[in] duplicator Optional pointer to function that duplicates the objects being added
105 * from \p src into \p trg set. If not provided, the \p trg set will point to the exact same
106 * objects as the \p src set.
Radek Krejciba03a5a2020-08-27 14:40:41 +0200107 * @return LY_SUCCESS in case of success
108 * @return LY_EINVAL in case of invalid input parameters.
109 * @return LY_EMEM in case of memory allocation failure.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200110 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100111LIBYANG_API_DECL LY_ERR ly_set_merge(struct ly_set *trg, const struct ly_set *src, ly_bool list, void *(*duplicator)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200112
113/**
Michal Vasko52927e22020-03-16 17:26:14 +0100114 * @brief Learn whether the set contains the specified object.
115 *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200116 * @param[in] set Set to explore.
117 * @param[in] object Object to be found in the set.
Radek Krejciba03a5a2020-08-27 14:40:41 +0200118 * @param[out] index_p Optional pointer to return index of the searched @p object.
Radek Krejci857189e2020-09-01 13:26:36 +0200119 * @return Boolean value whether the @p object was found in the @p set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200120 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100121LIBYANG_API_DECL ly_bool ly_set_contains(const struct ly_set *set, void *object, uint32_t *index_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200122
123/**
124 * @brief Remove all objects from the set, but keep the set container for further use.
125 *
126 * @param[in] set Set to clean.
Radek Krejcia40f21b2018-09-18 10:42:08 +0200127 * @param[in] destructor Optional function to free the objects in the set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200128 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100129LIBYANG_API_DECL void ly_set_clean(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200130
131/**
132 * @brief Remove an object from the set.
133 *
134 * Note that after removing the object from a set, indexes of other objects in the set can change
135 * (the last object is placed instead of the removed object).
136 *
137 * @param[in] set Set from which the \p node will be removed.
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200138 * @param[in] object The object to be removed from the \p set.
Radek Krejci820d2262018-09-20 12:15:31 +0200139 * @param[in] destructor Optional function to free the objects being removed.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200140 * @return LY_ERR return value.
141 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100142LIBYANG_API_DECL LY_ERR ly_set_rm(struct ly_set *set, void *object, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200143
144/**
145 * @brief Remove an object on the specific set index.
146 *
147 * Note that after removing the object from a set, indexes of other nodes in the set can change
148 * (the last object is placed instead of the removed object).
149 *
150 * @param[in] set Set from which a node will be removed.
151 * @param[in] index Index of the object to remove in the \p set.
Radek Krejci820d2262018-09-20 12:15:31 +0200152 * @param[in] destructor Optional function to free the objects being removed.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200153 * @return LY_ERR return value.
154 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100155LIBYANG_API_DECL LY_ERR ly_set_rm_index(struct ly_set *set, uint32_t index, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200156
157/**
Radek Krejcia40f21b2018-09-18 10:42:08 +0200158 * @brief Free the ::ly_set data. If the destructor is not provided, it frees only the set structure
159 * content, not the referred data.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200160 *
161 * @param[in] set The set to be freed.
Radek Krejcia40f21b2018-09-18 10:42:08 +0200162 * @param[in] destructor Optional function to free the objects in the set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200163 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100164LIBYANG_API_DECL void ly_set_free(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejcia40f21b2018-09-18 10:42:08 +0200165
166/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200167 * @brief Alternative to the ::ly_set_free() for static ::ly_set objects - in contrast to ::ly_set_free()
Radek Krejcia40f21b2018-09-18 10:42:08 +0200168 * it does not free the provided ::ly_set object.
169 *
170 * @param[in] set The set to be erased.
171 * @param[in] destructor Optional function to free the objects in the set.
172 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100173LIBYANG_API_DECL void ly_set_erase(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200174
175/** @} lyset */
176
177#ifdef __cplusplus
178}
179#endif
180
181#endif /* LY_SET_H_ */