-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
84 lines (73 loc) · 2.17 KB
/
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h> /* exit */
#include <math.h> /* floor, ceil, log2 */
#include "foundation.h"
void
array_extend(Arena* storage, Array* array, int elem_size)
{
assert(elem_size > 0);
assert(array->elem_count >= array->capacity);
int last_segment;
int segment_capacity;
last_segment = floor(log2(array->capacity/16 + 1));
if (last_segment >= array->data.segment_count) {
printf("\nMaximum array capacity has been reached.\n");
exit(1);
}
segment_capacity = 16 * (1 << last_segment);
array->data.segments[last_segment] = arena_malloc(storage, elem_size * segment_capacity);
array->capacity = 16 * ((1 << (last_segment + 1)) - 1);
}
Array*
array_create(Arena* storage, int elem_size, int segment_count)
{
assert(elem_size > 0);
assert(segment_count >= 1 && segment_count <= 16);
Array* array;
array = arena_malloc(storage, sizeof(Array) + sizeof(void*) * segment_count);
array_init(storage, array, elem_size, segment_count);
return array;
}
void
array_init(Arena* storage, Array* array, int elem_size, int segment_count)
{
assert(elem_size > 0);
assert(segment_count >= 1);
array->elem_count = 0;
array->capacity = 16;
array->data.segment_count = segment_count;
array->data.segments[0] = arena_malloc(storage, 16 * elem_size);
}
void*
segment_locate_cell(SegmentTable* data, int i, int elem_size)
{
assert(elem_size > 0);
int segment_index, elem_offset;
void* elem_slot;
segment_index = floor(log2(i/16 + 1));
elem_offset = i - 16 * ((1 << segment_index) - 1);
elem_slot = data->segments[segment_index] + elem_offset * elem_size;
return elem_slot;
}
void*
array_get(Array* array, int i, int elem_size)
{
assert(elem_size > 0);
assert(i >= 0 && i < array->elem_count);
void* elem_slot;
elem_slot = segment_locate_cell(&array->data, i, elem_size);
return elem_slot;
}
void*
array_append(Arena* storage, Array* array, int elem_size)
{
assert(elem_size > 0);
void* elem_slot;
if (array->elem_count >= array->capacity) {
array_extend(storage, array, elem_size);
}
elem_slot = segment_locate_cell(&array->data, array->elem_count, elem_size);
array->elem_count += 1;
return elem_slot;
}