Fast IFS using RPN notation
python
c
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpn_ifs.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2020 Weber Yann
  3. *
  4. * This file is part of pyrpn.
  5. *
  6. * pyrpn is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * pyrpn is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __rpn_ifs__h__
  20. #define __rpn_ifs__h__
  21. #include "config.h"
  22. #include <string.h>
  23. #include "rpn_jit.h"
  24. #include "rpn_if.h"
  25. /**@defgroup ifs Iterated function system
  26. * @brief IFS implementation with RPN expressions
  27. *
  28. * IFS are basically a list of @ref ifs_if associated with a probability
  29. * of evaluation.
  30. *
  31. * This implementation aims to :
  32. * - optimize @ref ifs_if calls
  33. * - optimize random number generation and IF random calls
  34. *
  35. * @note It aims to provide an API close to @ref ifs_if API, in order to
  36. * be able to use both IFS and IF almost tansparently via Python API.
  37. */
  38. /**@brief struct @ref rpn_ifs_s shortcut */
  39. typedef struct rpn_ifs_s rpn_ifs_t;
  40. /**@brief Stores IFS informations */
  41. struct rpn_ifs_s
  42. {
  43. /**@brief Parameters shared by all IF of the system */
  44. rpn_if_param_t params;
  45. /**@brief Memory map shared by all iterated functions */
  46. rpn_value_t *mem;
  47. /**@brief If 1 the mem has to be munmap at free time */
  48. short self_mem;
  49. /**@brief Pointers on iterated function structures */
  50. rpn_if_t **rpn_if;
  51. /**@brief Number of iterated functions in the system */
  52. size_t if_sz;
  53. /**@brief Current position in memory map */
  54. size_t pos;
  55. /**@brief Stores the original chance of choosing corresponding IF */
  56. unsigned int *weight;
  57. /** @brief Stores an array of 255 pointers on IF allowing fast
  58. * random choice. Last ptr can be NULL*/
  59. rpn_if_t *if_proba[255];
  60. /**@brief Stores the RPN expressions pointer of the IF contained in
  61. * the system */
  62. rpn_expr_t **flat_rpn;
  63. /**Number of rpn expression in the system */
  64. size_t flat_sz;
  65. };
  66. /**@brief Initialize a new IFS struct given params
  67. * @param params Individual, but shared, parameters for iteraed functions
  68. * in the system
  69. * @param memmap A suitable memory map or NULL
  70. * @return NULL if error and set errno
  71. * @note Initialized system is empty. Use @ref rpn_ifs_add_if to add an iterated
  72. * function to the system
  73. */
  74. rpn_ifs_t* rpn_ifs_new(rpn_if_param_t *params, rpn_value_t *memmap);
  75. /**@brief Free ressources of an iterated function system
  76. * @param rifs The iterated function system to free
  77. */
  78. void rpn_ifs_free(rpn_ifs_t *rifs);
  79. /**@brief Add a new iterated function to the system
  80. * @param rifs The iterated function system
  81. * @param weight The new expression weight
  82. * @return 0 on error, else returns the new @ref rpn_if_s index
  83. * @note if epxrs is NULL empty RPN expressions are used
  84. * @todo change the exprs argument when if init will be updated
  85. */
  86. size_t rpn_ifs_add_if(rpn_ifs_t *rifs, unsigned int weight);
  87. /**@brief Delete an iterated function from the system given its index
  88. * @param rifs The iterated function system
  89. * @param if_idx The iterated function index in the system
  90. * @return -1 if error else 0
  91. * @note Update sizes but do not calls realloc
  92. */
  93. int rpn_ifs_del_if(rpn_ifs_t *rifs, size_t if_idx);
  94. /**@brief Call IFS n times
  95. * @note Make n random choices and call corresponding IF
  96. * @param rifs The iterated function system
  97. * @param n consecutive IFS calls
  98. * @return 1 if error else 0
  99. */
  100. int rpn_ifs_run(rpn_ifs_t *rifs, size_t n);
  101. /**@brief Updates the @ref rpn_ifs_s.if_proba array using
  102. * @ref rpn_ifs_s.if_proba values
  103. * @param rifs The iterated function system
  104. * @return -1 if error else 0
  105. */
  106. int rpn_ifs_weight_update(rpn_ifs_t *rifs);
  107. /**@brief Returns an array of pointer on all RPN expressions contained in the
  108. * system, allowing to recompile/update them
  109. * @param rifs The IF system
  110. * @returns An array of pointer on @ref rpn_expr_s
  111. */
  112. rpn_expr_t **rpn_ifs_flat_rpn(rpn_ifs_t *rifs);
  113. /**@brief Randomly choose an IF and make a step updating ifs current posisition
  114. * @return -1 on error else 0
  115. */
  116. int rpn_ifs_step(rpn_ifs_t *rifs);
  117. #endif