prod.c 7.01 KB
Newer Older
1
#include <string.h>
2 3 4 5 6 7 8 9 10
#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;
11
	it->buff = NULL;
12 13 14 15 16 17 18 19 20 21 22
	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);
23
		free(it->buff);
24 25 26 27 28 29 30 31 32 33 34
	}
}

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;
35 36 37 38 39 40
	result->buff = (ssize_t *) malloc(src->dimension * sizeof(ssize_t));
	if (!result->buff){
		free(result->its);
		return -EXCIT_ENOMEM;
	}

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
	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;
}

74
static int prod_it_size(const excit_t data, ssize_t *size)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
{
	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;
}

98
static int prod_it_nth(const excit_t data, ssize_t n, ssize_t *indexes)
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
{
	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;
}

128 129
static int prod_it_rank(const excit_t data, const ssize_t *indexes,
			ssize_t *n)
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
{
	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;
}

156
static int prod_it_pos(const excit_t data, ssize_t *n)
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
{
	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;
}

182
static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes,
183 184 185 186 187 188 189 190 191 192 193 194 195
					  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--) {
196 197
		offset -= it->its[i]->dimension;
		next_indexes = it->buff + offset;
198 199 200 201 202 203 204 205
		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;
	}
206 207
	offset -= it->its[i]->dimension;
	next_indexes = it->buff + offset;
208 209 210 211 212 213
	if (looped)
		err = excit_next(it->its[0], next_indexes);
	else
		err = excit_peek(it->its[0], next_indexes);
	if (err)
		return err;
214 215 216

	if(indexes)
		memcpy(indexes, it->buff, data->dimension * sizeof(ssize_t)); 
217 218 219
	return EXCIT_SUCCESS;
}

220
static int prod_it_peek(const excit_t data, ssize_t *indexes)
221 222 223 224
{
	return prod_it_peeknext_helper(data, indexes, 0);
}

225
static int prod_it_next(excit_t data, ssize_t *indexes)
226 227 228 229
{
	return prod_it_peeknext_helper(data, indexes, 1);
}

230
int excit_product_count(const excit_t it, ssize_t *count)
231 232 233 234 235 236 237 238
{
	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,
239
			    excit_t *results)
240 241 242 243 244 245 246 247 248 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
{
	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)
{
297 298
	int err = EXCIT_SUCCESS;
	
299 300 301 302 303 304 305
	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 =
306 307
	    (excit_t *) realloc(prod_it->its, mew_count * sizeof(excit_t));	

308 309
	if (!new_its)
		return -EXCIT_ENOMEM;
310 311 312 313 314 315 316 317 318 319

	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;
	}
	
320
	prod_it->its = new_its;
321
	prod_it->buff = new_buff;
322
	prod_it->its[prod_it->count] = added_it;
323
	prod_it->count = mew_count;	
324 325
	it->dimension += added_it->dimension;
	return EXCIT_SUCCESS;
326 327 328 329

 exit_with_new_its:
	free(new_its);
	return err;
330 331 332 333 334 335 336 337 338 339
}

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,
340
	NULL,
341 342 343 344
	prod_it_nth,
	prod_it_rank,
	prod_it_pos
};