Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 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 | |
| 18 | API struct ly_set * |
| 19 | ly_set_new(void) |
| 20 | { |
| 21 | struct ly_set *new; |
| 22 | |
| 23 | new = calloc(1, sizeof(struct ly_set)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 24 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 25 | return new; |
| 26 | } |
| 27 | |
| 28 | API void |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 29 | ly_set_clean(struct ly_set *set, void (*destructor)(void *obj)) |
| 30 | { |
| 31 | unsigned int u; |
| 32 | |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame^] | 33 | if (!set) { |
| 34 | return; |
| 35 | } |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 36 | |
| 37 | if (destructor) { |
| 38 | for (u = 0; u < set->count; ++u) { |
| 39 | destructor(set->objs[u]); |
| 40 | } |
| 41 | } |
| 42 | set->count = 0; |
| 43 | } |
| 44 | |
| 45 | API void |
| 46 | ly_set_erase(struct ly_set *set, void (*destructor)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 47 | { |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame^] | 48 | if (!set) { |
| 49 | return; |
| 50 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 51 | |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 52 | ly_set_clean(set, destructor); |
| 53 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 54 | free(set->objs); |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 55 | set->size = 0; |
| 56 | set->objs = NULL; |
| 57 | } |
| 58 | |
| 59 | API void |
| 60 | ly_set_free(struct ly_set *set, void (*destructor)(void *obj)) |
| 61 | { |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame^] | 62 | if (!set) { |
| 63 | return; |
| 64 | } |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 65 | |
| 66 | ly_set_erase(set, destructor); |
| 67 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 68 | free(set); |
| 69 | } |
| 70 | |
| 71 | API int |
| 72 | ly_set_contains(const struct ly_set *set, void *object) |
| 73 | { |
| 74 | unsigned int i; |
| 75 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 76 | LY_CHECK_ARG_RET(NULL, set, -1); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 77 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 78 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 79 | if (set->objs[i] == object) { |
| 80 | /* object found */ |
| 81 | return i; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* object not found */ |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | API struct ly_set * |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 90 | ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 91 | { |
| 92 | struct ly_set *new; |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 93 | unsigned int u; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 94 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 95 | LY_CHECK_ARG_RET(NULL, set, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 96 | |
| 97 | new = malloc(sizeof *new); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 98 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 99 | new->count = set->count; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 100 | new->size = set->size; |
| 101 | new->objs = malloc(new->size * sizeof *(new->objs)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 102 | LY_CHECK_ERR_RET(!new->objs, LOGMEM(NULL); free(new), NULL); |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 103 | if (duplicator) { |
| 104 | for (u = 0; u < set->count; ++u) { |
| 105 | new->objs[u] = duplicator(set->objs[u]); |
| 106 | } |
| 107 | } else { |
| 108 | memcpy(new->objs, set->objs, new->size * sizeof *(new->objs)); |
| 109 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 110 | |
| 111 | return new; |
| 112 | } |
| 113 | |
| 114 | API int |
| 115 | ly_set_add(struct ly_set *set, void *object, int options) |
| 116 | { |
| 117 | unsigned int i; |
| 118 | void **new; |
| 119 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 120 | LY_CHECK_ARG_RET(NULL, set, object, -1); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 121 | |
| 122 | if (!(options & LY_SET_OPT_USEASLIST)) { |
| 123 | /* search for duplication */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 124 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 125 | if (set->objs[i] == object) { |
| 126 | /* already in set */ |
| 127 | return i; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 132 | if (set->size == set->count) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 133 | new = realloc(set->objs, (set->size + 8) * sizeof *(set->objs)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 134 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), -1); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 135 | set->size += 8; |
| 136 | set->objs = new; |
| 137 | } |
| 138 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 139 | set->objs[set->count++] = object; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 140 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 141 | return set->count - 1; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | API int |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 145 | ly_set_merge(struct ly_set *trg, struct ly_set *src, int options, void *(*duplicator)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 146 | { |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 147 | unsigned int u, c, ret = 0; |
| 148 | int i; |
| 149 | void *obj; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 150 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 151 | LY_CHECK_ARG_RET(NULL, trg, -1); |
| 152 | LY_CHECK_ARG_RET(NULL, src, 0); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 153 | |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 154 | for (u = 0; u < src->count; ++u) { |
| 155 | if (duplicator) { |
| 156 | obj = duplicator(src->objs[u]); |
| 157 | } else { |
| 158 | obj = src->objs[u]; |
| 159 | } |
| 160 | c = trg->count; |
| 161 | i = ly_set_add(trg, obj, options); |
| 162 | if (i > 0 && (unsigned int)i == c) { |
| 163 | ++ret; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | API LY_ERR |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 171 | ly_set_rm_index(struct ly_set *set, unsigned int index, void (*destructor)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 172 | { |
Radek Krejci | 1324639 | 2018-09-07 14:57:41 +0200 | [diff] [blame] | 173 | LY_CHECK_ARG_RET(NULL, set, LY_EINVAL); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 174 | LY_CHECK_ERR_RET(index >= set->count, LOGARG(NULL, index), LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 175 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 176 | if (destructor) { |
| 177 | destructor(set->objs[index]); |
| 178 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 179 | if (index == set->count - 1) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 180 | /* removing last item in set */ |
| 181 | set->objs[index] = NULL; |
| 182 | } else { |
| 183 | /* removing item somewhere in a middle, so put there the last item */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 184 | set->objs[index] = set->objs[set->count - 1]; |
| 185 | set->objs[set->count - 1] = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 186 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 187 | set->count--; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 188 | |
| 189 | return LY_SUCCESS; |
| 190 | } |
| 191 | |
| 192 | API LY_ERR |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 193 | ly_set_rm(struct ly_set *set, void *object, void (*destructor)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 194 | { |
| 195 | unsigned int i; |
| 196 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 197 | LY_CHECK_ARG_RET(NULL, set, object, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 198 | |
| 199 | /* get index */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 200 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 201 | if (set->objs[i] == object) { |
| 202 | break; |
| 203 | } |
| 204 | } |
Radek Krejci | 5661616 | 2018-09-18 14:11:00 +0200 | [diff] [blame] | 205 | LY_CHECK_ERR_RET((i == set->count), LOGARG(NULL, object), LY_EINVAL); /* object is not in set */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 206 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 207 | return ly_set_rm_index(set, i, destructor); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 208 | } |
| 209 | |