blob: 1074e1f40fdc3677119589aedd5e1c2aa7953329 [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 *
66 * @return Created ::ly_set structure or NULL in case of error.
67 */
Michal Vaskob34480a2018-09-17 10:34:45 +020068struct ly_set *ly_set_new(void);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020069
70/**
71 * @brief Duplicate the existing set.
72 *
73 * @param[in] set Original set to duplicate
Radek Krejci2f2bd902018-09-18 17:04:24 +020074 * @param[in] duplicator Optional pointer to function that duplicates the objects stored
75 * in the original set. If not provided, the new set points to the exact same objects as
76 * the original set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020077 * @return Duplication of the original set.
78 */
Radek Krejci2f2bd902018-09-18 17:04:24 +020079struct ly_set *ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj));
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 *
84 * Since it is a set, the function checks for duplicity and if the
85 * node is already in the set, the index of the previously added
86 * node is returned.
87 *
88 * @param[in] set Set where the \p object will be added.
89 * @param[in] object Object to be added into the \p set;
90 * @param[in] options Options to change behavior of the function. Accepted options are:
91 * - #LY_SET_OPT_USEASLIST - do not check for duplicities
92 * @return -1 on failure, index of the \p node in the set on success
93 */
Michal Vaskob34480a2018-09-17 10:34:45 +020094int ly_set_add(struct ly_set *set, void *object, int options);
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.
103 * @param[in] options Options to change behavior of the function. Accepted options are:
Radek Krejci2f2bd902018-09-18 17:04:24 +0200104 * - #LY_SET_OPT_USEASLIST - add without checking for duplicities
105 * @param[in] duplicator Optional pointer to function that duplicates the objects being added
106 * from \p src into \p trg set. If not provided, the \p trg set will point to the exact same
107 * objects as the \p src set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200108 * @return -1 on failure, number of objects added into \p trg on success.
109 */
Radek Krejci2f2bd902018-09-18 17:04:24 +0200110int ly_set_merge(struct ly_set *trg, struct ly_set *src, int options, void *(*duplicator)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200111
112/**
Michal Vasko52927e22020-03-16 17:26:14 +0100113 * @brief Learn whether the set contains the specified object.
114 *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200115 * @param[in] set Set to explore.
116 * @param[in] object Object to be found in the set.
117 * @return Index of the object in the set or -1 if the object is not present in the set.
118 */
Michal Vaskob34480a2018-09-17 10:34:45 +0200119int ly_set_contains(const struct ly_set *set, void *object);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200120
121/**
122 * @brief Remove all objects from the set, but keep the set container for further use.
123 *
124 * @param[in] set Set to clean.
Radek Krejcia40f21b2018-09-18 10:42:08 +0200125 * @param[in] destructor Optional function to free the objects in the set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200126 */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200127void ly_set_clean(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200128
129/**
130 * @brief Remove an object from the set.
131 *
132 * Note that after removing the object from a set, indexes of other objects in the set can change
133 * (the last object is placed instead of the removed object).
134 *
135 * @param[in] set Set from which the \p node will be removed.
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200136 * @param[in] object The object to be removed from the \p set.
Radek Krejci820d2262018-09-20 12:15:31 +0200137 * @param[in] destructor Optional function to free the objects being removed.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200138 * @return LY_ERR return value.
139 */
Radek Krejci820d2262018-09-20 12:15:31 +0200140LY_ERR ly_set_rm(struct ly_set *set, void *object, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200141
142/**
143 * @brief Remove an object on the specific set index.
144 *
145 * Note that after removing the object from a set, indexes of other nodes in the set can change
146 * (the last object is placed instead of the removed object).
147 *
148 * @param[in] set Set from which a node will be removed.
149 * @param[in] index Index of the object to remove in the \p set.
Radek Krejci820d2262018-09-20 12:15:31 +0200150 * @param[in] destructor Optional function to free the objects being removed.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200151 * @return LY_ERR return value.
152 */
Michal Vasko52927e22020-03-16 17:26:14 +0100153LY_ERR ly_set_rm_index(struct ly_set *set, uint32_t index, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200154
155/**
Radek Krejcia40f21b2018-09-18 10:42:08 +0200156 * @brief Free the ::ly_set data. If the destructor is not provided, it frees only the set structure
157 * content, not the referred data.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200158 *
159 * @param[in] set The set to be freed.
Radek Krejcia40f21b2018-09-18 10:42:08 +0200160 * @param[in] destructor Optional function to free the objects in the set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200161 */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200162void ly_set_free(struct ly_set *set, void (*destructor)(void *obj));
163
164/**
165 * @brief Alternative to the ly_set_free() for static ::ly_set objects - in contrast to ly_set_free()
166 * it does not free the provided ::ly_set object.
167 *
168 * @param[in] set The set to be erased.
169 * @param[in] destructor Optional function to free the objects in the set.
170 */
171void ly_set_erase(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200172
173/** @} lyset */
174
175#ifdef __cplusplus
176}
177#endif
178
179#endif /* LY_SET_H_ */