prod.c 7.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*******************************************************************************
 * Copyright 2019 UChicago Argonne, LLC.
 * (c.f. AUTHORS, LICENSE)
 *
 * This file is part of the EXCIT project.
 * For more info, see https://xgitlab.cels.anl.gov/argo/excit
 *
 * SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/
10
#include <string.h>
11
12
13
14
15
16
17
18
19
#include "dev/excit.h"
#include "prod.h"

static int prod_it_alloc(excit_t data)
{
	struct prod_it_s *it = (struct prod_it_s *)data->data;

	it->count = 0;
	it->its = NULL;
20
	it->buff = NULL;
21
22
23
24
25
26
27
28
29
30
31
	return EXCIT_SUCCESS;
}

static void prod_it_free(excit_t data)
{
	struct prod_it_s *it = (struct prod_it_s *)data->data;

	if (it->its) {
		for (ssize_t i = 0; i < it->count; i++)
			excit_free(it->its[i]);
		free(it->its);
32
		free(it->buff);
33
34
35
36
37
38
39
40
41
42
43
	}
}

static int prod_it_copy(excit_t dst, const excit_t src)
{
	const struct prod_it_s *it = (const struct prod_it_s *)src->data;
	struct prod_it_s *result = (struct prod_it_s *)dst->data;

	result->its = (excit_t *) malloc(it->count * sizeof(excit_t));
	if (!result->its)
		return -EXCIT_ENOMEM;
44
45
46
47
48
49
	result->buff = (ssize_t *) malloc(src->dimension * sizeof(ssize_t));
	if (!result->buff){
		free(result->its);
		return -EXCIT_ENOMEM;
	}

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
	ssize_t i;

	for (i = 0; i < it->count; i++) {
		result->its[i] = excit_dup(it->its[i]);
		if (!result->its[i]) {
			i--;
			goto error;
		}
	}
	result->count = it->count;
	return EXCIT_SUCCESS;
error:
	while (i >= 0) {
		free(result->its[i]);
		i--;
	}
	free(result->its);
	return -EXCIT_ENOMEM;
}

static int prod_it_rewind(excit_t data)
{
	struct prod_it_s *it = (struct prod_it_s *)data->data;

	for (ssize_t i = 0; i < it->count; i++) {
		int err = excit_rewind(it->its[i]);

		if (err)
			return err;
	}
	return EXCIT_SUCCESS;
}

83
static int prod_it_size(const excit_t data, ssize_t *size)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{
	const struct prod_it_s *it = (const struct prod_it_s *)data->data;
	ssize_t tmp_size = 0;

	if (!size)
		return -EXCIT_EINVAL;
	if (it->count == 0)
		*size = 0;
	else {
		*size = 1;
		for (ssize_t i = 0; i < it->count; i++) {
			int err = excit_size(it->its[i], &tmp_size);

			if (err) {
				*size = 0;
				return err;
			}
			*size *= tmp_size;
		}
	}
	return EXCIT_SUCCESS;
}

107
static int prod_it_nth(const excit_t data, ssize_t n, ssize_t *indexes)
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
{
	ssize_t size;
	int err = prod_it_size(data, &size);

	if (err)
		return err;
	if (n < 0 || n >= size)
		return -EXCIT_EDOM;
	const struct prod_it_s *it = (const struct prod_it_s *)data->data;

	if (indexes) {
		ssize_t subsize = 0;
		ssize_t offset = data->dimension;

		for (ssize_t i = it->count - 1; i >= 0; i--) {
			offset -= it->its[i]->dimension;
			err = excit_size(it->its[i], &subsize);
			if (err)
				return err;
			err = excit_nth(it->its[i], n % subsize,
					indexes + offset);
			if (err)
				return err;
			n /= subsize;
		}
	}
	return EXCIT_SUCCESS;
}

137
138
static int prod_it_rank(const excit_t data, const ssize_t *indexes,
			ssize_t *n)
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
{
	const struct prod_it_s *it = (const struct prod_it_s *)data->data;

	if (it->count == 0)
		return -EXCIT_EINVAL;
	ssize_t offset = 0;
	ssize_t product = 0;
	ssize_t inner_n;
	ssize_t subsize;

	for (ssize_t i = 0; i < it->count; i++) {
		int err = excit_rank(it->its[i], indexes + offset, &inner_n);
		if (err)
			return err;
		err = excit_size(it->its[i], &subsize);
		if (err)
			return err;
		product *= subsize;
		product += inner_n;
		offset += it->its[i]->dimension;
	}
	if (n)
		*n = product;
	return EXCIT_SUCCESS;
}

165
static int prod_it_pos(const excit_t data, ssize_t *n)
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
{
	const struct prod_it_s *it = (const struct prod_it_s *)data->data;

	if (it->count == 0)
		return -EXCIT_EINVAL;
	ssize_t product = 0;
	ssize_t inner_n;
	ssize_t subsize;

	for (ssize_t i = 0; i < it->count; i++) {
		int err = excit_pos(it->its[i], &inner_n);

		if (err)
			return err;
		err = excit_size(it->its[i], &subsize);
		if (err)
			return err;
		product *= subsize;
		product += inner_n;
	}
	if (n)
		*n = product;
	return EXCIT_SUCCESS;
}

191
static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes,
192
193
194
195
196
197
198
199
200
201
202
203
204
					  int next)
{
	struct prod_it_s *it = (struct prod_it_s *)data->data;
	int err;
	int looped;
	ssize_t i;
	ssize_t *next_indexes;
	ssize_t offset = data->dimension;

	if (it->count == 0)
		return -EXCIT_EINVAL;
	looped = next;
	for (i = it->count - 1; i > 0; i--) {
205
206
		offset -= it->its[i]->dimension;
		next_indexes = it->buff + offset;
207
208
209
210
211
212
213
214
		if (looped)
			err = excit_cyclic_next(it->its[i], next_indexes,
						&looped);
		else
			err = excit_peek(it->its[i], next_indexes);
		if (err)
			return err;
	}
215
216
	offset -= it->its[i]->dimension;
	next_indexes = it->buff + offset;
217
218
219
220
221
222
	if (looped)
		err = excit_next(it->its[0], next_indexes);
	else
		err = excit_peek(it->its[0], next_indexes);
	if (err)
		return err;
223
224
225

	if(indexes)
		memcpy(indexes, it->buff, data->dimension * sizeof(ssize_t)); 
226
227
228
	return EXCIT_SUCCESS;
}

229
static int prod_it_peek(const excit_t data, ssize_t *indexes)
230
231
232
233
{
	return prod_it_peeknext_helper(data, indexes, 0);
}

234
static int prod_it_next(excit_t data, ssize_t *indexes)
235
236
237
238
{
	return prod_it_peeknext_helper(data, indexes, 1);
}

239
int excit_product_count(const excit_t it, ssize_t *count)
240
241
242
243
244
245
246
247
{
	if (!it || it->type != EXCIT_PRODUCT || !count)
		return -EXCIT_EINVAL;
	*count = ((struct prod_it_s *)it->data)->count;
	return EXCIT_SUCCESS;
}

int excit_product_split_dim(const excit_t it, ssize_t dim, ssize_t n,
248
			    excit_t *results)
249
250
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
{
	if (!it || it->type != EXCIT_PRODUCT)
		return -EXCIT_EINVAL;
	if (n <= 0)
		return -EXCIT_EDOM;
	ssize_t count;
	int err = excit_product_count(it, &count);

	if (err)
		return err;
	if (dim >= count)
		return -EXCIT_EDOM;
	struct prod_it_s *prod_it = (struct prod_it_s *)it->data;

	err = excit_split(prod_it->its[dim], n, results);
	if (err)
		return err;
	if (!results)
		return EXCIT_SUCCESS;
	for (int i = 0; i < n; i++) {
		excit_t tmp = results[i];

		results[i] = excit_dup(it);
		if (!tmp) {
			excit_free(tmp);
			err = -EXCIT_ENOMEM;
			goto error;
		}
		struct prod_it_s *new_prod_it =
		    (struct prod_it_s *)results[i]->data;
		excit_free(new_prod_it->its[dim]);
		new_prod_it->its[dim] = tmp;
	}
	return EXCIT_SUCCESS;
error:
	for (int i = 0; i < n; i++)
		excit_free(results[i]);
	return err;
}

int excit_product_add_copy(excit_t it, excit_t added_it)
{
	int err = 0;
	excit_t copy = excit_dup(added_it);

	if (!copy)
		return -EXCIT_EINVAL;
	err = excit_product_add(it, copy);
	if (err) {
		excit_free(added_it);
		return err;
	}
	return EXCIT_SUCCESS;
}

int excit_product_add(excit_t it, excit_t added_it)
{
306
307
	int err = EXCIT_SUCCESS;
	
308
309
310
311
312
313
314
	if (!it || it->type != EXCIT_PRODUCT || !it->data || !added_it)
		return -EXCIT_EINVAL;

	struct prod_it_s *prod_it = (struct prod_it_s *)it->data;
	ssize_t mew_count = prod_it->count + 1;

	excit_t *new_its =
315
316
	    (excit_t *) realloc(prod_it->its, mew_count * sizeof(excit_t));	

317
318
	if (!new_its)
		return -EXCIT_ENOMEM;
319
320
321
322
323
324
325
326
327
328

	ssize_t *new_buff =
		realloc(prod_it->buff,
			(added_it->dimension + it->dimension) * sizeof(ssize_t));

	if (!new_buff){
	        err = -EXCIT_ENOMEM;
		goto exit_with_new_its;
	}
	
329
	prod_it->its = new_its;
330
	prod_it->buff = new_buff;
331
	prod_it->its[prod_it->count] = added_it;
Brice Videau's avatar
Brice Videau committed
332
	prod_it->count = mew_count;
333
334
	it->dimension += added_it->dimension;
	return EXCIT_SUCCESS;
335
336
337
338

 exit_with_new_its:
	free(new_its);
	return err;
339
340
341
342
343
344
345
346
347
348
}

struct excit_func_table_s excit_prod_func_table = {
	prod_it_alloc,
	prod_it_free,
	prod_it_copy,
	prod_it_next,
	prod_it_peek,
	prod_it_size,
	prod_it_rewind,
Brice Videau's avatar
Brice Videau committed
349
	NULL,
350
351
352
353
	prod_it_nth,
	prod_it_rank,
	prod_it_pos
};