excit.c 6.91 KB
Newer Older
1
#include <stdlib.h>
2
#include "excit.h"
3
#include "dev/excit.h"
4
#include "composition.h"
5
6
#include "prod.h"
#include "cons.h"
7
#include "repeat.h"
8
#include "hilbert2d.h"
9
#include "range.h"
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
10
#include "index.h"
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
11
#include "tleaf.h"
Brice Videau's avatar
Brice Videau committed
12

Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
13
#define CASE(val) case val: return #val; break
14

15
const char *excit_type_name(enum excit_type_e type)
16
{
17
	switch (type) {
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
18
		CASE(EXCIT_INDEX);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
19
20
21
22
23
		CASE(EXCIT_RANGE);
		CASE(EXCIT_CONS);
		CASE(EXCIT_REPEAT);
		CASE(EXCIT_HILBERT2D);
		CASE(EXCIT_PRODUCT);
24
		CASE(EXCIT_COMPOSITION);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
25
26
		CASE(EXCIT_USER);
		CASE(EXCIT_TYPE_MAX);
27
28
29
30
31
	default:
		return NULL;
	}
}

32
const char *excit_error_name(enum excit_error_e err)
33
{
34
	switch (err) {
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
35
36
37
38
39
40
41
		CASE(EXCIT_SUCCESS);
		CASE(EXCIT_STOPIT);
		CASE(EXCIT_ENOMEM);
		CASE(EXCIT_EINVAL);
		CASE(EXCIT_EDOM);
		CASE(EXCIT_ENOTSUP);
		CASE(EXCIT_ERROR_MAX);
42
43
44
45
46
47
48
	default:
		return NULL;
	}
}

#undef CASE

49
50
int excit_set_dimension(excit_t it, ssize_t dimension)
{
51
52
53
54
55
56
	if (!it)
		return -EXCIT_EINVAL;
	if (it->type != EXCIT_USER)
		return -EXCIT_ENOTSUP;
	it->dimension = dimension;
	return EXCIT_SUCCESS;
57
58
}

59
int excit_get_data(excit_t it, void **data)
60
{
61
62
63
64
65
66
	if (!it)
		return -EXCIT_EINVAL;
	if (it->type != EXCIT_USER)
		return -EXCIT_ENOTSUP;
	*data = it->data;
	return EXCIT_SUCCESS;
67
68
}

69
int excit_set_func_table(excit_t it, struct excit_func_table_s *func_table)
70
{
71
72
73
74
	if (!it)
		return -EXCIT_EINVAL;
	it->func_table = func_table;
	return EXCIT_SUCCESS;
75
76
}

77
int excit_get_func_table(excit_t it, struct excit_func_table_s **func_table)
78
{
79
80
81
82
	if (!it)
		return -EXCIT_EINVAL;
	*func_table = it->func_table;
	return EXCIT_SUCCESS;
83
84
}

85
86
/*--------------------------------------------------------------------*/

Brice Videau's avatar
Brice Videau committed
87
88
89
90
91
92
93
94
95
96
97
#define ALLOC_EXCIT(op) { \
	it = malloc(sizeof(struct excit_s) + sizeof(struct op## _it_s)); \
	if (!it) \
		return NULL; \
	it->data = (void *)((char *)it + sizeof(struct excit_s)); \
	if (!excit_ ##op## _func_table.alloc) \
		goto error; \
	it->func_table = &excit_ ##op## _func_table; \
	it->dimension = 0; \
	if (excit_ ##op## _func_table.alloc(it)) \
		goto error; \
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
98
}
99

Brice Videau's avatar
Brice Videau committed
100
excit_t excit_alloc(enum excit_type_e type)
101
{
102
103
104
	excit_t it = NULL;

	switch (type) {
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
105
106
107
	case EXCIT_INDEX:
		ALLOC_EXCIT(index);
		break;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	case EXCIT_RANGE:
		ALLOC_EXCIT(range);
		break;
	case EXCIT_CONS:
		ALLOC_EXCIT(cons);
		break;
	case EXCIT_REPEAT:
		ALLOC_EXCIT(repeat);
		break;
	case EXCIT_HILBERT2D:
		ALLOC_EXCIT(hilbert2d);
		break;
	case EXCIT_PRODUCT:
		ALLOC_EXCIT(prod);
		break;
123
124
	case EXCIT_COMPOSITION:
		ALLOC_EXCIT(composition);
125
126
127
128
129
130
131
132
133
		break;
	case EXCIT_TLEAF:
		ALLOC_EXCIT(tleaf);
		break;
	default:
		goto error;
	}
	it->type = type;
	return it;
134
error:
135
136
	free(it);
	return NULL;
137
138
}

139
140
excit_t excit_alloc_user(struct excit_func_table_s *func_table,
			 size_t data_size)
141
{
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	excit_t it;

	if (!func_table || !data_size)
		return NULL;
	it = malloc(sizeof(struct excit_s) + data_size);
	if (!it)
		return NULL;
	it->data = (void *)((char *)it + sizeof(struct excit_s));
	if (!func_table->alloc)
		goto error;
	it->func_table = func_table;
	it->dimension = 0;
	it->type = EXCIT_USER;
	if (func_table->alloc(it))
		goto error;
	return it;
158
error:
159
160
	free(it);
	return NULL;
161
162
}

163
excit_t excit_dup(excit_t it)
164
{
Brice Videau's avatar
Brice Videau committed
165
	excit_t result = NULL;
166

167
	if (!it || !it->data || !it->func_table || !it->func_table->copy)
168
		return NULL;
169
	result = excit_alloc(it->type);
170
171
	if (!result)
		return NULL;
172
	result->dimension = it->dimension;
173
	if (it->func_table->copy(result, it))
174
175
176
		goto error;
	return result;
error:
177
178
	excit_free(result);
	return NULL;
179
180
}

181
void excit_free(excit_t it)
182
{
183
184
185
186
187
188
	if (!it)
		return;
	if (!it->func_table)
		goto error;
	if (it->func_table->free)
		it->func_table->free(it);
189
error:
190
	free(it);
191
192
}

193
int excit_dimension(excit_t it, ssize_t *dimension)
194
{
195
196
197
198
	if (!it || !dimension)
		return -EXCIT_EINVAL;
	*dimension = it->dimension;
	return EXCIT_SUCCESS;
199
200
}

201
int excit_type(excit_t it, enum excit_type_e *type)
202
{
203
204
205
206
207
208
	if (!it || !type)
		return -EXCIT_EINVAL;
	*type = it->type;
	return EXCIT_SUCCESS;
}

209
int excit_next(excit_t it, ssize_t *indexes)
210
{
211
212
213
214
215
	if (!it || !it->func_table)
		return -EXCIT_EINVAL;
	if (!it->func_table->next)
		return -EXCIT_ENOTSUP;
	return it->func_table->next(it, indexes);
216
217
}

218
int excit_peek(const excit_t it, ssize_t *indexes)
219
{
220
221
222
223
224
	if (!it || !it->func_table)
		return -EXCIT_EINVAL;
	if (!it->func_table->peek)
		return -EXCIT_ENOTSUP;
	return it->func_table->peek(it, indexes);
225
226
}

227
int excit_size(const excit_t it, ssize_t *size)
228
{
229
230
231
232
233
	if (!it || !it->func_table || !size)
		return -EXCIT_EINVAL;
	if (!it->func_table->size)
		return -EXCIT_ENOTSUP;
	return it->func_table->size(it, size);
234
235
}

236
int excit_rewind(excit_t it)
237
{
238
239
240
241
242
	if (!it || !it->func_table)
		return -EXCIT_EINVAL;
	if (!it->func_table->rewind)
		return -EXCIT_ENOTSUP;
	return it->func_table->rewind(it);
243
244
}

245
int excit_split(const excit_t it, ssize_t n, excit_t *results)
246
{
247
	if (!it || !it->func_table)
Brice Videau's avatar
Brice Videau committed
248
		return -EXCIT_EINVAL;
249
	if (n <= 0)
Brice Videau's avatar
Brice Videau committed
250
		return -EXCIT_EDOM;
Brice Videau's avatar
Brice Videau committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
	if (!it->func_table->split) {
		ssize_t size;
		int err = excit_size(it, &size);

		if (err)
			return err;
		if (size < n)
			return -EXCIT_EDOM;
		if (!results)
			return EXCIT_SUCCESS;
		excit_t range = excit_alloc(EXCIT_RANGE);

		if (!range)
			return -EXCIT_ENOMEM;
		err = excit_range_init(range, 0, size - 1, 1);
		if (err)
			goto error1;
		err = excit_split(range, n, results);
		if (err)
			goto error1;
		for (int i = 0; i < n; i++) {
			excit_t tmp, tmp2;

			tmp = excit_dup(it);
			if (!tmp)
				goto error2;
			tmp2 = results[i];
278
			results[i] = excit_alloc(EXCIT_COMPOSITION);
Brice Videau's avatar
Brice Videau committed
279
280
281
282
			if (!results[i]) {
				excit_free(tmp2);
				goto error2;
			}
283
			err = excit_composition_init(results[i], tmp, tmp2);
Brice Videau's avatar
Brice Videau committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
			if (err) {
				excit_free(tmp2);
				goto error2;
			}
		}
		excit_free(range);
		return EXCIT_SUCCESS;
error2:
		for (int i = 0; i < n; i++)
			excit_free(results[i]);
error1:
		excit_free(range);
		return err;
	} else
		return it->func_table->split(it, n, results);
299
300
}

301
int excit_nth(const excit_t it, ssize_t n, ssize_t *indexes)
302
{
303
304
305
306
307
	if (!it || !it->func_table)
		return -EXCIT_EINVAL;
	if (!it->func_table->nth)
		return -EXCIT_ENOTSUP;
	return it->func_table->nth(it, n, indexes);
308
309
}

Brice Videau's avatar
Brice Videau committed
310
int excit_rank(const excit_t it, const ssize_t *indexes, ssize_t *n)
311
{
312
313
314
315
316
	if (!it || !it->func_table || !indexes)
		return -EXCIT_EINVAL;
	if (!it->func_table->rank)
		return -EXCIT_ENOTSUP;
	return it->func_table->rank(it, indexes, n);
317
318
}

319
int excit_pos(const excit_t it, ssize_t *n)
320
{
321
322
323
324
325
	if (!it || !it->func_table)
		return -EXCIT_EINVAL;
	if (!it->func_table->pos)
		return -EXCIT_ENOTSUP;
	return it->func_table->pos(it, n);
326
327
}

328
int excit_cyclic_next(excit_t it, ssize_t *indexes, int *looped)
329
{
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
	int err;

	if (!it)
		return -EXCIT_EINVAL;
	*looped = 0;
	err = excit_next(it, indexes);
	switch (err) {
	case EXCIT_SUCCESS:
		break;
	case EXCIT_STOPIT:
		err = excit_rewind(it);
		if (err)
			return err;
		*looped = 1;
		err = excit_next(it, indexes);
		if (err)
			return err;
		break;
	default:
		return err;
	}
	if (excit_peek(it, NULL) == EXCIT_STOPIT) {
		err = excit_rewind(it);
		if (err)
			return err;
		*looped = 1;
	}
	return EXCIT_SUCCESS;
358
359
}

360
int excit_skip(excit_t it)
361
{
362
	return excit_next(it, NULL);
363
}
364