-
Notifications
You must be signed in to change notification settings - Fork 0
/
arraylist.c
173 lines (145 loc) · 3.97 KB
/
arraylist.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (C) 2022-2023 Nicolai Brand, Callum Gran
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "arraylist.h"
#include "common.h"
/* arraylist implementation */
void arraylist_init(struct arraylist_t *arr, u32 T_size)
{
#ifdef DARR_STARTING_CAP
da->cap = DARR_STARTING_CAP;
#else
arr->cap = 0;
arr->cap = GROW_CAPACITY(arr->cap);
#endif
arr->size = 0;
arr->T_size = T_size;
arr->data = malloc(T_size * arr->cap);
}
void arraylist_free(struct arraylist_t *arr)
{
free(arr->data);
}
static void ensure_capacity(struct arraylist_t *arr, size_t idx)
{
if (idx >= arr->cap) {
/* increase capacity */
arr->cap = GROW_CAPACITY(arr->cap);
arr->data = nicc_internal_realloc(arr->data, arr->T_size * arr->cap);
}
}
static void *get_element(struct arraylist_t *arr, size_t idx)
{
uintptr_t result = (uintptr_t)arr->data + (uintptr_t)(idx * arr->T_size);
return (void *)result;
}
bool arraylist_set(struct arraylist_t *arr, void *val, size_t idx)
{
/* this is a bit conservative maybe, but it's not possible to set a value at a
* position greater than the size as we don't have any mechanisms to keep
* track of this. a bit map or something may be useful, idk.
*/
if (idx > arr->size)
return false;
ensure_capacity(arr, idx);
memcpy(get_element(arr, idx), val, arr->T_size);
/* special case where we actually appended to the arraylist */
if (idx == arr->size)
arr->size++;
return true;
}
bool arraylist_append(struct arraylist_t *arr, void *val)
{
bool success = arraylist_set(arr, val, arr->size);
if (!success)
return false;
return true;
}
void *arraylist_get(struct arraylist_t *arr, size_t idx)
{
if (idx >= arr->size)
return NULL;
return get_element(arr, idx);
}
void arraylist_get_copy(struct arraylist_t *arr, size_t idx, void *return_ptr)
{
void *element = arraylist_get(arr, idx);
if (element == NULL) {
return_ptr = NULL;
return;
}
memcpy(return_ptr, element, arr->T_size);
}
bool arraylist_pop(struct arraylist_t *arr)
{
if (arr->size == 0)
return false;
arr->size--;
return true;
}
bool arraylist_pop_and_copy(struct arraylist_t *arr, void *return_ptr)
{
if (arr->size == 0) {
return_ptr = NULL;
return false;
}
arraylist_get_copy(arr, arr->size - 1, return_ptr);
arr->size--;
return true;
}
size_t arraylist_index_of(struct arraylist_t *arr, void *val, equality_fn_t *eq)
{
if (val == NULL)
return NICC_NOT_FOUND;
for (size_t i = 0; i < arr->size; i++) {
if (eq(get_element(arr, i), val)) {
return i;
}
}
return NICC_NOT_FOUND;
}
bool arraylist_rm(struct arraylist_t *arr, size_t idx)
{
if (idx >= arr->size)
return false;
for (size_t i = idx + 1; i < arr->size; i++)
memcpy(get_element(arr, i - 1), get_element(arr, i), arr->T_size);
arr->size--;
return true;
}
bool arraylist_rmv(struct arraylist_t *arr, void *val, equality_fn_t *eq)
{
if (val == NULL)
return false;
for (size_t i = 0; i < arr->size; i++) {
if (eq(get_element(arr, i), val)) {
return arraylist_rm(arr, i);
}
}
return false;
}
bool arraylist_sort(struct arraylist_t *arr, compare_fn_t *cmp)
{
if (arr->size == 0)
return false;
if (arr->size == 1)
return true;
qsort(arr->data, arr->size, arr->T_size, cmp);
return true;
}