Commit cad06907 authored by NICOLAS DENOYELLE's avatar NICOLAS DENOYELLE

add checking in next and peek for nilpotence when rising STOPIT

parent f36f2f4d
......@@ -151,12 +151,15 @@ static int cons_it_peek(const excit_t data, ssize_t *indexes)
int n = it->n;
if (indexes) {
circular_fifo_dump(&it->fifo, indexes);
err = excit_peek(it->it, indexes + dim * (n - 1));
if (err)
return err;
circular_fifo_dump(&it->fifo, indexes);
} else
err = excit_peek(it->it, NULL);
if (err)
return err;
return EXCIT_SUCCESS;
}
......@@ -167,9 +170,11 @@ static int cons_it_next(excit_t data, ssize_t *indexes)
int dim = it->it->dimension;
int n = it->n;
if (indexes) {
circular_fifo_dump(&it->fifo, indexes);
if (indexes) {
err = excit_next(it->it, indexes + dim * (n - 1));
if (err)
return err;
circular_fifo_dump(&it->fifo, indexes);
} else
err = excit_next(it->it, NULL);
if (err)
......
#include <string.h>
#include "dev/excit.h"
#include "prod.h"
......@@ -7,6 +8,7 @@ static int prod_it_alloc(excit_t data)
it->count = 0;
it->its = NULL;
it->buff = NULL;
return EXCIT_SUCCESS;
}
......@@ -18,6 +20,7 @@ static void prod_it_free(excit_t data)
for (ssize_t i = 0; i < it->count; i++)
excit_free(it->its[i]);
free(it->its);
free(it->buff);
}
}
......@@ -29,6 +32,12 @@ static int prod_it_copy(excit_t dst, const excit_t src)
result->its = (excit_t *) malloc(it->count * sizeof(excit_t));
if (!result->its)
return -EXCIT_ENOMEM;
result->buff = (ssize_t *) malloc(src->dimension * sizeof(ssize_t));
if (!result->buff){
free(result->its);
return -EXCIT_ENOMEM;
}
ssize_t i;
for (i = 0; i < it->count; i++) {
......@@ -184,11 +193,8 @@ static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes,
return -EXCIT_EINVAL;
looped = next;
for (i = it->count - 1; i > 0; i--) {
if (indexes) {
offset -= it->its[i]->dimension;
next_indexes = indexes + offset;
} else
next_indexes = NULL;
offset -= it->its[i]->dimension;
next_indexes = it->buff + offset;
if (looped)
err = excit_cyclic_next(it->its[i], next_indexes,
&looped);
......@@ -197,17 +203,17 @@ static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes,
if (err)
return err;
}
if (indexes) {
offset -= it->its[i]->dimension;
next_indexes = indexes + offset;
} else
next_indexes = NULL;
offset -= it->its[i]->dimension;
next_indexes = it->buff + offset;
if (looped)
err = excit_next(it->its[0], next_indexes);
else
err = excit_peek(it->its[0], next_indexes);
if (err)
return err;
if(indexes)
memcpy(indexes, it->buff, data->dimension * sizeof(ssize_t));
return EXCIT_SUCCESS;
}
......@@ -288,6 +294,8 @@ int excit_product_add_copy(excit_t it, excit_t added_it)
int excit_product_add(excit_t it, excit_t added_it)
{
int err = EXCIT_SUCCESS;
if (!it || it->type != EXCIT_PRODUCT || !it->data || !added_it)
return -EXCIT_EINVAL;
......@@ -295,14 +303,30 @@ int excit_product_add(excit_t it, excit_t added_it)
ssize_t mew_count = prod_it->count + 1;
excit_t *new_its =
(excit_t *) realloc(prod_it->its, mew_count * sizeof(excit_t));
(excit_t *) realloc(prod_it->its, mew_count * sizeof(excit_t));
if (!new_its)
return -EXCIT_ENOMEM;
ssize_t *new_buff =
realloc(prod_it->buff,
(added_it->dimension + it->dimension) * sizeof(ssize_t));
if (!new_buff){
err = -EXCIT_ENOMEM;
goto exit_with_new_its;
}
prod_it->its = new_its;
prod_it->buff = new_buff;
prod_it->its[prod_it->count] = added_it;
prod_it->count = mew_count;
prod_it->count = mew_count;
it->dimension += added_it->dimension;
return EXCIT_SUCCESS;
exit_with_new_its:
free(new_its);
return err;
}
struct excit_func_table_s excit_prod_func_table = {
......
......@@ -6,6 +6,7 @@
struct prod_it_s {
ssize_t count;
ssize_t* buff;
excit_t *its;
};
......
......@@ -107,11 +107,12 @@ void test_peek(excit_t it1)
excit_dimension_test(it1, &dim1);
ssize_t *indexes1, *indexes2;
ssize_t *indexes1, *indexes2, *indexes3;
ssize_t buff_dim = dim1 * sizeof(ssize_t);
indexes1 = (ssize_t *) malloc(buff_dim);
indexes2 = (ssize_t *) malloc(buff_dim);
indexes3 = (ssize_t *) malloc(buff_dim);
while (excit_next(it1, indexes1) == ES) {
assert(excit_peek(it2, indexes2) == ES);
......@@ -119,11 +120,17 @@ void test_peek(excit_t it1)
assert(excit_next(it2, indexes2) == ES);
assert(memcmp(indexes1, indexes2, buff_dim) == 0);
}
memset(indexes2, 1, buff_dim);
memset(indexes3, 1, buff_dim);
assert(excit_peek(it2, indexes2) == EXCIT_STOPIT);
assert(! memcmp(indexes2, indexes3, buff_dim));
assert(excit_next(it2, indexes2) == EXCIT_STOPIT);
assert(! memcmp(indexes2, indexes3, buff_dim));
free(indexes1);
free(indexes2);
free(indexes3);
excit_free(it2);
}
......
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