-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboolean_set.c
58 lines (42 loc) · 1.15 KB
/
boolean_set.c
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
// Licensed under the MIT License.
#include <stdlib.h>
#include <string.h>
#include "boolean_set.h"
Exception boolean_set(BooleanSet instance, size_t capacity)
{
instance->begin = calloc(capacity, sizeof * instance->begin);
if (!instance->begin)
{
return EXCEPTION_OUT_OF_MEMORY;
}
instance->end = instance->begin + capacity;
return 0;
}
Exception boolean_set_ensure_capacity(BooleanSet instance, size_t capacity)
{
size_t length = instance->end - instance->begin;
if (length >= capacity)
{
return 0;
}
size_t size = capacity * sizeof * instance->begin;
bool* newBegin = realloc(instance->begin, size);
if (!newBegin)
{
return EXCEPTION_OUT_OF_MEMORY;
}
instance->begin = newBegin;
instance->end = instance->begin + capacity;
memset(instance->begin + length, 0, capacity - length);
return 0;
}
void boolean_set_from_array(BooleanSet instance, bool values[], size_t length)
{
instance->begin = values;
instance->end = values + length;
}
void finalize_boolean_set(BooleanSet instance)
{
free(instance->begin);
instance->begin = NULL;
}