~donmcc/ood

ref: 1182412fd4ece0cb55c4b3ebefee78b22c8c15bd ood/src/libood/ood/ptr_array.c -rw-r--r-- 3.0 KiB
1182412fDon McCaughey Add `ood_halt()` function. 7 months ago
                                                                                
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
#include "ptr_array.h"

#include <memory.h>
#include <stddef.h>
#include <stdlib.h>


static bool
is_valid_index(struct ood_ptr_array const *array, int i)
{
    return i >= 0 && i < array->capacity;
}


static void
skip_empty_indices(struct ood_ptr_array const *array, int *i)
{
    while (*i < array->capacity && !array->items[*i]) ++*i;
}


static void
skip_empty_indices_reverse(struct ood_ptr_array const *array, int *i)
{
    while (*i >= 0 && !array->items[*i]) --*i;
}


struct ood_ptr_array *
ood_ptr_array_alloc(int capacity)
{
    struct ood_ptr_array *array = calloc(1, sizeof(struct ood_ptr_array));
    if (array) {
        array->items = calloc(capacity, sizeof array->items[0]);
        if (!array->items) {
            free(array);
            return NULL;
        }
        array->capacity = capacity;
    }
    return array;
}


void
ood_ptr_array_free(struct ood_ptr_array *array)
{
    if (array) {
        free(array->items);
        free(array);
    }
}


bool
ood_ptr_array_has_capacity(struct ood_ptr_array *array)
{
    return array->count < array->capacity;
}


enum ood_result
ood_ptr_array_add(struct ood_ptr_array *array, void *item)
{
    for (int i = 0; i < array->capacity; ++i) {
        if (!array->items[i]) {
            array->items[i] = item;
            ++array->count;
            return ood_okay;
        }
    }
    return ood_error;
}


enum ood_result
ood_ptr_array_add_realloc(struct ood_ptr_array *array, void *item)
{
    if (array->count == array->capacity) {
        int new_capacity = (array->capacity < 16) ? 16 : (array->capacity / 2 * 3);
        size_t new_size = new_capacity * sizeof array->items[0];

        void **new_items = realloc(array->items, new_size);
        if (!new_items) return ood_error;

        void **uninitialized = new_items + array->capacity;
        size_t old_size = array->capacity * sizeof array->items[0];
        size_t uninitialized_size = new_size - old_size;
        memset(uninitialized, 0, uninitialized_size);

        array->capacity = new_capacity;
        array->items = new_items;
    }
    return ood_ptr_array_add(array, item);
}


enum ood_result
ood_ptr_array_clear(struct ood_ptr_array *array, void *item)
{
    for (int i = 0; i < array->capacity; ++i) {
        if (item == array->items[i]) {
            array->items[i] = NULL;
            --array->count;
            return ood_okay;
        }
    }
    return ood_error;
}


void *
ood_ptr_array_first(struct ood_ptr_array const *array)
{
    int i = 0;
    return ood_ptr_array_next(array, &i);
}


void *
ood_ptr_array_last(struct ood_ptr_array const *array)
{
    int i = array->capacity - 1;
    return ood_ptr_array_previous(array, &i);
}


void *
ood_ptr_array_next(struct ood_ptr_array const *array, int *i)
{
    skip_empty_indices(array, i);
    return is_valid_index(array, *i) ? array->items[(*i)++] : NULL;
}


void *
ood_ptr_array_previous(struct ood_ptr_array const *array, int *i)
{
    skip_empty_indices_reverse(array, i);
    return is_valid_index(array, *i) ? array->items[(*i)--] : NULL;
}