PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/unuran-1.8.0/src/parser/stringparser_lists.ch.in

#
Autoconf | 304 lines | 253 code | 47 blank | 4 comment | 58 complexity | d5f8461693a30ae7dda50d48e9c8585f MD5 | raw file
Possible License(s): GPL-2.0
  1. /*****************************************************************************
  2. * *
  3. * UNURAN -- Universal Non-Uniform Random number generator *
  4. * *
  5. *****************************************************************************
  6. * *
  7. * FILE: stringparser_lists.ch.in / stringparser_lists.ch *
  8. * *
  9. * *
  10. * DESCRIPTION: *
  11. * *
  12. * Switch lists for string parser. *
  13. * (See file stringparser.c) *
  14. * *
  15. *****************************************************************************
  16. * *
  17. * This file is parsed by the perl script make_stringparser.pl which *
  18. * replaces the *
  19. * =INPUT keyword *
  20. * tags with search lists created from the information found within the *
  21. * header files of the source code. *
  22. * These lists (implemented via 'if' rules together with switch lists to *
  23. * with the first letter of the set calls as simple hash function) are *
  24. * used to call the corresponding ..._set and ..._new calls for the *
  25. * the keywords found in the string. *
  26. * *
  27. *****************************************************************************
  28. * *
  29. * Copyright (c) 2000-2006 Wolfgang Hoermann and Josef Leydold *
  30. * Department of Statistics and Mathematics, WU Wien, Austria *
  31. * *
  32. * This program is free software; you can redistribute it and/or modify *
  33. * it under the terms of the GNU General Public License as published by *
  34. * the Free Software Foundation; either version 2 of the License, or *
  35. * (at your option) any later version. *
  36. * *
  37. * This program is distributed in the hope that it will be useful, *
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  40. * GNU General Public License for more details. *
  41. * *
  42. * You should have received a copy of the GNU General Public License *
  43. * along with this program; if not, write to the *
  44. * Free Software Foundation, Inc., *
  45. * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA *
  46. * *
  47. *****************************************************************************/
  48. /*****************************************************************************/
  49. /** Distributions **/
  50. /*****************************************************************************/
  51. struct unur_distr *
  52. _unur_str_distr_new( char *distribution )
  53. /*----------------------------------------------------------------------*/
  54. /* get new distribution object */
  55. /* */
  56. /* parameters: */
  57. /* distribution ... string that contains distribution name */
  58. /* params ... string that contains list of parameters */
  59. /* */
  60. /* return: */
  61. /* distribution object (pointer to structure) */
  62. /* */
  63. /* error: */
  64. /* return NULL */
  65. /*----------------------------------------------------------------------*/
  66. {
  67. struct unur_distr *distr = NULL; /* pointer to distribution object */
  68. char distr_unknown;
  69. char *name; /* pointer to name of distribution */
  70. char *params; /* pointer to parameter list of distr */
  71. double *darray = NULL; /* array of arguments for distribution */
  72. int n_darray = 0; /* size of array of parameters */
  73. /* name of distribution */
  74. name = distribution;
  75. /* get parameter list */
  76. params = strchr(distribution,'(');
  77. if (params != NULL) {
  78. *params = '\0'; /* terminate key string */
  79. ++params; /* set pointer to value string */
  80. }
  81. /* get parameter list */
  82. n_darray = _unur_parse_dlist(params, &darray );
  83. #ifdef UNUR_ENABLE_LOGGING
  84. /* write info into LOG file */
  85. if (_unur_default_debugflag & UNUR_DEBUG_SETUP)
  86. _unur_str_debug_distr(1,name,darray,n_darray);
  87. #endif
  88. /* mark distribution as unknown (this is a very ugly hack) */
  89. distr = (struct unur_distr *) &distr_unknown;
  90. =INPUT list_of_distributions
  91. if (distr == (struct unur_distr *) &distr_unknown) {
  92. /* unknown method */
  93. _unur_error_unknown(distribution,"distribution");
  94. distr = NULL;
  95. }
  96. else if (distr == NULL) {
  97. /* invalid data for chosen method */
  98. _unur_error_invalid(distribution,"distribution");
  99. }
  100. /* clear memory */
  101. if (darray) free(darray);
  102. /* return result */
  103. return distr;
  104. } /* end of _unur_str_distr_new() */
  105. /*---------------------------------------------------------------------------*/
  106. int
  107. _unur_str_distr_set( UNUR_DISTR **ptr_distr, const char *key, char *value )
  108. /*----------------------------------------------------------------------*/
  109. /* set parameters for distribution */
  110. /* */
  111. /* it also makes a distribution object for an order statistics for */
  112. /* the given distribution when the key word "orderstatistics" occurs. */
  113. /* Thus the distribution object itself might be changed. */
  114. /* */
  115. /* parameters: */
  116. /* ptr_distr ... holds pointer to distribution object */
  117. /* key ... string that contains key for parameter */
  118. /* value ... string that contains list of arguments for parameter */
  119. /* */
  120. /* return: */
  121. /* UNUR_SUCCESS ... on success */
  122. /* error code ... on error */
  123. /* */
  124. /* error: */
  125. /* return error code */
  126. /*----------------------------------------------------------------------*/
  127. {
  128. int result; /* result of UNU.RAN set call */
  129. /* derefence pointer to distribution object */
  130. struct unur_distr *distr = *ptr_distr;
  131. /* storing arguments of set calls: */
  132. char type_args[MAX_SET_ARGS+1]; /* array containing info about arguments */
  133. char *args[MAX_SET_ARGS+1]; /* pointers to argument strings */
  134. /* tokenize argument string */
  135. if (_unur_str_set_args( value, type_args, args, MAX_SET_ARGS ) < 0) {
  136. /* error */
  137. return UNUR_ERR_STR_SYNTAX;
  138. }
  139. /* set result indicator to unknown */
  140. result = UNUR_ERR_STR_UNKNOWN;
  141. /* find and execute set call */
  142. =INPUT list_of_distr_sets
  143. /* special keyword */
  144. if (result == UNUR_ERR_STR_UNKNOWN)
  145. if (distr->type == UNUR_DISTR_CONT) {
  146. if ( !strcmp(key, "orderstatistics") ) {
  147. /* make order statistics and replace distribution object */
  148. *ptr_distr = _unur_str_distr_make_os (distr, key, type_args, args);
  149. result = (*ptr_distr == NULL) ? UNUR_ERR_STR_SYNTAX : UNUR_SUCCESS;
  150. }
  151. }
  152. if (result == UNUR_ERR_STR_UNKNOWN) {
  153. /* unknown parameter */
  154. _unur_error_unknown(key,"parameter for given distribution");
  155. return UNUR_ERR_STR_UNKNOWN;
  156. }
  157. else if (result != UNUR_SUCCESS) {
  158. /* invalid data for parameter */
  159. _unur_error_invalid(key,"set call");
  160. return UNUR_ERR_STR_SYNTAX;
  161. }
  162. return UNUR_SUCCESS;
  163. } /* end of _unur_str_distr_set() */
  164. /*---------------------------------------------------------------------------*/
  165. /*****************************************************************************/
  166. /** Methods **/
  167. /*****************************************************************************/
  168. /*---------------------------------------------------------------------------*/
  169. struct unur_par *
  170. _unur_str_par_new( const char *method, const UNUR_DISTR *distr )
  171. /*----------------------------------------------------------------------*/
  172. /* get new parameter object for method */
  173. /* */
  174. /* parameters: */
  175. /* method ... string that contains method name */
  176. /* distr ... pointer to distribution object */
  177. /* */
  178. /* return: */
  179. /* default parameters (pointer to structure) */
  180. /* */
  181. /* error: */
  182. /* return NULL */
  183. /*----------------------------------------------------------------------*/
  184. {
  185. struct unur_par *par = NULL;
  186. char method_unknown;
  187. #ifdef UNUR_ENABLE_LOGGING
  188. /* write info into LOG file */
  189. if (_unur_default_debugflag & UNUR_DEBUG_SETUP)
  190. _unur_str_debug_string(1,"method",method);
  191. #endif
  192. /* mark method as unknown (this is a very ugly hack) */
  193. par = (struct unur_par *) &method_unknown;
  194. =INPUT list_of_methods
  195. if (par == (struct unur_par *) &method_unknown) {
  196. /* unknown method */
  197. _unur_error_unknown(method,"method");
  198. par = NULL;
  199. }
  200. else if (par == NULL) {
  201. /* invalid data for chosen method */
  202. _unur_error_invalid(method,"method");
  203. }
  204. return par;
  205. } /* end of _unur_str_par_new() */
  206. /*---------------------------------------------------------------------------*/
  207. int
  208. _unur_str_par_set( UNUR_PAR *par, const char *key, char *value, struct unur_slist *mlist )
  209. /*----------------------------------------------------------------------*/
  210. /* set parameters for method */
  211. /* */
  212. /* parameters: */
  213. /* par ... pointer to parameter for building generator object */
  214. /* key ... string that contains key for parameter */
  215. /* value ... string that contains list of arguments for parameter */
  216. /* mlist ... list of allocated memory blocks */
  217. /* */
  218. /* return: */
  219. /* UNUR_SUCCESS ... on success */
  220. /* error code ... on error */
  221. /*----------------------------------------------------------------------*/
  222. {
  223. int result = 0; /* result of UNU.RAN set call (0 or 1) */
  224. /* storing arguments of set calls: */
  225. char type_args[MAX_SET_ARGS+1]; /* array containing info about arguments */
  226. char *args[MAX_SET_ARGS+1]; /* pointers to argument strings */
  227. /* tokenize argument string */
  228. if (_unur_str_set_args( value, type_args, args, MAX_SET_ARGS ) < 0) {
  229. /* error */
  230. return UNUR_ERR_STR_SYNTAX;
  231. }
  232. /* set result indicator to unknown */
  233. result = UNUR_ERR_STR_UNKNOWN;
  234. /* find and execute set call */
  235. =INPUT list_of_par_sets
  236. if (result == UNUR_ERR_STR_UNKNOWN) {
  237. /* no valid parameter found */
  238. /* try extra keywords */
  239. if ( !strcmp(key, "debug") ) {
  240. /* n = 1; type = u: UNUR_PAR *parameters, unsigned DEBUG */
  241. result = _unur_str_par_set_u(par,key,type_args,args,unur_set_debug);
  242. }
  243. }
  244. /* check result */
  245. if (result == UNUR_ERR_STR_UNKNOWN) {
  246. /* unknown parameter */
  247. _unur_error_unknown(key,"parameter for given method");
  248. return UNUR_ERR_STR_UNKNOWN;
  249. }
  250. else if (result != UNUR_SUCCESS) {
  251. /* invalid data for parameter */
  252. _unur_error_invalid(key,"set call");
  253. return UNUR_ERR_STR_SYNTAX;
  254. }
  255. return UNUR_SUCCESS;
  256. } /* end of _unur_str_par_set() */
  257. /*---------------------------------------------------------------------------*/