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 * |
| 84 | ly_set_dup(const struct ly_set *set) |
| 85 | { |
| 86 | struct ly_set *new; |
| 87 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 88 | LY_CHECK_ARG_RET(NULL, set, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 89 | |
| 90 | new = malloc(sizeof *new); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 91 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 92 | new->count = set->count; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 93 | new->size = set->size; |
| 94 | new->objs = malloc(new->size * sizeof *(new->objs)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 95 | LY_CHECK_ERR_RET(!new->objs, LOGMEM(NULL); free(new), NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 96 | memcpy(new->objs, set->objs, new->size * sizeof *(new->objs)); |
| 97 | |
| 98 | return new; |
| 99 | } |
| 100 | |
| 101 | API int |
| 102 | ly_set_add(struct ly_set *set, void *object, int options) |
| 103 | { |
| 104 | unsigned int i; |
| 105 | void **new; |
| 106 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 107 | LY_CHECK_ARG_RET(NULL, set, object, -1); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 108 | |
| 109 | if (!(options & LY_SET_OPT_USEASLIST)) { |
| 110 | /* search for duplication */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 111 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 112 | if (set->objs[i] == object) { |
| 113 | /* already in set */ |
| 114 | return i; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 119 | if (set->size == set->count) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 120 | new = realloc(set->objs, (set->size + 8) * sizeof *(set->objs)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 121 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), -1); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 122 | set->size += 8; |
| 123 | set->objs = new; |
| 124 | } |
| 125 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 126 | set->objs[set->count++] = object; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 127 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 128 | return set->count - 1; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | API int |
| 132 | ly_set_merge(struct ly_set *trg, struct ly_set *src, int options) |
| 133 | { |
| 134 | unsigned int i, ret; |
| 135 | void **new; |
| 136 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 137 | LY_CHECK_ARG_RET(NULL, trg, -1); |
| 138 | LY_CHECK_ARG_RET(NULL, src, 0); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 139 | |
| 140 | if (!(options & LY_SET_OPT_USEASLIST)) { |
| 141 | /* remove duplicates */ |
| 142 | i = 0; |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 143 | while (i < src->count) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 144 | if (ly_set_contains(trg, src->objs[i]) > -1) { |
| 145 | ly_set_rm_index(src, i); |
| 146 | } else { |
| 147 | ++i; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* allocate more memory if needed */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 153 | if (trg->size < trg->count + src->count) { |
| 154 | new = realloc(trg->objs, (trg->count + src->count) * sizeof *(trg->objs)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 155 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), -1); |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 156 | trg->size = trg->count + src->count; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 157 | trg->objs = new; |
| 158 | } |
| 159 | |
| 160 | /* copy contents from src into trg */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 161 | memcpy(trg->objs + trg->count, src->objs, src->count * sizeof *(src->objs)); |
| 162 | ret = src->count; |
| 163 | trg->count += ret; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 164 | |
| 165 | /* cleanup */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame^] | 166 | ly_set_free(src, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | API LY_ERR |
| 171 | ly_set_rm_index(struct ly_set *set, unsigned int index) |
| 172 | { |
Radek Krejci | 1324639 | 2018-09-07 14:57:41 +0200 | [diff] [blame] | 173 | LY_CHECK_ARG_RET(NULL, set, LY_EINVAL); |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 174 | LY_CHECK_ERR_RET(((index + 1) > set->count), LOGARG(NULL, set), LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 175 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 176 | if (index == set->count - 1) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 177 | /* removing last item in set */ |
| 178 | set->objs[index] = NULL; |
| 179 | } else { |
| 180 | /* removing item somewhere in a middle, so put there the last item */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 181 | set->objs[index] = set->objs[set->count - 1]; |
| 182 | set->objs[set->count - 1] = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 183 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 184 | set->count--; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 185 | |
| 186 | return LY_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | API LY_ERR |
| 190 | ly_set_rm(struct ly_set *set, void *object) |
| 191 | { |
| 192 | unsigned int i; |
| 193 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 194 | LY_CHECK_ARG_RET(NULL, set, object, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 195 | |
| 196 | /* get index */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 197 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 198 | if (set->objs[i] == object) { |
| 199 | break; |
| 200 | } |
| 201 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 202 | LY_CHECK_ERR_RET((i == set->count), LOGARG(NULL, set), LY_EINVAL); /* object is not in set */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 203 | |
| 204 | return ly_set_rm_index(set, i); |
| 205 | } |
| 206 | |