blob: 7e567ebc5a72cdeb1275c814e79b87e7cb4f37a5 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file set.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Generic set routines implementations
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#include "libyang.h"
16#include "common.h"
17
18API struct ly_set *
19ly_set_new(void)
20{
21 struct ly_set *new;
22
23 new = calloc(1, sizeof(struct ly_set));
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020024 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020025 return new;
26}
27
28API void
Radek Krejcia40f21b2018-09-18 10:42:08 +020029ly_set_clean(struct ly_set *set, void (*destructor)(void *obj))
30{
31 unsigned int u;
32
33 LY_CHECK_ARG_RET(NULL, set,);
34
35 if (destructor) {
36 for (u = 0; u < set->count; ++u) {
37 destructor(set->objs[u]);
38 }
39 }
40 set->count = 0;
41}
42
43API void
44ly_set_erase(struct ly_set *set, void (*destructor)(void *obj))
Radek Krejci5aeea3a2018-09-05 13:29:36 +020045{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020046 LY_CHECK_ARG_RET(NULL, set,);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020047
Radek Krejcia40f21b2018-09-18 10:42:08 +020048 ly_set_clean(set, destructor);
49
Radek Krejci5aeea3a2018-09-05 13:29:36 +020050 free(set->objs);
Radek Krejcia40f21b2018-09-18 10:42:08 +020051 set->size = 0;
52 set->objs = NULL;
53}
54
55API void
56ly_set_free(struct ly_set *set, void (*destructor)(void *obj))
57{
58 LY_CHECK_ARG_RET(NULL, set,);
59
60 ly_set_erase(set, destructor);
61
Radek Krejci5aeea3a2018-09-05 13:29:36 +020062 free(set);
63}
64
65API int
66ly_set_contains(const struct ly_set *set, void *object)
67{
68 unsigned int i;
69
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020070 LY_CHECK_ARG_RET(NULL, set, -1);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020071
Michal Vaskob34480a2018-09-17 10:34:45 +020072 for (i = 0; i < set->count; i++) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +020073 if (set->objs[i] == object) {
74 /* object found */
75 return i;
76 }
77 }
78
79 /* object not found */
80 return -1;
81}
82
83API struct ly_set *
Radek Krejci2f2bd902018-09-18 17:04:24 +020084ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj))
Radek Krejci5aeea3a2018-09-05 13:29:36 +020085{
86 struct ly_set *new;
Radek Krejci2f2bd902018-09-18 17:04:24 +020087 unsigned int u;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020088
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020089 LY_CHECK_ARG_RET(NULL, set, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020090
91 new = malloc(sizeof *new);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020092 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
Michal Vaskob34480a2018-09-17 10:34:45 +020093 new->count = set->count;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020094 new->size = set->size;
95 new->objs = malloc(new->size * sizeof *(new->objs));
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020096 LY_CHECK_ERR_RET(!new->objs, LOGMEM(NULL); free(new), NULL);
Radek Krejci2f2bd902018-09-18 17:04:24 +020097 if (duplicator) {
98 for (u = 0; u < set->count; ++u) {
99 new->objs[u] = duplicator(set->objs[u]);
100 }
101 } else {
102 memcpy(new->objs, set->objs, new->size * sizeof *(new->objs));
103 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200104
105 return new;
106}
107
108API int
109ly_set_add(struct ly_set *set, void *object, int options)
110{
111 unsigned int i;
112 void **new;
113
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200114 LY_CHECK_ARG_RET(NULL, set, object, -1);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200115
116 if (!(options & LY_SET_OPT_USEASLIST)) {
117 /* search for duplication */
Michal Vaskob34480a2018-09-17 10:34:45 +0200118 for (i = 0; i < set->count; i++) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200119 if (set->objs[i] == object) {
120 /* already in set */
121 return i;
122 }
123 }
124 }
125
Michal Vaskob34480a2018-09-17 10:34:45 +0200126 if (set->size == set->count) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200127 new = realloc(set->objs, (set->size + 8) * sizeof *(set->objs));
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200128 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), -1);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200129 set->size += 8;
130 set->objs = new;
131 }
132
Michal Vaskob34480a2018-09-17 10:34:45 +0200133 set->objs[set->count++] = object;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200134
Michal Vaskob34480a2018-09-17 10:34:45 +0200135 return set->count - 1;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200136}
137
138API int
Radek Krejci2f2bd902018-09-18 17:04:24 +0200139ly_set_merge(struct ly_set *trg, struct ly_set *src, int options, void *(*duplicator)(void *obj))
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200140{
Radek Krejci2f2bd902018-09-18 17:04:24 +0200141 unsigned int u, c, ret = 0;
142 int i;
143 void *obj;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200144
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200145 LY_CHECK_ARG_RET(NULL, trg, -1);
146 LY_CHECK_ARG_RET(NULL, src, 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200147
Radek Krejci2f2bd902018-09-18 17:04:24 +0200148 for (u = 0; u < src->count; ++u) {
149 if (duplicator) {
150 obj = duplicator(src->objs[u]);
151 } else {
152 obj = src->objs[u];
153 }
154 c = trg->count;
155 i = ly_set_add(trg, obj, options);
156 if (i > 0 && (unsigned int)i == c) {
157 ++ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200158 }
159 }
160
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200161 return ret;
162}
163
164API LY_ERR
Radek Krejci820d2262018-09-20 12:15:31 +0200165ly_set_rm_index(struct ly_set *set, unsigned int index, void (*destructor)(void *obj))
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200166{
Radek Krejci13246392018-09-07 14:57:41 +0200167 LY_CHECK_ARG_RET(NULL, set, LY_EINVAL);
Radek Krejci820d2262018-09-20 12:15:31 +0200168 LY_CHECK_ERR_RET(index >= set->count, LOGARG(NULL, index), LY_EINVAL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200169
Radek Krejci820d2262018-09-20 12:15:31 +0200170 if (destructor) {
171 destructor(set->objs[index]);
172 }
Michal Vaskob34480a2018-09-17 10:34:45 +0200173 if (index == set->count - 1) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200174 /* removing last item in set */
175 set->objs[index] = NULL;
176 } else {
177 /* removing item somewhere in a middle, so put there the last item */
Michal Vaskob34480a2018-09-17 10:34:45 +0200178 set->objs[index] = set->objs[set->count - 1];
179 set->objs[set->count - 1] = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200180 }
Michal Vaskob34480a2018-09-17 10:34:45 +0200181 set->count--;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200182
183 return LY_SUCCESS;
184}
185
186API LY_ERR
Radek Krejci820d2262018-09-20 12:15:31 +0200187ly_set_rm(struct ly_set *set, void *object, void (*destructor)(void *obj))
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200188{
189 unsigned int i;
190
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200191 LY_CHECK_ARG_RET(NULL, set, object, LY_EINVAL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200192
193 /* get index */
Michal Vaskob34480a2018-09-17 10:34:45 +0200194 for (i = 0; i < set->count; i++) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200195 if (set->objs[i] == object) {
196 break;
197 }
198 }
Radek Krejci56616162018-09-18 14:11:00 +0200199 LY_CHECK_ERR_RET((i == set->count), LOGARG(NULL, object), LY_EINVAL); /* object is not in set */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200200
Radek Krejci820d2262018-09-20 12:15:31 +0200201 return ly_set_rm_index(set, i, destructor);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200202}
203