/
rcp_array.c
345 lines (298 loc) · 9.05 KB
/
rcp_array.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "rcp_type_pch.h"
#include "rcp_utility.h"
#include "rcp_type_list.h"
#include "rcp_type_utility.h"
#include "rcp_record.h"
#include "rcp_dict.h"
#define RCP_INTERNAL_STRUCTURE
#include "rcp_type.h"
#include "rcp_array.h"
rcp_type_ref rcp_array_type_data_type(rcp_type_ref type)
{
return ((struct rcp_type_array_ext*)(type+1))->data_type;
}
rcp_array_ref rcp_array_new(rcp_type_ref array_type)
{
return (rcp_array_ref)rcp_new(array_type);
}
rcp_extern void rcp_array_delete(
rcp_type_ref array_type, rcp_array_ref array)
{
rcp_delete(array_type, (rcp_data_ref)array);
}
//arraying type
void rcp_array_init(rcp_type_ref type, rcp_data_ref data)
{
rcp_array_ref core = (rcp_array_ref)data;
core->array = NULL;
core->data_count = 0;
core->capacity = 0;
}
void rcp_array_deinit(rcp_type_ref type, rcp_data_ref data)
{
rcp_array_ref core = (rcp_array_ref)data;
if (!rcp_array_owning_data(core))
return;
rcp_array_clear_data(type, core);
free(core->array);
}
void rcp_array_copy(
rcp_type_ref type, rcp_data_ref src, rcp_data_ref dst)
{
rcp_type_ref data_type = rcp_array_type_data_type(type);
rcp_array_ref src_core = (rcp_array_ref)src;
rcp_array_ref dst_core = (rcp_array_ref)dst;
rcp_init(type, dst);//not required
dst_core->array = malloc(type->size*src_core->data_count);
dst_core->data_count = src_core->data_count;
dst_core->capacity = src_core->capacity;
int i;
for (i = 0; i<src_core->data_count; i++){
size_t offset = i*data_type->size;
rcp_data_ref src_data = src_core->array+offset;
rcp_data_ref dst_data = dst_core->array+offset;
rcp_copy(data_type, src_data, dst_data);
}
}
void rcp_array_copied(rcp_type_ref type, rcp_data_ref data)
{
rcp_type_ref data_type = rcp_array_type_data_type(type);
rcp_array_ref core = (rcp_array_ref)data;
if (!rcp_array_owning_data(core))
return;
const void *old_data = core->array;
const size_t old_data_count = core->data_count;
rcp_init(type, data);
rcp_array_resize(type, core, old_data_count);
core->data_count = old_data_count;
memcpy(core->array, old_data, old_data_count*data_type->size);
int i;
for (i = 0; i<old_data_count; i++){
rcp_copied(data_type, core->array+i*data_type->size);
}
}
void rcp_array_set(rcp_type_ref type, rcp_data_ref data,
rcp_type_ref key_type, rcp_data_ref key_data,
rcp_type_ref data_type, rcp_data_ref data_data)
{
rcp_array_ref array = (rcp_array_ref)data;
if (rcp_array_type_data_type(type)!= data_type)
return;
if (key_type == rcp_ref_type){
rcp_record_ref key_rec = *(rcp_record_ref*)key_data;
key_type = rcp_record_type(key_rec);
key_data = rcp_record_data(key_rec);
}
if (!rcp_type_is_uint(key_type))
return;
uint64_t idx = rcp_uint_as_uint(key_type, key_data);
rcp_data_ref out = rcp_array_data_at(type, array, idx);
if (!out)
return;
rcp_deinit(data_type, out);
rcp_copy(data_type, data_data, out);
}
rcp_extern void rcp_array_append(
rcp_type_ref array_type, rcp_data_ref array,
rcp_type_ref data_type, rcp_data_ref data)
{
if (rcp_array_type_data_type(array_type)!= data_type)
return;
rcp_array_append_data(array_type, (rcp_array_ref)array, data);
}
void rcp_array_at(
rcp_type_ref *io_type, rcp_data_ref *io_data,
rcp_type_ref key_type, rcp_data_ref key_data)
{
rcp_type_ref array_type= *io_type;
rcp_array_ref array= (rcp_array_ref)*io_data;
*io_data = NULL;
*io_type = NULL;
if (! array_type) return;
if (! array) return;
if (rcp_type_is_uint(key_type)){
uint64_t idx = rcp_uint_as_uint(key_type, key_data);
*io_data = rcp_array_data_at(array_type, array, idx);
*io_type = rcp_array_type_data_type(array_type);
return;
}
}
rcp_extern int rcp_array_owning_data(rcp_array_ref array)
{
if (array->array && array->capacity == 0 && array->data_count != 0)
return 0;
return 1;
}
void* rcp_array_raw_data(rcp_array_ref array)
{
rcp_array_ref core = array;
return core->array;
}
size_t rcp_array_len(rcp_array_ref array){
rcp_array_ref core = array;
return core->data_count;
}
int8_t rcp_array_empty(rcp_array_ref array){
if (array->data_count)
return 0;
return 1;
}
rcp_extern int8_t rcp_array_replace(
rcp_type_ref array_type, rcp_data_ref target_array_data,
int32_t range_begin, int32_t range_end,
rcp_data_ref input_array_data)
{
rcp_array_ref target_array = (rcp_array_ref)target_array_data;
rcp_array_ref input_array = (rcp_array_ref)input_array_data;
rcp_type_ref data_type = rcp_array_type_data_type(array_type);
int i;
if (!rcp_array_owning_data(target_array)){
return -1;
}
if (range_begin < 0)
range_begin = target_array->data_count+1+range_begin;
if (range_end < 0)
range_end = target_array->data_count+1+range_end;
if (range_begin>range_end)
return -1;
if (range_end>target_array->data_count)
return -1;
size_t total_data_count= target_array->data_count -
(range_end-range_begin)+input_array->data_count;
rcp_array_resize(array_type, target_array, total_data_count);
for (i = range_begin; i<range_end; i++){
rcp_deinit(data_type, target_array->array+i*data_type->size);
}
//move back part
size_t move_dst_idx = range_begin+input_array->data_count;
int32_t move_count = target_array->data_count - range_end;
memmove(target_array->array+move_dst_idx*data_type->size,
target_array->array+range_end*data_type->size,
move_count*data_type->size);
//insert input
if (input_array->data_count){
memcpy(target_array->array+range_begin*data_type->size,
input_array->array,
input_array->data_count*data_type->size);
int i;
for (i = 0; i<input_array->data_count; i++){
rcp_copied(data_type,
target_array->array+(range_begin+i)*data_type->size);
}
}
target_array->data_count = total_data_count;
return 0;
}
void rcp_array_resize(
rcp_type_ref array_type, rcp_array_ref array, size_t new_size)
{
rcp_type_ref data_type = rcp_array_type_data_type(array_type);
const size_t initial_capacity = 16;
const size_t block_size = 4096;
if (array->capacity >= new_size)
return;
size_t min_capacity_bytes = new_size*data_type->size;
size_t new_data_size;
size_t new_capacity = initial_capacity;
if (min_capacity_bytes <= block_size){
// Fit in a block.
while (new_capacity < new_size){
new_capacity *= 2;
}
new_data_size = new_capacity*data_type->size;
if (new_data_size > block_size){
new_data_size = block_size;
new_capacity = new_data_size/data_type->size;
}
}
else{
// Require 2 or more blocks.
new_data_size = (min_capacity_bytes/block_size+1)*block_size;
new_capacity = new_data_size/data_type->size;
}
array->array = realloc(array->array, new_data_size);
array->capacity = new_capacity;
}
rcp_extern void rcp_array_append_data(
rcp_type_ref array_type, rcp_array_ref array,
rcp_data_ref data)
{
rcp_type_ref data_type = rcp_array_type_data_type(array_type);
if (!rcp_array_owning_data(array)){
//Adding data to array with extra data refarence are not allowed.
return;
}
rcp_array_resize(array_type, array, array->data_count+1);
void* dst = array->array+array->data_count*data_type->size;
rcp_copy(data_type, data, dst);
array->data_count++;
}
void rcp_array_pop_data(
rcp_type_ref array_type, rcp_array_ref array,
rcp_data_ref out)
{
if (rcp_array_empty(array))
return;
rcp_type_ref data_type = rcp_array_type_data_type(array_type);
array->data_count--;
rcp_data_ref back = array->array + data_type->size * array->data_count;
rcp_copy(data_type, back, out);
rcp_deinit(data_type, back);
}
rcp_extern void rcp_array_clear_data(
rcp_type_ref array_type, rcp_array_ref array)
{
if (!rcp_array_owning_data(array))
return;
rcp_type_ref data_type = rcp_array_type_data_type(array_type);
void* end = array->array+array->data_count*data_type->size;
void* ptr = array->array;
for ( ; ptr<end; ptr += data_type->size)
rcp_deinit(data_type, ptr);
array->data_count = 0;
}
rcp_extern rcp_data_ref rcp_array_data_at(
rcp_type_ref array_type, rcp_array_ref array,
uint64_t idx)
{
if (idx>=array->data_count)
return NULL;
return array->array +
rcp_array_type_data_type(array_type)->size*idx;
}
rcp_extern rcp_data_ref rcp_array_last(
rcp_type_ref array_type, rcp_array_ref array)
{
if (!array->data_count)
return NULL;
return array->array +
rcp_array_type_data_type(array_type)->size*(array->data_count-1);
}
rcp_array_ref rcp_record_as_array(rcp_record_ref rec){
return (rcp_array_ref)rcp_record_data(rec);
}
rcp_array_ref rcp_dict_node_as_array(
rcp_type_ref dict_type, rcp_dict_node_ref node){
return (rcp_array_ref)rcp_dict_node_data(dict_type, node);
}
rcp_array_iterater_ref rcp_array_begin(rcp_array_ref array)
{
if (array->data_count)
return array->array;
return NULL;
}
rcp_array_iterater_ref rcp_array_iterater_next(
rcp_type_ref array_type, rcp_array_ref array,
rcp_array_iterater_ref itr)
{
rcp_type_ref data_type = rcp_array_type_data_type(array_type);
void* end = array->array+array->data_count*data_type->size;
void* next = (void*)itr+data_type->size;
if (next<end)
return next;
return NULL;
}
rcp_data_ref rcp_array_iterater_data(rcp_array_iterater_ref itr)
{
return (rcp_data_ref)itr;
}