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 | // Copyright 2018,2023 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
#include "array.h"
#include <assert.h>
#include <stdlib.h>
static array_t array_elements = NULL;
static size_t array_capacity = 0;
static size_t array_count = 0;
array_t array_create(size_t capacity) {
assert(capacity);
array_capacity = capacity;
array_count = 0;
return array_elements = (void**)malloc(capacity * sizeof(void*));
}
void array_destroy(array_t array) { free(array); }
int array_increase_capacity(array_t array) {
size_t new_capacity = 10 * array_capacity;
array_t new_elements = (void**)realloc(array, new_capacity * sizeof(void*));
if (new_elements == NULL) return -1;
array_capacity = new_capacity;
array_elements = new_elements;
return 0; // Success
}
int array_decrease_capacity(array_t array) {
size_t new_capacity = array_capacity / 10;
if (new_capacity < 10) return 0;
array_t new_elements = (void**)realloc(array, new_capacity * sizeof(void*));
if (new_elements == NULL) return -1;
array_capacity = new_capacity;
array_elements = new_elements;
return 0; // Success
}
size_t array_get_count(const array_t array) {
(void)array;
return array_count;
}
void* array_get_element(array_t array, size_t index) {
assert(index < array_get_count(array));
return array[index];
}
int array_append(array_t array, void* element) {
if (array_count == array_capacity) {
if (!array_increase_capacity(array)) {
return -1;
}
}
array[array_count++] = element;
return 0; // Success
}
size_t array_find_first(const array_t array, const void* element,
size_t start_pos) {
for (size_t index = start_pos; index < array_count; ++index) {
if (array[index] == element) {
return index;
}
}
return array_not_found;
}
int array_remove_first(array_t array, const void* element, size_t start_pos) {
size_t index = array_find_first(array, element, start_pos);
if (index == array_not_found) return -1;
for (--array_count; index < array_count; ++index) {
array_elements[index] = array_elements[index + 1];
}
if (array_count == array_capacity / 10) {
array_decrease_capacity(array);
}
return 0; // Removed
}
|