Repository navigation
Expand file tree
/
Copy patharray.c
More file actions
164 lines (128 loc) · 3.42 KB
/
Copy patharray.c
File metadata and controls
164 lines (128 loc) · 3.42 KB
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
//
// array.c
//
// Created by Thomas Foster on 9/30/22.
//
#include "array.h"
#include "common.h"
#include <string.h>
void * Push(Array * arr, void * element) {
return Insert(arr, element, arr->count);
}
void * Get(Array * arr, int i) {
if ( i >= arr->count )
return NULL;
return (u8 *)arr->data + arr->esize * i;
}
void Clear(Array * arr) {
arr->count = 0;
}
Array * NewArray(int slots, size_t esize, int resize)
{
ASSERT(slots > 0 || resize != 0);
ASSERT(esize > 0);
Array * arr = malloc(sizeof(*arr));
ASSERT(arr != NULL);
if ( slots != 0 ) {
arr->data = malloc(slots * esize);
}
arr->slots = slots;
arr->count = 0;
arr->esize = esize;
arr->resize = resize;
return arr;
}
void FreeArray(Array * arr) {
ASSERT(arr);
if ( arr->data )
free(arr->data);
free(arr);
}
static void Grow(Array * arr)
{
if ( arr->resize == 0 ) {
return;
}
if ( arr->resize == ARRAY_DOUBLE ) {
arr->slots *= 2;
} else {
arr->slots += arr->resize;
}
arr->data = realloc(arr->data, arr->slots * arr->esize);
ASSERT(arr->data != NULL);
}
void Resize(Array * arr, int slots)
{
ASSERT(slots >= 0);
arr->slots = slots;
if ( slots < arr->count ) {
arr->count = slots; // We've lots some elements.
}
if ( arr->data ) {
if ( slots == 0 ) {
free(arr->data);
} else {
arr->data = realloc(arr->data, slots * arr->esize);
ASSERT(arr->data != NULL);
}
}
}
void * Insert(Array * arr, void * element, int i)
{
ASSERT(i <= arr->count); // Inserting at arr->count will resize the array.
if ( arr->count + 1 > arr->slots ) {
Grow(arr);
}
// In case someone tried to add to a full static array.
ASSERT(arr->count + 1 <= arr->slots);
// Move the latter part of the array right.
memmove((u8 *)arr->data + arr->esize * (i + 1),
(u8 *)arr->data + arr->esize * i,
arr->esize * (arr->count - i));
// Insert element at i.
memmove((u8 *)arr->data + arr->esize * i, element, arr->esize);
++arr->count;
return (u8 *)arr->data + (arr->count - 1) * arr->esize;
}
void Remove(Array * arr, int i)
{
ASSERT((unsigned)i < (unsigned)arr->count);
// move the latter part of the array left
memmove((u8 *)arr->data + arr->esize * i,
(u8 *)arr->data + arr->esize * (i + 1),
arr->esize * (arr->count - i - 1));
--arr->count;
}
void FastRemove(Array * arr, int i)
{
ASSERT((unsigned)i < (unsigned)arr->count)
// move the last element to i
memmove((u8 *)arr->data + arr->esize * i,
(u8 *)arr->data + arr->esize * (arr->count - 1),
arr->esize);
--arr->count;
}
void Replace(Array * arr, void * element, int i)
{
ASSERT((unsigned)i < (unsigned)arr->count);
memmove((u8 *)arr->data + arr->esize * i, element, arr->esize);
}
void Pop(Array * arr, void * out)
{
ASSERT(arr->count > 0);
memcpy(out, Peek(arr), arr->esize);
arr->count--;
}
void * Peek(Array * arr)
{
ASSERT(arr->count > 0);
return (u8 *)arr->data + (arr->count - 1) * arr->esize;
}
Array * DeepCopy(Array * arr)
{
Array * copy = NewArray(arr->count, arr->esize, arr->resize);
memcpy(copy->data, arr->data, arr->count * arr->esize);
// Manually set `count` - only Insert/Push etc. set it!
copy->count = arr->count;
return copy;
}