prod.c 7.48 KB
Newer Older
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
#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;
	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);
	}
}

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

65
static int prod_it_size(const excit_t data, ssize_t *size)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
{
	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;
}

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

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

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

173
static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes,
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
					  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--) {
		if (indexes) {
			offset -= it->its[i]->dimension;
			next_indexes = indexes + offset;
		} else
			next_indexes = NULL;
		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;
	}
	if (indexes) {
		offset -= it->its[i]->dimension;
		next_indexes = indexes + offset;
	} else
		next_indexes = NULL;
	if (looped)
		err = excit_next(it->its[0], next_indexes);
	else
		err = excit_peek(it->its[0], next_indexes);
	if (err)
		return err;
	return EXCIT_SUCCESS;
}

214
static int prod_it_peek(const excit_t data, ssize_t *indexes)
215 216 217 218
{
	return prod_it_peeknext_helper(data, indexes, 0);
}

219
static int prod_it_next(excit_t data, ssize_t *indexes)
220 221 222 223
{
	return prod_it_peeknext_helper(data, indexes, 1);
}

224
static int prod_it_split(const excit_t data, ssize_t n, excit_t *results)
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
{
	ssize_t size;
	int err = prod_it_size(data, &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(data);
		if (!tmp)
			goto error2;
		tmp2 = results[i];
		results[i] = excit_alloc(EXCIT_SLICE);
		if (!results[i]) {
			excit_free(tmp2);
			goto error2;
		}
		err = excit_slice_init(results[i], tmp, tmp2);
		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;
}

273
int excit_product_count(const excit_t it, ssize_t *count)
274 275 276 277 278 279 280 281
{
	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,
282
			    excit_t *results)
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 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 358 359 360 361 362 363 364 365 366 367 368 369
{
	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)
{
	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 =
	    (excit_t *) realloc(prod_it->its, mew_count * sizeof(excit_t));
	if (!new_its)
		return -EXCIT_ENOMEM;
	prod_it->its = new_its;
	prod_it->its[prod_it->count] = added_it;
	prod_it->count = mew_count;
	it->dimension += added_it->dimension;
	return EXCIT_SUCCESS;
}

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,
	prod_it_split,
	prod_it_nth,
	prod_it_rank,
	prod_it_pos
};