Commit c294a1ee authored by Nicolas Denoyelle's avatar Nicolas Denoyelle

Merge branch 'master' into tleaf

parents 5be95032 4fbca5f8
......@@ -84,57 +84,6 @@ static int cons_it_size(const excit_t data, ssize_t *size)
return EXCIT_SUCCESS;
}
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;
error2:
for (; i >= 0; i--)
excit_free(results[i]);
error1:
excit_free(range);
return err;
}
static int cons_it_nth(const excit_t data, ssize_t n, ssize_t *indexes)
{
ssize_t size;
......@@ -295,7 +244,7 @@ struct excit_func_table_s excit_cons_func_table = {
cons_it_peek,
cons_it_size,
cons_it_rewind,
cons_it_split,
NULL,
cons_it_nth,
cons_it_rank,
cons_it_pos
......
......@@ -239,13 +239,58 @@ int excit_rewind(excit_t it)
int excit_split(const excit_t it, ssize_t n, excit_t *results)
{
if (!it || !it->func_table)
return -EXCIT_EINVAL;
if (n <= 0)
return -EXCIT_EDOM;
if (!it->func_table->split)
return -EXCIT_ENOTSUP;
return it->func_table->split(it, n, results);
if (!it || !it->func_table)
return -EXCIT_EINVAL;
if (n <= 0)
return -EXCIT_EDOM;
if (!it->func_table->split) {
ssize_t size;
int err = excit_size(it, &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(it);
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;
} else
return it->func_table->split(it, n, results);
}
int excit_nth(const excit_t it, ssize_t n, ssize_t *indexes)
......
......@@ -87,66 +87,66 @@ struct excit_func_table_s {
* allocation, the inner data pointer will already be set.
* Returns EXCIT_SUCCESS or an error code.
*/
int (*alloc) (excit_t it);
int (*alloc)(excit_t it);
/*
* This function is called during excit_free. After this function is
* called the iterator and the data will be freed.
*/
void (*free) (excit_t it);
void (*free)(excit_t it);
/*
* This funciton is called during excit_dup. It is responsible for
* duplicating the content of the inner data between src_it and dst_it.
* Returns EXCIT_SUCCESS or an error code.
*/
int (*copy) (excit_t dst_it, const excit_t src_it);
int (*copy)(excit_t dst_it, const excit_t src_it);
/*
* This function is responsible for implementing the next functionality
* of the iterator.
* Returns EXCIT_SUCCESS, EXCIT_STOPIT or an error code.
*/
int (*next) (excit_t it, ssize_t * indexes);
int (*next)(excit_t it, ssize_t *indexes);
/*
* This function is responsible for implementing the peek functionality
* of the iterator.
* Returns EXCIT_SUCCESS, EXCIT_STOPIT or an error code.
*/
int (*peek) (const excit_t it, ssize_t * indexes);
int (*peek)(const excit_t it, ssize_t *indexes);
/*
* This function is responsible for implementing the size functionality
* of the iterator.
* Returns EXCIT_SUCCESS or an error code.
*/
int (*size) (const excit_t it, ssize_t * size);
int (*size)(const excit_t it, ssize_t *size);
/*
* This function is responsible for implementing the rewind
* functionality of the iterator.
* Returns EXCIT_SUCCESS or an error code.
*/
int (*rewind) (excit_t it);
int (*rewind)(excit_t it);
/*
* This function is responsible for implementing the split
* functionality of the iterator.
* Returns EXCIT_SUCCESS or an error code.
*/
int (*split) (const excit_t it, ssize_t n, excit_t * results);
int (*split)(const excit_t it, ssize_t n, excit_t *results);
/*
* This function is responsible for implementing the nth functionality
* of the iterator.
* Returns EXCIT_SUCCESS or an error code.
*/
int (*nth) (const excit_t it, ssize_t n, ssize_t * indexes);
int (*nth)(const excit_t it, ssize_t n, ssize_t *indexes);
/*
* This function is responsible for implementing the rank functionality
* of the iterator.
* Returns EXCIT_SUCCESS or an error code.
*/
int (*rank) (const excit_t it, const ssize_t * indexes, ssize_t * n);
int (*rank)(const excit_t it, const ssize_t *indexes, ssize_t *n);
/*
* This function is responsible for implementing the pos functionality
* of the iterator.
* Returns EXCIT_SUCCESS, EXCIT_STOPIT or an error code.
*/
int (*pos) (const excit_t it, ssize_t * n);
int (*pos)(const excit_t it, ssize_t *n);
};
/*
......@@ -213,7 +213,7 @@ int excit_type(excit_t it, enum excit_type_e *type);
* "dimension": a pointer where the result will be written.
* Returns EXCIT_SUCCESS or an error code.
*/
int excit_dimension(excit_t it, ssize_t * dimension);
int excit_dimension(excit_t it, ssize_t *dimension);
/*
* Gets the current element of an iterator and increments it.
......@@ -223,7 +223,7 @@ int excit_dimension(excit_t it, ssize_t * dimension);
* Returns EXCIT_SUCCESS if a valid element was retured or EXCIT_STOPIT if the
* iterator is depleted or an error code.
*/
int excit_next(excit_t it, ssize_t * indexes);
int excit_next(excit_t it, ssize_t *indexes);
/*
* Gets the current element of an iterator.
......@@ -233,7 +233,7 @@ int excit_next(excit_t it, ssize_t * indexes);
* Returns EXCIT_SUCCESS if a valid element was retured or EXCIT_STOPIT if the
* iterator is depleted or an error code.
*/
int excit_peek(const excit_t it, ssize_t * indexes);
int excit_peek(const excit_t it, ssize_t *indexes);
/*
* Rewinds an iterator to its initial state.
......@@ -248,7 +248,7 @@ int excit_rewind(excit_t it);
* "size": an pointer to the variable where the result will be stored.
* Returns EXCIT_SUCCESS or an error code.
*/
int excit_size(const excit_t it, ssize_t * size);
int excit_size(const excit_t it, ssize_t *size);
/*
* Splits an iterator envenly into several suub iterators.
......@@ -259,7 +259,7 @@ int excit_size(const excit_t it, ssize_t * size);
* Returns EXCIT_SUCCESS, -EXCIT_EDOM if the source iterator is too small to be
* subdivised in the wanted number or an error code.
*/
int excit_split(const excit_t it, ssize_t n, excit_t * results);
int excit_split(const excit_t it, ssize_t n, excit_t *results);
/*
* Gets the nth element of an iterator.
......@@ -270,7 +270,7 @@ int excit_split(const excit_t it, ssize_t n, excit_t * results);
* iterator, no results is returned if NULL.
* Returns EXCIT_SUCCESS or an error code.
*/
int excit_nth(const excit_t it, ssize_t rank, ssize_t * indexes);
int excit_nth(const excit_t it, ssize_t rank, ssize_t *indexes);
/*
* Gets the rank of an element of an iterator.
......@@ -280,7 +280,7 @@ int excit_nth(const excit_t it, ssize_t rank, ssize_t * indexes);
* returned if NULL.
* Returns EXCIT_SUCCESS or an error code.
*/
int excit_rank(const excit_t it, const ssize_t * indexes, ssize_t * rank);
int excit_rank(const excit_t it, const ssize_t *indexes, ssize_t *rank);
/*
* Gets the position of the iterator.
......@@ -290,7 +290,7 @@ int excit_rank(const excit_t it, const ssize_t * indexes, ssize_t * rank);
* Returns EXCIT_SUCCESS or EXCIT_STOPIT if the iterator is depleted or an error
* code.
*/
int excit_pos(const excit_t it, ssize_t * rank);
int excit_pos(const excit_t it, ssize_t *rank);
/*
* Increments the iterator.
......@@ -308,7 +308,7 @@ int excit_skip(excit_t it);
* iterator, no results is returned if NULL.
* Returns EXCIT_SUCCESS or an error code.
*/
int excit_cyclic_next(excit_t it, ssize_t * indexes, int *looped);
int excit_cyclic_next(excit_t it, ssize_t *indexes, int *looped);
/*
* Initializes a range iterator to iterate from first to last (included) by sep.
......@@ -339,6 +339,17 @@ int excit_cons_init(excit_t it, excit_t src, ssize_t n);
*/
int excit_repeat_init(excit_t it, excit_t src, ssize_t n);
/*
* Splits a repeat iterator between repetitions.
* "it": a product iterator.
* "n": number of iterators desired.
* "results": an array of at least n excit_t, or NULL in which case no iterator
* is created.
* Returns EXCIT_SUCCESS, -EXCIT_EDOM if the selected iterator is too small to
* be subdivised in the wanted number or an error code.
*/
int excit_repeat_split(const excit_t it, ssize_t n, excit_t *results);
/*
* Creates a two dimensional Hilbert space-filling curve iterator.
* "it": an hilbert2d iterator.
......@@ -370,7 +381,7 @@ int excit_product_add_copy(excit_t it, excit_t added_it);
* "count": a pointer to a variable where the result will be stored.
* Returns EXCIT_SUCCESS or an error code.
*/
int excit_product_count(const excit_t it, ssize_t * count);
int excit_product_count(const excit_t it, ssize_t *count);
/*
* Splits a product iterator along the nth iterator.
......@@ -384,7 +395,7 @@ int excit_product_count(const excit_t it, ssize_t * count);
* be subdivised in the wanted number or an error code.
*/
int excit_product_split_dim(const excit_t it, ssize_t dim, ssize_t n,
excit_t * results);
excit_t *results);
/*
* Initializes a slice iterator by giving asrc iterator and an indexer iterator.
......
......@@ -221,55 +221,6 @@ static int prod_it_next(excit_t data, ssize_t *indexes)
return prod_it_peeknext_helper(data, indexes, 1);
}
static int prod_it_split(const excit_t data, ssize_t n, excit_t *results)
{
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;
}
int excit_product_count(const excit_t it, ssize_t *count)
{
if (!it || it->type != EXCIT_PRODUCT || !count)
......@@ -362,7 +313,7 @@ struct excit_func_table_s excit_prod_func_table = {
prod_it_peek,
prod_it_size,
prod_it_rewind,
prod_it_split,
NULL,
prod_it_nth,
prod_it_rank,
prod_it_pos
......
......@@ -96,7 +96,34 @@ static int repeat_it_pos(const excit_t data, ssize_t *n)
return EXCIT_SUCCESS;
}
static int repeat_it_split(const excit_t data, ssize_t n, excit_t *results)
struct excit_func_table_s excit_repeat_func_table = {
repeat_it_alloc,
repeat_it_free,
repeat_it_copy,
repeat_it_next,
repeat_it_peek,
repeat_it_size,
repeat_it_rewind,
NULL,
repeat_it_nth,
NULL,
repeat_it_pos
};
int excit_repeat_init(excit_t it, excit_t src, ssize_t n)
{
if (!it || it->type != EXCIT_REPEAT || !src || n <= 0)
return -EXCIT_EINVAL;
struct repeat_it_s *repeat_it = (struct repeat_it_s *)it->data;
excit_free(repeat_it->it);
it->dimension = src->dimension;
repeat_it->it = src;
repeat_it->n = n;
repeat_it->counter = 0;
return EXCIT_SUCCESS;
}
int excit_repeat_split(const excit_t data, ssize_t n, excit_t *results)
{
const struct repeat_it_s *it = (const struct repeat_it_s *)data->data;
int err = excit_split(it->it, n, results);
......@@ -125,31 +152,3 @@ error:
excit_free(results[i]);
return err;
}
struct excit_func_table_s excit_repeat_func_table = {
repeat_it_alloc,
repeat_it_free,
repeat_it_copy,
repeat_it_next,
repeat_it_peek,
repeat_it_size,
repeat_it_rewind,
repeat_it_split,
repeat_it_nth,
NULL,
repeat_it_pos
};
int excit_repeat_init(excit_t it, excit_t src, ssize_t n)
{
if (!it || it->type != EXCIT_REPEAT || !src || n <= 0)
return -EXCIT_EINVAL;
struct repeat_it_s *repeat_it = (struct repeat_it_s *)it->data;
excit_free(repeat_it->it);
it->dimension = src->dimension;
repeat_it->it = src;
repeat_it->n = n;
repeat_it->counter = 0;
return EXCIT_SUCCESS;
}
......@@ -447,7 +447,7 @@ void test_repeat_iterator(void)
assert(excit_peek(it, indexes) == EXCIT_STOPIT);
assert(excit_next(it, indexes) == EXCIT_STOPIT);
assert(excit_split(it, 2, its) == ES);
assert(excit_repeat_split(it, 2, its) == ES);
for (int i = 0; i <= 1; i++) {
for (int j = 0; j < 2; j++) {
assert(excit_peek(its[0], indexes) == ES);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment