1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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;
}
|