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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "rpn_jit.h"
  23. #include "rpn_if.h"
  24. /**@defgroup ifs Iterated function system
  25. * @brief IFS implementation with RPN expressions
  26. *
  27. * IFS are basically a list of @ref ifs_if associated with a probability
  28. * of evaluation.
  29. *
  30. * This implementation aims to :
  31. * - optimize @ref ifs_if calls
  32. * - optimize random number generation and IF random calls
  33. *
  34. * @note It aims to provide an API close to @ref ifs_if API, in order to
  35. * be able to use both IFS and IF almost tansparently via Python API.
  36. */
  37. /**@brief struct @ref rpn_ifs_s shortcut */
  38. typedef struct rpn_ifs_s rpn_ifs_t;
  39. /**@brief Stores IFS informations */
  40. struct rpn_ifs_s
  41. {
  42. /**@brief Parameters shared by all IF of the system */
  43. rpn_if_param_t params;
  44. /**@brief Memory map shared by all iterated functions */
  45. rpn_value_t *mem;
  46. /**@brief If 1 the mem has to be munmap at free time */
  47. short self_mem;
  48. /**@brief Pointers on iterated function structures */
  49. rpn_if_t **rpn_if;
  50. /**@brief Number of iterated functions in the system */
  51. size_t if_sz;
  52. /**@brief Current position in memory map */
  53. size_t pos;
  54. /**@brief Stores the original chance of choosing corresponding IF */
  55. unsigned int *weight;
  56. /** @brief Stores an array of 255 pointers on IF allowing fast
  57. * random choice */
  58. rpn_if_t *if_proba[255];
  59. /**@brief Stores the RPN expressions pointer of the IF contained in
  60. * the system */
  61. rpn_expr_t **flat_rpn;
  62. /**Number of rpn expression in the system */
  63. size_t flat_sz;
  64. };
  65. /**@brief Initialize a new IFS struct given params
  66. * @param params Individual, but shared, parameters for iteraed functions
  67. * in the system
  68. * @param memmap A suitable memory map or NULL
  69. * @return NULL if error and set errno
  70. * @note Initialized system is empty. Use @ref rpn_ifs_add_if to add an iterated
  71. * function to the system
  72. */
  73. rpn_ifs_t* rpn_ifs_new(rpn_if_param_t *params, rpn_value_t *memmap);
  74. /**@brief Free ressources of an iterated function system
  75. * @param rifs The iterated function system to free
  76. */
  77. void rpn_ifs_free(rpn_ifs_t *rifs);
  78. /**@brief Add a new iterated function to the system
  79. * @param rifs The iterated function system
  80. * @param weight The new expression weight
  81. * @return 0 on error, else returns the new @ref rpn_if_s index
  82. * @note if epxrs is NULL empty RPN expressions are used
  83. * @todo change the exprs argument when if init will be updated
  84. */
  85. size_t rpn_ifs_add_if(rpn_ifs_t *rifs, unsigned int weight);
  86. /**@brief Delete an iterated function from the system given its index
  87. * @param rifs The iterated function system
  88. * @param if_idx The iterated function index in the system
  89. * @return -1 if error else 0
  90. */
  91. int rpn_ifs_del_if(rpn_ifs_t *rifs, size_t if_idx);
  92. /**@brief Updates the @ref rpn_ifs_s.if_proba array using
  93. * @ref rpn_ifs_s.if_proba values
  94. * @param rifs The iterated function system
  95. * @return -1 if error else 0
  96. */
  97. int rpn_ifs_weight_update(rpn_ifs_t *rifs);
  98. /**@brief Returns an array of pointer on all RPN expressions contained in the
  99. * system, allowing to recompile/update them
  100. * @param rifs The IF system
  101. * @returns An array of pointer on @ref rpn_expr_s
  102. */
  103. rpn_expr_t **rpn_ifs_flat_rpn(rpn_ifs_t *rifs);
  104. /**@brief Randomly choose an IF and make a step updating ifs current posisition
  105. * @return -1 on error else 0
  106. */
  107. int rpn_ifs_step(rpn_ifs_t *rifs);
  108. #endif