composition.c 4.17 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 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
#include "dev/excit.h"
#include "composition.h"

static int composition_it_alloc(excit_t data)
{
	struct composition_it_s *it = (struct composition_it_s *)data->data;

	it->src = NULL;
	it->indexer = NULL;
	return EXCIT_SUCCESS;
}

static void composition_it_free(excit_t data)
{
	struct composition_it_s *it = (struct composition_it_s *)data->data;

	excit_free(it->src);
	excit_free(it->indexer);
}

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

	result->src = excit_dup(it->src);
	if (!result->src)
		return -EXCIT_ENOMEM;
	result->indexer = excit_dup(it->indexer);
	if (!result->indexer) {
		excit_free(it->src);
		return -EXCIT_ENOMEM;
	}
	return EXCIT_SUCCESS;
}

static int composition_it_next(excit_t data, ssize_t *indexes)
{
	struct composition_it_s *it = (struct composition_it_s *)data->data;
	ssize_t n;
	int err = excit_next(it->indexer, &n);

	if (err)
		return err;
	return excit_nth(it->src, n, indexes);
}

static int composition_it_peek(const excit_t data, ssize_t *indexes)
{
	const struct composition_it_s *it = (const struct composition_it_s *)data->data;
	ssize_t n;
	int err = excit_peek(it->indexer, &n);

	if (err)
		return err;
	return excit_nth(it->src, n, indexes);
}

static int composition_it_size(const excit_t data, ssize_t *size)
{
	const struct composition_it_s *it = (const struct composition_it_s *)data->data;

	return excit_size(it->indexer, size);
}

static int composition_it_rewind(excit_t data)
{
	struct composition_it_s *it = (struct composition_it_s *)data->data;

	return excit_rewind(it->indexer);
}

static int composition_it_nth(const excit_t data, ssize_t n, ssize_t *indexes)
{
	const struct composition_it_s *it = (const struct composition_it_s *)data->data;
	ssize_t p;
	int err = excit_nth(it->indexer, n, &p);

	if (err)
		return err;
	return excit_nth(it->src, p, indexes);
}

static int composition_it_rank(const excit_t data, const ssize_t *indexes,
			 ssize_t *n)
{
	const struct composition_it_s *it = (const struct composition_it_s *)data->data;
	ssize_t inner_n;
	int err = excit_rank(it->src, indexes, &inner_n);

	if (err)
		return err;
	return excit_rank(it->indexer, &inner_n, n);
}

static int composition_it_pos(const excit_t data, ssize_t *n)
{
	const struct composition_it_s *it = (const struct composition_it_s *)data->data;

	return excit_pos(it->indexer, n);
}

static int composition_it_split(const excit_t data, ssize_t n, excit_t *results)
{
	const struct composition_it_s *it = (const struct composition_it_s *)data->data;
	int err = excit_split(it->indexer, n, results);

	if (err)
		return err;
	if (!results)
		return EXCIT_SUCCESS;
	for (int i = 0; i < n; i++) {
		excit_t tmp;
		excit_t tmp2;

		tmp = results[i];
		results[i] = excit_alloc(EXCIT_COMPOSITION);
		if (!results[i]) {
			excit_free(tmp);
			err = -EXCIT_ENOMEM;
			goto error;
		}
		tmp2 = excit_dup(it->src);
		if (!tmp2) {
			excit_free(tmp);
			err = -EXCIT_ENOMEM;
			goto error;
		}
		err = excit_composition_init(results[i], tmp2, tmp);
		if (err) {
			excit_free(tmp);
			excit_free(tmp2);
			goto error;
		}
	}
	return EXCIT_SUCCESS;
error:
	for (int i = 0; i < n; i++)
		excit_free(results[i]);
	return err;
}

struct excit_func_table_s excit_composition_func_table = {
	composition_it_alloc,
	composition_it_free,
	composition_it_copy,
	composition_it_next,
	composition_it_peek,
	composition_it_size,
	composition_it_rewind,
	composition_it_split,
	composition_it_nth,
	composition_it_rank,
	composition_it_pos
};

int excit_composition_init(excit_t it, excit_t src, excit_t indexer)
{
	if (!it || it->type != EXCIT_COMPOSITION || !src || !indexer
	    || indexer->dimension != 1)
		return -EXCIT_EINVAL;
	struct composition_it_s *composition_it = (struct composition_it_s *)it->data;
	ssize_t size_src;
	ssize_t size_indexer;
	int err = excit_size(src, &size_src);

	if (err)
		return err;
	err = excit_size(indexer, &size_indexer);
	if (err)
		return err;
	if (size_indexer > size_src)
		return -EXCIT_EDOM;
	composition_it->src = src;
	composition_it->indexer = indexer;
	it->dimension = src->dimension;
	return EXCIT_SUCCESS;
}