Commit 1cd36fe0 authored by Brice Videau's avatar Brice Videau

Renamed citerator to excit.

parent 34e5a4b6
......@@ -2,7 +2,7 @@
AC_INIT([excit],[0.0.1],[brice.videau@imag.fr])
# are we in the right source dir ?
AC_CONFIG_SRCDIR([src/citerators.h])
AC_CONFIG_SRCDIR([src/excit.h])
# build artefacts in separate dir
AC_CONFIG_AUX_DIR([m4])
......
lib_LTLIBRARIES = libexcit.la
libexcit_la_SOURCES = citerators.c citerators.h
libexcit_la_SOURCES = excit.c excit.h
include_HEADERS = citerators.h
include_HEADERS = excit.h
#ifndef CITERATORS_H
#define CITERATORS_H 1
enum citerator_type_e {
CITERATOR_RANGE,
CITERATOR_CONS,
CITERATOR_REPEAT,
CITERATOR_HILBERT2D,
CITERATOR_PRODUCT,
CITERATOR_SLICE
};
typedef struct citerator_s *citerator_t;
typedef int citerator_index_t;
citerator_t citerator_alloc(enum citerator_type_e type);
void citerator_free(citerator_t iterator);
citerator_t citerator_dup(const citerator_t iterator);
int citerator_type(citerator_t iterator, enum citerator_type_e *type);
int citerator_dimension(citerator_t iterator, citerator_index_t *dimension);
int citerator_next(citerator_t iterator, citerator_index_t *indexes);
int citerator_peek(const citerator_t iterator, citerator_index_t *indexes);
int citerator_size(const citerator_t iterator, citerator_index_t *size);
int citerator_rewind(citerator_t iterator);
int citerator_split(const citerator_t iterator, citerator_index_t n,
citerator_t *results);
int citerator_nth(const citerator_t iterator, citerator_index_t n,
citerator_index_t *indexes);
int citerator_n(const citerator_t iterator, const citerator_index_t *indexes,
citerator_index_t *n);
int citerator_pos(const citerator_t iterator, citerator_index_t *n);
int citerator_skip(citerator_t iterator);
int citerator_cyclic_next(citerator_t iterator, citerator_index_t *indexes,
int *looped);
int citerator_range_init(citerator_t iterator, citerator_index_t first,
citerator_index_t last, citerator_index_t step);
int citerator_cons_init(citerator_t iterator, citerator_t src,
citerator_index_t n);
int citerator_repeat_init(citerator_t iterator, citerator_t src,
citerator_index_t n);
int citerator_hilbert2d_init(citerator_t iterator, citerator_index_t order);
int citerator_product_add(citerator_t iterator, citerator_t added_iterator);
int citerator_product_add_copy(citerator_t iterator,
citerator_t added_iterator);
int citerator_product_count(const citerator_t iterator,
citerator_index_t *count);
int citerator_product_split_dim(const citerator_t iterator,
citerator_index_t dim, citerator_index_t n,
citerator_t *results);
int citerator_slice_init(citerator_t iterator, citerator_t src,
citerator_t indexer);
#endif
#include <errno.h>
#include <stdlib.h>
#include <citerators.h>
struct citerator_func_table_s {
int (*alloc)(citerator_t data);
void (*free)(citerator_t data);
int (*copy)(citerator_t dst, const citerator_t src);
int (*next)(citerator_t data, citerator_index_t *indexes);
int (*peek)(const citerator_t data, citerator_index_t *indexes);
int (*size)(const citerator_t data, citerator_index_t *size);
int (*rewind)(citerator_t data);
int (*split)(const citerator_t data, citerator_index_t n,
citerator_t *results);
int (*nth)(const citerator_t data, citerator_index_t n,
citerator_index_t *indexes);
int (*n)(const citerator_t data, const citerator_index_t *indexes,
citerator_index_t *n);
int (*pos)(const citerator_t iterator, citerator_index_t *n);
#include <excit.h>
struct excit_func_table_s {
int (*alloc)(excit_t data);
void (*free)(excit_t data);
int (*copy)(excit_t dst, const excit_t src);
int (*next)(excit_t data, excit_index_t *indexes);
int (*peek)(const excit_t data, excit_index_t *indexes);
int (*size)(const excit_t data, excit_index_t *size);
int (*rewind)(excit_t data);
int (*split)(const excit_t data, excit_index_t n,
excit_t *results);
int (*nth)(const excit_t data, excit_index_t n,
excit_index_t *indexes);
int (*n)(const excit_t data, const excit_index_t *indexes,
excit_index_t *n);
int (*pos)(const excit_t iterator, excit_index_t *n);
};
struct citerator_s {
const struct citerator_func_table_s *functions;
citerator_index_t dimension;
enum citerator_type_e type;
struct excit_s {
const struct excit_func_table_s *functions;
excit_index_t dimension;
enum excit_type_e type;
void *data;
};
/*--------------------------------------------------------------------*/
struct slice_iterator_s {
citerator_t src;
citerator_t indexer;
excit_t src;
excit_t indexer;
};
static int slice_iterator_alloc(citerator_t data)
static int slice_iterator_alloc(excit_t data)
{
data->data = malloc(sizeof(struct slice_iterator_s));
if (!data->data)
......@@ -46,152 +46,152 @@ static int slice_iterator_alloc(citerator_t data)
return 0;
}
static void slice_iterator_free(citerator_t data)
static void slice_iterator_free(excit_t data)
{
struct slice_iterator_s *iterator =
(struct slice_iterator_s *) data->data;
citerator_free(iterator->src);
citerator_free(iterator->indexer);
excit_free(iterator->src);
excit_free(iterator->indexer);
free(data->data);
}
static int slice_iterator_copy(citerator_t dst, const citerator_t src)
static int slice_iterator_copy(excit_t dst, const excit_t src)
{
const struct slice_iterator_s *iterator =
(const struct slice_iterator_s *) src->data;
struct slice_iterator_s *result = (struct slice_iterator_s *) dst->data;
result->src = citerator_dup(iterator->src);
result->src = excit_dup(iterator->src);
if (!result->src)
return -ENOMEM;
result->indexer = citerator_dup(iterator->indexer);
result->indexer = excit_dup(iterator->indexer);
if (!result->indexer) {
citerator_free(iterator->src);
excit_free(iterator->src);
return -ENOMEM;
}
return 0;
}
static int slice_iterator_next(citerator_t data, citerator_index_t *indexes)
static int slice_iterator_next(excit_t data, excit_index_t *indexes)
{
struct slice_iterator_s *iterator =
(struct slice_iterator_s *) data->data;
citerator_index_t n;
int err = citerator_next(iterator->indexer, &n);
excit_index_t n;
int err = excit_next(iterator->indexer, &n);
if (err)
return err;
return citerator_nth(iterator->src, n, indexes);
return excit_nth(iterator->src, n, indexes);
}
static int slice_iterator_peek(const citerator_t data,
citerator_index_t *indexes)
static int slice_iterator_peek(const excit_t data,
excit_index_t *indexes)
{
const struct slice_iterator_s *iterator =
(const struct slice_iterator_s *)data->data;
citerator_index_t n;
int err = citerator_peek(iterator->indexer, &n);
excit_index_t n;
int err = excit_peek(iterator->indexer, &n);
if (err)
return err;
return citerator_nth(iterator->src, n, indexes);
return excit_nth(iterator->src, n, indexes);
}
static int slice_iterator_size(const citerator_t data, citerator_index_t *size)
static int slice_iterator_size(const excit_t data, excit_index_t *size)
{
const struct slice_iterator_s *iterator =
(const struct slice_iterator_s *)data->data;
return citerator_size(iterator->indexer, size);
return excit_size(iterator->indexer, size);
}
static int slice_iterator_rewind(citerator_t data)
static int slice_iterator_rewind(excit_t data)
{
struct slice_iterator_s *iterator =
(struct slice_iterator_s *) data->data;
return citerator_rewind(iterator->indexer);
return excit_rewind(iterator->indexer);
}
static int slice_iterator_nth(const citerator_t data, citerator_index_t n,
citerator_index_t *indexes)
static int slice_iterator_nth(const excit_t data, excit_index_t n,
excit_index_t *indexes)
{
const struct slice_iterator_s *iterator =
(const struct slice_iterator_s *)data->data;
citerator_index_t p;
int err = citerator_nth(iterator->indexer, n, &p);
excit_index_t p;
int err = excit_nth(iterator->indexer, n, &p);
if (err)
return err;
return citerator_nth(iterator->src, p, indexes);
return excit_nth(iterator->src, p, indexes);
}
static int slice_iterator_n(const citerator_t data,
const citerator_index_t *indexes,
citerator_index_t *n)
static int slice_iterator_n(const excit_t data,
const excit_index_t *indexes,
excit_index_t *n)
{
const struct slice_iterator_s *iterator =
(const struct slice_iterator_s *)data->data;
citerator_index_t inner_n;
int err = citerator_n(iterator->src, indexes, &inner_n);
excit_index_t inner_n;
int err = excit_n(iterator->src, indexes, &inner_n);
if (err)
return err;
return citerator_n(iterator->indexer, &inner_n, n);
return excit_n(iterator->indexer, &inner_n, n);
}
static int slice_iterator_pos(const citerator_t data, citerator_index_t *n)
static int slice_iterator_pos(const excit_t data, excit_index_t *n)
{
const struct slice_iterator_s *iterator =
(const struct slice_iterator_s *)data->data;
return citerator_pos(iterator->indexer, n);
return excit_pos(iterator->indexer, n);
}
static int slice_iterator_split(const citerator_t data, citerator_index_t n,
citerator_t *results)
static int slice_iterator_split(const excit_t data, excit_index_t n,
excit_t *results)
{
const struct slice_iterator_s *iterator =
(const struct slice_iterator_s *)data->data;
int err = citerator_split(iterator->indexer, n, results);
int err = excit_split(iterator->indexer, n, results);
if (err)
return err;
if (!results)
return 0;
for (int i = 0; i < n; i++) {
citerator_t tmp;
citerator_t tmp2;
excit_t tmp;
excit_t tmp2;
tmp = results[i];
results[i] = citerator_alloc(CITERATOR_SLICE);
results[i] = excit_alloc(EXCIT_SLICE);
if (!results[i]) {
citerator_free(tmp);
excit_free(tmp);
err = -ENOMEM;
goto error;
}
tmp2 = citerator_dup(iterator->src);
tmp2 = excit_dup(iterator->src);
if (!tmp2) {
citerator_free(tmp);
excit_free(tmp);
err = -ENOMEM;
goto error;
}
err = citerator_slice_init(results[i], tmp, tmp2);
err = excit_slice_init(results[i], tmp, tmp2);
if (err) {
citerator_free(tmp);
citerator_free(tmp2);
excit_free(tmp);
excit_free(tmp2);
goto error;
}
}
return 0;
error:
for (int i = 0; i < n; i++)
citerator_free(results[i]);
excit_free(results[i]);
return err;
}
static const struct citerator_func_table_s citerator_slice_func_table = {
static const struct excit_func_table_s excit_slice_func_table = {
slice_iterator_alloc,
slice_iterator_free,
slice_iterator_copy,
......@@ -205,21 +205,21 @@ static const struct citerator_func_table_s citerator_slice_func_table = {
slice_iterator_pos
};
int citerator_slice_init(citerator_t iterator, citerator_t src,
citerator_t indexer)
int excit_slice_init(excit_t iterator, excit_t src,
excit_t indexer)
{
if (!iterator || iterator->type != CITERATOR_SLICE || !src || !indexer
if (!iterator || iterator->type != EXCIT_SLICE || !src || !indexer
|| indexer->dimension != 1)
return -EINVAL;
struct slice_iterator_s *it =
(struct slice_iterator_s *) iterator->data;
citerator_index_t size_src;
citerator_index_t size_indexer;
int err = citerator_size(src, &size_src);
excit_index_t size_src;
excit_index_t size_indexer;
int err = excit_size(src, &size_src);
if (err)
return err;
err = citerator_size(indexer, &size_indexer);
err = excit_size(indexer, &size_indexer);
if (err)
return err;
if (size_indexer > size_src)
......@@ -233,11 +233,11 @@ int citerator_slice_init(citerator_t iterator, citerator_t src,
/*--------------------------------------------------------------------*/
struct product_iterator_s {
citerator_index_t count;
citerator_t *iterators;
excit_index_t count;
excit_t *iterators;
};
static int product_iterator_alloc(citerator_t data)
static int product_iterator_alloc(excit_t data)
{
data->data = malloc(sizeof(struct product_iterator_s));
if (!data->data)
......@@ -250,19 +250,19 @@ static int product_iterator_alloc(citerator_t data)
return 0;
}
static void product_iterator_free(citerator_t data)
static void product_iterator_free(excit_t data)
{
struct product_iterator_s *iterator =
(struct product_iterator_s *) data->data;
if (iterator->iterators) {
for (int i = 0; i < iterator->count; i++)
citerator_free(iterator->iterators[i]);
for (excit_index_t i = 0; i < iterator->count; i++)
excit_free(iterator->iterators[i]);
free(iterator->iterators);
}
free(data->data);
}
static int product_iterator_copy(citerator_t dst, const citerator_t src)
static int product_iterator_copy(excit_t dst, const excit_t src)
{
const struct product_iterator_s *iterator =
(const struct product_iterator_s *)src->data;
......@@ -270,13 +270,13 @@ static int product_iterator_copy(citerator_t dst, const citerator_t src)
(struct product_iterator_s *) dst->data;
result->iterators =
(citerator_t *) malloc(iterator->count * sizeof(citerator_t));
(excit_t *) malloc(iterator->count * sizeof(excit_t));
if (!result->iterators)
return -ENOMEM;
int i;
excit_index_t i;
for (i = 0; i < iterator->count; i++) {
result->iterators[i] = citerator_dup(iterator->iterators[i]);
result->iterators[i] = excit_dup(iterator->iterators[i]);
if (!result->iterators[i]) {
i--;
goto error;
......@@ -293,13 +293,13 @@ error:
return -ENOMEM;
}
static int product_iterator_rewind(citerator_t data)
static int product_iterator_rewind(excit_t data)
{
struct product_iterator_s *iterator =
(struct product_iterator_s *) data->data;
for (int i = 0; i < iterator->count; i++) {
int err = citerator_rewind(iterator->iterators[i]);
for (excit_index_t i = 0; i < iterator->count; i++) {
int err = excit_rewind(iterator->iterators[i]);
if (err)
return err;
......@@ -307,12 +307,12 @@ static int product_iterator_rewind(citerator_t data)
return 0;
}
static int product_iterator_size(const citerator_t data,
citerator_index_t *size)
static int product_iterator_size(const excit_t data,
excit_index_t *size)
{
const struct product_iterator_s *iterator =
(const struct product_iterator_s *) data->data;
citerator_index_t tmp_size = 0;
excit_index_t tmp_size = 0;
if (!size)
return -EINVAL;
......@@ -320,9 +320,9 @@ static int product_iterator_size(const citerator_t data,
*size = 0;
else {
*size = 1;
for (int i = 0; i < iterator->count; i++) {
for (excit_index_t i = 0; i < iterator->count; i++) {
int err =
citerator_size(iterator->iterators[i], &tmp_size);
excit_size(iterator->iterators[i], &tmp_size);
if (err) {
*size = 0;
......@@ -334,10 +334,10 @@ static int product_iterator_size(const citerator_t data,
return 0;
}
static int product_iterator_nth(const citerator_t data, citerator_index_t n,
citerator_index_t *indexes)
static int product_iterator_nth(const excit_t data, excit_index_t n,
excit_index_t *indexes)
{
int size;
excit_index_t size;
int err = product_iterator_size(data, &size);
if (err)
......@@ -348,16 +348,16 @@ static int product_iterator_nth(const citerator_t data, citerator_index_t n,
(const struct product_iterator_s *) data->data;
if (indexes) {
citerator_index_t subsize = 0;
citerator_index_t offset = data->dimension;
excit_index_t subsize = 0;
excit_index_t offset = data->dimension;
for (int i = iterator->count - 1; i >= 0; i--) {
for (excit_index_t i = iterator->count - 1; i >= 0; i--) {
offset -= iterator->iterators[i]->dimension;
err = citerator_size(iterator->iterators[i], &subsize);
err = excit_size(iterator->iterators[i], &subsize);
if (err)
return err;
err =
citerator_nth(iterator->iterators[i], n % subsize,
excit_nth(iterator->iterators[i], n % subsize,
indexes + offset);
if (err)
return err;
......@@ -367,27 +367,27 @@ static int product_iterator_nth(const citerator_t data, citerator_index_t n,
return 0;
}
static int product_iterator_n(const citerator_t data,
const citerator_index_t *indexes,
citerator_index_t *n)
static int product_iterator_n(const excit_t data,
const excit_index_t *indexes,
excit_index_t *n)
{
const struct product_iterator_s *iterator =
(const struct product_iterator_s *) data->data;
if (iterator->count == 0)
return -EINVAL;
citerator_index_t offset = 0;
citerator_index_t product = 0;
citerator_index_t inner_n;
citerator_index_t subsize;
excit_index_t offset = 0;
excit_index_t product = 0;
excit_index_t inner_n;
excit_index_t subsize;
for (int i = 0; i < iterator->count; i++) {
for (excit_index_t i = 0; i < iterator->count; i++) {
int err =
citerator_n(iterator->iterators[i], indexes + offset,
excit_n(iterator->iterators[i], indexes + offset,
&inner_n);
if (err)
return err;
err = citerator_size(iterator->iterators[i], &subsize);
err = excit_size(iterator->iterators[i], &subsize);
if (err)
return err;
product *= subsize;
......@@ -399,23 +399,23 @@ static int product_iterator_n(const citerator_t data,
return 0;
}
static int product_iterator_pos(const citerator_t data, citerator_index_t *n)
static int product_iterator_pos(const excit_t data, excit_index_t *n)
{
const struct product_iterator_s *iterator =
(const struct product_iterator_s *) data->data;
if (iterator->count == 0)
return -EINVAL;
citerator_index_t product = 0;
citerator_index_t inner_n;
citerator_index_t subsize;
excit_index_t product = 0;
excit_index_t inner_n;
excit_index_t subsize;
for (int i = 0; i < iterator->count; i++) {
int err = citerator_pos(iterator->iterators[i], &inner_n);
for (excit_index_t i = 0; i < iterator->count; i++) {
int err = excit_pos(iterator->iterators[i], &inner_n);
if (err)
return err;
err = citerator_size(iterator->iterators[i], &subsize);
err = excit_size(iterator->iterators[i], &subsize);
if (err)
return err;
product *= subsize;
......@@ -426,17 +426,17 @@ static int product_iterator_pos(const citerator_t data, citerator_index_t *n)
return 0;
}
static inline int product_iterator_peeknext_helper(citerator_t data,
citerator_index_t *indexes,
static inline int product_iterator_peeknext_helper(excit_t data,
excit_index_t *indexes,
int next)
{
struct product_iterator_s *iterator =
(struct product_iterator_s *) data->data;
int err;
int looped;
int i;
citerator_index_t *next_indexes;
citerator_index_t offset = data->dimension;
excit_index_t i;
excit_index_t *next_indexes;
excit_index_t offset = data->dimension;
if (iterator->count == 0)
return -EINVAL;
......@@ -449,11 +449,11 @@ static inline int product_iterator_peeknext_helper(citerator_t data,
next_indexes = NULL;
if (looped)
err =
citerator_cyclic_next(iterator->iterators[i],
excit_cyclic_next(iterator->iterators[i],
next_indexes, &looped);
else
err =
citerator_peek(iterator->iterators[i],
excit_peek(iterator->iterators[i],
next_indexes);
if (err)
return err;
......@@ -464,29 +464,29 @@ static inline int product_iterator_peeknext_helper(citerator_t data,
} else
next_indexes = NULL;
if (looped)
err = citerator_next(iterator->iterators[0], next_indexes);
err = excit_next(iterator->iterators[0], next_indexes);
else
err = citerator_peek(iterator->iterators[0], next_indexes);
err = excit_peek(iterator->iterators[0], next_indexes);
if (err)
return err;
return 0;
}
static int product_iterator_peek(const citerator_t data,
citerator_index_t *indexes)
static int product_iterator_peek(const excit_t data,
excit_index_t *indexes)
{
return product_iterator_peeknext_helper(data, indexes, 0);
}
static int product_iterator_next(citerator_t data, citerator_index_t *indexes)
static int product_iterator_next(excit_t data, excit_index_t *indexes)
{
return product_iterator_peeknext_helper(data, indexes, 1);
}
static int product_iterator_split(const citerator_t data, citerator_index_t n,
citerator_t *results)
static int product_iterator_split(const excit_t data, excit_index_t n,
excit_t *results)
{
int size;
excit_index_t size;
int err = product_iterator_size(data, &size);
if (err)
......@@ -495,63 +495,63 @@ static int product_iterator_split(const citerator_t data, citerator_index_t n,
return -EDOM;
if (!results)
return 0;
citerator_t range = citerator_alloc(CITERATOR_RANGE);
excit_t range = excit_alloc(EXCIT_RANGE);
if (!range)
return -ENOMEM;
err = citerator_range_init(range, 0, size - 1, 1);
err = excit_range_init(range, 0, size - 1, 1);
if (err)
goto error1;
err = citerator_split(range, n, results);
err = excit_split(range, n, results);
if (err)
goto error1;
for (int i = 0; i < n; i++) {
citerator_t tmp, tmp2;
excit_t tmp, tmp2;
tmp = citerator_dup(data);
tmp = excit_dup(data);
if (!tmp)
goto error2;
tmp2 = results[i];
results[i] = citerator_alloc(CITERATOR_SLICE);
results[i] = excit_alloc(EXCIT_SLICE);
if (!results[i]) {
citerator_free(tmp2);
excit_free(tmp2);
goto error2;
}
err = citerator_slice_init(results[i], tmp, tmp2);
err = excit_slice_init(results[i], tmp, tmp2);
if (err) {
citerator_free(tmp2);
excit_free(tmp2);
goto error2;
}
}
citerator_free(range);
excit_free(range);
return 0;
error2:
for (int i = 0; i < n; i++)
citerator_free(results[i]);
excit_free(results[i]);
error1:
citerator_free(range);
excit_free(range);
return err;
}
int citerator_product_count(const citerator_t iterator,
citerator_index_t *count)
int excit_product_count(const excit_t iterator,
excit_index_t *count)
{
if (!iterator || iterator->type != CITERATOR_PRODUCT || !count)
if (!iterator || iterator->type != EXCIT_PRODUCT || !count)
return -EINVAL;
*count = ((struct product_iterator_s *) iterator->data)->count;
return 0;
}
int citerator_product_split_dim(const citerator_t iterator,
citerator_index_t dim, citerator_index_t n,
citerator_t *results)
int excit_product_split_dim(const excit_t iterator,
excit_index_t dim, excit_index_t n,
excit_t *results)
{
if (!iterator || iterator->type != CITERATOR_PRODUCT)