Commit 17132c39 authored by Brice Videau's avatar Brice Videau

Normalized split functions.

parent d1db2a56
......@@ -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,8 +239,53 @@ int excit_split(const excit_t it, ssize_t n, excit_t *results)
return -EXCIT_EINVAL;
if (n <= 0)
return -EXCIT_EDOM;
if (!it->func_table->split)
return -EXCIT_ENOTSUP;
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);
}
......
......@@ -331,6 +331,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.
......
......@@ -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,30 +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;
}
......@@ -372,7 +372,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