blob: 4a78f19edf42fc64474bf134e184edb8fd8f93ae [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));
24 LY_CHECK_ERR_RETURN(!new, LOGMEM(NULL), NULL);
25 return new;
26}
27
28API void
29ly_set_free(struct ly_set *set)
30{
31 LY_CHECK_ARG_NON_NULL_RETURN(NULL, set,);
32
33 free(set->objs);
34 free(set);
35}
36
37API int
38ly_set_contains(const struct ly_set *set, void *object)
39{
40 unsigned int i;
41
42 LY_CHECK_ARG_NON_NULL_RETURN(NULL, set, -1);
43
44 for (i = 0; i < set->number; i++) {
45 if (set->objs[i] == object) {
46 /* object found */
47 return i;
48 }
49 }
50
51 /* object not found */
52 return -1;
53}
54
55API struct ly_set *
56ly_set_dup(const struct ly_set *set)
57{
58 struct ly_set *new;
59
60 LY_CHECK_ARG_NON_NULL_RETURN(NULL, set, NULL);
61
62 new = malloc(sizeof *new);
63 LY_CHECK_ERR_RETURN(!new, LOGMEM(NULL), NULL);
64 new->number = set->number;
65 new->size = set->size;
66 new->objs = malloc(new->size * sizeof *(new->objs));
67 LY_CHECK_ERR_RETURN(!new->objs, LOGMEM(NULL); free(new), NULL);
68 memcpy(new->objs, set->objs, new->size * sizeof *(new->objs));
69
70 return new;
71}
72
73API int
74ly_set_add(struct ly_set *set, void *object, int options)
75{
76 unsigned int i;
77 void **new;
78
79 LY_CHECK_ARG_NON_NULL_RETURN(NULL, set, object, -1);
80
81 if (!(options & LY_SET_OPT_USEASLIST)) {
82 /* search for duplication */
83 for (i = 0; i < set->number; i++) {
84 if (set->objs[i] == object) {
85 /* already in set */
86 return i;
87 }
88 }
89 }
90
91 if (set->size == set->number) {
92 new = realloc(set->objs, (set->size + 8) * sizeof *(set->objs));
93 LY_CHECK_ERR_RETURN(!new, LOGMEM(NULL), -1);
94 set->size += 8;
95 set->objs = new;
96 }
97
98 set->objs[set->number++] = object;
99
100 return set->number - 1;
101}
102
103API int
104ly_set_merge(struct ly_set *trg, struct ly_set *src, int options)
105{
106 unsigned int i, ret;
107 void **new;
108
109 LY_CHECK_ARG_NON_NULL_RETURN(NULL, trg, -1);
110 LY_CHECK_ARG_NON_NULL_RETURN(NULL, src, 0);
111
112 if (!(options & LY_SET_OPT_USEASLIST)) {
113 /* remove duplicates */
114 i = 0;
115 while (i < src->number) {
116 if (ly_set_contains(trg, src->objs[i]) > -1) {
117 ly_set_rm_index(src, i);
118 } else {
119 ++i;
120 }
121 }
122 }
123
124 /* allocate more memory if needed */
125 if (trg->size < trg->number + src->number) {
126 new = realloc(trg->objs, (trg->number + src->number) * sizeof *(trg->objs));
127 LY_CHECK_ERR_RETURN(!new, LOGMEM(NULL), -1);
128 trg->size = trg->number + src->number;
129 trg->objs = new;
130 }
131
132 /* copy contents from src into trg */
133 memcpy(trg->objs + trg->number, src->objs, src->number * sizeof *(src->objs));
134 ret = src->number;
135 trg->number += ret;
136
137 /* cleanup */
138 ly_set_free(src);
139 return ret;
140}
141
142API LY_ERR
143ly_set_rm_index(struct ly_set *set, unsigned int index)
144{
145 LY_CHECK_ARG_NON_NULL_RETURN(NULL, set, -1);
146 LY_CHECK_ERR_RETURN(((index + 1) > set->number), LOGARG(NULL, set), LY_EINVAL);
147
148 if (index == set->number - 1) {
149 /* removing last item in set */
150 set->objs[index] = NULL;
151 } else {
152 /* removing item somewhere in a middle, so put there the last item */
153 set->objs[index] = set->objs[set->number - 1];
154 set->objs[set->number - 1] = NULL;
155 }
156 set->number--;
157
158 return LY_SUCCESS;
159}
160
161API LY_ERR
162ly_set_rm(struct ly_set *set, void *object)
163{
164 unsigned int i;
165
166 LY_CHECK_ARG_NON_NULL_RETURN(NULL, set, object, LY_EINVAL);
167
168 /* get index */
169 for (i = 0; i < set->number; i++) {
170 if (set->objs[i] == object) {
171 break;
172 }
173 }
174 LY_CHECK_ERR_RETURN((i == set->number), LOGARG(NULL, set), LY_EINVAL); /* object is not in set */
175
176 return ly_set_rm_index(set, i);
177}
178
179API LY_ERR
180ly_set_clean(struct ly_set *set)
181{
182 LY_CHECK_ARG_NON_NULL_RETURN(NULL, set, LY_EINVAL);
183
184 set->number = 0;
185 return LY_SUCCESS;
186}