aboutsummaryrefslogtreecommitdiffstats
path: root/moon-abe/pbc-0.5.14/misc/darray.c
blob: 50b127f3a500c2d3ca8534358ee1ab9759c1bbad (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "pbc_memory.h"
#include "darray.h"

#define NDEBUG
#include <assert.h>

enum {
    max_init = 8
};

void darray_init(darray_ptr a)
{
    a->max = max_init;
    a->count = 0;
    a->item = pbc_malloc(sizeof(void *) * a->max);
}

darray_ptr darray_new(void)
{
    darray_ptr res = pbc_malloc(sizeof(darray_t));
    darray_init(res);
    return res;
}

void darray_remove_all(darray_ptr a)
{
    a->max = max_init;
    a->count = 0;
    pbc_free(a->item);
    a->item = pbc_malloc(sizeof(void *) * a->max);
}

void darray_remove_last(darray_ptr a)
{
    assert(a->count > 0);
    a->count--;
}

static void darray_realloc(darray_ptr a, int size)
{
    a->max = size;
    a->item = pbc_realloc(a->item, sizeof(void *) * a->max);
}

void darray_append(darray_ptr a, void *p)
{
    if (a->count == a->max) {
        if (!a->max) a->max = max_init;
        else a->max *= 2;
        a->item = pbc_realloc(a->item, sizeof(void *) * a->max);
    }
    a->item[a->count] = p;
    a->count++;
}

int darray_index_of(darray_ptr a, void *p)
{
    int i;
    for (i=0; i<a->count; i++) {
        if (a->item[i] == p) return i;
    }
    return -1;
}

void darray_clear(darray_t a)
{
    pbc_free(a->item);
    a->max = 0;
    a->count = 0;
}

void darray_free(darray_ptr a) {
  darray_clear(a);
  pbc_free(a);
}

void darray_remove_index(darray_ptr a, int n)
{
    assert(a->count >= n-1);
    a->count--;
    memmove(&a->item[n], &a->item[n+1], sizeof(void *) * (a->count - n));
}

void darray_remove(darray_ptr a, void *p)
{
    int i;
    for (i=0; i<a->count; i++) {
        if (a->item[i] == p) {
            a->count--;
            memmove(&a->item[i], &a->item[i+1], sizeof(void *) * (a->count - i));
            return;
        }
    }
    assert(0);
}

void darray_remove_with_test(darray_ptr a, int (*test)(void *))
{
    int i;
    for (i=0; i<a->count; i++) {
        if (test(a->item[i])) {
            for (;i<a->count; i++) {
                a->item[i] = a->item[i+1];
            }
            a->count--;
        }
    }
}

void darray_copy(darray_ptr dst, darray_ptr src)
{
    darray_realloc(dst, src->count);
    memcpy(dst->item, src->item, src->count * sizeof(void *));
    dst->count = src->count;
}

void darray_forall(darray_t a, void (*func)(void *))
{
    int i, n = a->count;
    for (i=0; i<n; i++) {
        func(a->item[i]);
    }
}

void darray_forall2(darray_t a,
                    void (*func)(void *darray_item, void *scope_ptr),
                    void *scope_ptr)
{
    int i, n = a->count;
    for (i=0; i<n; i++) {
        func(a->item[i], scope_ptr);
    }
}

void darray_forall3(darray_t a,
                    void (*func)(void *darray_item,
                                 void *scope_ptr1,
                                 void *scope_ptr2),
                    void *scope_ptr1,
                    void *scope_ptr2)
{
    int i, n = a->count;
    for (i=0; i<n; i++) {
        func(a->item[i], scope_ptr1, scope_ptr2);
    }
}

void darray_forall4(darray_t a,
                    void (*func)(void *darray_item,
                                 void *scope_ptr1,
                                 void *scope_ptr2,
                                 void *scope_ptr3),
                    void *scope_ptr1,
                    void *scope_ptr2,
                    void *scope_ptr3)
{
    int i, n = a->count;
    for (i=0; i<n; i++) {
        func(a->item[i], scope_ptr1, scope_ptr2, scope_ptr3);
    }
}

void *darray_at_test(darray_ptr a,
                     int (*test)(void *data, void *scope_ptr),
                     void *scope_ptr)
{
    int i;
    for (i = 0; i < a->count; i++) {
        void *p = a->item[i];
        if (test(p, scope_ptr)) return p;
    }
    return NULL;
}