diff options
author | orivej <orivej@yandex-team.ru> | 2022-02-10 16:44:49 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:49 +0300 |
commit | 718c552901d703c502ccbefdfc3c9028d608b947 (patch) | |
tree | 46534a98bbefcd7b1f3faa5b52c138ab27db75b7 /contrib/restricted/aws/s2n/utils/s2n_set.c | |
parent | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (diff) | |
download | ydb-718c552901d703c502ccbefdfc3c9028d608b947.tar.gz |
Restoring authorship annotation for <orivej@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/restricted/aws/s2n/utils/s2n_set.c')
-rw-r--r-- | contrib/restricted/aws/s2n/utils/s2n_set.c | 294 |
1 files changed, 147 insertions, 147 deletions
diff --git a/contrib/restricted/aws/s2n/utils/s2n_set.c b/contrib/restricted/aws/s2n/utils/s2n_set.c index 4073a4cc5d..9c5d5769fa 100644 --- a/contrib/restricted/aws/s2n/utils/s2n_set.c +++ b/contrib/restricted/aws/s2n/utils/s2n_set.c @@ -1,147 +1,147 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ -#include "utils/s2n_blob.h" -#include "utils/s2n_mem.h" -#include "utils/s2n_result.h" -#include "utils/s2n_safety.h" -#include "utils/s2n_set.h" -#include "utils/s2n_array.h" - -#define S2N_INITIAL_SET_SIZE 16 - -S2N_RESULT s2n_set_validate(const struct s2n_set *set) -{ - ENSURE_REF(set); - GUARD_RESULT(s2n_array_validate(set->data)); - return S2N_RESULT_OK; -} - -/* Sets "out" to the index at which the element should be inserted. - * Returns an error if the element already exists */ -static S2N_RESULT s2n_set_binary_search(struct s2n_set *set, void *element, uint32_t* out) -{ - GUARD_RESULT(s2n_set_validate(set)); - ENSURE(S2N_MEM_IS_READABLE(element, set->data->element_size), S2N_ERR_NULL); - ENSURE_REF(out); - struct s2n_array *array = set->data; - int (*comparator)(const void*, const void*) = set->comparator; - - uint32_t len = 0; - GUARD_RESULT(s2n_array_num_elements(array, &len)); - - if (len == 0) { - *out = 0; - return S2N_RESULT_OK; - } - - /* Use 64 bit ints to avoid possibility of overflow */ - int64_t low = 0; - int64_t top = len - 1; - - while (low <= top) { - int64_t mid = low + ((top - low) / 2); - void* array_element = NULL; - GUARD_RESULT(s2n_array_get(array, mid, &array_element)); - int m = comparator(array_element, element); - - /* the element is already in the set */ - if (m == 0) { - BAIL(S2N_ERR_SET_DUPLICATE_VALUE); - } - - if (m > 0) { - top = mid - 1; - } else { - low = mid + 1; - } - } - - *out = low; - return S2N_RESULT_OK; -} - -struct s2n_set *s2n_set_new(uint32_t element_size, int (*comparator)(const void*, const void*)) -{ - notnull_check_ptr(comparator); - struct s2n_blob mem = {0}; - GUARD_POSIX_PTR(s2n_alloc(&mem, sizeof(struct s2n_set))); - struct s2n_set *set = (void *) mem.data; - *set = (struct s2n_set) {.data = s2n_array_new(element_size), .comparator = comparator}; - if(set->data == NULL) { - GUARD_POSIX_PTR(s2n_free(&mem)); - return NULL; - } - return set; -} - -S2N_RESULT s2n_set_add(struct s2n_set *set, void *element) -{ - GUARD_RESULT(s2n_set_validate(set)); - - uint32_t index = 0; - GUARD_RESULT(s2n_set_binary_search(set, element, &index)); - GUARD_RESULT(s2n_array_insert_and_copy(set->data, index, element)); - - return S2N_RESULT_OK; -} - -S2N_RESULT s2n_set_get(struct s2n_set *set, uint32_t index, void **element) -{ - GUARD_RESULT(s2n_set_validate(set)); - ENSURE_REF(element); - - GUARD_RESULT(s2n_array_get(set->data, index, element)); - - return S2N_RESULT_OK; -} - -S2N_RESULT s2n_set_remove(struct s2n_set *set, uint32_t index) -{ - GUARD_RESULT(s2n_set_validate(set)); - GUARD_RESULT(s2n_array_remove(set->data, index)); - - return S2N_RESULT_OK; -} - -S2N_RESULT s2n_set_free_p(struct s2n_set **pset) -{ - ENSURE_REF(pset); - struct s2n_set *set = *pset; - - ENSURE_REF(set); - GUARD_RESULT(s2n_array_free(set->data)); - - /* And finally the set object. */ - GUARD_AS_RESULT(s2n_free_object((uint8_t **)pset, sizeof(struct s2n_set))); - - return S2N_RESULT_OK; - -} - -S2N_RESULT s2n_set_free(struct s2n_set *set) -{ - ENSURE_REF(set); - return s2n_set_free_p(&set); -} - - -S2N_RESULT s2n_set_len(struct s2n_set *set, uint32_t *len) -{ - GUARD_RESULT(s2n_set_validate(set)); - - GUARD_RESULT(s2n_array_num_elements(set->data, len)); - - return S2N_RESULT_OK; -} +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +#include "utils/s2n_blob.h" +#include "utils/s2n_mem.h" +#include "utils/s2n_result.h" +#include "utils/s2n_safety.h" +#include "utils/s2n_set.h" +#include "utils/s2n_array.h" + +#define S2N_INITIAL_SET_SIZE 16 + +S2N_RESULT s2n_set_validate(const struct s2n_set *set) +{ + ENSURE_REF(set); + GUARD_RESULT(s2n_array_validate(set->data)); + return S2N_RESULT_OK; +} + +/* Sets "out" to the index at which the element should be inserted. + * Returns an error if the element already exists */ +static S2N_RESULT s2n_set_binary_search(struct s2n_set *set, void *element, uint32_t* out) +{ + GUARD_RESULT(s2n_set_validate(set)); + ENSURE(S2N_MEM_IS_READABLE(element, set->data->element_size), S2N_ERR_NULL); + ENSURE_REF(out); + struct s2n_array *array = set->data; + int (*comparator)(const void*, const void*) = set->comparator; + + uint32_t len = 0; + GUARD_RESULT(s2n_array_num_elements(array, &len)); + + if (len == 0) { + *out = 0; + return S2N_RESULT_OK; + } + + /* Use 64 bit ints to avoid possibility of overflow */ + int64_t low = 0; + int64_t top = len - 1; + + while (low <= top) { + int64_t mid = low + ((top - low) / 2); + void* array_element = NULL; + GUARD_RESULT(s2n_array_get(array, mid, &array_element)); + int m = comparator(array_element, element); + + /* the element is already in the set */ + if (m == 0) { + BAIL(S2N_ERR_SET_DUPLICATE_VALUE); + } + + if (m > 0) { + top = mid - 1; + } else { + low = mid + 1; + } + } + + *out = low; + return S2N_RESULT_OK; +} + +struct s2n_set *s2n_set_new(uint32_t element_size, int (*comparator)(const void*, const void*)) +{ + notnull_check_ptr(comparator); + struct s2n_blob mem = {0}; + GUARD_POSIX_PTR(s2n_alloc(&mem, sizeof(struct s2n_set))); + struct s2n_set *set = (void *) mem.data; + *set = (struct s2n_set) {.data = s2n_array_new(element_size), .comparator = comparator}; + if(set->data == NULL) { + GUARD_POSIX_PTR(s2n_free(&mem)); + return NULL; + } + return set; +} + +S2N_RESULT s2n_set_add(struct s2n_set *set, void *element) +{ + GUARD_RESULT(s2n_set_validate(set)); + + uint32_t index = 0; + GUARD_RESULT(s2n_set_binary_search(set, element, &index)); + GUARD_RESULT(s2n_array_insert_and_copy(set->data, index, element)); + + return S2N_RESULT_OK; +} + +S2N_RESULT s2n_set_get(struct s2n_set *set, uint32_t index, void **element) +{ + GUARD_RESULT(s2n_set_validate(set)); + ENSURE_REF(element); + + GUARD_RESULT(s2n_array_get(set->data, index, element)); + + return S2N_RESULT_OK; +} + +S2N_RESULT s2n_set_remove(struct s2n_set *set, uint32_t index) +{ + GUARD_RESULT(s2n_set_validate(set)); + GUARD_RESULT(s2n_array_remove(set->data, index)); + + return S2N_RESULT_OK; +} + +S2N_RESULT s2n_set_free_p(struct s2n_set **pset) +{ + ENSURE_REF(pset); + struct s2n_set *set = *pset; + + ENSURE_REF(set); + GUARD_RESULT(s2n_array_free(set->data)); + + /* And finally the set object. */ + GUARD_AS_RESULT(s2n_free_object((uint8_t **)pset, sizeof(struct s2n_set))); + + return S2N_RESULT_OK; + +} + +S2N_RESULT s2n_set_free(struct s2n_set *set) +{ + ENSURE_REF(set); + return s2n_set_free_p(&set); +} + + +S2N_RESULT s2n_set_len(struct s2n_set *set, uint32_t *len) +{ + GUARD_RESULT(s2n_set_validate(set)); + + GUARD_RESULT(s2n_array_num_elements(set->data, len)); + + return S2N_RESULT_OK; +} |