ferret 0.11.6 → 0.11.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/README +10 -22
  2. data/RELEASE_CHANGES +137 -0
  3. data/RELEASE_NOTES +60 -0
  4. data/Rakefile +379 -274
  5. data/TODO +100 -8
  6. data/bin/ferret-browser +0 -0
  7. data/ext/BZLIB_blocksort.c +1094 -0
  8. data/ext/BZLIB_bzlib.c +1578 -0
  9. data/ext/BZLIB_compress.c +672 -0
  10. data/ext/BZLIB_crctable.c +104 -0
  11. data/ext/BZLIB_decompress.c +626 -0
  12. data/ext/BZLIB_huffman.c +205 -0
  13. data/ext/BZLIB_randtable.c +84 -0
  14. data/ext/{api.c → STEMMER_api.c} +7 -10
  15. data/ext/{libstemmer.c → STEMMER_libstemmer.c} +3 -2
  16. data/ext/{stem_ISO_8859_1_danish.c → STEMMER_stem_ISO_8859_1_danish.c} +123 -124
  17. data/ext/{stem_ISO_8859_1_dutch.c → STEMMER_stem_ISO_8859_1_dutch.c} +177 -188
  18. data/ext/STEMMER_stem_ISO_8859_1_english.c +1117 -0
  19. data/ext/{stem_ISO_8859_1_finnish.c → STEMMER_stem_ISO_8859_1_finnish.c} +276 -306
  20. data/ext/STEMMER_stem_ISO_8859_1_french.c +1246 -0
  21. data/ext/{stem_ISO_8859_1_german.c → STEMMER_stem_ISO_8859_1_german.c} +161 -170
  22. data/ext/STEMMER_stem_ISO_8859_1_hungarian.c +1230 -0
  23. data/ext/STEMMER_stem_ISO_8859_1_italian.c +1065 -0
  24. data/ext/STEMMER_stem_ISO_8859_1_norwegian.c +297 -0
  25. data/ext/{stem_ISO_8859_1_porter.c → STEMMER_stem_ISO_8859_1_porter.c} +263 -290
  26. data/ext/{stem_ISO_8859_1_portuguese.c → STEMMER_stem_ISO_8859_1_portuguese.c} +362 -380
  27. data/ext/STEMMER_stem_ISO_8859_1_spanish.c +1093 -0
  28. data/ext/STEMMER_stem_ISO_8859_1_swedish.c +307 -0
  29. data/ext/STEMMER_stem_ISO_8859_2_romanian.c +998 -0
  30. data/ext/{stem_KOI8_R_russian.c → STEMMER_stem_KOI8_R_russian.c} +244 -245
  31. data/ext/STEMMER_stem_UTF_8_danish.c +339 -0
  32. data/ext/{stem_UTF_8_dutch.c → STEMMER_stem_UTF_8_dutch.c} +192 -211
  33. data/ext/STEMMER_stem_UTF_8_english.c +1125 -0
  34. data/ext/{stem_UTF_8_finnish.c → STEMMER_stem_UTF_8_finnish.c} +284 -324
  35. data/ext/STEMMER_stem_UTF_8_french.c +1256 -0
  36. data/ext/{stem_UTF_8_german.c → STEMMER_stem_UTF_8_german.c} +170 -187
  37. data/ext/STEMMER_stem_UTF_8_hungarian.c +1234 -0
  38. data/ext/STEMMER_stem_UTF_8_italian.c +1073 -0
  39. data/ext/STEMMER_stem_UTF_8_norwegian.c +299 -0
  40. data/ext/{stem_UTF_8_porter.c → STEMMER_stem_UTF_8_porter.c} +271 -310
  41. data/ext/STEMMER_stem_UTF_8_portuguese.c +1023 -0
  42. data/ext/STEMMER_stem_UTF_8_romanian.c +1004 -0
  43. data/ext/STEMMER_stem_UTF_8_russian.c +694 -0
  44. data/ext/STEMMER_stem_UTF_8_spanish.c +1097 -0
  45. data/ext/STEMMER_stem_UTF_8_swedish.c +309 -0
  46. data/ext/STEMMER_stem_UTF_8_turkish.c +2205 -0
  47. data/ext/{utilities.c → STEMMER_utilities.c} +100 -68
  48. data/ext/analysis.c +276 -121
  49. data/ext/analysis.h +190 -143
  50. data/ext/api.h +3 -4
  51. data/ext/array.c +5 -3
  52. data/ext/array.h +52 -43
  53. data/ext/bitvector.c +38 -482
  54. data/ext/bitvector.h +446 -124
  55. data/ext/bzlib.h +282 -0
  56. data/ext/bzlib_private.h +503 -0
  57. data/ext/compound_io.c +23 -22
  58. data/ext/config.h +21 -11
  59. data/ext/document.c +43 -40
  60. data/ext/document.h +31 -21
  61. data/ext/except.c +20 -38
  62. data/ext/except.h +89 -76
  63. data/ext/extconf.rb +3 -2
  64. data/ext/ferret.c +49 -35
  65. data/ext/ferret.h +14 -11
  66. data/ext/field_index.c +262 -0
  67. data/ext/field_index.h +52 -0
  68. data/ext/filter.c +11 -10
  69. data/ext/fs_store.c +65 -47
  70. data/ext/global.c +245 -165
  71. data/ext/global.h +252 -54
  72. data/ext/hash.c +200 -243
  73. data/ext/hash.h +205 -163
  74. data/ext/hashset.c +118 -96
  75. data/ext/hashset.h +110 -82
  76. data/ext/header.h +19 -19
  77. data/ext/helper.c +11 -10
  78. data/ext/helper.h +14 -6
  79. data/ext/index.c +745 -366
  80. data/ext/index.h +503 -529
  81. data/ext/internal.h +1020 -0
  82. data/ext/lang.c +10 -0
  83. data/ext/lang.h +35 -15
  84. data/ext/mempool.c +5 -4
  85. data/ext/mempool.h +30 -22
  86. data/ext/modules.h +35 -7
  87. data/ext/multimapper.c +43 -2
  88. data/ext/multimapper.h +32 -23
  89. data/ext/posh.c +0 -0
  90. data/ext/posh.h +4 -38
  91. data/ext/priorityqueue.c +10 -12
  92. data/ext/priorityqueue.h +33 -21
  93. data/ext/q_boolean.c +22 -9
  94. data/ext/q_const_score.c +3 -2
  95. data/ext/q_filtered_query.c +15 -12
  96. data/ext/q_fuzzy.c +147 -135
  97. data/ext/q_match_all.c +3 -2
  98. data/ext/q_multi_term.c +28 -32
  99. data/ext/q_parser.c +451 -173
  100. data/ext/q_phrase.c +158 -79
  101. data/ext/q_prefix.c +16 -18
  102. data/ext/q_range.c +363 -31
  103. data/ext/q_span.c +130 -141
  104. data/ext/q_term.c +21 -21
  105. data/ext/q_wildcard.c +19 -23
  106. data/ext/r_analysis.c +369 -242
  107. data/ext/r_index.c +421 -434
  108. data/ext/r_qparser.c +142 -92
  109. data/ext/r_search.c +790 -407
  110. data/ext/r_store.c +44 -44
  111. data/ext/r_utils.c +264 -96
  112. data/ext/ram_store.c +29 -23
  113. data/ext/scanner.c +895 -0
  114. data/ext/scanner.h +36 -0
  115. data/ext/scanner_mb.c +6701 -0
  116. data/ext/scanner_utf8.c +4415 -0
  117. data/ext/search.c +210 -87
  118. data/ext/search.h +556 -488
  119. data/ext/similarity.c +17 -16
  120. data/ext/similarity.h +51 -44
  121. data/ext/sort.c +157 -354
  122. data/ext/stem_ISO_8859_1_hungarian.h +16 -0
  123. data/ext/stem_ISO_8859_2_romanian.h +16 -0
  124. data/ext/stem_UTF_8_hungarian.h +16 -0
  125. data/ext/stem_UTF_8_romanian.h +16 -0
  126. data/ext/stem_UTF_8_turkish.h +16 -0
  127. data/ext/stopwords.c +287 -278
  128. data/ext/store.c +57 -51
  129. data/ext/store.h +308 -286
  130. data/ext/symbol.c +10 -0
  131. data/ext/symbol.h +23 -0
  132. data/ext/term_vectors.c +14 -293
  133. data/ext/threading.h +22 -22
  134. data/ext/win32.h +12 -4
  135. data/lib/ferret.rb +2 -1
  136. data/lib/ferret/browser.rb +1 -1
  137. data/lib/ferret/field_symbol.rb +94 -0
  138. data/lib/ferret/index.rb +221 -34
  139. data/lib/ferret/number_tools.rb +6 -6
  140. data/lib/ferret/version.rb +3 -0
  141. data/test/{unit → long_running}/largefile/tc_largefile.rb +1 -1
  142. data/test/test_helper.rb +7 -2
  143. data/test/test_installed.rb +1 -0
  144. data/test/threading/thread_safety_index_test.rb +10 -1
  145. data/test/threading/thread_safety_read_write_test.rb +4 -7
  146. data/test/threading/thread_safety_test.rb +0 -0
  147. data/test/unit/analysis/tc_analyzer.rb +29 -27
  148. data/test/unit/analysis/tc_token_stream.rb +23 -16
  149. data/test/unit/index/tc_index.rb +116 -11
  150. data/test/unit/index/tc_index_reader.rb +27 -27
  151. data/test/unit/index/tc_index_writer.rb +10 -0
  152. data/test/unit/index/th_doc.rb +38 -21
  153. data/test/unit/search/tc_filter.rb +31 -10
  154. data/test/unit/search/tc_index_searcher.rb +6 -0
  155. data/test/unit/search/tm_searcher.rb +53 -1
  156. data/test/unit/store/tc_fs_store.rb +40 -2
  157. data/test/unit/store/tc_ram_store.rb +0 -0
  158. data/test/unit/store/tm_store.rb +0 -0
  159. data/test/unit/store/tm_store_lock.rb +7 -6
  160. data/test/unit/tc_field_symbol.rb +26 -0
  161. data/test/unit/ts_analysis.rb +0 -0
  162. data/test/unit/ts_index.rb +0 -0
  163. data/test/unit/ts_store.rb +0 -0
  164. data/test/unit/ts_utils.rb +0 -0
  165. data/test/unit/utils/tc_number_tools.rb +0 -0
  166. data/test/utils/content_generator.rb +226 -0
  167. metadata +262 -221
  168. data/ext/inc/lang.h +0 -48
  169. data/ext/inc/threading.h +0 -31
  170. data/ext/stem_ISO_8859_1_english.c +0 -1156
  171. data/ext/stem_ISO_8859_1_french.c +0 -1276
  172. data/ext/stem_ISO_8859_1_italian.c +0 -1091
  173. data/ext/stem_ISO_8859_1_norwegian.c +0 -296
  174. data/ext/stem_ISO_8859_1_spanish.c +0 -1119
  175. data/ext/stem_ISO_8859_1_swedish.c +0 -307
  176. data/ext/stem_UTF_8_danish.c +0 -344
  177. data/ext/stem_UTF_8_english.c +0 -1176
  178. data/ext/stem_UTF_8_french.c +0 -1296
  179. data/ext/stem_UTF_8_italian.c +0 -1113
  180. data/ext/stem_UTF_8_norwegian.c +0 -302
  181. data/ext/stem_UTF_8_portuguese.c +0 -1055
  182. data/ext/stem_UTF_8_russian.c +0 -709
  183. data/ext/stem_UTF_8_spanish.c +0 -1137
  184. data/ext/stem_UTF_8_swedish.c +0 -313
  185. data/lib/ferret_version.rb +0 -3
@@ -1,50 +1,80 @@
1
1
  #ifndef FRT_GLOBAL_H
2
2
  #define FRT_GLOBAL_H
3
3
 
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
4
8
  #include "config.h"
5
9
  #include "except.h"
6
10
  #include "lang.h"
7
11
  #include <stdlib.h>
8
12
  #include <stdio.h>
9
13
  #include <stdarg.h>
14
+ #include <assert.h>
15
+ #include <string.h>
16
+
17
+ #define FRT_MAX_WORD_SIZE 255
18
+ #define FRT_MAX_FILE_PATH 1024
19
+ #define FRT_BUFFER_SIZE 1024
20
+
21
+ #define FRT_DBL2S "%#.7g"
22
+
23
+ #if defined(__GNUC__) && !defined(__cplusplus)
24
+ # define FRT_INLINE
25
+ #else
26
+ # define FRT_INLINE
27
+ #endif
10
28
 
11
- #define MAX_WORD_SIZE 255
12
- #define MAX_FILE_PATH 1024
29
+ #if __GNUC__ >= 3
30
+ # define FRT_ATTR_ALWAYS_INLINE inline __attribute__ ((always_inline))
31
+ # define FRT_ATTR_MALLOC __attribute__ ((malloc))
32
+ # define FRT_ATTR_PURE __attribute__ ((pure))
33
+ # define FRT_ATTR_CONST __attribute__ ((const))
34
+ # define likely(x) __builtin_expect(!!(x), 1)
35
+ # define unlikely(x) __builtin_expect(!!(x), 0)
36
+ #else
37
+ # define FRT_ATTR_ALWAYS_INLINE
38
+ # define FRT_ATTR_MALLOC
39
+ # define FRT_ATTR_PURE
40
+ # define FRT_ATTR_CONST
41
+ # define likely(x) (x)
42
+ # define unlikely(x) (x)
43
+ #endif
13
44
 
14
- #if defined(__GNUC__)
15
- # define INLINE __inline__
45
+ #ifdef __cplusplus
46
+ #define FRT_EXTERNC extern "C"
16
47
  #else
17
- # define INLINE
48
+ #define FRT_EXTERNC
18
49
  #endif
19
50
 
20
- typedef void (*free_ft)(void *key);
51
+ typedef void (*frt_free_ft)(void *key);
21
52
 
22
- #define NELEMS(array) ((int)(sizeof(array)/sizeof(array[0])))
53
+ #define FRT_NELEMS(array) ((int)(sizeof(array)/sizeof(array[0])))
23
54
 
24
55
 
25
- #define ZEROSET(ptr, type) memset(ptr, 0, sizeof(type))
26
- #define ZEROSET_N(ptr, type, n) memset(ptr, 0, sizeof(type)*(n))
56
+ #define FRT_ZEROSET(ptr, type) memset(ptr, 0, sizeof(type))
57
+ #define FRT_ZEROSET_N(ptr, type, n) memset(ptr, 0, sizeof(type)*(n))
27
58
 
28
- /*
29
- #define ALLOC_AND_ZERO(type) (type*)memset(emalloc(sizeof(type)), 0, sizeof(type))
30
- #define ALLOC_AND_ZERO_N(type,n) (type*)memset(emalloc(sizeof(type)*(n)), 0, sizeof(type)*(n))
31
- */
32
- #define ALLOC_AND_ZERO(type) (type*)frt_calloc(sizeof(type))
33
- #define ALLOC_AND_ZERO_N(type,n) (type*)frt_calloc(sizeof(type)*(n))
59
+ #define FRT_ALLOC_AND_ZERO(type) (type*)frt_ecalloc(sizeof(type))
60
+ #define FRT_ALLOC_AND_ZERO_N(type,n) (type*)frt_ecalloc(sizeof(type)*(n))
34
61
 
35
- #define REF(a) (a)->ref_cnt++
36
- #define DEREF(a) (a)->ref_cnt--
62
+ #define FRT_REF(a) (a)->ref_cnt++
63
+ #define FRT_DEREF(a) (a)->ref_cnt--
37
64
 
38
- #define NEXT_NUM(index, size) (((index) + 1) % (size))
39
- #define PREV_NUM(index, size) (((index) + (size) - 1) % (size))
65
+ #define FRT_NEXT_NUM(index, size) (((index) + 1) % (size))
66
+ #define FRT_PREV_NUM(index, size) (((index) + (size) - 1) % (size))
40
67
 
41
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
42
- #define MAX(a, b) ((a) > (b) ? (a) : (b))
68
+ #define FRT_MIN(a, b) ((a) < (b) ? (a) : (b))
69
+ #define FRT_MAX(a, b) ((a) > (b) ? (a) : (b))
43
70
 
44
- #define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
45
- #define MAX3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))
71
+ #define FRT_MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
72
+ #define FRT_MAX3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))
46
73
 
47
- #define RECAPA(self, len, capa, ptr, type) \
74
+ #define FRT_ABS(n) ((n >= 0) ? n : -n)
75
+ #define FRT_TO_WORD(n) (((n - 1) >> 5) + 1)
76
+
77
+ #define FRT_RECAPA(self, len, capa, ptr, type) \
48
78
  do {\
49
79
  if (self->len >= self->capa) {\
50
80
  if (self->capa > 0) {\
@@ -52,7 +82,7 @@ typedef void (*free_ft)(void *key);
52
82
  } else {\
53
83
  self->capa = 4;\
54
84
  }\
55
- REALLOC_N(self->ptr, type, self->capa);\
85
+ self->ptr = (type *)frt_erealloc(self->ptr, sizeof(type) * self->capa);\
56
86
  }\
57
87
  } while (0)
58
88
 
@@ -64,39 +94,37 @@ typedef void (*free_ft)(void *key);
64
94
  # define Xj fprintf(stdout,"%s, %d: %s\n", __FILE__, __LINE__, __func__);
65
95
  #endif
66
96
 
67
- extern char *progname();
68
- extern void setprogname(const char *str);
69
-
70
- extern unsigned int *imalloc(unsigned int value);
71
- extern unsigned long *lmalloc(unsigned long value);
72
- extern f_u32 *u32malloc(f_u32 value);
73
- extern f_u64 *u64malloc(f_u64 value);
97
+ extern unsigned int *frt_imalloc(unsigned int value);
98
+ extern unsigned long *frt_lmalloc(unsigned long value);
99
+ extern frt_u32 *frt_u32malloc(frt_u32 value);
100
+ extern frt_u64 *frt_u64malloc(frt_u64 value);
74
101
 
75
- extern void *emalloc(size_t n);
76
- extern void *ecalloc(size_t n);
77
- extern void *erealloc(void *ptr, size_t n);
78
- extern char *estrdup(const char *s);
79
- extern char *estrcat(char *str, char *str_cat);
102
+ extern char *frt_estrdup(const char *s);
103
+ extern char *frt_estrcat(char *str, char *str_cat);
104
+ extern void frt_weprintf(const char *fmt, ...);
105
+ extern char *frt_epstrdup(const char *fmt, int len, ...);
80
106
 
81
- extern const char *EMPTY_STRING;
107
+ extern const char *FRT_EMPTY_STRING;
82
108
 
83
- extern int scmp(const void *p1, const void *p2);
84
- extern int icmp(const void *p1, const void *p2);
85
- extern int icmp_risky(const void *p1, const void *p2);
109
+ extern int frt_scmp(const void *p1, const void *p2);
110
+ extern int frt_icmp(const void *p1, const void *p2);
111
+ extern int frt_icmp_risky(const void *p1, const void *p2);
112
+ extern void frt_strsort(char **string_array, int size);
86
113
 
87
- extern int min2(int a, int b);
88
- extern int min3(int a, int b, int c);
89
- extern int max2(int a, int b);
90
- extern int max3(int a, int b, int c);
114
+ extern int frt_min2(int a, int b);
115
+ extern int frt_min3(int a, int b, int c);
116
+ extern int frt_max2(int a, int b);
117
+ extern int frt_max3(int a, int b, int c);
91
118
 
92
- extern char *dbl_to_s(char *buf, double num);
93
- extern char *strfmt(const char *fmt, ...);
94
- extern char *vstrfmt(const char *fmt, va_list args);
119
+ extern char *frt_dbl_to_s(char *buf, double num);
120
+ extern char *frt_strfmt(const char *fmt, ...);
121
+ extern char *frt_vstrfmt(const char *fmt, va_list args);
95
122
 
96
- extern void micro_sleep(const int micro_seconds);
123
+ extern char *frt_get_stacktrace();
124
+ extern void frt_print_stacktrace();
97
125
 
98
- extern void register_for_cleanup(void *p, free_ft free_func);
99
- extern void do_clean_up();
126
+ extern void frt_register_for_cleanup(void *p, frt_free_ft free_func);
127
+ extern void frt_do_clean_up();
100
128
 
101
129
  /**
102
130
  * A dummy function which can be passed to functions which expect a free
@@ -105,13 +133,183 @@ extern void do_clean_up();
105
133
  *
106
134
  * @param p the object which this function will be called on.
107
135
  */
108
- extern void dummy_free(void *p);
136
+ extern void frt_dummy_free(void *p);
137
+
138
+ /**
139
+ * Returns the count of leading [MSB] 0 bits in +word+.
140
+ */
141
+ static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
142
+ int frt_count_leading_zeros(frt_u32 word)
143
+ {
144
+ #ifdef __GNUC__
145
+ if (word)
146
+ return __builtin_clz(word);
147
+ return 32;
148
+ #else
149
+ static const int count_leading_zeros[] = {
150
+ 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
151
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
152
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
154
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
155
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
156
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
157
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
158
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
162
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
166
+ };
167
+ if (word & 0xff) return count_leading_zeros[word & 0xff];
168
+ word >>= 8; if (word & 0xff) return count_leading_zeros[word & 0xff] + 8;
169
+ word >>= 8; if (word & 0xff) return count_leading_zeros[word & 0xff] + 16;
170
+ word >>= 8; return count_leading_zeros[word & 0xff] + 24;
171
+ #endif
172
+ }
173
+
174
+ static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
175
+ int frt_count_leading_ones(frt_u32 word)
176
+ {
177
+ return frt_count_leading_zeros(~word);
178
+ }
179
+
180
+ /**
181
+ * Return the count of trailing [LSB] 0 bits in +word+.
182
+ */
183
+
184
+ static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
185
+ int frt_count_trailing_zeros(frt_u32 word)
186
+ {
187
+ #ifdef __GNUC__
188
+ if (word)
189
+ return __builtin_ctz(word);
190
+ return 32;
191
+ #else
192
+ static const int count_trailing_zeros[] = {
193
+ 8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
194
+ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
195
+ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
196
+ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
197
+ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
198
+ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
199
+ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
200
+ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
201
+ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
202
+ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
203
+ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
204
+ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
205
+ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
206
+ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
207
+ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
208
+ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
209
+ };
210
+ if (word & 0xff) return count_trailing_zeros[word & 0xff];
211
+ word >>= 8; if (word & 0xff) return count_trailing_zeros[word & 0xff] + 8;
212
+ word >>= 8; if (word & 0xff) return count_trailing_zeros[word & 0xff] + 16;
213
+ word >>= 8; return count_trailing_zeros[word & 0xff] + 24;
214
+ #endif
215
+ }
216
+
217
+ static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
218
+ int frt_count_trailing_ones(frt_u32 word)
219
+ {
220
+ return frt_count_trailing_zeros(~word);
221
+ }
222
+
223
+ static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
224
+ int frt_count_ones(frt_u32 word)
225
+ {
226
+ #ifdef __GNUC__
227
+ return __builtin_popcount(word);
228
+ #else
229
+ static const frt_uchar count_ones[] = {
230
+ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
231
+ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
232
+ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
233
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
234
+ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
235
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
236
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
237
+ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
238
+ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
239
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
240
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
241
+ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
242
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
243
+ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
244
+ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
245
+ 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
246
+ };
247
+ return count_ones[(word ) & 0xff]
248
+ + count_ones[(word >> 8 ) & 0xff]
249
+ + count_ones[(word >> 16) & 0xff]
250
+ + count_ones[(word >> 24) & 0xff];
251
+ #endif
252
+ }
253
+
254
+ static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
255
+ int frt_count_zeros(frt_u32 word)
256
+ {
257
+ return frt_count_ones(~word);
258
+ }
259
+
260
+ /**
261
+ * Round up to the next power of 2
262
+ */
263
+ static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
264
+ int frt_round2(frt_u32 word)
265
+ {
266
+ return 1 << (32 - frt_count_leading_zeros(word));
267
+ }
268
+
269
+ /**
270
+ * For coverage, we don't want FRT_XEXIT to actually exit on uncaught
271
+ * exceptions. +frt_x_abort_on_exception+ is +true+ by default, set it to
272
+ * +false+, and +frt_x_has_aborted+ will be set as appropriate. We also
273
+ * don't want spurious errors to be printed out to stderr, so we give
274
+ * the option to set where errors go to with +frt_x_exception_stream+.
275
+ */
276
+
277
+ extern bool frt_x_abort_on_exception;
278
+ extern bool frt_x_has_aborted;
279
+ extern FILE *frt_x_exception_stream;
280
+
281
+ /**
282
+ * The convenience macro +EXCEPTION_STREAM+ returns stderr when
283
+ * +frt_x_exception_stream+ isn't explicitely set.
284
+ */
285
+ #define EXCEPTION_STREAM (frt_x_exception_stream ? frt_x_exception_stream : stderr)
109
286
 
110
287
  #ifdef DEBUG
111
- extern bool x_do_logging;
112
- #define xlog if (x_do_logging) printf
288
+ extern bool frt_x_do_logging;
289
+ #define xlog if (frt_x_do_logging) printf
113
290
  #else
114
291
  #define xlog()
115
292
  #endif
116
293
 
294
+ extern void frt_init(int arc, const char *const argv[]);
295
+ extern void frt_setprogname(const char *str);
296
+ extern const char *frt_progname();
297
+ extern void frt_micro_sleep(const int micro_seconds);
298
+ extern void frt_clean_up();
299
+
300
+ #ifdef __cplusplus
301
+ } // extern "C"
302
+ #endif
303
+
304
+
305
+ /**
306
+ * For general use during testing. Switch this on and off for print statements
307
+ * to only print when p_on is called and not after p_off is called
308
+ */
309
+ extern void p(const char *format, ...);
310
+ extern void p_on();
311
+ extern void p_off();
312
+ extern void p_pause();
313
+ extern void p_resume();
314
+
117
315
  #endif
data/ext/hash.c CHANGED
@@ -1,27 +1,30 @@
1
1
  #include "hash.h"
2
2
  #include "global.h"
3
3
  #include <string.h>
4
+ #include "internal.h"
4
5
 
5
6
  /****************************************************************************
6
7
  *
7
- * HashTable
8
+ * Hash
8
9
  *
9
10
  * This hash table is modeled after Python's dictobject and a description of
10
11
  * the algorithm can be found in the file dictobject.c in Python's src
11
12
  ****************************************************************************/
12
13
 
13
14
  static char *dummy_key = "";
15
+ static char *dummy_int_key = "i";
16
+
14
17
 
15
18
  #define PERTURB_SHIFT 5
16
19
  #define MAX_FREE_HASH_TABLES 80
17
20
 
18
- static HashTable *free_hts[MAX_FREE_HASH_TABLES];
21
+ static Hash *free_hts[MAX_FREE_HASH_TABLES];
19
22
  static int num_free_hts = 0;
20
23
 
21
24
  unsigned long str_hash(const char *const str)
22
25
  {
23
26
  register unsigned long h = 0;
24
- register unsigned char *p = (unsigned char *) str;
27
+ register unsigned char *p = (unsigned char *)str;
25
28
 
26
29
  for (; *p; p++) {
27
30
  h = 37 * h + *p;
@@ -40,33 +43,26 @@ int ptr_eq(const void *q1, const void *q2)
40
43
  return q1 == q2;
41
44
  }
42
45
 
43
- static int int_eq(const void *q1, const void *q2)
44
- {
45
- (void)q1;
46
- (void)q2;
47
- return true;
48
- }
49
-
50
- static unsigned long int_hash(const void *i)
46
+ static int str_eq(const void *q1, const void *q2)
51
47
  {
52
- return *((unsigned long *)i);
48
+ return strcmp((const char *)q1, (const char *)q2) == 0;
53
49
  }
54
50
 
55
- typedef HashEntry *(*lookup_ft)(struct HashTable *ht, register const void *key);
51
+ typedef HashEntry *(*lookup_ft)(struct Hash *self, register const void *key);
56
52
 
57
53
  /**
58
54
  * Fast lookup function for resizing as we know there are no equal elements or
59
55
  * deletes to worry about.
60
56
  *
61
- * @param ht the HashTable to do the fast lookup in
57
+ * @param self the Hash to do the fast lookup in
62
58
  * @param the hashkey we are looking for
63
59
  */
64
- static INLINE HashEntry *h_resize_lookup(HashTable *ht,
65
- register const unsigned long hash)
60
+ static INLINE HashEntry *h_resize_lookup(Hash *self,
61
+ register const unsigned long hash)
66
62
  {
67
63
  register unsigned long perturb;
68
- register int mask = ht->mask;
69
- register HashEntry *he0 = ht->table;
64
+ register int mask = self->mask;
65
+ register HashEntry *he0 = self->table;
70
66
  register int i = hash & mask;
71
67
  register HashEntry *he = &he0[i];
72
68
 
@@ -85,12 +81,12 @@ static INLINE HashEntry *h_resize_lookup(HashTable *ht,
85
81
  }
86
82
  }
87
83
 
88
- HashEntry *h_lookup_int(HashTable *ht, const void *key)
84
+ static HashEntry *h_lookup_ptr(Hash *self, const void *key)
89
85
  {
90
- register unsigned long hash = *((int *)key);
86
+ register const unsigned long hash = (long)key;
91
87
  register unsigned long perturb;
92
- register int mask = ht->mask;
93
- register HashEntry *he0 = ht->table;
88
+ register int mask = self->mask;
89
+ register HashEntry *he0 = self->table;
94
90
  register int i = hash & mask;
95
91
  register HashEntry *he = &he0[i];
96
92
  register HashEntry *freeslot = NULL;
@@ -122,61 +118,16 @@ HashEntry *h_lookup_int(HashTable *ht, const void *key)
122
118
  }
123
119
  }
124
120
 
125
- HashEntry *h_lookup_str(HashTable *ht, register const char *key)
121
+ HashEntry *h_lookup(Hash *self, register const void *key)
126
122
  {
127
- register unsigned long hash = str_hash(key);
123
+ register const unsigned long hash = self->hash_i(key);
128
124
  register unsigned long perturb;
129
- register int mask = ht->mask;
130
- register HashEntry *he0 = ht->table;
125
+ register int mask = self->mask;
126
+ register HashEntry *he0 = self->table;
131
127
  register int i = hash & mask;
132
128
  register HashEntry *he = &he0[i];
133
- register HashEntry *freeslot;
134
-
135
- if (he->key == NULL || he->key == key) {
136
- he->hash = hash;
137
- return he;
138
- }
139
- if (he->key == dummy_key) {
140
- freeslot = he;
141
- }
142
- else {
143
- if ((he->hash == hash) && (strcmp(he->key, key) == 0)) {
144
- return he;
145
- }
146
- freeslot = NULL;
147
- }
148
-
149
- for (perturb = hash;; perturb >>= PERTURB_SHIFT) {
150
- i = (i << 2) + i + perturb + 1;
151
- he = &he0[i & mask];
152
- if (he->key == NULL) {
153
- if (freeslot != NULL) {
154
- he = freeslot;
155
- }
156
- he->hash = hash;
157
- return he;
158
- }
159
- if (he->key == key
160
- || (he->hash == hash
161
- && he->key != dummy_key && strcmp(he->key, key) == 0)) {
162
- return he;
163
- }
164
- if (he->key == dummy_key && freeslot == NULL) {
165
- freeslot = he;
166
- }
167
- }
168
- }
169
-
170
- HashEntry *h_lookup(HashTable *ht, register const void *key)
171
- {
172
- register unsigned int hash = ht->hash_i(key);
173
- register unsigned int perturb;
174
- register int mask = ht->mask;
175
- register HashEntry *he0 = ht->table;
176
- register int i = hash & mask;
177
- register HashEntry *he = &he0[i];
178
- register HashEntry *freeslot;
179
- eq_ft eq = ht->eq_i;
129
+ register HashEntry *freeslot = NULL;
130
+ eq_ft eq = self->eq_i;
180
131
 
181
132
  if (he->key == NULL || he->key == key) {
182
133
  he->hash = hash;
@@ -189,7 +140,6 @@ HashEntry *h_lookup(HashTable *ht, register const void *key)
189
140
  if ((he->hash == hash) && eq(he->key, key)) {
190
141
  return he;
191
142
  }
192
- freeslot = NULL;
193
143
  }
194
144
 
195
145
  for (perturb = hash;; perturb >>= PERTURB_SHIFT) {
@@ -213,58 +163,63 @@ HashEntry *h_lookup(HashTable *ht, register const void *key)
213
163
  }
214
164
  }
215
165
 
216
- HashTable *h_new_str(free_ft free_key, free_ft free_value)
166
+ Hash *h_new_str(free_ft free_key, free_ft free_value)
217
167
  {
218
- HashTable *ht;
168
+ Hash *self;
219
169
  if (num_free_hts > 0) {
220
- ht = free_hts[--num_free_hts];
170
+ self = free_hts[--num_free_hts];
221
171
  }
222
172
  else {
223
- ht = ALLOC(HashTable);
224
- }
225
- ht->fill = 0;
226
- ht->size = 0;
227
- ht->mask = HASH_MINSIZE - 1;
228
- ht->table = ht->smalltable;
229
- memset(ht->smalltable, 0, sizeof(ht->smalltable));
230
- ht->lookup_i = (lookup_ft)&h_lookup_str;
231
-
232
- ht->free_key_i = free_key != NULL ? free_key : &dummy_free;
233
- ht->free_value_i = free_value != NULL ? free_value : &dummy_free;
234
- ht->ref_cnt = 1;
235
- return ht;
173
+ self = ALLOC(Hash);
174
+ }
175
+ self->fill = 0;
176
+ self->size = 0;
177
+ self->mask = HASH_MINSIZE - 1;
178
+ self->table = self->smalltable;
179
+ memset(self->smalltable, 0, sizeof(self->smalltable));
180
+ self->lookup_i = (lookup_ft)&h_lookup;
181
+ self->eq_i = str_eq;
182
+ self->hash_i = (hash_ft)str_hash;
183
+
184
+ self->free_key_i = free_key != NULL ? free_key : &dummy_free;
185
+ self->free_value_i = free_value != NULL ? free_value : &dummy_free;
186
+ self->ref_cnt = 1;
187
+ return self;
236
188
  }
237
189
 
238
- HashTable *h_new_int(free_ft free_value)
190
+ Hash *h_new_int(free_ft free_value)
239
191
  {
240
- HashTable *ht = h_new_str(NULL, free_value);
241
- ht->lookup_i = &h_lookup_int;
242
- ht->eq_i = int_eq;
243
- ht->hash_i = int_hash;
244
- return ht;
192
+ Hash *self = h_new_str(NULL, free_value);
193
+
194
+ self->lookup_i = &h_lookup_ptr;
195
+ self->eq_i = NULL;
196
+ self->hash_i = NULL;
197
+
198
+ return self;
245
199
  }
246
200
 
247
- HashTable *h_new(hash_ft hash, eq_ft eq, free_ft free_key, free_ft free_value)
201
+ Hash *h_new(hash_ft hash, eq_ft eq, free_ft free_key, free_ft free_value)
248
202
  {
249
- HashTable *ht = h_new_str(free_key, free_value);
203
+ Hash *self = h_new_str(free_key, free_value);
250
204
 
251
- ht->lookup_i = &h_lookup;
252
- ht->eq_i = eq;
253
- ht->hash_i = hash;
254
- return ht;
205
+ self->lookup_i = &h_lookup;
206
+ self->eq_i = eq;
207
+ self->hash_i = hash;
208
+
209
+ return self;
255
210
  }
256
211
 
257
- void h_clear(HashTable *ht)
212
+ void h_clear(Hash *self)
258
213
  {
259
214
  int i;
260
215
  HashEntry *he;
261
- free_ft free_key = ht->free_key_i;
262
- free_ft free_value = ht->free_value_i;
216
+ free_ft free_key = self->free_key_i;
217
+ free_ft free_value = self->free_value_i;
263
218
 
264
219
  /* Clear all the hash values and keys as necessary */
265
220
  if (free_key != dummy_free || free_value != dummy_free) {
266
- for (i = 0; i <= ht->mask; i++) {
267
- he = &ht->table[i];
221
+ for (i = 0; i <= self->mask; i++) {
222
+ he = &self->table[i];
268
223
  if (he->key != NULL && he->key != dummy_key) {
269
224
  free_value(he->value);
270
225
  free_key(he->key);
@@ -272,50 +227,46 @@ void h_clear(HashTable *ht)
272
227
  he->key = NULL;
273
228
  }
274
229
  }
275
- ZEROSET_N(ht->table, HashEntry, ht->mask + 1);
276
- ht->size = 0;
277
- ht->fill = 0;
230
+ ZEROSET_N(self->table, HashEntry, self->mask + 1);
231
+ self->size = 0;
232
+ self->fill = 0;
278
233
  }
279
234
 
280
- void h_destroy(HashTable *ht)
235
+ void h_destroy(Hash *self)
281
236
  {
282
- if (--(ht->ref_cnt) <= 0) {
283
- h_clear(ht);
237
+ if (--(self->ref_cnt) <= 0) {
238
+ h_clear(self);
284
239
 
285
240
  /* if a new table was created, be sure to free it */
286
- if (ht->table != ht->smalltable) {
287
- free(ht->table);
241
+ if (self->table != self->smalltable) {
242
+ free(self->table);
288
243
  }
289
244
 
290
- #ifdef DEBUG
291
- free(ht);
292
- #else
293
245
  if (num_free_hts < MAX_FREE_HASH_TABLES) {
294
- free_hts[num_free_hts++] = ht;
246
+ free_hts[num_free_hts++] = self;
295
247
  }
296
248
  else {
297
- free(ht);
249
+ free(self);
298
250
  }
299
- #endif
300
251
  }
301
252
  }
302
253
 
303
- void *h_get(HashTable *ht, const void *key)
254
+ void *h_get(Hash *self, const void *key)
304
255
  {
305
256
  /* Note: lookup_i will never return NULL. */
306
- return ht->lookup_i(ht, key)->value;
257
+ return self->lookup_i(self, key)->value;
307
258
  }
308
259
 
309
- int h_del(HashTable *ht, const void *key)
260
+ int h_del(Hash *self, const void *key)
310
261
  {
311
- HashEntry *he = ht->lookup_i(ht, key);
262
+ HashEntry *he = self->lookup_i(self, key);
312
263
 
313
264
  if (he->key != NULL && he->key != dummy_key) {
314
- ht->free_key_i(he->key);
315
- ht->free_value_i(he->value);
265
+ self->free_key_i(he->key);
266
+ self->free_value_i(he->value);
316
267
  he->key = dummy_key;
317
268
  he->value = NULL;
318
- ht->size--;
269
+ self->size--;
319
270
  return true;
320
271
  }
321
272
  else {
@@ -323,20 +274,20 @@ int h_del(HashTable *ht, const void *key)
323
274
  }
324
275
  }
325
276
 
326
- void *h_rem(HashTable *ht, const void *key, bool destroy_key)
277
+ void *h_rem(Hash *self, const void *key, bool destroy_key)
327
278
  {
328
279
  void *val;
329
- HashEntry *he = ht->lookup_i(ht, key);
280
+ HashEntry *he = self->lookup_i(self, key);
330
281
 
331
282
  if (he->key != NULL && he->key != dummy_key) {
332
283
  if (destroy_key) {
333
- ht->free_key_i(he->key);
284
+ self->free_key_i(he->key);
334
285
  }
335
286
 
336
287
  he->key = dummy_key;
337
288
  val = he->value;
338
289
  he->value = NULL;
339
- ht->size--;
290
+ self->size--;
340
291
  return val;
341
292
  }
342
293
  else {
@@ -344,7 +295,7 @@ void *h_rem(HashTable *ht, const void *key, bool destroy_key)
344
295
  }
345
296
  }
346
297
 
347
- static int h_resize(HashTable *ht, int min_newsize)
298
+ static int h_resize(Hash *self, int min_newsize)
348
299
  {
349
300
  HashEntry smallcopy[HASH_MINSIZE];
350
301
  HashEntry *oldtable;
@@ -355,171 +306,167 @@ static int h_resize(HashTable *ht, int min_newsize)
355
306
  for (newsize = HASH_MINSIZE; newsize < min_newsize; newsize <<= 1) {
356
307
  }
357
308
 
358
- oldtable = ht->table;
309
+ oldtable = self->table;
359
310
  if (newsize == HASH_MINSIZE) {
360
- if (ht->table == ht->smalltable) {
361
- /* need to copy the d*(int *)ata out so we can rebuild the table into
311
+ if (self->table == self->smalltable) {
312
+ /* need to copy the data out so we can rebuild the table into
362
313
  * the same space */
363
- memcpy(smallcopy, ht->smalltable, sizeof(smallcopy));
314
+ memcpy(smallcopy, self->smalltable, sizeof(smallcopy));
364
315
  oldtable = smallcopy;
365
316
  }
366
317
  else {
367
- ht->table = ht->smalltable;
318
+ self->table = self->smalltable;
368
319
  }
369
320
  }
370
321
  else {
371
- ht->table = ALLOC_N(HashEntry, newsize);
322
+ self->table = ALLOC_N(HashEntry, newsize);
372
323
  }
373
- memset(ht->table, 0, sizeof(HashEntry) * newsize);
374
- ht->fill = ht->size;
375
- ht->mask = newsize - 1;
324
+ memset(self->table, 0, sizeof(HashEntry) * newsize);
325
+ self->fill = self->size;
326
+ self->mask = newsize - 1;
376
327
 
377
- for (num_active = ht->size, he_old = oldtable; num_active > 0; he_old++) {
328
+ for (num_active = self->size, he_old = oldtable; num_active > 0; he_old++) {
378
329
  if (he_old->key && he_old->key != dummy_key) { /* active entry */
379
- /*he_new = ht->lookup_i(ht, he_old->key); */
380
- he_new = h_resize_lookup(ht, he_old->hash);
330
+ /*he_new = self->lookup_i(self, he_old->key); */
331
+ he_new = h_resize_lookup(self, he_old->hash);
381
332
  he_new->key = he_old->key;
382
333
  he_new->value = he_old->value;
383
334
  num_active--;
384
335
  } /* else empty entry so nothing to do */
385
336
  }
386
- if (oldtable != smallcopy && oldtable != ht->smalltable) {
337
+ if (oldtable != smallcopy && oldtable != self->smalltable) {
387
338
  free(oldtable);
388
339
  }
389
340
  return 0;
390
341
  }
391
342
 
392
- int h_set(HashTable *ht, const void *key, void *value)
343
+ INLINE bool h_set_ext(Hash *self, const void *key, HashEntry **he)
393
344
  {
394
- int ret_val = HASH_KEY_DOES_NOT_EXIST;
395
- HashEntry *he = ht->lookup_i(ht, key);
396
- if (he->key == NULL) {
397
- if (ht->fill * 3 > ht->mask * 2) {
398
- h_resize(ht, ht->size * ((ht->size > SLOW_DOWN) ? 4 : 2));
399
- he = ht->lookup_i(ht, key);
400
- }
401
- ht->fill++;
402
- ht->size++;
403
- }
404
- else if (he->key == dummy_key) {
405
- ht->size++;
406
- }
407
- else if (he->key != key) {
408
- ht->free_key_i(he->key);
409
- if (he->value != value) {
410
- ht->free_value_i(he->value);
345
+ *he = self->lookup_i(self, key);
346
+ if ((*he)->key == NULL) {
347
+ if (self->fill * 3 > self->mask * 2) {
348
+ h_resize(self, self->size * ((self->size > SLOW_DOWN) ? 4 : 2));
349
+ *he = self->lookup_i(self, key);
411
350
  }
412
- ret_val = HASH_KEY_EQUAL;
351
+ self->fill++;
352
+ self->size++;
353
+ return true;
413
354
  }
414
- else {
415
- /* safety check. Only free old value if it isn't the new value */
416
- if (he->value != value) {
417
- ht->free_value_i(he->value);
418
- }
419
- ret_val = HASH_KEY_SAME;
355
+ else if ((*he)->key == dummy_key) {
356
+ self->size++;
357
+ return true;
420
358
  }
421
- he->key = (void *)key;
422
- he->value = value;
423
359
 
424
- /*
425
- if ((ht->fill > fill) && (ht->fill * 3 > ht->mask * 2)) {
426
- h_resize(ht, ht->size * ((ht->size > SLOW_DOWN) ? 4 : 2));
427
- }
428
- */
429
- return ret_val;
360
+ return false;
430
361
  }
431
362
 
432
- HashEntry *h_set_ext(HashTable *ht, const void *key)
363
+ HashKeyStatus h_set(Hash *self, const void *key, void *value)
433
364
  {
434
- HashEntry *he = ht->lookup_i(ht, key);
435
- if (he->key == NULL) {
436
- if (ht->fill * 3 > ht->mask * 2) {
437
- h_resize(ht, ht->size * ((ht->size > SLOW_DOWN) ? 4 : 2));
438
- he = ht->lookup_i(ht, key);
365
+ HashKeyStatus ret_val = HASH_KEY_DOES_NOT_EXIST;
366
+ HashEntry *he;
367
+ if (!h_set_ext(self, key, &he)) {
368
+ if (he->key != key) {
369
+ self->free_key_i(he->key);
370
+ if (he->value != value) {
371
+ self->free_value_i(he->value);
372
+ }
373
+ ret_val = HASH_KEY_EQUAL;
374
+ }
375
+ else {
376
+ /* Only free old value if it isn't the new value */
377
+ if (he->value != value) {
378
+ self->free_value_i(he->value);
379
+ }
380
+ ret_val = HASH_KEY_SAME;
439
381
  }
440
- ht->fill++;
441
- ht->size++;
442
- }
443
- else if (he->key == dummy_key) {
444
- ht->size++;
445
382
  }
383
+ he->key = (void *)key;
384
+ he->value = value;
446
385
 
447
- return he;
386
+ return ret_val;
448
387
  }
449
388
 
450
- int h_set_safe(HashTable *ht, const void *key, void *value)
389
+ int h_set_safe(Hash *self, const void *key, void *value)
451
390
  {
452
- HashEntry *he = ht->lookup_i(ht, key);
453
- int fill = ht->fill;
454
- if (he->key == NULL) {
455
- ht->fill++;
456
- ht->size++;
457
- }
458
- else if (he->key == dummy_key) {
459
- ht->size++;
391
+ HashEntry *he;
392
+ if (h_set_ext(self, key, &he)) {
393
+ he->key = (void *)key;
394
+ he->value = value;
395
+ return true;
460
396
  }
461
397
  else {
462
398
  return false;
463
399
  }
464
- he->key = (void *)key;
465
- he->value = value;
466
-
467
- if ((ht->fill > fill) && (ht->fill * 3 > ht->mask * 2)) {
468
- h_resize(ht, ht->size * ((ht->size > SLOW_DOWN) ? 4 : 2));
469
- }
470
- return true;
471
400
  }
472
401
 
473
- int h_has_key(HashTable *ht, const void *key)
402
+ HashKeyStatus h_has_key(Hash *self, const void *key)
474
403
  {
475
- HashEntry *he = ht->lookup_i(ht, key);
404
+ HashEntry *he = self->lookup_i(self, key);
476
405
  if (he->key == NULL || he->key == dummy_key) {
477
406
  return HASH_KEY_DOES_NOT_EXIST;
478
407
  }
479
408
  else if (he->key == key) {
480
409
  return HASH_KEY_SAME;
481
410
  }
482
- else {
483
- return HASH_KEY_EQUAL;
484
- }
411
+ return HASH_KEY_EQUAL;
485
412
  }
486
413
 
487
- void *h_get_int(HashTable *self, const unsigned long key)
414
+ INLINE void *h_get_int(Hash *self, const unsigned long key)
488
415
  {
489
- return h_get(self, &key);
416
+ return h_get(self, (const void *)key);
490
417
  }
491
418
 
492
- int h_del_int(HashTable *self, const unsigned long key)
419
+ INLINE int h_del_int(Hash *self, const unsigned long key)
493
420
  {
494
- return h_del(self, &key);
421
+ return h_del(self, (const void *)key);
495
422
  }
496
423
 
497
- void *h_rem_int(HashTable *self, const unsigned long key)
424
+ INLINE void *h_rem_int(Hash *self, const unsigned long key)
498
425
  {
499
- return h_rem(self, &key, false);
426
+ return h_rem(self, (const void *)key, false);
500
427
  }
501
428
 
502
- int h_set_int(HashTable *self, const unsigned long key, void *value)
429
+ INLINE HashKeyStatus h_set_int(Hash *self,
430
+ const unsigned long key,
431
+ void *value)
503
432
  {
504
- return h_set(self, &key, value);
433
+ HashKeyStatus ret_val = HASH_KEY_DOES_NOT_EXIST;
434
+ HashEntry *he;
435
+ if (!h_set_ext(self, (const void *)key, &he)) {
436
+ /* Only free old value if it isn't the new value */
437
+ if (he->value != value) {
438
+ self->free_value_i(he->value);
439
+ }
440
+ ret_val = HASH_KEY_EQUAL;
441
+ }
442
+ he->key = dummy_int_key;
443
+ he->value = value;
444
+
445
+ return ret_val;
505
446
  }
506
447
 
507
- int h_set_safe_int(HashTable *self, const unsigned long key, void *value)
448
+ INLINE int h_set_safe_int(Hash *self, const unsigned long key, void *value)
508
449
  {
509
- return h_set_safe(self, &key, value);
450
+ HashEntry *he;
451
+ if (h_set_ext(self, (const void *)key, &he)) {
452
+ he->key = dummy_int_key;
453
+ he->value = value;
454
+ return true;
455
+ }
456
+ return false;
510
457
  }
511
458
 
512
- int h_has_key_int(HashTable *self, const unsigned long key)
459
+ INLINE int h_has_key_int(Hash *self, const unsigned long key)
513
460
  {
514
- return h_has_key(self, &key);
461
+ return h_has_key(self, (const void *)key);
515
462
  }
516
463
 
517
- void h_each(HashTable *ht,
464
+ void h_each(Hash *self,
518
465
  void (*each_kv) (void *key, void *value, void *arg), void *arg)
519
466
  {
520
467
  HashEntry *he;
521
- int i = ht->size;
522
- for (he = ht->table; i > 0; he++) {
468
+ int i = self->size;
469
+ for (he = self->table; i > 0; he++) {
523
470
  if (he->key && he->key != dummy_key) { /* active entry */
524
471
  each_kv(he->key, he->value, arg);
525
472
  i--;
@@ -527,22 +474,19 @@ void h_each(HashTable *ht,
527
474
  }
528
475
  }
529
476
 
530
- HashTable *h_clone(HashTable *ht,
531
- h_clone_func_t clone_key, h_clone_func_t clone_value)
477
+ Hash *h_clone(Hash *self, h_clone_ft clone_key, h_clone_ft clone_value)
532
478
  {
533
479
  void *key, *value;
534
480
  HashEntry *he;
535
- int i = ht->size;
536
- HashTable *ht_clone;
481
+ int i = self->size;
482
+ Hash *ht_clone;
537
483
 
538
- if (ht->lookup_i == (lookup_ft)&h_lookup_str) {
539
- ht_clone = h_new_str(ht->free_key_i, ht->free_value_i);
540
- }
541
- else {
542
- ht_clone = h_new(ht->hash_i, ht->eq_i, ht->free_key_i, ht->free_value_i);
543
- }
484
+ ht_clone = h_new(self->hash_i,
485
+ self->eq_i,
486
+ self->free_key_i,
487
+ self->free_value_i);
544
488
 
545
- for (he = ht->table; i > 0; he++) {
489
+ for (he = self->table; i > 0; he++) {
546
490
  if (he->key && he->key != dummy_key) { /* active entry */
547
491
  key = clone_key ? clone_key(he->key) : he->key;
548
492
  value = clone_value ? clone_value(he->value) : he->value;
@@ -553,15 +497,28 @@ HashTable *h_clone(HashTable *ht,
553
497
  return ht_clone;
554
498
  }
555
499
 
556
- void h_str_print_keys(HashTable *ht)
500
+ void h_str_print_keys(Hash *self, FILE *out)
557
501
  {
558
502
  HashEntry *he;
559
- int i = ht->size;
560
- printf("keys:\n");
561
- for (he = ht->table; i > 0; he++) {
503
+ int i = self->size;
504
+ char **keys = ALLOC_N(char *, self->size);
505
+ for (he = self->table; i > 0; he++) {
562
506
  if (he->key && he->key != dummy_key) { /* active entry */
563
- printf("\t%s\n", (char *)he->key);
564
507
  i--;
508
+ keys[i] = (char *)he->key;
565
509
  }
566
510
  }
511
+ strsort(keys, self->size);
512
+ fprintf(out, "keys:\n");
513
+ for (i = 0; i < self->size; i++) {
514
+ fprintf(out, "\t%s\n", keys[i]);
515
+ }
516
+ free(keys);
517
+ }
518
+
519
+ void hash_finalize()
520
+ {
521
+ while (num_free_hts > 0) {
522
+ free(free_hts[--num_free_hts]);
523
+ }
567
524
  }