-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathooarray.c
More file actions
249 lines (192 loc) · 4.71 KB
/
Copy pathooarray.c
File metadata and controls
249 lines (192 loc) · 4.71 KB
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
/**
* Copyright (c) 2011 by Dmitri Dmitriev
* All rights reserved.
*
* This file is part of the OOmnik Conceptual Processor,
* and as such it is subject to the license stated
* in the LICENSE file which you have received
* as part of this distribution.
*
* Project homepage:
* <http://www.oomnik.ru>
*
* Initial author and maintainer:
* Dmitri Dmitriev aka M0nsteR <dmitri@globbie.net>
*
* Code contributors:
* * Jenia Krylov <info@e-krylov.ru>
*
* ---------
* ooarray.c
* OOmnik Dynamic Array implementation
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ooarray.h"
static oo_compar_func local_function;
int compare_array_items(const void *a1,
const void *a2)
{
const void **item_1, **item_2;
item_1 = (const void**)a1;
item_2 = (const void**)a2;
return local_function(*item_1, *item_2);
}
static int
ooArray_del(struct ooArray *self)
{
self->clear(self);
free(self);
return oo_OK;
}
static int
ooArray_set_item(struct ooArray *self,
void *data,
size_t pos)
{
if (pos >= self->size)
return oo_FAIL;
self->data[pos] = data;
return oo_OK;
}
static
void* ooArray_get_item(struct ooArray *self,
size_t pos)
{
if (pos >= self->size)
return NULL;
return self->data[pos];
}
static int
ooArray_clear(struct ooArray *self)
{
free(self->data);
self->data = NULL;
self->size = 0;
return oo_OK;
}
static int
ooArray_add(struct ooArray *self,
void *val,
size_t pos)
{
unsigned int i;
void **data;
if (pos >= self->size)
return oo_FAIL;
data = (void**)realloc(self->data,
sizeof(void*) * (self->size + 1));
if (!data) return oo_NOMEM;
for (i = pos + 1; i < self->size + 1; i++)
data[i] = data[i - 1];
data[pos] = val;
self->data = data;
self->size++;
return oo_OK;
}
static int
ooArray_push(struct ooArray *self,
void *val)
{
return ooArray_add(self, val, self->size);
}
static struct ooArray*
ooArray_get_subsequence(struct ooArray *self,
size_t start,
size_t length)
{
int ret;
struct ooArray *res;
ret = ooArray_new(&res);
if (ret != oo_OK) return NULL;
if (start > length)
return res;
if (start + length > self->size)
length = self->size - start;
res->data = (void**)malloc(sizeof(void*) * length);
memcpy(res->data, self->data + start, sizeof(void*) * length);
res->size = length;
return res;
}
static int
ooArray_remove(struct ooArray *self,
size_t pos)
{
unsigned int i;
void *res;
if (pos >= self->size)
return oo_FAIL;
self->size--;
res = self->data[pos];
for (i = pos; i < self->size; ++i)
self->data[i] = self->data[i + 1];
self->data = (void**)realloc(self->data, sizeof(void*) * self->size);
return oo_OK;
}
static int
ooArray_pop(struct ooArray *self)
{
return ooArray_remove(self, self->size - 1);
}
static int
ooArray_resize(struct ooArray *self,
size_t new_size)
{
void **data;
unsigned int size = self->size;
self->size = new_size;
data = (void**)realloc(self->data, self->size * sizeof(void*));
if (!data) return oo_NOMEM;
self->data = data;
if (size < new_size)
memset(self->data + size, 0, (new_size - size) * sizeof(void*));
return oo_OK;
}
static void
ooArray_sort(struct ooArray *self,
oo_compar_func function)
{
local_function = function;
qsort(self->data, self->size, sizeof(void*), compare_array_items);
}
static void*
ooArray_find(struct ooArray *self,
void *data,
oo_compar_func function)
{
size_t i;
for (i = 0; i < self->size; ++i)
if (function(data, self->data[i]) == 0)
return self->data[i];
return NULL;
}
static int
ooArray_init(struct ooArray *self)
{
self->del = ooArray_del;
self->add = ooArray_add;
self->push = ooArray_push;
self->remove = ooArray_remove;
self->pop = ooArray_pop;
self->set_item = ooArray_set_item;
self->get_item = ooArray_get_item;
self->clear = ooArray_clear;
self->get_subsequence = ooArray_get_subsequence;
self->resize = ooArray_resize;
self->sort = ooArray_sort;
self->find = ooArray_find;
self->data = NULL;
self->size = 0;
return oo_OK;
}
/* constructor */
extern int
ooArray_new(struct ooArray **array)
{
struct ooArray *self = malloc(sizeof(struct ooArray));
if (!self) return oo_NOMEM;
ooArray_init(self);
*array = self;
return oo_OK;
}