cons.c 5.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "dev/excit.h"
#include "cons.h"

static void circular_fifo_add(struct circular_fifo_s *fifo, ssize_t elem)
{
	if (fifo->size == fifo->length) {
		fifo->start = (fifo->start + 1) % fifo->length;
		fifo->end = (fifo->end + 1) % fifo->length;
	} else {
		fifo->end = (fifo->end + 1) % fifo->length;
		fifo->size++;
	}
	fifo->buffer[fifo->end] = elem;
}

static void circular_fifo_dump(const struct circular_fifo_s *fifo,
17
			       ssize_t *vals)
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
{
	ssize_t i;
	ssize_t j;

	for (i = 0, j = fifo->start; i < fifo->size; i++) {
		vals[i] = fifo->buffer[j];
		j = (j + 1) % fifo->length;
	}
}

static int cons_it_alloc(excit_t data)
{
	struct cons_it_s *it = (struct cons_it_s *)data->data;

	it->it = NULL;
	it->n = 0;
	it->fifo.length = 0;
	it->fifo.start = 0;
	it->fifo.end = -1;
	it->fifo.size = 0;
	it->fifo.buffer = NULL;
	return EXCIT_SUCCESS;
}

static void cons_it_free(excit_t data)
{
	struct cons_it_s *it = (struct cons_it_s *)data->data;

	excit_free(it->it);
	free(it->fifo.buffer);
}

static int cons_it_copy(excit_t ddst, const excit_t dsrc)
{
	struct cons_it_s *dst = (struct cons_it_s *)ddst->data;
	const struct cons_it_s *src = (const struct cons_it_s *)dsrc->data;
	excit_t copy = excit_dup(src->it);

	if (!copy)
		return -EXCIT_EINVAL;
	dst->it = copy;
	dst->n = src->n;
	dst->fifo.length = src->fifo.length;
	dst->fifo.start = src->fifo.start;
	dst->fifo.end = src->fifo.end;
	dst->fifo.size = src->fifo.size;
	dst->fifo.buffer =
	    (ssize_t *) malloc(src->fifo.length * sizeof(ssize_t));
	if (!dst->fifo.buffer) {
		excit_free(copy);
		return -EXCIT_ENOMEM;
	}
	for (int i = 0; i < dst->fifo.length; i++)
		dst->fifo.buffer[i] = src->fifo.buffer[i];
	return EXCIT_SUCCESS;
}

75
static int cons_it_size(const excit_t data, ssize_t *size)
76 77 78 79 80 81 82 83 84 85 86
{
	const struct cons_it_s *it = (const struct cons_it_s *)data->data;
	ssize_t tmp_size = 0;
	int err = excit_size(it->it, &tmp_size);

	if (err)
		return err;
	*size = tmp_size - (it->n - 1);
	return EXCIT_SUCCESS;
}

87
static int cons_it_nth(const excit_t data, ssize_t n, ssize_t *indexes)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
	ssize_t size;
	int err = cons_it_size(data, &size);

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

	if (indexes) {
		for (int i = 0; i < it->n; i++) {
			err = excit_nth(it->it, n + i, indexes + dim * i);
			if (err)
				return err;
		}
	}
	return EXCIT_SUCCESS;
}

109 110
static int cons_it_rank(const excit_t data, const ssize_t *indexes,
			ssize_t *n)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
{
	const struct cons_it_s *it = (const struct cons_it_s *)data->data;
	ssize_t inner_n, inner_n_tmp;
	int err = excit_rank(it->it, indexes, &inner_n);

	if (err)
		return err;
	int dim = it->it->dimension;

	for (int i = 1; i < it->n; i++) {
		err = excit_rank(it->it, indexes + dim * i, &inner_n_tmp);
		if (err)
			return err;
		if (inner_n_tmp != inner_n + 1)
			return -EXCIT_EINVAL;
		inner_n = inner_n_tmp;
	}
	if (n)
		*n = inner_n - (it->n - 1);
	return EXCIT_SUCCESS;
}

133
static int cons_it_pos(const excit_t data, ssize_t *n)
134 135 136 137 138 139 140 141 142 143 144 145
{
	ssize_t inner_n;
	const struct cons_it_s *it = (const struct cons_it_s *)data->data;
	int err = excit_pos(it->it, &inner_n);

	if (err)
		return err;
	if (n)
		*n = inner_n - (it->n - 1);
	return EXCIT_SUCCESS;
}

146
static int cons_it_peek(const excit_t data, ssize_t *indexes)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
	const struct cons_it_s *it = (const struct cons_it_s *)data->data;
	int err;
	int dim = it->it->dimension;
	int n = it->n;

	if (indexes) {
		circular_fifo_dump(&it->fifo, indexes);
		err = excit_peek(it->it, indexes + dim * (n - 1));
	} else
		err = excit_peek(it->it, NULL);
	if (err)
		return err;
	return EXCIT_SUCCESS;
}

163
static int cons_it_next(excit_t data, ssize_t *indexes)
164 165 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
{
	struct cons_it_s *it = (struct cons_it_s *)data->data;
	int err;
	int dim = it->it->dimension;
	int n = it->n;

	if (indexes) {
		circular_fifo_dump(&it->fifo, indexes);
		err = excit_next(it->it, indexes + dim * (n - 1));
	} else
		err = excit_next(it->it, NULL);
	if (err)
		return err;
	if (indexes)
		for (int i = dim * (n - 1); i < dim * n; i++)
			circular_fifo_add(&it->fifo, indexes[i]);
	return EXCIT_SUCCESS;
}

static int cons_it_rewind(excit_t data)
{
	struct cons_it_s *it = (struct cons_it_s *)data->data;
	int err = excit_rewind(it->it);

	if (err)
		return err;
	it->fifo.start = 0;
	it->fifo.end = -1;
	it->fifo.size = 0;

	for (int i = 0; i < it->n - 1; i++) {
		int err;

		err =
		    excit_next(it->it, it->fifo.buffer + it->it->dimension * i);
		if (err)
			return err;
		it->fifo.size += it->it->dimension;
		it->fifo.end += it->it->dimension;
	}
	return EXCIT_SUCCESS;
}

int excit_cons_init(excit_t it, excit_t src, ssize_t n)
{
	ssize_t src_size;
	int err;

	if (!it || it->type != EXCIT_CONS || !src || n <= 0)
		return -EXCIT_EINVAL;
	err = excit_size(src, &src_size);
	if (err)
		return err;
	if (src_size < n)
		return -EXCIT_EINVAL;
	struct cons_it_s *cons_it = (struct cons_it_s *)it->data;

	free(cons_it->fifo.buffer);
	excit_free(cons_it->it);
	it->dimension = n * src->dimension;
	cons_it->it = src;
	cons_it->n = n;
	cons_it->fifo.length = src->dimension * (n - 1);
	cons_it->fifo.buffer =
	    (ssize_t *) malloc(cons_it->fifo.length * sizeof(ssize_t));
	if (!cons_it->fifo.buffer)
		return -EXCIT_ENOMEM;
	err = cons_it_rewind(it);
	if (err) {
		free(cons_it->fifo.buffer);
		return err;
	}
	return EXCIT_SUCCESS;
}

struct excit_func_table_s excit_cons_func_table = {
	cons_it_alloc,
	cons_it_free,
	cons_it_copy,
	cons_it_next,
	cons_it_peek,
	cons_it_size,
	cons_it_rewind,
247
	NULL,
248 249 250 251
	cons_it_nth,
	cons_it_rank,
	cons_it_pos
};