isomorfeus-ferret 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +612 -0
  3. data/README.md +44 -0
  4. data/ext/isomorfeus_ferret_ext/benchmark.c +223 -0
  5. data/ext/isomorfeus_ferret_ext/benchmark.h +45 -0
  6. data/ext/isomorfeus_ferret_ext/benchmarks_all.h +25 -0
  7. data/ext/isomorfeus_ferret_ext/bm_bitvector.c +123 -0
  8. data/ext/isomorfeus_ferret_ext/bm_hash.c +118 -0
  9. data/ext/isomorfeus_ferret_ext/bm_micro_string.c +40 -0
  10. data/ext/isomorfeus_ferret_ext/bm_store.c +93 -0
  11. data/ext/isomorfeus_ferret_ext/email.rl +21 -0
  12. data/ext/isomorfeus_ferret_ext/extconf.rb +5 -0
  13. data/ext/isomorfeus_ferret_ext/fio_tmpfile.h +53 -0
  14. data/ext/isomorfeus_ferret_ext/frb_analysis.c +2577 -0
  15. data/ext/isomorfeus_ferret_ext/frb_index.c +3457 -0
  16. data/ext/isomorfeus_ferret_ext/frb_lang.c +9 -0
  17. data/ext/isomorfeus_ferret_ext/frb_lang.h +17 -0
  18. data/ext/isomorfeus_ferret_ext/frb_qparser.c +629 -0
  19. data/ext/isomorfeus_ferret_ext/frb_search.c +4460 -0
  20. data/ext/isomorfeus_ferret_ext/frb_store.c +515 -0
  21. data/ext/isomorfeus_ferret_ext/frb_threading.h +30 -0
  22. data/ext/isomorfeus_ferret_ext/frb_utils.c +1127 -0
  23. data/ext/isomorfeus_ferret_ext/frt_analysis.c +1644 -0
  24. data/ext/isomorfeus_ferret_ext/frt_analysis.h +247 -0
  25. data/ext/isomorfeus_ferret_ext/frt_array.c +124 -0
  26. data/ext/isomorfeus_ferret_ext/frt_array.h +54 -0
  27. data/ext/isomorfeus_ferret_ext/frt_bitvector.c +95 -0
  28. data/ext/isomorfeus_ferret_ext/frt_bitvector.h +586 -0
  29. data/ext/isomorfeus_ferret_ext/frt_compound_io.c +374 -0
  30. data/ext/isomorfeus_ferret_ext/frt_config.h +44 -0
  31. data/ext/isomorfeus_ferret_ext/frt_document.c +134 -0
  32. data/ext/isomorfeus_ferret_ext/frt_document.h +52 -0
  33. data/ext/isomorfeus_ferret_ext/frt_except.c +95 -0
  34. data/ext/isomorfeus_ferret_ext/frt_except.h +188 -0
  35. data/ext/isomorfeus_ferret_ext/frt_field_index.c +233 -0
  36. data/ext/isomorfeus_ferret_ext/frt_field_index.h +42 -0
  37. data/ext/isomorfeus_ferret_ext/frt_filter.c +157 -0
  38. data/ext/isomorfeus_ferret_ext/frt_fs_store.c +502 -0
  39. data/ext/isomorfeus_ferret_ext/frt_global.c +427 -0
  40. data/ext/isomorfeus_ferret_ext/frt_global.h +290 -0
  41. data/ext/isomorfeus_ferret_ext/frt_hash.c +518 -0
  42. data/ext/isomorfeus_ferret_ext/frt_hash.h +466 -0
  43. data/ext/isomorfeus_ferret_ext/frt_hashset.c +191 -0
  44. data/ext/isomorfeus_ferret_ext/frt_hashset.h +206 -0
  45. data/ext/isomorfeus_ferret_ext/frt_helper.c +62 -0
  46. data/ext/isomorfeus_ferret_ext/frt_helper.h +13 -0
  47. data/ext/isomorfeus_ferret_ext/frt_ind.c +353 -0
  48. data/ext/isomorfeus_ferret_ext/frt_ind.h +54 -0
  49. data/ext/isomorfeus_ferret_ext/frt_index.c +6377 -0
  50. data/ext/isomorfeus_ferret_ext/frt_index.h +880 -0
  51. data/ext/isomorfeus_ferret_ext/frt_lang.c +104 -0
  52. data/ext/isomorfeus_ferret_ext/frt_lang.h +44 -0
  53. data/ext/isomorfeus_ferret_ext/frt_mempool.c +87 -0
  54. data/ext/isomorfeus_ferret_ext/frt_mempool.h +33 -0
  55. data/ext/isomorfeus_ferret_ext/frt_multimapper.c +349 -0
  56. data/ext/isomorfeus_ferret_ext/frt_multimapper.h +52 -0
  57. data/ext/isomorfeus_ferret_ext/frt_posh.c +1006 -0
  58. data/ext/isomorfeus_ferret_ext/frt_posh.h +973 -0
  59. data/ext/isomorfeus_ferret_ext/frt_priorityqueue.c +147 -0
  60. data/ext/isomorfeus_ferret_ext/frt_priorityqueue.h +147 -0
  61. data/ext/isomorfeus_ferret_ext/frt_q_boolean.c +1612 -0
  62. data/ext/isomorfeus_ferret_ext/frt_q_const_score.c +157 -0
  63. data/ext/isomorfeus_ferret_ext/frt_q_filtered_query.c +209 -0
  64. data/ext/isomorfeus_ferret_ext/frt_q_fuzzy.c +281 -0
  65. data/ext/isomorfeus_ferret_ext/frt_q_match_all.c +147 -0
  66. data/ext/isomorfeus_ferret_ext/frt_q_multi_term.c +672 -0
  67. data/ext/isomorfeus_ferret_ext/frt_q_parser.c +3084 -0
  68. data/ext/isomorfeus_ferret_ext/frt_q_phrase.c +1182 -0
  69. data/ext/isomorfeus_ferret_ext/frt_q_prefix.c +98 -0
  70. data/ext/isomorfeus_ferret_ext/frt_q_range.c +665 -0
  71. data/ext/isomorfeus_ferret_ext/frt_q_span.c +2386 -0
  72. data/ext/isomorfeus_ferret_ext/frt_q_term.c +311 -0
  73. data/ext/isomorfeus_ferret_ext/frt_q_wildcard.c +166 -0
  74. data/ext/isomorfeus_ferret_ext/frt_ram_store.c +460 -0
  75. data/ext/isomorfeus_ferret_ext/frt_scanner.c +899 -0
  76. data/ext/isomorfeus_ferret_ext/frt_scanner.h +28 -0
  77. data/ext/isomorfeus_ferret_ext/frt_scanner_mb.c +6705 -0
  78. data/ext/isomorfeus_ferret_ext/frt_scanner_utf8.c +4419 -0
  79. data/ext/isomorfeus_ferret_ext/frt_search.c +1824 -0
  80. data/ext/isomorfeus_ferret_ext/frt_search.h +924 -0
  81. data/ext/isomorfeus_ferret_ext/frt_similarity.c +150 -0
  82. data/ext/isomorfeus_ferret_ext/frt_similarity.h +79 -0
  83. data/ext/isomorfeus_ferret_ext/frt_sort.c +796 -0
  84. data/ext/isomorfeus_ferret_ext/frt_stopwords.c +395 -0
  85. data/ext/isomorfeus_ferret_ext/frt_store.c +680 -0
  86. data/ext/isomorfeus_ferret_ext/frt_store.h +789 -0
  87. data/ext/isomorfeus_ferret_ext/frt_term_vectors.c +72 -0
  88. data/ext/isomorfeus_ferret_ext/frt_threading.h +23 -0
  89. data/ext/isomorfeus_ferret_ext/frt_win32.h +54 -0
  90. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.c +409 -0
  91. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.h +95 -0
  92. data/ext/isomorfeus_ferret_ext/libstemmer.c +93 -0
  93. data/ext/isomorfeus_ferret_ext/libstemmer.h +73 -0
  94. data/ext/isomorfeus_ferret_ext/q_parser.y +1366 -0
  95. data/ext/isomorfeus_ferret_ext/scanner.h +28 -0
  96. data/ext/isomorfeus_ferret_ext/scanner.in +43 -0
  97. data/ext/isomorfeus_ferret_ext/scanner.rl +84 -0
  98. data/ext/isomorfeus_ferret_ext/scanner_mb.rl +200 -0
  99. data/ext/isomorfeus_ferret_ext/scanner_utf8.rl +85 -0
  100. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.c +324 -0
  101. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.h +7 -0
  102. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.c +610 -0
  103. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.h +6 -0
  104. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.c +1104 -0
  105. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.h +6 -0
  106. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.c +749 -0
  107. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.h +7 -0
  108. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.c +1233 -0
  109. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.h +6 -0
  110. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.c +490 -0
  111. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.h +6 -0
  112. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.c +1217 -0
  113. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.h +7 -0
  114. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.c +1052 -0
  115. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.h +6 -0
  116. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.c +283 -0
  117. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.h +6 -0
  118. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.c +735 -0
  119. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.h +6 -0
  120. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.c +1003 -0
  121. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.h +7 -0
  122. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.c +1079 -0
  123. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.h +6 -0
  124. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.c +293 -0
  125. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.h +6 -0
  126. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.c +984 -0
  127. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.h +6 -0
  128. data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.c +686 -0
  129. data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.h +6 -0
  130. data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.c +325 -0
  131. data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.h +6 -0
  132. data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.c +620 -0
  133. data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.h +6 -0
  134. data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.c +1111 -0
  135. data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.h +6 -0
  136. data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.c +754 -0
  137. data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.h +6 -0
  138. data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.c +1242 -0
  139. data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.h +6 -0
  140. data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.c +495 -0
  141. data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.h +6 -0
  142. data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.c +1220 -0
  143. data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.h +6 -0
  144. data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.c +1059 -0
  145. data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.h +6 -0
  146. data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.c +285 -0
  147. data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.h +6 -0
  148. data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.c +741 -0
  149. data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.h +6 -0
  150. data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.c +1009 -0
  151. data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.h +6 -0
  152. data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.c +990 -0
  153. data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.h +6 -0
  154. data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.c +680 -0
  155. data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.h +6 -0
  156. data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.c +1083 -0
  157. data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.h +6 -0
  158. data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.c +294 -0
  159. data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.h +6 -0
  160. data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.c +2191 -0
  161. data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.h +6 -0
  162. data/ext/isomorfeus_ferret_ext/stem_api.c +66 -0
  163. data/ext/isomorfeus_ferret_ext/stem_api.h +26 -0
  164. data/ext/isomorfeus_ferret_ext/stem_header.h +57 -0
  165. data/ext/isomorfeus_ferret_ext/stem_modules.h +190 -0
  166. data/ext/isomorfeus_ferret_ext/stem_modules.txt +50 -0
  167. data/ext/isomorfeus_ferret_ext/stem_utilities.c +478 -0
  168. data/ext/isomorfeus_ferret_ext/test.c +850 -0
  169. data/ext/isomorfeus_ferret_ext/test.h +416 -0
  170. data/ext/isomorfeus_ferret_ext/test_1710.c +63 -0
  171. data/ext/isomorfeus_ferret_ext/test_analysis.c +1221 -0
  172. data/ext/isomorfeus_ferret_ext/test_array.c +272 -0
  173. data/ext/isomorfeus_ferret_ext/test_bitvector.c +600 -0
  174. data/ext/isomorfeus_ferret_ext/test_compound_io.c +170 -0
  175. data/ext/isomorfeus_ferret_ext/test_document.c +156 -0
  176. data/ext/isomorfeus_ferret_ext/test_except.c +244 -0
  177. data/ext/isomorfeus_ferret_ext/test_fields.c +522 -0
  178. data/ext/isomorfeus_ferret_ext/test_file_deleter.c +185 -0
  179. data/ext/isomorfeus_ferret_ext/test_filter.c +331 -0
  180. data/ext/isomorfeus_ferret_ext/test_fs_store.c +25 -0
  181. data/ext/isomorfeus_ferret_ext/test_global.c +299 -0
  182. data/ext/isomorfeus_ferret_ext/test_hash.c +485 -0
  183. data/ext/isomorfeus_ferret_ext/test_hashset.c +288 -0
  184. data/ext/isomorfeus_ferret_ext/test_helper.c +47 -0
  185. data/ext/isomorfeus_ferret_ext/test_highlighter.c +548 -0
  186. data/ext/isomorfeus_ferret_ext/test_index.c +2323 -0
  187. data/ext/isomorfeus_ferret_ext/test_lang.c +74 -0
  188. data/ext/isomorfeus_ferret_ext/test_mempool.c +102 -0
  189. data/ext/isomorfeus_ferret_ext/test_multimapper.c +64 -0
  190. data/ext/isomorfeus_ferret_ext/test_priorityqueue.c +213 -0
  191. data/ext/isomorfeus_ferret_ext/test_q_const_score.c +84 -0
  192. data/ext/isomorfeus_ferret_ext/test_q_filtered.c +61 -0
  193. data/ext/isomorfeus_ferret_ext/test_q_fuzzy.c +241 -0
  194. data/ext/isomorfeus_ferret_ext/test_q_parser.c +464 -0
  195. data/ext/isomorfeus_ferret_ext/test_q_span.c +575 -0
  196. data/ext/isomorfeus_ferret_ext/test_ram_store.c +77 -0
  197. data/ext/isomorfeus_ferret_ext/test_search.c +1874 -0
  198. data/ext/isomorfeus_ferret_ext/test_segments.c +167 -0
  199. data/ext/isomorfeus_ferret_ext/test_similarity.c +25 -0
  200. data/ext/isomorfeus_ferret_ext/test_sort.c +333 -0
  201. data/ext/isomorfeus_ferret_ext/test_store.c +591 -0
  202. data/ext/isomorfeus_ferret_ext/test_store.h +3 -0
  203. data/ext/isomorfeus_ferret_ext/test_term.c +351 -0
  204. data/ext/isomorfeus_ferret_ext/test_term_vectors.c +373 -0
  205. data/ext/isomorfeus_ferret_ext/test_test.c +83 -0
  206. data/ext/isomorfeus_ferret_ext/test_threading.c +188 -0
  207. data/ext/isomorfeus_ferret_ext/testhelper.c +561 -0
  208. data/ext/isomorfeus_ferret_ext/testhelper.h +25 -0
  209. data/ext/isomorfeus_ferret_ext/tests_all.h +87 -0
  210. data/ext/isomorfeus_ferret_ext/uchar-ucs4.rl +1854 -0
  211. data/ext/isomorfeus_ferret_ext/uchar-utf8.rl +1999 -0
  212. data/ext/isomorfeus_ferret_ext/url.rl +27 -0
  213. data/ext/isomorfeus_ferret_ext/word_list.h +15156 -0
  214. data/lib/isomorfeus/ferret/document.rb +132 -0
  215. data/lib/isomorfeus/ferret/field_symbol.rb +85 -0
  216. data/lib/isomorfeus/ferret/index/field_infos.rb +48 -0
  217. data/lib/isomorfeus/ferret/index/index.rb +970 -0
  218. data/lib/isomorfeus/ferret/monitor.rb +323 -0
  219. data/lib/isomorfeus/ferret/stdlib_patches.rb +151 -0
  220. data/lib/isomorfeus/ferret/version.rb +5 -0
  221. data/lib/isomorfeus-ferret.rb +8 -0
  222. metadata +307 -0
@@ -0,0 +1,586 @@
1
+ #ifndef FRT_BIT_VECTOR_H
2
+ #define FRT_BIT_VECTOR_H
3
+
4
+ #include "frt_global.h"
5
+
6
+ #define FRT_BV_INIT_CAPA 256
7
+
8
+ typedef struct FrtBitVector
9
+ {
10
+ /** The bits are held in an array of 32-bit integers */
11
+ frt_u32 *bits;
12
+
13
+ /** size is equal to 1 + the highest order bit set */
14
+ int size;
15
+
16
+ /** capa is the number of words (U32) allocated for the bits */
17
+ int capa;
18
+
19
+ /** count is the running count of bits set. This is kept up to
20
+ * date by frt_bv_set and frt_bv_unset. You can reset this value
21
+ * by calling frt_bv_recount */
22
+ int count;
23
+
24
+ /** curr_bit is used by scan_next to record the previously scanned bit */
25
+ int curr_bit;
26
+
27
+ bool extends_as_ones : 1;
28
+ int ref_cnt;
29
+ } FrtBitVector;
30
+
31
+ /**
32
+ * Create a new FrtBitVector with a capacity of
33
+ * +FRT_BV_INIT_CAPA+. Note that the FrtBitVector is growable and will
34
+ * adjust it's capacity when you use frt_bv_set.
35
+ *
36
+ * @return FrtBitVector with a capacity of +FRT_BV_INIT_CAPA+.
37
+ */
38
+ extern FRT_ATTR_MALLOC
39
+ FrtBitVector *frt_bv_new();
40
+
41
+ /**
42
+ * Create a new FrtBitVector with a capacity of +capa+. Note that the
43
+ * FrtBitVector is growable and will adjust it's capacity when you use
44
+ * frt_bv_set.
45
+ *
46
+ * @param capa the initial capacity of the FrtBitVector
47
+ * @return FrtBitVector with a capacity of +capa+.
48
+ */
49
+ extern FRT_ATTR_MALLOC
50
+ FrtBitVector *frt_bv_new_capa(int capa);
51
+
52
+ /**
53
+ * Destroy a FrtBitVector, freeing all memory allocated to that
54
+ * FrtBitVector
55
+ *
56
+ * @param bv FrtBitVector to destroy
57
+ */
58
+ extern void frt_bv_destroy(FrtBitVector *bv);
59
+
60
+ /**
61
+ * Set the bit at position +index+ with +value+. If +index+ is outside
62
+ * of the range of the FrtBitVector, that is >= FrtBitVector.size,
63
+ * FrtBitVector.size will be set to +index+ + 1. If it is greater than
64
+ * the capacity of the FrtBitVector, the capacity will be expanded to
65
+ * accomodate.
66
+ *
67
+ * @param bv the FrtBitVector to set the bit in
68
+ * @param index the index of the bit to set
69
+ * @param value the boolean value
70
+ */
71
+
72
+ /*
73
+ * FIXME: if the top set bit is unset, size is not adjusted. This will not
74
+ * cause any bugs in this code but could cause problems if users are relying
75
+ * on the fact that size is accurate.
76
+ */
77
+ static FRT_ATTR_ALWAYS_INLINE
78
+ void frt_bv_set_value(FrtBitVector *bv, int bit, bool value)
79
+ {
80
+ frt_u32 *word_p;
81
+ int word = bit >> 5;
82
+ frt_u32 bitmask = 1 << (bit & 31);
83
+
84
+ /* Check to see if we need to grow the BitVector */
85
+ if (unlikely(bit >= bv->size)) {
86
+ bv->size = bit + 1; /* size is max range of bits set */
87
+ if (word >= bv->capa) {
88
+ int capa = bv->capa << 1;
89
+ while (capa <= word) {
90
+ capa <<= 1;
91
+ }
92
+ FRT_REALLOC_N(bv->bits, frt_u32, capa);
93
+ memset(bv->bits + bv->capa, (bv->extends_as_ones ? 0xFF : 0),
94
+ sizeof(frt_u32) * (capa - bv->capa));
95
+ bv->capa = capa;
96
+ }
97
+ }
98
+
99
+ /* Set the required bit */
100
+ word_p = &(bv->bits[word]);
101
+ if ((!!(bitmask & *word_p)) != value) {
102
+ if (value) {
103
+ bv->count++;
104
+ *word_p |= bitmask;
105
+ }
106
+ else {
107
+ bv->count--;
108
+ *word_p &= ~bitmask;
109
+ }
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Set the bit at position +index+. If +index+ is outside of the range
115
+ * of the FrtBitVector, that is >= FrtBitVector.size,
116
+ * FrtBitVector.size will be set to +index+ + 1. If it is greater than
117
+ * the capacity of the FrtBitVector, the capacity will be expanded to
118
+ * accomodate.
119
+ *
120
+ * @param bv the FrtBitVector to set the bit in
121
+ * @param index the index of the bit to set
122
+ */
123
+ static FRT_ATTR_ALWAYS_INLINE
124
+ void frt_bv_set(FrtBitVector *bv, int bit)
125
+ {
126
+ frt_bv_set_value(bv, bit, 1);
127
+ }
128
+
129
+ /**
130
+ * Unsafely set the bit at position +index+. If you choose to use this
131
+ * function you must create the FrtBitVector with a large enough
132
+ * capacity to accomodate all of the frt_bv_set_fast operations. You
133
+ * must also set bits in order and only one time per bit. Otherwise,
134
+ * use the safe frt_bv_set function.
135
+ *
136
+ * So this is ok;
137
+ * <pre>
138
+ * FrtBitVector *bv = frt_bv_new_capa(1000);
139
+ * frt_bv_set_fast(bv, 900);
140
+ * frt_bv_set_fast(bv, 920);
141
+ * frt_bv_set_fast(bv, 999);
142
+ * </pre>
143
+ *
144
+ * While these are not ok;
145
+ * <pre>
146
+ * FrtBitVector *bv = frt_bv_new_capa(90);
147
+ * frt_bv_set_fast(bv, 80);
148
+ * frt_bv_set_fast(bv, 79); // <= Bad: Out of Order
149
+ * frt_bv_set_fast(bv, 80); // <= Bad: Already set
150
+ * frt_bv_set_fast(bv, 90); // <= Bad: Out of Range. index must be < capa
151
+ * </pre>
152
+ *
153
+ * @param bv the FrtBitVector to set the bit in
154
+ * @param index the index of the bit to set
155
+ */
156
+ static FRT_ATTR_ALWAYS_INLINE
157
+ void frt_bv_set_fast(FrtBitVector *bv, int bit)
158
+ {
159
+ bv->count++;
160
+ bv->size = bit + 1;
161
+ bv->bits[bit >> 5] |= (1 << (bit & 31));
162
+ }
163
+
164
+ /**
165
+ * Return 1 if the bit at +index+ was set or 0 otherwise. If +index+
166
+ * is out of range, that is greater then the BitVectors capacity, it
167
+ * will also return 0.
168
+ *
169
+ * @param bv the FrtBitVector to check in
170
+ * @param index the index of the bit to check
171
+ * @return 1 if the bit was set, 0 otherwise
172
+ */
173
+ static FRT_ATTR_ALWAYS_INLINE
174
+ int frt_bv_get(FrtBitVector *bv, int bit)
175
+ {
176
+ /* out of range so return 0 because it can't have been set */
177
+ if (unlikely(bit >= bv->size)) {
178
+ return bv->extends_as_ones;
179
+ }
180
+ return (bv->bits[bit >> 5] >> (bit & 31)) & 0x01;
181
+ }
182
+
183
+ /**
184
+ * Unset the bit at position +index+. If the +index+ was out of range,
185
+ * that is greater than the BitVectors capacity then do
186
+ * nothing. (frt_bv_get will return 0 in this case anyway).
187
+ *
188
+ * @param bv the FrtBitVector to unset the bit in
189
+ * @param index the index of the bit to unset
190
+ */
191
+ static FRT_ATTR_ALWAYS_INLINE
192
+ void frt_bv_unset(FrtBitVector *bv, int bit)
193
+ {
194
+ frt_bv_set_value(bv, bit, 0);
195
+ }
196
+
197
+ /**
198
+ * Clear all set bits. This function will set all set bits to 0.
199
+ *
200
+ * @param bv the FrtBitVector to clear
201
+ */
202
+ extern void frt_bv_clear(FrtBitVector *bv);
203
+
204
+ /**
205
+ * Resets the set bit count by running through the whole FrtBitVector
206
+ * and counting all set bits. A running count of the bits is kept by
207
+ * frt_bv_set, *frt_bv_get and frt_bv_set_fast so this function is
208
+ * only necessary if the count could have been corrupted somehow or if
209
+ * the FrtBitVector has been constructed in a different way (for
210
+ * example being read from the file_system).
211
+ *
212
+ * @param bv the FrtBitVector to count the bits in
213
+ * @return the number of set bits in the FrtBitVector. FrtBitVector.count is also
214
+ * set
215
+ */
216
+ static FRT_ATTR_ALWAYS_INLINE
217
+ int frt_bv_recount(FrtBitVector *bv)
218
+ {
219
+ unsigned int extra = ((bv->size & 31) >> 3) + 1;
220
+ unsigned int len = bv->size >> 5;
221
+ unsigned int idx, count = 0;
222
+
223
+ if (bv->extends_as_ones) {
224
+ for (idx = 0; idx < len; ++idx) {
225
+ count += frt_count_zeros(bv->bits[idx]);
226
+ }
227
+ switch (extra) {
228
+ case 4: count += frt_count_zeros(bv->bits[idx] | 0x00ffffff);
229
+ case 3: count += frt_count_zeros(bv->bits[idx] | 0xff00ffff);
230
+ case 2: count += frt_count_zeros(bv->bits[idx] | 0xffff00ff);
231
+ case 1: count += frt_count_zeros(bv->bits[idx] | 0xffffff00);
232
+ }
233
+ }
234
+ else {
235
+ for (idx = 0; idx < len; ++idx) {
236
+ count += frt_count_ones(bv->bits[idx]);
237
+ }
238
+ switch (extra) {
239
+ case 4: count += frt_count_ones(bv->bits[idx] & 0xff000000);
240
+ case 3: count += frt_count_ones(bv->bits[idx] & 0x00ff0000);
241
+ case 2: count += frt_count_ones(bv->bits[idx] & 0x0000ff00);
242
+ case 1: count += frt_count_ones(bv->bits[idx] & 0x000000ff);
243
+ }
244
+ }
245
+ return bv->count = count;
246
+ }
247
+
248
+ /**
249
+ * Reset the FrtBitVector for scanning. This function should be called
250
+ * before using frt_bv_scan_next to scan through all set bits in the
251
+ * FrtBitVector. This is not necessary when using
252
+ * frt_bv_scan_next_from.
253
+ *
254
+ * @param bv the FrtBitVector to reset for scanning
255
+ */
256
+ extern void frt_bv_scan_reset(FrtBitVector *bv);
257
+
258
+ /**
259
+ * Scan the FrtBitVector for the next set bit after +from+. If no more
260
+ * bits are set then return -1, otherwise return the index of teh next
261
+ * set bit.
262
+ *
263
+ * @param bv the FrtBitVector to scan
264
+ * @return the next set bit's index or -1 if no more bits are set
265
+ */
266
+ static FRT_ATTR_ALWAYS_INLINE
267
+ int frt_bv_scan_next_from(FrtBitVector *bv, const int bit)
268
+ {
269
+ frt_u32 pos = bit >> 5;
270
+ frt_u32 word = bv->bits[pos];
271
+
272
+ if (bit >= bv->size)
273
+ return -1;
274
+
275
+ /* Keep only the bits above this position */
276
+ word &= (frt_u32)~0 << (bit & 31);
277
+ if (word) {
278
+ goto done;
279
+ }
280
+ else {
281
+ frt_u32 word_size = FRT_TO_WORD(bv->size);
282
+ for (pos++; pos < word_size; ++pos)
283
+ {
284
+ if ( (word = bv->bits[pos]) )
285
+ goto done;
286
+ }
287
+ }
288
+ return -1;
289
+ done:
290
+ return bv->curr_bit = (pos << 5) + frt_count_trailing_zeros(word);
291
+ }
292
+
293
+ /**
294
+ * Scan the FrtBitVector for the next set bit. Before using this
295
+ * function you should reset the FrtBitVector for scanning using
296
+ * +frt_bv_scan_reset+. You can the repeatedly call frt_bv_scan_next
297
+ * to get each set bit until it finally returns -1.
298
+ *
299
+ * @param bv the FrtBitVector to scan
300
+ * @return the next set bits index or -1 if no more bits are set
301
+ */
302
+ static FRT_ATTR_ALWAYS_INLINE
303
+ int frt_bv_scan_next(FrtBitVector *bv)
304
+ {
305
+ return frt_bv_scan_next_from(bv, bv->curr_bit + 1);
306
+ }
307
+
308
+ /**
309
+ * Scan the FrtBitVector for the next unset bit after +from+. If no
310
+ * more bits are unset then return -1, otherwise return the index of
311
+ * teh next unset bit.
312
+ *
313
+ * @param bv the FrtBitVector to scan
314
+ * @return the next unset bit's index or -1 if no more bits are unset
315
+ */
316
+ static FRT_ATTR_ALWAYS_INLINE
317
+ int frt_bv_scan_next_unset_from(FrtBitVector *bv, const int bit)
318
+ {
319
+ frt_u32 pos = bit >> 5;
320
+ frt_u32 word = bv->bits[pos];
321
+
322
+ if (bit >= bv->size)
323
+ return -1;
324
+
325
+ /* Set all of the bits below this position */
326
+ word |= (1 << (bit & 31)) - 1;
327
+ if (~word) {
328
+ goto done;
329
+ }
330
+ else {
331
+ frt_u32 word_size = FRT_TO_WORD(bv->size);
332
+ for (pos++; pos < word_size; ++pos)
333
+ {
334
+ if ( ~(word = bv->bits[pos]) )
335
+ goto done;
336
+ }
337
+ }
338
+ return -1;
339
+ done:
340
+ return bv->curr_bit = (pos << 5) + frt_count_trailing_ones(word);
341
+ }
342
+
343
+ /**
344
+ * Scan the FrtBitVector for the next unset bit. Before using this
345
+ * function you should reset the FrtBitVector for scanning using
346
+ * +frt_bv_scan_reset+. You can the repeated call frt_bv_scan_next to
347
+ * get each unset bit until it finally returns -1.
348
+ *
349
+ * @param bv the FrtBitVector to scan
350
+ * @return the next unset bits index or -1 if no more bits are unset
351
+ */
352
+ static FRT_ATTR_ALWAYS_INLINE
353
+ int frt_bv_scan_next_unset(FrtBitVector *bv)
354
+ {
355
+ return frt_bv_scan_next_unset_from(bv, bv->curr_bit + 1);
356
+ }
357
+
358
+ /**
359
+ * Check whether the two BitVectors have the same bits set.
360
+ *
361
+ * @param bv1 first FrtBitVector to compare
362
+ * @param bv2 second BitVectors to compare
363
+ * @return true if bv1 == bv2
364
+ */
365
+ extern int frt_bv_eq(FrtBitVector *bv1, FrtBitVector *bv2);
366
+
367
+ /**
368
+ * Determines a hash value for the FrtBitVector
369
+ *
370
+ * @param bv the FrtBitVector to hash
371
+ * @return A hash value for the FrtBitVector
372
+ */
373
+ extern unsigned long long frt_bv_hash(FrtBitVector *bv);
374
+
375
+ static FRT_ATTR_ALWAYS_INLINE
376
+ void frt_bv_capa(FrtBitVector *bv, int capa, int size)
377
+ {
378
+ int word_size = FRT_TO_WORD(size);
379
+ if (bv->capa < capa)
380
+ {
381
+ FRT_REALLOC_N(bv->bits, frt_u32, capa);
382
+ bv->capa = capa;
383
+ memset(bv->bits + word_size, (bv->extends_as_ones ? 0xFF : 0),
384
+ sizeof(frt_u32) * (capa - word_size));
385
+ }
386
+ bv->size = size;
387
+ }
388
+
389
+ #define frt_bv_and_ext(dest, src, extends_as_ones, i, max) do { \
390
+ if (extends_as_ones) \
391
+ memcpy(&dest[i], &src[i], sizeof(*dest)*(max - i)); \
392
+ else memset(&dest[i], 0x00 , sizeof(*dest)*(max - i)); \
393
+ } while(0)
394
+
395
+ #define frt_bv_or_ext(dest, src, extends_as_ones, i, max) do { \
396
+ if (extends_as_ones) \
397
+ memset(&dest[i], 0xFF , sizeof(*dest)*(max - i)); \
398
+ else memcpy(&dest[i], &src[i], sizeof(*dest)*(max - i)); \
399
+ } while(0)
400
+
401
+ #define frt_bv_xor_ext(dest, src, extends_as_ones, i, max) do { \
402
+ frt_u32 n = (extends_as_ones ? 0xffffffff : 0); \
403
+ for (; i < max; ++i) \
404
+ dest[i] = src[i] ^ n; \
405
+ } while(0)
406
+
407
+ #define FRT_BV_OP(bv, a, b, op, ext_cb) do { \
408
+ int i; \
409
+ int a_wsz = FRT_TO_WORD(a->size); \
410
+ int b_wsz = FRT_TO_WORD(b->size); \
411
+ int max_size = FRT_MAX(a->size, b->size); \
412
+ int min_size = FRT_MIN(a->size, b->size); \
413
+ int max_word_size = FRT_TO_WORD(max_size); \
414
+ int min_word_size = FRT_TO_WORD(min_size); \
415
+ int capa = FRT_MAX(frt_round2(max_word_size), 4); \
416
+ \
417
+ bv->extends_as_ones = (a->extends_as_ones op b->extends_as_ones); \
418
+ frt_bv_capa(bv, capa, max_size); \
419
+ \
420
+ for (i = 0; i < min_word_size; ++i) \
421
+ bv->bits[i] = a->bits[i] op b->bits[i]; \
422
+ \
423
+ if (a_wsz != b_wsz) { \
424
+ frt_u32 *bits = a->bits; \
425
+ bool extends_as_ones = b->extends_as_ones; \
426
+ if (a_wsz < b_wsz) { \
427
+ bits = b->bits; \
428
+ extends_as_ones = a->extends_as_ones; \
429
+ } \
430
+ ext_cb(bv->bits, bits, extends_as_ones, i, max_word_size); \
431
+ } \
432
+ frt_bv_recount(bv); \
433
+ } while(0)
434
+
435
+ static FRT_ATTR_ALWAYS_INLINE
436
+ FrtBitVector *frt_bv_and_i(FrtBitVector *bv,
437
+ FrtBitVector *a, FrtBitVector *b)
438
+ {
439
+ FRT_BV_OP(bv, a, b, &, frt_bv_and_ext);
440
+ return bv;
441
+ }
442
+
443
+ static FRT_ATTR_ALWAYS_INLINE
444
+ FrtBitVector *frt_bv_or_i(FrtBitVector *bv,
445
+ FrtBitVector *a, FrtBitVector *b)
446
+ {
447
+ FRT_BV_OP(bv, a, b, |, frt_bv_or_ext);
448
+ return bv;
449
+ }
450
+
451
+ static FRT_ATTR_ALWAYS_INLINE
452
+ FrtBitVector *frt_bv_xor_i(FrtBitVector *bv,
453
+ FrtBitVector *a, FrtBitVector *b)
454
+ {
455
+ FRT_BV_OP(bv, a, b, ^, frt_bv_xor_ext);
456
+ return bv;
457
+ }
458
+
459
+ static FRT_ATTR_ALWAYS_INLINE
460
+ FrtBitVector *frt_bv_not_i(FrtBitVector *bv, FrtBitVector *bv1)
461
+ {
462
+ int i;
463
+ int word_size = FRT_TO_WORD(bv1->size);
464
+ int capa = FRT_MAX(frt_round2(word_size), 4);
465
+
466
+ bv->extends_as_ones = !bv1->extends_as_ones;
467
+ frt_bv_capa(bv, capa, bv1->size);
468
+
469
+ for (i = 0; i < word_size; i++)
470
+ bv->bits[i] = ~(bv1->bits[i]);
471
+
472
+ memset(bv->bits + word_size, (bv->extends_as_ones ? 0xFF : 0),
473
+ sizeof(frt_u32) * (bv->capa - word_size));
474
+
475
+ frt_bv_recount(bv);
476
+ return bv;
477
+ }
478
+
479
+ /**
480
+ * ANDs two BitVectors (+bv1+ and +bv2+) together and return the resultant
481
+ * FrtBitVector
482
+ *
483
+ * @param bv1 first FrtBitVector to AND
484
+ * @param bv2 second FrtBitVector to AND
485
+ * @return A FrtBitVector with all bits set that are set in both bv1 and bv2
486
+ */
487
+ static FRT_ATTR_ALWAYS_INLINE
488
+ FrtBitVector *frt_bv_and(FrtBitVector *bv1, FrtBitVector *bv2)
489
+ {
490
+ return frt_bv_and_i(frt_bv_new(), bv1, bv2);
491
+ }
492
+
493
+ /**
494
+ * ORs two BitVectors (+bv1+ and +bv2+) together and return the resultant
495
+ * FrtBitVector
496
+ *
497
+ * @param bv1 first FrtBitVector to OR
498
+ * @param bv2 second FrtBitVector to OR
499
+ * @return A FrtBitVector with all bits set that are set in both bv1 and bv2
500
+ */
501
+ static FRT_ATTR_ALWAYS_INLINE
502
+ FrtBitVector *frt_bv_or(FrtBitVector *bv1, FrtBitVector *bv2)
503
+ {
504
+ return frt_bv_or_i(frt_bv_new(), bv1, bv2);
505
+ }
506
+
507
+
508
+ /**
509
+ * XORs two BitVectors (+bv1+ and +bv2+) together and return the resultant
510
+ * FrtBitVector
511
+ *
512
+ * @param bv1 first FrtBitVector to XOR
513
+ * @param bv2 second FrtBitVector to XOR
514
+ * @return A FrtBitVector with all bits set that are equal in bv1 and bv2
515
+ */
516
+ static FRT_ATTR_ALWAYS_INLINE
517
+ FrtBitVector *frt_bv_xor(FrtBitVector *bv1, FrtBitVector *bv2)
518
+ {
519
+ return frt_bv_xor_i(frt_bv_new(), bv1, bv2);
520
+ }
521
+
522
+ /**
523
+ * Returns FrtBitVector with all of +bv+'s bits flipped
524
+ *
525
+ * @param bv FrtBitVector to flip
526
+ * @return A FrtBitVector with all bits set that are set in both bv1 and bv2
527
+ */
528
+ static FRT_ATTR_ALWAYS_INLINE
529
+ FrtBitVector *frt_bv_not(FrtBitVector *bv)
530
+ {
531
+ return frt_bv_not_i(frt_bv_new(), bv);
532
+ }
533
+
534
+ /**
535
+ * ANDs two BitVectors together +bv1+ and +bv2+ in place of +bv1+
536
+ *
537
+ * @param bv1 first FrtBitVector to AND
538
+ * @param bv2 second FrtBitVector to AND
539
+ * @return A FrtBitVector
540
+ * @return bv1 with all bits set that where set in both bv1 and bv2
541
+ */
542
+ static FRT_ATTR_ALWAYS_INLINE
543
+ FrtBitVector *frt_bv_and_x(FrtBitVector *bv1, FrtBitVector *bv2)
544
+ {
545
+ return frt_bv_and_i(bv1, bv1, bv2);
546
+ }
547
+
548
+ /**
549
+ * ORs two BitVectors together
550
+ *
551
+ * @param bv1 first FrtBitVector to OR
552
+ * @param bv2 second FrtBitVector to OR
553
+ * @return bv1
554
+ */
555
+ static FRT_ATTR_ALWAYS_INLINE
556
+ FrtBitVector *frt_bv_or_x(FrtBitVector *bv1, FrtBitVector *bv2)
557
+ {
558
+ return frt_bv_or_i(bv1, bv1, bv2);
559
+ }
560
+
561
+ /**
562
+ * XORs two BitVectors together +bv1+ and +bv2+ in place of +bv1+
563
+ *
564
+ * @param bv1 first FrtBitVector to XOR
565
+ * @param bv2 second FrtBitVector to XOR
566
+ * @return bv1
567
+ */
568
+ static FRT_ATTR_ALWAYS_INLINE
569
+ FrtBitVector *frt_bv_xor_x(FrtBitVector *bv1, FrtBitVector *bv2)
570
+ {
571
+ return frt_bv_xor_i(bv1, bv1, bv2);
572
+ }
573
+
574
+ /**
575
+ * Flips all bits in the FrtBitVector +bv+
576
+ *
577
+ * @param bv FrtBitVector to flip
578
+ * @return A +bv+ with all it's bits flipped
579
+ */
580
+ static FRT_ATTR_ALWAYS_INLINE
581
+ FrtBitVector *frt_bv_not_x(FrtBitVector *bv)
582
+ {
583
+ return frt_bv_not_i(bv, bv);
584
+ }
585
+
586
+ #endif