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.

232 lines
7.0 KiB

  1. /**CHeaderFile*****************************************************************
  2. FileName [st.h]
  3. PackageName [st]
  4. Synopsis [Symbol table package.]
  5. Description [The st library provides functions to create, maintain,
  6. and query symbol tables.]
  7. SeeAlso []
  8. Author []
  9. Copyright []
  10. Revision [$Id: st.h,v 1.10 2004/01/02 07:40:31 fabio Exp fabio $]
  11. ******************************************************************************/
  12. #ifndef ST_INCLUDED
  13. #define ST_INCLUDED
  14. /*---------------------------------------------------------------------------*/
  15. /* Nested includes */
  16. /*---------------------------------------------------------------------------*/
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*---------------------------------------------------------------------------*/
  21. /* Constant declarations */
  22. /*---------------------------------------------------------------------------*/
  23. #define ST_DEFAULT_MAX_DENSITY 5
  24. #define ST_DEFAULT_INIT_TABLE_SIZE 11
  25. #define ST_DEFAULT_GROW_FACTOR 2.0
  26. #define ST_DEFAULT_REORDER_FLAG 0
  27. #define ST_OUT_OF_MEM -10000
  28. /*---------------------------------------------------------------------------*/
  29. /* Stucture declarations */
  30. /*---------------------------------------------------------------------------*/
  31. /*---------------------------------------------------------------------------*/
  32. /* Type declarations */
  33. /*---------------------------------------------------------------------------*/
  34. typedef struct st_table_entry st_table_entry;
  35. struct st_table_entry {
  36. char *key;
  37. char *record;
  38. st_table_entry *next;
  39. };
  40. typedef struct st_table st_table;
  41. struct st_table {
  42. int (*compare)(const char *, const char *);
  43. int (*hash)(char *, int);
  44. int num_bins;
  45. int num_entries;
  46. int max_density;
  47. int reorder_flag;
  48. double grow_factor;
  49. st_table_entry **bins;
  50. };
  51. typedef struct st_generator st_generator;
  52. struct st_generator {
  53. st_table *table;
  54. st_table_entry *entry;
  55. int index;
  56. };
  57. enum st_retval {ST_CONTINUE, ST_STOP, ST_DELETE};
  58. typedef enum st_retval (*ST_PFSR)(char *, char *, char *);
  59. typedef int (*ST_PFICPCP)(const char *, const char *); /* type for comparison function */
  60. typedef int (*ST_PFICPI)(char *, int); /* type for hash function */
  61. /*---------------------------------------------------------------------------*/
  62. /* Variable declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /*---------------------------------------------------------------------------*/
  65. /* Macro declarations */
  66. /*---------------------------------------------------------------------------*/
  67. /**Macro***********************************************************************
  68. Synopsis [Checks whethere `key' is in `table'.]
  69. Description [Returns 1 if there is an entry under `key' in `table', 0
  70. otherwise.]
  71. SideEffects [None]
  72. SeeAlso [st_lookup]
  73. ******************************************************************************/
  74. #define st_is_member(table,key) st_lookup(table,key,(char **) 0)
  75. /**Macro***********************************************************************
  76. Synopsis [Returns the number of entries in the table `table'.]
  77. Description [Returns the number of entries in the table `table'.]
  78. SideEffects [None]
  79. SeeAlso []
  80. ******************************************************************************/
  81. #define st_count(table) ((table)->num_entries)
  82. /**Macro***********************************************************************
  83. Synopsis [Iteration macro.]
  84. Description [An iteration macro which loops over all the entries in
  85. `table', setting `key' to point to the key and `value' to the
  86. associated value (if it is not nil). `gen' is a generator variable
  87. used internally. Sample usage:
  88. <pre>
  89. char *key, *value;
  90. </pre>
  91. <pre>
  92. st_generator *gen;
  93. </pre>
  94. <pre>
  95. st_foreach_item(table, gen, &key, &value) {
  96. </pre>
  97. <pre>
  98. process_item(value);
  99. </pre>
  100. <pre>
  101. }
  102. </pre>
  103. ]
  104. SideEffects [None]
  105. SeeAlso [st_foreach_item_int st_foreach]
  106. ******************************************************************************/
  107. #define st_foreach_item(table, gen, key, value) \
  108. for(gen=st_init_gen(table); st_gen(gen,key,value) || (st_free_gen(gen),0);)
  109. /**Macro***********************************************************************
  110. Synopsis [Iteration macro.]
  111. Description [An iteration macro which loops over all the entries in
  112. `table', setting `key' to point to the key and `value' to the
  113. associated value (if it is not nil). `value' is assumed to be a
  114. pointer to an integer. `gen' is a generator variable used
  115. internally. Sample usage:
  116. <pre>
  117. char *key;
  118. </pre>
  119. <pre>
  120. int value;
  121. </pre>
  122. <pre>
  123. st_generator *gen;
  124. </pre>
  125. <pre>
  126. st_foreach_item_int(table, gen, &key, &value) {
  127. </pre>
  128. <pre>
  129. process_item(value);
  130. </pre>
  131. <pre>
  132. }
  133. </pre>
  134. ]
  135. SideEffects [None]
  136. SeeAlso [st_foreach_item st_foreach]
  137. ******************************************************************************/
  138. #define st_foreach_item_int(table, gen, key, value) \
  139. for(gen=st_init_gen(table); st_gen_int(gen,key,value) || (st_free_gen(gen),0);)
  140. /**AutomaticStart*************************************************************/
  141. /*---------------------------------------------------------------------------*/
  142. /* Function prototypes */
  143. /*---------------------------------------------------------------------------*/
  144. extern st_table *st_init_table_with_params (ST_PFICPCP, ST_PFICPI, int, int, double, int);
  145. extern st_table *st_init_table (ST_PFICPCP, ST_PFICPI);
  146. extern void st_free_table (st_table *);
  147. extern int st_lookup (st_table *, void *, void *);
  148. extern int st_lookup_int (st_table *, void *, int *);
  149. extern int st_insert (st_table *, void *, void *);
  150. extern int st_add_direct (st_table *, void *, void *);
  151. extern int st_find_or_add (st_table *, void *, void *);
  152. extern int st_find (st_table *, void *, void *);
  153. extern st_table *st_copy (st_table *);
  154. extern int st_delete (st_table *, void *, void *);
  155. extern int st_delete_int (st_table *, void *, int *);
  156. extern int st_foreach (st_table *, ST_PFSR, char *);
  157. extern int st_strhash (char *, int);
  158. extern int st_numhash (char *, int);
  159. extern int st_ptrhash (char *, int);
  160. extern int st_numcmp (const char *, const char *);
  161. extern int st_ptrcmp (const char *, const char *);
  162. extern st_generator *st_init_gen (st_table *);
  163. extern int st_gen (st_generator *, void *, void *);
  164. extern int st_gen_int (st_generator *, void *, int *);
  165. extern void st_free_gen (st_generator *);
  166. /**AutomaticEnd***************************************************************/
  167. #ifdef __cplusplus
  168. } /* end of extern "C" */
  169. #endif
  170. #endif /* ST_INCLUDED */