-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxchunk.c
210 lines (189 loc) · 5.42 KB
/
axchunk.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// Created by easy on 16.06.24.
//
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "axchunk.h"
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
static void *(*malloc_)(size_t) = malloc;
static void *(*realloc_)(void *, size_t) = realloc;
static void (*free_)(void *) = free;
/**
* Same as axc_index, but without bounds checking.
*/
static inline void *axc__index__(axchunk *c, uint64_t i) {
return (char *) c->chunks + i * c->width;
}
void axc_memoryfn(void *(*malloc_fn)(size_t), void *(*realloc_fn)(void *, size_t), void (*free_fn)(void *)) {
malloc_ = malloc_fn ? malloc_fn : malloc;
realloc_ = realloc_fn ? realloc_fn : realloc;
free_ = free_fn ? free_fn : free;
}
axchunk *axc_new(uint64_t width) {
return axc_newSized(width, 7);
}
axchunk *axc_newSized(uint64_t width, uint64_t size) {
size += !size;
width += !width;
axchunk *c = malloc_(sizeof *c);
if (c)
c->chunks = malloc_(size * width);
if (!c || !c->chunks) {
free_(c);
return NULL;
}
c->len = 0;
c->cap = size;
c->width = width;
c->destroy = NULL;
c->resizeEventHandler = NULL;
c->context = NULL;
return c;
}
void *axc_destroy(axchunk *c) {
if (c->destroy) {
for (char *chunk = c->chunks; c->len; --c->len) {
c->destroy(chunk);
chunk += c->width;
}
}
void *context = c->context;
free_(c->chunks);
free_(c);
return context;
}
void *axc_destroySoft(axchunk *c) {
void *chunks = c->chunks;
free_(c);
return chunks;
}
bool axc_resize(axchunk *c, uint64_t size) {
size += !size;
if (size == c->cap)
return false;
intptr_t oldChunks = (intptr_t) c->chunks;
void *chunks = realloc_(c->chunks, size * c->width);
if (!chunks)
return true;
ptrdiff_t offset = (intptr_t) chunks - oldChunks;
c->chunks = chunks;
c->cap = size;
if (c->resizeEventHandler)
c->resizeEventHandler(c, offset);
return false;
}
axchunk *axc_swap(axchunk *c, uint64_t i1, uint64_t i2) {
if (i1 == i2 || i1 >= c->len || i2 >= c->len)
return c;
enum {BUFSIZE = 16};
char buf[BUFSIZE];
char *chunk1 = axc__index__(c, i1);
char *chunk2 = axc__index__(c, i2);
uint64_t k = c->width;
while (k >= BUFSIZE) {
memcpy(buf, chunk1, BUFSIZE);
memcpy(chunk1, chunk2, BUFSIZE);
memcpy(chunk2, buf, BUFSIZE);
chunk1 += BUFSIZE;
chunk2 += BUFSIZE;
k -= BUFSIZE;
}
if (k) {
axc__quick_memcpy__(buf, chunk1, k);
axc__quick_memcpy__(chunk1, chunk2, k);
axc__quick_memcpy__(chunk2, buf, k);
}
return c;
}
axchunk *axc_foreach(axchunk *c, bool (*f)(void *, void *), void *arg) {
char *chunk = c->chunks;
for (uint64_t i = 0; i < c->len; ++i) {
if (!f(chunk, arg))
return c;
chunk += c->width;
}
return c;
}
axchunk *axc_filter(axchunk *c, bool (*f)(const void *, void *), void *arg) {
const bool shouldDestroy = c->destroy;
char *chunk = c->chunks;
char *filterChunk = c->chunks;
for (uint64_t i = 0; i < c->len; ++i) {
if (f(chunk, arg)) {
if (chunk != filterChunk)
axc__quick_memcpy__(filterChunk, chunk, c->width);
filterChunk += c->width;
} else if (shouldDestroy) {
c->destroy(chunk);
}
chunk += c->width;
}
c->len -= (chunk - filterChunk) / c->width;
return c;
}
axchunk *axc_clear(axchunk *c) {
if (c->destroy) {
for (char *chunk = c->chunks; c->len; --c->len) {
c->destroy(chunk);
chunk += c->width;
}
} else {
c->len = 0;
}
return c;
}
axchunk *axc_discard(axchunk *c, uint64_t n) {
n = c->len - MIN(c->len, n);
if (c->destroy) {
for (char *chunk = axc__index__(c, c->len); c->len > n; --c->len)
c->destroy(chunk -= c->width);
} else {
c->len = n;
}
return c;
}
void *axc_internalCopy(axchunk *c) {
uint64_t size = MAX(c->len * c->width, 1);
void *copy = malloc_(size);
if (!copy)
return NULL;
return memcpy(copy, c->chunks, size);
}
axchunk *axc_copy(axchunk *c) {
axchunk *copy = axc_newSized(c->width, c->cap);
if (!copy)
return NULL;
memcpy(copy->chunks, c->chunks, c->width * c->len);
copy->len = c->len;
return copy;
}
bool axc_write(axchunk *c, uint64_t i, void *chunks, uint64_t chkcount) {
if (i + chkcount > c->cap) {
uint64_t size1 = (c->cap << 1) | 1;
uint64_t size2 = i + chkcount;
if (axc_resize(c, MAX(size1, size2)))
return true;
}
if (c->destroy) {
char *chunk = axc__index__(c, i);
for (uint64_t k = 0; k < chkcount && k + i < c->len; ++k) {
c->destroy(chunk);
chunk += c->width;
}
}
axc__quick_memmove__(axc__index__(c, i), chunks, chkcount * c->width);
c->len = MAX(i + chkcount, c->len);
return false;
}
uint64_t axc_read(axchunk *c, uint64_t i, void *chunks, uint64_t chkcount) {
if (i >= c->len)
return 0;
if (i + chkcount > c->len)
chkcount -= i + chkcount - c->len;
axc__quick_memmove__(chunks, axc__index__(c, i), chkcount * c->width);
return chkcount;
}