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