blob: f16feae9f71a01b6a8a712b4c657c342213f7808 [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
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/**
23 * @defgroup lyset Generic sets
24 *
25 * Structure and functions to hold and manipulate with sets of nodes from schema or data trees.
26 *
27 * @{
28 */
29
30/**
Radek Krejcie53a8dc2018-10-17 12:52:40 +020031 * @brief Structure to hold a set of (not necessary somehow connected) objects. Usually used for ::lyd_node,
32 * ::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 +020033 * added to the set and according to its knowledge about the set content, it can access objects via the members
34 * of the set union.
35 *
36 * Until ly_set_rm() or ly_set_rm_index() is used, the set keeps the order of the inserted items as they
37 * were added into the set, so the first added item is on array index 0.
38 *
39 * To free the structure, use ly_set_free() function, to manipulate with the structure, use other
40 * ly_set_* functions.
41 */
42struct ly_set
43{
44 unsigned int size; /**< allocated size of the set array */
Michal Vaskob34480a2018-09-17 10:34:45 +020045 unsigned int count; /**< number of elements in (used size of) the set array */
Radek Krejcie53a8dc2018-10-17 12:52:40 +020046 void **objs; /**< set array of generic object pointers */
Radek Krejci5aeea3a2018-09-05 13:29:36 +020047};
48
49/**
50 * @brief Option for ly_set_add() to allow duplicities in the ly_set structure so the
51 * set is not used as a set, but as a list of (container for) items.
52 */
53#define LY_SET_OPT_USEASLIST 0x01
54
55/**
56 * @brief Create and initiate new ::ly_set structure.
57 *
58 * @return Created ::ly_set structure or NULL in case of error.
59 */
Michal Vaskob34480a2018-09-17 10:34:45 +020060struct ly_set *ly_set_new(void);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020061
62/**
63 * @brief Duplicate the existing set.
64 *
65 * @param[in] set Original set to duplicate
Radek Krejci2f2bd902018-09-18 17:04:24 +020066 * @param[in] duplicator Optional pointer to function that duplicates the objects stored
67 * in the original set. If not provided, the new set points to the exact same objects as
68 * the original set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020069 * @return Duplication of the original set.
70 */
Radek Krejci2f2bd902018-09-18 17:04:24 +020071struct ly_set *ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +020072
73/**
Radek Krejcie53a8dc2018-10-17 12:52:40 +020074 * @brief Add an object into the set
Radek Krejci5aeea3a2018-09-05 13:29:36 +020075 *
76 * Since it is a set, the function checks for duplicity and if the
77 * node is already in the set, the index of the previously added
78 * node is returned.
79 *
80 * @param[in] set Set where the \p object will be added.
81 * @param[in] object Object to be added into the \p set;
82 * @param[in] options Options to change behavior of the function. Accepted options are:
83 * - #LY_SET_OPT_USEASLIST - do not check for duplicities
84 * @return -1 on failure, index of the \p node in the set on success
85 */
Michal Vaskob34480a2018-09-17 10:34:45 +020086int ly_set_add(struct ly_set *set, void *object, int options);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020087
88/**
89 * @brief Add all objects from \p src to \p trg.
90 *
91 * Since it is a set, the function checks for duplicities.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020092 *
93 * @param[in] trg Target (result) set.
94 * @param[in] src Source set.
95 * @param[in] options Options to change behavior of the function. Accepted options are:
Radek Krejci2f2bd902018-09-18 17:04:24 +020096 * - #LY_SET_OPT_USEASLIST - add without checking for duplicities
97 * @param[in] duplicator Optional pointer to function that duplicates the objects being added
98 * from \p src into \p trg set. If not provided, the \p trg set will point to the exact same
99 * objects as the \p src set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200100 * @return -1 on failure, number of objects added into \p trg on success.
101 */
Radek Krejci2f2bd902018-09-18 17:04:24 +0200102int ly_set_merge(struct ly_set *trg, struct ly_set *src, int options, void *(*duplicator)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200103
104/**
105 * @brief Get know if the set contains the specified object.
106 * @param[in] set Set to explore.
107 * @param[in] object Object to be found in the set.
108 * @return Index of the object in the set or -1 if the object is not present in the set.
109 */
Michal Vaskob34480a2018-09-17 10:34:45 +0200110int ly_set_contains(const struct ly_set *set, void *object);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200111
112/**
113 * @brief Remove all objects from the set, but keep the set container for further use.
114 *
115 * @param[in] set Set to clean.
Radek Krejcia40f21b2018-09-18 10:42:08 +0200116 * @param[in] destructor Optional function to free the objects in the set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200117 */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200118void ly_set_clean(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200119
120/**
121 * @brief Remove an object from the set.
122 *
123 * Note that after removing the object from a set, indexes of other objects in the set can change
124 * (the last object is placed instead of the removed object).
125 *
126 * @param[in] set Set from which the \p node will be removed.
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200127 * @param[in] object The object to be removed from the \p set.
Radek Krejci820d2262018-09-20 12:15:31 +0200128 * @param[in] destructor Optional function to free the objects being removed.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200129 * @return LY_ERR return value.
130 */
Radek Krejci820d2262018-09-20 12:15:31 +0200131LY_ERR ly_set_rm(struct ly_set *set, void *object, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200132
133/**
134 * @brief Remove an object on the specific set index.
135 *
136 * Note that after removing the object from a set, indexes of other nodes in the set can change
137 * (the last object is placed instead of the removed object).
138 *
139 * @param[in] set Set from which a node will be removed.
140 * @param[in] index Index of the object to remove in the \p set.
Radek Krejci820d2262018-09-20 12:15:31 +0200141 * @param[in] destructor Optional function to free the objects being removed.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200142 * @return LY_ERR return value.
143 */
Radek Krejci820d2262018-09-20 12:15:31 +0200144LY_ERR ly_set_rm_index(struct ly_set *set, unsigned int index, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200145
146/**
Radek Krejcia40f21b2018-09-18 10:42:08 +0200147 * @brief Free the ::ly_set data. If the destructor is not provided, it frees only the set structure
148 * content, not the referred data.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200149 *
150 * @param[in] set The set to be freed.
Radek Krejcia40f21b2018-09-18 10:42:08 +0200151 * @param[in] destructor Optional function to free the objects in the set.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200152 */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200153void ly_set_free(struct ly_set *set, void (*destructor)(void *obj));
154
155/**
156 * @brief Alternative to the ly_set_free() for static ::ly_set objects - in contrast to ly_set_free()
157 * it does not free the provided ::ly_set object.
158 *
159 * @param[in] set The set to be erased.
160 * @param[in] destructor Optional function to free the objects in the set.
161 */
162void ly_set_erase(struct ly_set *set, void (*destructor)(void *obj));
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200163
164/** @} lyset */
165
166#ifdef __cplusplus
167}
168#endif
169
170#endif /* LY_SET_H_ */