-
Notifications
You must be signed in to change notification settings - Fork 25
/
carray.c
165 lines (134 loc) · 2.77 KB
/
carray.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
#include "carray.h"
#include <stdlib.h>
#include <assert.h>
struct carray
{
int head; /*head index of array*/
int tail; /*tail index of array*/
int size; /*array's size*/
int count; /*count of values*/
void** array; /*array ptr*/
};
static int carray_full(struct carray* a);
struct carray* carray_new(int size)
{
struct carray* a = (struct carray *)malloc(sizeof(struct carray));
assert(a && size > 0);
a->head = 0;
a->tail = 0;
a->size = size;
a->count = 0;
a->array = (void**)malloc(sizeof(void *) * size);
assert(a->array);
return a;
}
static void carray_grow(struct carray* a)
{
int i, count;
struct carray* tmp = carray_new(a->size * 2);
count = carray_count(a);
for(i = 0; i < count; i ++){
carray_push_back(tmp, carray_at(a, i));
}
a->head = 0;
a->tail = tmp->tail;
a->size = tmp->size;
a->array = tmp->array;
free(tmp);
}
void carray_free(struct carray* a)
{
if(a != NULL){
free(a->array);
free(a);
}
}
static int carray_full(struct carray* a)
{
return a->count == a->size;
}
int carray_empty(struct carray* a)
{
return a->count == 0;
}
int carray_size(struct carray* a)
{
return a->size;
}
int carray_push_back(struct carray* a, void* p)
{
if(carray_full(a))
carray_grow(a);
a->array[a->tail] = p;
a->tail = (a->tail + 1) % a->size;
a->count ++;
return 0;
}
int carray_push_front(struct carray* a, void* p)
{
if(carray_full(a))
carray_grow(a);
if(carray_empty(a))
return carray_push_back(a, p);
if (a->head - 1 >= 0)
a->head--;
else
a->head = a->size - 1;
a->array[a->head] = p;
a->count++;
return 0;
}
void* carray_front(struct carray* a)
{
if (carray_empty(a))
return NULL;
return a->array[a->head];
}
void* carray_pop_front(struct carray* a)
{
void* p;
if (carray_empty(a))
return NULL;
p = a->array[a->head];
a->head = (a->head + 1) % a->size;
a->count--;
return p;
}
int carray_count(struct carray* a)
{
return a->count;
}
void* carray_at(struct carray* a, int i)
{
if(carray_empty(a))
return NULL;
return a->array[(a->head + i) % a->size];
}
int carray_count_match(struct carray* a, match_fn_t match_fn, void* arg)
{
int i, count = 0;
for (i = 0; i < carray_count(a); i++)
if (match_fn(arg, carray_at(a, i)))
count++;
return count;
}
static struct carray* _carray_map(struct carray* a, match_fn_t match_fn, void* arg, int match)
{
int i;
void* p;
struct carray* nw = carray_new(a->size);
for (i = 0; i < carray_count(a); i++) {
p = carray_at(a, i);
if (match_fn(arg, p) == match)
carray_push_back(nw, p);
}
return nw;
}
struct carray* carray_collect(struct carray* a, match_fn_t match_fn, void* arg)
{
return _carray_map(a, match_fn, arg, 1);
}
struct carray* carray_reject(struct carray* a, match_fn_t match_fn, void* arg)
{
return _carray_map(a, match_fn, arg, 0);
}