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.

269 lines
8.7 KiB

  1. /**
  2. @file
  3. @ingroup st
  4. @brief Symbol table package.
  5. @details The st library provides functions to create, maintain,
  6. and query symbol tables.
  7. @copyright@parblock
  8. Copyright (c) 1994-1998 The Regents of the Univ. of California.
  9. All rights reserved.
  10. Permission is hereby granted, without written agreement and without license
  11. or royalty fees, to use, copy, modify, and distribute this software and its
  12. documentation for any purpose, provided that the above copyright notice and
  13. the following two paragraphs appear in all copies of this software.
  14. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  15. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  16. OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  17. CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  19. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  21. "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  22. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. @endparblock
  24. @copyright@parblock
  25. Copyright (c) 1999-2015, Regents of the University of Colorado
  26. All rights reserved.
  27. Redistribution and use in source and binary forms, with or without
  28. modification, are permitted provided that the following conditions
  29. are met:
  30. Redistributions of source code must retain the above copyright
  31. notice, this list of conditions and the following disclaimer.
  32. Redistributions in binary form must reproduce the above copyright
  33. notice, this list of conditions and the following disclaimer in the
  34. documentation and/or other materials provided with the distribution.
  35. Neither the name of the University of Colorado nor the names of its
  36. contributors may be used to endorse or promote products derived from
  37. this software without specific prior written permission.
  38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  41. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  42. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  43. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  44. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  48. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  49. POSSIBILITY OF SUCH DAMAGE.
  50. @endparblock
  51. */
  52. #ifndef ST_H_
  53. #define ST_H_
  54. /*---------------------------------------------------------------------------*/
  55. /* Nested includes */
  56. /*---------------------------------------------------------------------------*/
  57. /*---------------------------------------------------------------------------*/
  58. /* Constant declarations */
  59. /*---------------------------------------------------------------------------*/
  60. /**
  61. * @brief Value returned if memory is exhausted.
  62. */
  63. #define ST_OUT_OF_MEM -10000
  64. /**
  65. * @brief Default value for the maximum table density.
  66. * @see st_init_table_with_params
  67. */
  68. #define ST_DEFAULT_MAX_DENSITY 5
  69. /**
  70. * @brief Default value for the initial table size.
  71. * @see st_init_table_with_params
  72. */
  73. #define ST_DEFAULT_INIT_TABLE_SIZE 11
  74. /**
  75. * @brief Default table growth factor.
  76. * @see st_init_table_with_params
  77. */
  78. #define ST_DEFAULT_GROW_FACTOR 2.0
  79. /**
  80. * @brief Default table reorder flag.
  81. * @see st_init_table_with_params
  82. */
  83. #define ST_DEFAULT_REORDER_FLAG 0
  84. /*---------------------------------------------------------------------------*/
  85. /* Stucture declarations */
  86. /*---------------------------------------------------------------------------*/
  87. /*---------------------------------------------------------------------------*/
  88. /* Type declarations */
  89. /*---------------------------------------------------------------------------*/
  90. /**
  91. * @brief Type of symbol tables.
  92. */
  93. typedef struct st_table st_table;
  94. /**
  95. * @brief Type of symbol table generators.
  96. */
  97. typedef struct st_generator st_generator;
  98. /**
  99. * @brief Type of return values for iterators.
  100. */
  101. enum st_retval {ST_CONTINUE, ST_STOP, ST_DELETE};
  102. /**
  103. * @brief Type for function passed to @ref st_foreach.
  104. */
  105. typedef enum st_retval (*st_foreach_t)(void *, void *, void *);
  106. /**
  107. * @brief Type of comparison functions.
  108. */
  109. typedef int (*st_compare_t)(void const *, void const *);
  110. /**
  111. * @brief Type of hash functions.
  112. */
  113. typedef int (*st_hash_t)(void const *, int);
  114. /**
  115. * @brief Type of comparison functions with extra argument.
  116. */
  117. typedef int (*st_compare_arg_t)(void const *, void const *, void const *);
  118. /**
  119. * @brief Type of hash functions with extra argument.
  120. */
  121. typedef int (*st_hash_arg_t)(void const *, int, void const *);
  122. /*---------------------------------------------------------------------------*/
  123. /* Variable declarations */
  124. /*---------------------------------------------------------------------------*/
  125. /*---------------------------------------------------------------------------*/
  126. /* Macro declarations */
  127. /*---------------------------------------------------------------------------*/
  128. /**
  129. @brief Checks whethere `key` is in `table`.
  130. @details Returns 1 if there is an entry under `key` in `table`, 0
  131. otherwise.
  132. @sideeffect None
  133. @see st_lookup
  134. */
  135. #define st_is_member(table,key) st_lookup(table,key,(void **) 0)
  136. /**
  137. @brief Iteration macro.
  138. @details
  139. An iteration macro which loops over all the entries in
  140. `table`, setting `key` to point to the key and `value` to the
  141. associated value (if it is not nil). `gen` is a generator variable
  142. used internally. Sample usage:
  143. void *key, *value;
  144. st_generator *gen;
  145. st_foreach_item(table, gen, &key, &value) {
  146. process_item(value);
  147. }
  148. @sideeffect None
  149. @see st_foreach_item_int st_foreach
  150. */
  151. #define st_foreach_item(table, gen, key, value) \
  152. for(gen=st_init_gen(table); st_gen(gen,key,value) || (st_free_gen(gen),0);)
  153. /**
  154. @brief Iteration macro.
  155. @details
  156. An iteration macro which loops over all the entries in
  157. `table`, setting `key` to point to the key and `value` to the
  158. associated value (if it is not nil). `value` is assumed to be a
  159. pointer to an integer. `gen` is a generator variable used
  160. internally. Sample usage:
  161. void *key;
  162. int value;
  163. st_generator *gen;
  164. st_foreach_item_int(table, gen, &key, &value) {
  165. process_item(value);
  166. }
  167. @sideeffect None
  168. @see st_foreach_item st_foreach
  169. */
  170. #define st_foreach_item_int(table, gen, key, value) \
  171. for(gen=st_init_gen(table); st_gen_int(gen,key,value) || (st_free_gen(gen),0);)
  172. /*---------------------------------------------------------------------------*/
  173. /* Function prototypes */
  174. /*---------------------------------------------------------------------------*/
  175. #ifdef __cplusplus
  176. extern "C" {
  177. #endif
  178. st_table *st_init_table_with_params (st_compare_t, st_hash_t, int, int, double, int);
  179. st_table *st_init_table (st_compare_t, st_hash_t);
  180. st_table *st_init_table_with_params_and_arg (st_compare_arg_t, st_hash_arg_t, void const *, int, int, double, int);
  181. st_table *st_init_table_with_arg (st_compare_arg_t, st_hash_arg_t, void const *);
  182. void st_free_table (st_table *);
  183. int st_lookup (st_table *, void const *, void **);
  184. int st_lookup_int (st_table *, void const *, int *);
  185. int st_insert (st_table *, void *, void *);
  186. int st_add_direct (st_table *, void *, void *);
  187. int st_find_or_add (st_table *, void *, void ***);
  188. int st_find (st_table *, void const *, void ***);
  189. st_table *st_copy (st_table const *);
  190. int st_delete (st_table *, void **, void **);
  191. int st_delete_int (st_table *, void **, int *);
  192. int st_count(st_table const *);
  193. int st_foreach (st_table *, st_foreach_t, void *);
  194. int st_strhash (void const *, int);
  195. int st_numhash (void const *, int);
  196. int st_ptrhash (void const *, int);
  197. int st_numcmp (void const *, void const *);
  198. int st_ptrcmp (void const *, void const *);
  199. st_generator *st_init_gen (st_table const *);
  200. int st_gen (st_generator *, void **, void **);
  201. int st_gen_int (st_generator *, void **, int *);
  202. void st_free_gen (st_generator *);
  203. #ifdef __cplusplus
  204. } /* end of extern "C" */
  205. #endif
  206. #endif /* ST_H_ */