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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. };
  60. /**@brief Initialize a new IFS struct given params
  61. * @param params Individual, but shared, parameters for iteraed functions
  62. * in the system
  63. * @param memmap A suitable memory map or NULL
  64. * @return NULL if error and set errno
  65. * @note Initialized system is empty. Use @ref rpn_ifs_add_if to add an iterated
  66. * function to the system
  67. */
  68. rpn_ifs_t* rpn_ifs_new(rpn_if_param_t *params, rpn_value_t *memmap);
  69. /**@brief Free ressources of an iterated function system
  70. * @param rifs The iterated function system to free
  71. */
  72. void rpn_ifs_free(rpn_ifs_t *rifs);
  73. /**@brief Add a new iterated function to the system
  74. * @param rifs The iterated function system
  75. * @param exprs Optionnal strings representing RPN expressions for
  76. * the new @ref rpn_if_s
  77. * @return 0 on error, else returns the new @ref rpn_if_s index
  78. * @note if epxrs is NULL empty RPN expressions are used
  79. * @todo change the exprs argument when if init will be updated
  80. */
  81. size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight);
  82. /**@brief Delete an iterated function from the system given its index
  83. * @param rifs The iterated function system
  84. * @param if_idx The iterated function index in the system
  85. * @return -1 if error else 0
  86. */
  87. int rpn_ifs_del_if(rpn_ifs_t *rifs, size_t if_idx);
  88. /**@brief Updates the @ref rpn_ifs_s.if_proba array using
  89. * @ref rpn_ifs_s.if_proba values
  90. * @param rifs The iterated function system
  91. * @return -1 if error else 0
  92. */
  93. int rpn_ifs_weight_update(rpn_ifs_t *rifs);
  94. /**@brief Randomly choose an IF and make a step updating ifs current posisition
  95. * @return -1 on error else 0
  96. */
  97. int rpn_ifs_step(rpn_ifs_t *rifs);
  98. #endif