Updated Iterated function API

This commit is contained in:
Yann Weber 2020-03-30 14:49:31 +02:00
commit c9afb5d5f2
6 changed files with 42 additions and 41 deletions

View file

@ -18,8 +18,7 @@
*/
#include "rpn_if.h"
rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn,
rpn_value_t *memmap)
rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_value_t *memmap)
{
rpn_if_t *res;
size_t i;
@ -133,19 +132,7 @@ size_t rpn_if_step(rpn_if_t *rif, size_t pos)
return newpos;
}
int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns)
rpn_expr_t **rpn_if_rpn_get(rpn_if_t *rif)
{
return rpn_if_rpn_upd_rng(rif, rpns, rif->params->rpn_sz, 0);
return &(rif->rpn);
}
int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
size_t offset)
{
size_t i;
for(i=offset; i<offset+sz; i++)
{
/**@todo continue implementation */
}
return 0;
}

View file

@ -98,8 +98,7 @@ struct rpn_if_s
* @todo Delete rpn argument : rpn expressions are initialized and
* have to be compiled later
*/
rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn,
rpn_value_t *memmap);
rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_value_t *memmap);
/**@brief Deallocate an @ref rpn_if_s and its ressources and close associated
* @ref rpn_expr_s
@ -114,23 +113,12 @@ void rpn_if_free(rpn_if_t *rif);
*/
size_t rpn_if_step(rpn_if_t *rif, size_t pos);
/**@brief Update all RPN expressions
* @param rif The concerned IF
* @param rpns A list of tokenized expressions (must be of rif->rpn_sz size)
* @return 0 if no error else -1
* @note Shortcut for @ref rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
*/
int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns);
/**@brief Update a range of RPN expressions
* @param rif The concerned IF
* @param rpns A list of tokenized expressions
* @param sz Number of rpn expression in rpn argument
* @param offset Start updating expressions from this offset
* @return 0 if no error else -1
/**@brief Returns the list of RPN expression : allowing to recompile them
* @return A list of RPN expressions
* @note The memory area returned must not be freed !
*/
int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
size_t offset);
rpn_expr_t **rpn_if_rpn_get(rpn_if_t *rif);
/**@brief New @ref rpn_if_s and partial initialisation
* @param mem_sz memory size in bytes

View file

@ -60,9 +60,9 @@ void rpn_ifs_free(rpn_ifs_t *rifs)
}
}
size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight)
size_t rpn_ifs_add_if(rpn_ifs_t *rifs, unsigned int weight)
{
size_t res;
size_t res, i, first_flat;
void *tmp;
res = rifs->if_sz + 1;
@ -81,11 +81,22 @@ size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight)
rifs->weight[rifs->if_sz] = weight;
//WRONG expr ARGUMENT !!!
rifs->rpn_if[rifs->if_sz] = rpn_if_new(&(rifs->params), *exprs, rifs->mem);
rifs->rpn_if[rifs->if_sz] = rpn_if_new(&(rifs->params), rifs->mem);
if(!rifs->rpn_if[rifs->if_sz])
{
return 0;
}
first_flat = rifs->params.rpn_sz;
rifs->flat_sz += rifs->params.rpn_sz;
if(!(tmp = realloc(rifs->flat_rpn, sizeof(rpn_expr_t*) * rifs->flat_sz)))
{
return 0;
}
for(i=0; i<rifs->params.rpn_sz;i++)
{
rifs->flat_rpn[first_flat + i] = &(rifs->rpn_if[rifs->if_sz]->rpn[i]);
}
rifs->if_sz++;
if(rpn_ifs_weight_update(rifs) < 0)
@ -95,3 +106,4 @@ size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight)
}
return res;
}

View file

@ -65,6 +65,12 @@ struct rpn_ifs_s
/** @brief Stores an array of 255 pointers on IF allowing fast
* random choice */
rpn_if_t *if_proba[255];
/**@brief Stores the RPN expressions pointer of the IF contained in
* the system */
rpn_expr_t **flat_rpn;
/**Number of rpn expression in the system */
size_t flat_sz;
};
/**@brief Initialize a new IFS struct given params
@ -84,13 +90,12 @@ void rpn_ifs_free(rpn_ifs_t *rifs);
/**@brief Add a new iterated function to the system
* @param rifs The iterated function system
* @param exprs Optionnal strings representing RPN expressions for
* the new @ref rpn_if_s
* @param weight The new expression weight
* @return 0 on error, else returns the new @ref rpn_if_s index
* @note if epxrs is NULL empty RPN expressions are used
* @todo change the exprs argument when if init will be updated
*/
size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight);
size_t rpn_ifs_add_if(rpn_ifs_t *rifs, unsigned int weight);
/**@brief Delete an iterated function from the system given its index
* @param rifs The iterated function system
@ -106,6 +111,13 @@ int rpn_ifs_del_if(rpn_ifs_t *rifs, size_t if_idx);
*/
int rpn_ifs_weight_update(rpn_ifs_t *rifs);
/**@brief Returns an array of pointer on all RPN expressions contained in the
* system, allowing to recompile/update them
* @param rifs The IF system
* @returns An array of pointer on @ref rpn_expr_s
*/
rpn_expr_t **rpn_ifs_flat_rpn(rpn_ifs_t *rifs);
/**@brief Randomly choose an IF and make a step updating ifs current posisition
* @return -1 on error else 0
*/

View file

@ -69,7 +69,7 @@ int rpn_expr_reinit(rpn_expr_t* expr)
}
#endif
bzero(expr->code_map, expr->code_map_sz);
bzero(expr->stack, sizeof(unsigned long) * stack_sz);
bzero(expr->stack, sizeof(unsigned long) * expr->stack_sz);
if(_rpn_expr_init_map(expr) < 0)
{
snprintf(expr->err_reason, 128,
@ -78,6 +78,7 @@ int rpn_expr_reinit(rpn_expr_t* expr)
expr->state = RPN_ERROR;
return -1;
}
return 0;
}
int rpn_expr_compile(rpn_expr_t *expr, const char *code)

View file

@ -146,7 +146,8 @@ struct rpn_expr_s
int rpn_expr_init(rpn_expr_t* expr, const unsigned char stack_sz,
const size_t args_count);
/**@brief Reinit an existing @ref rpn_expr_s
/**@brief Reinit an existing @ref rpn_expr_s avoiding mmap and malloc
* for executable memory map and for stack
* @param expr
*/
int rpn_expr_reinit(rpn_expr_t* expr);