Commit b8a35329 authored by Nicolas Denoyelle's avatar Nicolas Denoyelle

Merge branch 'master' into tleaf

parents a56b4120 2f228ce7
......@@ -2,6 +2,6 @@ AM_CFLAGS = -Wall -Werror -pedantic
lib_LTLIBRARIES = libexcit.la
libexcit_la_SOURCES = excit.c tleaf.c range.c slice.c repeat.c prod.c cons.c hilbert2d.c
libexcit_la_SOURCES = excit.c slice.c prod.c cons.c repeat.c hilbert2d.c range.c
include_HEADERS = excit.h
#include "dev/excit.h"
#include "cons.h"
#define EXCIT_DATA(data, it) \
if(data == NULL) { return EXCIT_EINVAL; } \
do{ \
int err = excit_get_data(data, (void**)(&it)); \
if(err != EXCIT_SUCCESS) { return err; } \
} while(0); \
if(it == NULL){ return EXCIT_EINVAL; }
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;
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,
static void circular_fifo_dump(const struct circular_fifo_s *fifo,
ssize_t *vals)
{
ssize_t i;
ssize_t j;
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;
}
for (i = 0, j = fifo->start; i < fifo->size; i++) {
vals[i] = fifo->buffer[j];
j = (j + 1) % fifo->length;
}
}
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,
cons_it_split,
cons_it_nth,
cons_it_rank,
cons_it_pos
};
int cons_it_alloc(excit_t data)
static int cons_it_alloc(excit_t data)
{
struct cons_it_s *it;
EXCIT_DATA(data, it);
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;
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;
}
void cons_it_free(excit_t data)
static void cons_it_free(excit_t data)
{
struct cons_it_s *it;
if(data == NULL) { return ; }
int err = excit_get_data(data, (void**)(&it));
if(err != EXCIT_SUCCESS) { return; }
if(it == NULL){ return; }
excit_free(it->it);
free(it->fifo.buffer);
struct cons_it_s *it = (struct cons_it_s *)data->data;
excit_free(it->it);
free(it->fifo.buffer);
}
int cons_it_copy(excit_t ddst, const excit_t dsrc)
static int cons_it_copy(excit_t ddst, const excit_t dsrc)
{
struct cons_it_s *dst;
EXCIT_DATA(ddst, dst);
const struct cons_it_s *src;
EXCIT_DATA(dsrc, src);
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;
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;
}
int cons_it_size(const excit_t data, ssize_t *size)
static int cons_it_size(const excit_t data, ssize_t *size)
{
const struct cons_it_s *it;
EXCIT_DATA(data, it);
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;
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;
}
int cons_it_split(const excit_t data, ssize_t n, excit_t *results)
static int cons_it_split(const excit_t data, ssize_t n, excit_t *results)
{
ssize_t size;
int err = cons_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;
int i;
for (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;
ssize_t size;
int err = cons_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;
int i;
for (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 (; i >= 0; i--)
excit_free(results[i]);
for (; i >= 0; i--)
excit_free(results[i]);
error1:
excit_free(range);
return err;
excit_free(range);
return err;
}
int cons_it_nth(const excit_t data, ssize_t n, ssize_t *indexes)
static int cons_it_nth(const excit_t data, ssize_t n, ssize_t *indexes)
{
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;
EXCIT_DATA(data, it);
int dim = excit_get_dimension(it->it);
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;
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;
}
int cons_it_rank(const excit_t data, const ssize_t *indexes, ssize_t *n)
static int cons_it_rank(const excit_t data, const ssize_t *indexes,
ssize_t *n)
{
const struct cons_it_s *it;
EXCIT_DATA(data, it);
ssize_t inner_n, inner_n_tmp;
int err = excit_rank(it->it, indexes, &inner_n);
if (err)
return err;
int dim = excit_get_dimension(it->it);
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;
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;
}
int cons_it_pos(const excit_t data, ssize_t *n)
static int cons_it_pos(const excit_t data, ssize_t *n)
{
ssize_t inner_n;
const struct cons_it_s *it;
EXCIT_DATA(data, it);
int err = excit_pos(it->it, &inner_n);
if (err)
return err;
if (n)
*n = inner_n - (it->n - 1);
return EXCIT_SUCCESS;
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;
}
int cons_it_peek(const excit_t data, ssize_t *indexes)
static int cons_it_peek(const excit_t data, ssize_t *indexes)
{
const struct cons_it_s *it;
EXCIT_DATA(data, it);
int err;
int dim = excit_get_dimension(it->it);
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;
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;
}
int cons_it_next(excit_t data, ssize_t *indexes)
static int cons_it_next(excit_t data, ssize_t *indexes)
{
struct cons_it_s *it;
EXCIT_DATA(data, it);
int err;
int dim = excit_get_dimension(it->it);
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;
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;
}
int cons_it_rewind(excit_t data)
static int cons_it_rewind(excit_t data)
{
struct cons_it_s *it;
EXCIT_DATA(data, it);
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 + excit_get_dimension(it->it) * i);
if (err)
return err;
it->fifo.size += excit_get_dimension(it->it);
it->fifo.end += excit_get_dimension(it->it);
}
return EXCIT_SUCCESS;
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 || !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;
EXCIT_DATA(it, cons_it);
free(cons_it->fifo.buffer);
excit_free(cons_it->it);
excit_set_dimension(it, excit_get_dimension(src) * n);
cons_it->it = src;
cons_it->n = n;
cons_it->fifo.length = excit_get_dimension(src) * (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;
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,
cons_it_split,
cons_it_nth,
cons_it_rank,
cons_it_pos
};
#ifndef CONS_H
#define CONS_H
#ifndef EXCIT_CONS_H
#define EXCIT_CONS_H
#include "excit.h"
#include "dev/excit.h"
struct circular_fifo_s {
ssize_t length;
ssize_t start;
ssize_t end;
ssize_t size;
ssize_t *buffer;
ssize_t length;
ssize_t start;
ssize_t end;
ssize_t size;
ssize_t *buffer;
};
struct cons_it_s {
......@@ -17,18 +18,6 @@ struct cons_it_s {
struct circular_fifo_s fifo;
};
int cons_it_alloc(excit_t data);
void cons_it_free(excit_t data);
int cons_it_copy(excit_t ddst, const excit_t dsrc);
int cons_it_size(const excit_t data, ssize_t *size);
int cons_it_split(const excit_t data, ssize_t n, excit_t *results);
int cons_it_nth(const excit_t data, ssize_t n, ssize_t *indexes);
int cons_it_rank(const excit_t data, const ssize_t *indexes, ssize_t *n);
int cons_it_pos(const excit_t data, ssize_t *n);
int cons_it_peek(const excit_t data, ssize_t *indexes);
int cons_it_next(excit_t data, ssize_t *indexes);
int cons_it_rewind(excit_t data);
extern struct excit_func_table_s excit_cons_func_table;
#endif
#endif //EXCIT_CONS_H
#ifndef EXCIT_DEV_H
#define EXCIT_DEV_H
#include "../excit.h"
struct excit_s {
struct excit_func_table_s *func_table;
ssize_t dimension;
enum excit_type_e type;
void *data;
};
#endif
#include <stdlib.h>
#include "excit.h"
#include "dev/excit.h"
#include "slice.h"
#include "range.h"
#include "repeat.h"
#include "prod.h"
#include "cons.h"
#include "repeat.h"
#include "hilbert2d.h"
#include "tleaf.h"
#include "range.h"
#define CASE(val) case val: return #val; break;
#define CASE(val) case val: return #val; break
const char * excit_type_name(enum excit_type_e type)
const char *excit_type_name(enum excit_type_e type)
{
switch (type) {
CASE(EXCIT_RANGE)
CASE(EXCIT_CONS)
CASE(EXCIT_REPEAT)
CASE(EXCIT_HILBERT2D)
CASE(EXCIT_PRODUCT)
CASE(EXCIT_SLICE)
CASE(EXCIT_TLEAF)
CASE(EXCIT_USER)
CASE(EXCIT_TYPE_MAX)
default:
return NULL;
}
switch (type) {
CASE(EXCIT_RANGE);
CASE(EXCIT_CONS);
CASE(EXCIT_REPEAT);
CASE(EXCIT_HILBERT2D);
CASE(EXCIT_PRODUCT);
CASE(EXCIT_SLICE);
CASE(EXCIT_USER);
CASE(EXCIT_TYPE_MAX);
default:
return NULL;
}
}
const char * excit_error_name(enum excit_error_e err)
const char *excit_error_name(enum excit_error_e err)
{
switch (err) {
CASE(EXCIT_SUCCESS)
CASE(EXCIT_STOPIT)
CASE(EXCIT_ENOMEM)
CASE(EXCIT_EINVAL)
CASE(EXCIT_EDOM)
CASE(EXCIT_ENOTSUP)
CASE(EXCIT_ERROR_MAX)
default:
return NULL;
}
switch (err) {
CASE(EXCIT_SUCCESS);
CASE(EXCIT_STOPIT);
CASE(EXCIT_ENOMEM);
CASE(EXCIT_EINVAL);
CASE(EXCIT_EDOM);
CASE(EXCIT_ENOTSUP);
CASE(EXCIT_ERROR_MAX);
default:
return NULL;
}
}
#undef CASE
struct excit_s {
struct excit_func_table_s *func_table;
ssize_t dimension;
enum excit_type_e type;
void *data;
};
int excit_set_dimension(excit_t it, ssize_t dimension)
{
if (!it)
......@@ -61,54 +53,46 @@ int excit_set_dimension(excit_t it, ssize_t dimension)