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,796 @@
1
+ #include <string.h>
2
+ #include "frt_search.h"
3
+ #include "frt_index.h"
4
+ #include "frt_field_index.h"
5
+
6
+ /***************************************************************************
7
+ *
8
+ * FrtSortField
9
+ *
10
+ ***************************************************************************/
11
+
12
+ static FrtSortField *sort_field_alloc(FrtSymbol field,
13
+ SortType type,
14
+ bool reverse,
15
+ int (*compare)(void *index_ptr, FrtHit *hit1, FrtHit *hit2),
16
+ void (*get_val)(void *index_ptr, FrtHit *hit1, FrtComparable *comparable),
17
+ const FrtFieldIndexClass *field_index_class)
18
+ {
19
+ FrtSortField *self = FRT_ALLOC(FrtSortField);
20
+ self->field = field;
21
+ self->type = type;
22
+ self->reverse = reverse;
23
+ self->field_index_class = field_index_class;
24
+ self->compare = compare;
25
+ self->get_val = get_val;
26
+ return self;
27
+ }
28
+
29
+ FrtSortField *frt_sort_field_new(FrtSymbol field, SortType type, bool reverse)
30
+ {
31
+ FrtSortField *sf = NULL;
32
+ switch (type) {
33
+ case FRT_SORT_TYPE_SCORE:
34
+ sf = frt_sort_field_score_new(reverse);
35
+ break;
36
+ case FRT_SORT_TYPE_DOC:
37
+ sf = frt_sort_field_doc_new(reverse);
38
+ break;
39
+ case FRT_SORT_TYPE_BYTE:
40
+ sf = frt_sort_field_byte_new(field, reverse);
41
+ break;
42
+ case FRT_SORT_TYPE_INTEGER:
43
+ sf = frt_sort_field_int_new(field, reverse);
44
+ break;
45
+ case FRT_SORT_TYPE_FLOAT:
46
+ sf = frt_sort_field_float_new(field, reverse);
47
+ break;
48
+ case FRT_SORT_TYPE_STRING:
49
+ sf = frt_sort_field_string_new(field, reverse);
50
+ break;
51
+ case FRT_SORT_TYPE_AUTO:
52
+ sf = frt_sort_field_auto_new(field, reverse);
53
+ break;
54
+ }
55
+ return sf;
56
+ }
57
+
58
+ void frt_sort_field_destroy(void *p)
59
+ {
60
+ free(p);
61
+ }
62
+
63
+ /*
64
+ * field:<type>!
65
+ */
66
+ char *frt_sort_field_to_s(FrtSortField *self)
67
+ {
68
+ char *str;
69
+ const char *type = NULL;
70
+ switch (self->type) {
71
+ case FRT_SORT_TYPE_SCORE:
72
+ type = "<SCORE>";
73
+ break;
74
+ case FRT_SORT_TYPE_DOC:
75
+ type = "<DOC>";
76
+ break;
77
+ case FRT_SORT_TYPE_BYTE:
78
+ type = "<byte>";
79
+ break;
80
+ case FRT_SORT_TYPE_INTEGER:
81
+ type = "<integer>";
82
+ break;
83
+ case FRT_SORT_TYPE_FLOAT:
84
+ type = "<float>";
85
+ break;
86
+ case FRT_SORT_TYPE_STRING:
87
+ type = "<string>";
88
+ break;
89
+ case FRT_SORT_TYPE_AUTO:
90
+ type = "<auto>";
91
+ break;
92
+ }
93
+ if (self->field) {
94
+ const char *field_name = rb_id2name(self->field);
95
+ str = FRT_ALLOC_N(char, 3 + strlen(field_name) + strlen(type));
96
+ sprintf(str, "%s:%s%s", field_name, type, (self->reverse ? "!" : ""));
97
+ }
98
+ else {
99
+ str = FRT_ALLOC_N(char, 2 + strlen(type));
100
+ sprintf(str, "%s%s", type, (self->reverse ? "!" : ""));
101
+ }
102
+ return str;
103
+ }
104
+
105
+ /***************************************************************************
106
+ * ScoreSortField
107
+ ***************************************************************************/
108
+
109
+ static void sf_score_get_val(void *index, FrtHit *hit, FrtComparable *comparable)
110
+ {
111
+ (void)index;
112
+ comparable->val.f = hit->score;
113
+ }
114
+
115
+ static int sf_score_compare(void *index_ptr, FrtHit *hit2, FrtHit *hit1)
116
+ {
117
+ float val1 = hit1->score;
118
+ float val2 = hit2->score;
119
+ (void)index_ptr;
120
+
121
+ if (val1 > val2) return 1;
122
+ else if (val1 < val2) return -1;
123
+ else return 0;
124
+ }
125
+
126
+ FrtSortField *frt_sort_field_score_new(bool reverse)
127
+ {
128
+ return sort_field_alloc((ID)NULL, FRT_SORT_TYPE_SCORE, reverse, &sf_score_compare, &sf_score_get_val, NULL);
129
+ }
130
+
131
+ const FrtSortField FRT_SORT_FIELD_SCORE = {
132
+ NULL, /* field_index_class */
133
+ (ID)NULL, /* field */
134
+ FRT_SORT_TYPE_SCORE, /* type */
135
+ false, /* reverse */
136
+ &sf_score_compare, /* compare */
137
+ &sf_score_get_val, /* get_val */
138
+ };
139
+
140
+ const FrtSortField FRT_SORT_FIELD_SCORE_REV = {
141
+ NULL, /* field_index_class */
142
+ (ID)NULL, /* field */
143
+ FRT_SORT_TYPE_SCORE, /* type */
144
+ true, /* reverse */
145
+ &sf_score_compare, /* compare */
146
+ &sf_score_get_val, /* get_val */
147
+ };
148
+
149
+ /**************************************************************************
150
+ * DocSortField
151
+ ***************************************************************************/
152
+
153
+ static void sf_doc_get_val(void *index, FrtHit *hit, FrtComparable *comparable)
154
+ {
155
+ (void)index;
156
+ comparable->val.l = hit->doc;
157
+ }
158
+
159
+ static int sf_doc_compare(void *index_ptr, FrtHit *hit1, FrtHit *hit2)
160
+ {
161
+ int val1 = hit1->doc;
162
+ int val2 = hit2->doc;
163
+ (void)index_ptr;
164
+
165
+ if (val1 > val2) return 1;
166
+ else if (val1 < val2) return -1;
167
+ else return 0;
168
+ }
169
+
170
+ FrtSortField *frt_sort_field_doc_new(bool reverse)
171
+ {
172
+ return sort_field_alloc((ID)NULL, FRT_SORT_TYPE_DOC, reverse,
173
+ &sf_doc_compare, &sf_doc_get_val, NULL);
174
+ }
175
+
176
+ const FrtSortField FRT_SORT_FIELD_DOC = {
177
+ NULL, /* field_index_class */
178
+ (ID)NULL, /* field */
179
+ FRT_SORT_TYPE_DOC, /* type */
180
+ false, /* reverse */
181
+ &sf_doc_compare, /* compare */
182
+ &sf_doc_get_val, /* get_val */
183
+ };
184
+
185
+ const FrtSortField FRT_SORT_FIELD_DOC_REV = {
186
+ NULL, /* field_index_class */
187
+ (ID)NULL, /* field */
188
+ FRT_SORT_TYPE_DOC, /* type */
189
+ true, /* reverse */
190
+ &sf_doc_compare, /* compare */
191
+ &sf_doc_get_val, /* get_val */
192
+ };
193
+
194
+ /***************************************************************************
195
+ * ByteSortField
196
+ ***************************************************************************/
197
+
198
+ static void sf_byte_get_val(void *index, FrtHit *hit, FrtComparable *comparable)
199
+ {
200
+ comparable->val.l = ((long *)index)[hit->doc];
201
+ }
202
+
203
+ static int sf_byte_compare(void *index, FrtHit *hit1, FrtHit *hit2)
204
+ {
205
+ long val1 = ((long *)index)[hit1->doc];
206
+ long val2 = ((long *)index)[hit2->doc];
207
+ if (val1 > val2) return 1;
208
+ else if (val1 < val2) return -1;
209
+ else return 0;
210
+ }
211
+
212
+ FrtSortField *frt_sort_field_byte_new(FrtSymbol field, bool reverse)
213
+ {
214
+ return sort_field_alloc(field, FRT_SORT_TYPE_BYTE, reverse,
215
+ &sf_byte_compare, &sf_byte_get_val,
216
+ &FRT_BYTE_FIELD_INDEX_CLASS);
217
+ }
218
+
219
+ /***************************************************************************
220
+ * IntegerSortField
221
+ ***************************************************************************/
222
+
223
+ static void sf_int_get_val(void *index, FrtHit *hit, FrtComparable *comparable)
224
+ {
225
+ comparable->val.l = ((long *)index)[hit->doc];
226
+ }
227
+
228
+ static int sf_int_compare(void *index, FrtHit *hit1, FrtHit *hit2)
229
+ {
230
+ long val1 = ((long *)index)[hit1->doc];
231
+ long val2 = ((long *)index)[hit2->doc];
232
+ if (val1 > val2) return 1;
233
+ else if (val1 < val2) return -1;
234
+ else return 0;
235
+ }
236
+
237
+ FrtSortField *frt_sort_field_int_new(FrtSymbol field, bool reverse)
238
+ {
239
+ return sort_field_alloc(field, FRT_SORT_TYPE_INTEGER, reverse,
240
+ &sf_int_compare, &sf_int_get_val,
241
+ &FRT_INTEGER_FIELD_INDEX_CLASS);
242
+ }
243
+
244
+ /***************************************************************************
245
+ * FloatSortField
246
+ ***************************************************************************/
247
+
248
+ static void sf_float_get_val(void *index, FrtHit *hit, FrtComparable *comparable)
249
+ {
250
+ comparable->val.f = ((float *)index)[hit->doc];
251
+ }
252
+
253
+ static int sf_float_compare(void *index, FrtHit *hit1, FrtHit *hit2)
254
+ {
255
+ float val1 = ((float *)index)[hit1->doc];
256
+ float val2 = ((float *)index)[hit2->doc];
257
+ if (val1 > val2) return 1;
258
+ else if (val1 < val2) return -1;
259
+ else return 0;
260
+ }
261
+
262
+ FrtSortField *frt_sort_field_float_new(FrtSymbol field, bool reverse)
263
+ {
264
+ return sort_field_alloc(field, FRT_SORT_TYPE_FLOAT, reverse,
265
+ &sf_float_compare, &sf_float_get_val,
266
+ &FRT_FLOAT_FIELD_INDEX_CLASS);
267
+ }
268
+
269
+ /***************************************************************************
270
+ * StringSortField
271
+ ***************************************************************************/
272
+
273
+ static void sf_string_get_val(void *index, FrtHit *hit, FrtComparable *comparable)
274
+ {
275
+ comparable->val.s
276
+ = ((FrtStringIndex *)index)->values[
277
+ ((FrtStringIndex *)index)->index[hit->doc]];
278
+ }
279
+
280
+ static int sf_string_compare(void *index, FrtHit *hit1, FrtHit *hit2)
281
+ {
282
+ char *s1 = ((FrtStringIndex *)index)->values[
283
+ ((FrtStringIndex *)index)->index[hit1->doc]];
284
+ char *s2 = ((FrtStringIndex *)index)->values[
285
+ ((FrtStringIndex *)index)->index[hit2->doc]];
286
+
287
+ if (s1 == NULL) return s2 ? 1 : 0;
288
+ if (s2 == NULL) return -1;
289
+
290
+ #if defined POSH_OS_WIN32 || defined POSH_OS_WIN64
291
+ return strcmp(s1, s2);
292
+ #else
293
+ return strcoll(s1, s2);
294
+ #endif
295
+
296
+ /*
297
+ * TODO: investigate whether it would be a good idea to presort strings.
298
+ *
299
+ int val1 = index->index[hit1->doc];
300
+ int val2 = index->index[hit2->doc];
301
+ if (val1 > val2) return 1;
302
+ else if (val1 < val2) return -1;
303
+ else return 0;
304
+ */
305
+ }
306
+
307
+ FrtSortField *frt_sort_field_string_new(FrtSymbol field, bool reverse)
308
+ {
309
+ return sort_field_alloc(field, FRT_SORT_TYPE_STRING, reverse,
310
+ &sf_string_compare, &sf_string_get_val,
311
+ &FRT_STRING_FIELD_INDEX_CLASS);
312
+ }
313
+
314
+ /***************************************************************************
315
+ * AutoSortField
316
+ ***************************************************************************/
317
+
318
+ FrtSortField *frt_sort_field_auto_new(FrtSymbol field, bool reverse)
319
+ {
320
+ return sort_field_alloc(field, FRT_SORT_TYPE_AUTO, reverse, NULL, NULL, NULL);
321
+ }
322
+
323
+ /***************************************************************************
324
+ *
325
+ * FieldSortedHitQueue
326
+ *
327
+ ***************************************************************************/
328
+
329
+ /***************************************************************************
330
+ * Comparator
331
+ ***************************************************************************/
332
+
333
+ typedef struct Comparator {
334
+ void *index;
335
+ bool reverse : 1;
336
+ int (*compare)(void *index_ptr, FrtHit *hit1, FrtHit *hit2);
337
+ } Comparator;
338
+
339
+ static Comparator *comparator_new(void *index, bool reverse,
340
+ int (*compare)(void *index_ptr, FrtHit *hit1, FrtHit *hit2))
341
+ {
342
+ Comparator *self = FRT_ALLOC(Comparator);
343
+ self->index = index;
344
+ self->reverse = reverse;
345
+ self->compare = compare;
346
+ return self;
347
+ }
348
+
349
+ /***************************************************************************
350
+ * Sorter
351
+ ***************************************************************************/
352
+
353
+ typedef struct Sorter {
354
+ Comparator **comparators;
355
+ int c_cnt;
356
+ FrtSort *sort;
357
+ } Sorter;
358
+
359
+ #define SET_AUTO(upper_type, lower_type) \
360
+ sf->type = FRT_SORT_TYPE_ ## upper_type;\
361
+ sf->field_index_class = &FRT_ ## upper_type ## _FIELD_INDEX_CLASS;\
362
+ sf->compare = sf_ ## lower_type ## _compare;\
363
+ sf->get_val = sf_ ## lower_type ## _get_val
364
+
365
+ static void sort_field_auto_evaluate(FrtSortField *sf, char *text)
366
+ {
367
+ int int_val;
368
+ float float_val;
369
+ int text_len = 0, scan_len = 0;
370
+
371
+ text_len = (int)strlen(text);
372
+ sscanf(text, "%d%n", &int_val, &scan_len);
373
+ if (scan_len == text_len) {
374
+ SET_AUTO(INTEGER, int);
375
+ } else {
376
+ sscanf(text, "%f%n", &float_val, &scan_len);
377
+ if (scan_len == text_len) {
378
+ SET_AUTO(FLOAT, float);
379
+ } else {
380
+ SET_AUTO(STRING, string);
381
+ }
382
+ }
383
+ }
384
+
385
+ static Comparator *sorter_get_comparator(FrtSortField *sf, FrtIndexReader *ir)
386
+ {
387
+ void *index = NULL;
388
+ if (sf->type > FRT_SORT_TYPE_DOC) {
389
+ FrtFieldIndex *field_index = NULL;
390
+ if (sf->type == FRT_SORT_TYPE_AUTO) {
391
+ FrtTermEnum *te = frt_ir_terms(ir, sf->field);
392
+ if (te) {
393
+ if (!te->next(te) && (ir->num_docs(ir) > 0)) {
394
+ FRT_RAISE(FRT_ARG_ERROR,
395
+ "Cannot sort by field \"%s\" as there are no terms "
396
+ "in that field in the index.", rb_id2name(sf->field));
397
+ }
398
+ sort_field_auto_evaluate(sf, te->curr_term);
399
+ te->close(te);
400
+ }
401
+ }
402
+ frt_mutex_lock(&ir->field_index_mutex);
403
+ field_index = frt_field_index_get(ir, sf->field, sf->field_index_class);
404
+ frt_mutex_unlock(&ir->field_index_mutex);
405
+ index = field_index->index;
406
+ }
407
+ return comparator_new(index, sf->reverse, sf->compare);
408
+ }
409
+
410
+ static void sorter_destroy(Sorter *self)
411
+ {
412
+ int i;
413
+
414
+ for (i = 0; i < self->c_cnt; i++) {
415
+ free(self->comparators[i]);
416
+ }
417
+ free(self->comparators);
418
+ free(self);
419
+ }
420
+
421
+ static Sorter *sorter_new(FrtSort *sort)
422
+ {
423
+ Sorter *self = FRT_ALLOC(Sorter);
424
+ self->c_cnt = sort->size;
425
+ self->comparators = FRT_ALLOC_AND_ZERO_N(Comparator *, self->c_cnt);
426
+ self->sort = sort;
427
+ return self;
428
+ }
429
+
430
+ /***************************************************************************
431
+ * FieldSortedHitQueue
432
+ ***************************************************************************/
433
+
434
+ static bool fshq_less_than(const void *hit1, const void *hit2)
435
+ {
436
+ int cmp = 0;
437
+ printf("Whoops, shouldn't call this.\n");
438
+ if (cmp != 0) {
439
+ return cmp;
440
+ } else {
441
+ return ((FrtHit *)hit1)->score < ((FrtHit *)hit2)->score;
442
+ }
443
+ }
444
+
445
+ static bool fshq_lt(Sorter *sorter, FrtHit *hit1, FrtHit *hit2)
446
+ {
447
+ Comparator *comp;
448
+ int diff = 0, i;
449
+ for (i = 0; i < sorter->c_cnt && diff == 0; i++) {
450
+ comp = sorter->comparators[i];
451
+ if (comp->reverse) {
452
+ diff = comp->compare(comp->index, hit2, hit1);
453
+ } else {
454
+ diff = comp->compare(comp->index, hit1, hit2);
455
+ }
456
+ }
457
+
458
+ if (diff != 0) {
459
+ return diff > 0;
460
+ } else {
461
+ return hit1->doc > hit2->doc;
462
+ }
463
+ }
464
+
465
+ void fshq_pq_down(FrtPriorityQueue *pq)
466
+ {
467
+ register int i = 1;
468
+ register int j = 2; /* i << 1; */
469
+ register int k = 3; /* j + 1; */
470
+ FrtHit **heap = (FrtHit **)pq->heap;
471
+ FrtHit *node = heap[i]; /* save top node */
472
+ Sorter *sorter = (Sorter *)heap[0];
473
+
474
+ if ((k <= pq->size) && fshq_lt(sorter, heap[k], heap[j])) {
475
+ j = k;
476
+ }
477
+
478
+ while ((j <= pq->size) && fshq_lt(sorter, heap[j], node)) {
479
+ heap[i] = heap[j]; /* shift up child */
480
+ i = j;
481
+ j = i << 1;
482
+ k = j + 1;
483
+ if ((k <= pq->size) && fshq_lt(sorter, heap[k], heap[j])) {
484
+ j = k;
485
+ }
486
+ }
487
+ heap[i] = node;
488
+ }
489
+
490
+ FrtHit *frt_fshq_pq_pop(FrtPriorityQueue *pq)
491
+ {
492
+ if (pq->size > 0) {
493
+ FrtHit *hit = (FrtHit *)pq->heap[1]; /* save first value */
494
+ pq->heap[1] = pq->heap[pq->size]; /* move last to first */
495
+ pq->heap[pq->size] = NULL;
496
+ pq->size--;
497
+ fshq_pq_down(pq); /* adjust heap */
498
+ return hit;
499
+ } else {
500
+ return NULL;
501
+ }
502
+ }
503
+
504
+ static void fshq_pq_up(FrtPriorityQueue *pq)
505
+ {
506
+ FrtHit **heap = (FrtHit **)pq->heap;
507
+ FrtHit *node;
508
+ int i = pq->size;
509
+ int j = i >> 1;
510
+ Sorter *sorter = (Sorter *)heap[0];
511
+ node = heap[i];
512
+
513
+ while ((j > 0) && fshq_lt(sorter, node, heap[j])) {
514
+ heap[i] = heap[j];
515
+ i = j;
516
+ j = j >> 1;
517
+ }
518
+ heap[i] = node;
519
+ }
520
+
521
+ void frt_fshq_pq_insert(FrtPriorityQueue *pq, FrtHit *hit)
522
+ {
523
+ if (pq->size < pq->capa) {
524
+ FrtHit *new_hit = FRT_ALLOC(FrtHit);
525
+ memcpy(new_hit, hit, sizeof(FrtHit));
526
+ pq->size++;
527
+ if (pq->size >= pq->mem_capa) {
528
+ pq->mem_capa <<= 1;
529
+ FRT_REALLOC_N(pq->heap, void *, pq->mem_capa);
530
+ }
531
+ pq->heap[pq->size] = new_hit;
532
+ fshq_pq_up(pq);
533
+ } else if (pq->size > 0
534
+ && fshq_lt((Sorter *)pq->heap[0], (FrtHit *)pq->heap[1], hit)) {
535
+ memcpy(pq->heap[1], hit, sizeof(FrtHit));
536
+ fshq_pq_down(pq);
537
+ }
538
+ }
539
+
540
+ void frt_fshq_pq_destroy(FrtPriorityQueue *self)
541
+ {
542
+ sorter_destroy((Sorter *)self->heap[0]);
543
+ frt_pq_destroy(self);
544
+ }
545
+
546
+ FrtPriorityQueue *frt_fshq_pq_new(int size, FrtSort *sort, FrtIndexReader *ir)
547
+ {
548
+ FrtPriorityQueue *self = frt_pq_new(size, &fshq_less_than, &free);
549
+ int i;
550
+ Sorter *sorter = sorter_new(sort);
551
+ FrtSortField *sf;
552
+
553
+ for (i = 0; i < sort->size; i++) {
554
+ sf = sort->sort_fields[i];
555
+ sorter->comparators[i] = sorter_get_comparator(sf, ir);
556
+ }
557
+ self->heap[0] = sorter;
558
+
559
+ return self;
560
+ }
561
+
562
+ FrtHit *frt_fshq_pq_pop_fd(FrtPriorityQueue *pq)
563
+ {
564
+ if (pq->size <= 0) {
565
+ return NULL;
566
+ }
567
+ else {
568
+ int j;
569
+ Sorter *sorter = (Sorter *)pq->heap[0];
570
+ const int cmp_cnt = sorter->c_cnt;
571
+ FrtSortField **sort_fields = sorter->sort->sort_fields;
572
+ FrtHit *hit = (FrtHit *)pq->heap[1]; /* save first value */
573
+ FrtFieldDoc *field_doc;
574
+ FrtComparable *comparables;
575
+ Comparator **comparators = sorter->comparators;
576
+ pq->heap[1] = pq->heap[pq->size]; /* move last to first */
577
+ pq->heap[pq->size] = NULL;
578
+ pq->size--;
579
+ fshq_pq_down(pq); /* adjust heap */
580
+
581
+ field_doc = (FrtFieldDoc *)frt_emalloc(sizeof(FrtFieldDoc) + sizeof(FrtComparable) * cmp_cnt);
582
+ comparables = field_doc->comparables;
583
+ memcpy(field_doc, hit, sizeof(FrtHit));
584
+ field_doc->size = cmp_cnt;
585
+
586
+ for (j = 0; j < cmp_cnt; j++) {
587
+ FrtSortField *sf = sort_fields[j];
588
+ Comparator *comparator = comparators[j];
589
+ sf->get_val(comparator->index, hit, &(comparables[j]));
590
+ comparables[j].type = sf->type;
591
+ comparables[j].reverse = comparator->reverse;
592
+ }
593
+ free(hit);
594
+ return (FrtHit *)field_doc;
595
+ }
596
+ }
597
+
598
+ /***************************************************************************
599
+ * FieldDocSortedHitQueue
600
+ ***************************************************************************/
601
+
602
+ bool frt_fdshq_lt(FrtFieldDoc *fd1, FrtFieldDoc *fd2)
603
+ {
604
+ int i;
605
+ bool c = false;
606
+ bool all_equal = false;
607
+ int sc;
608
+ FrtComparable *cmps1 = fd1->comparables;
609
+ FrtComparable *cmps2 = fd2->comparables;
610
+
611
+ for (i = 0; (i < fd1->size) && (!c); i++) {
612
+ int type = cmps1[i].type;
613
+ if (cmps1[i].reverse) {
614
+ switch (type) {
615
+ case FRT_SORT_TYPE_SCORE:
616
+ if (cmps1[i].val.f > cmps2[i].val.f) { all_equal = false; c = true; }
617
+ else if (cmps1[i].val.f == cmps2[i].val.f) { if (!c) all_equal = true; }
618
+ else { all_equal = false; }
619
+ break;
620
+ case FRT_SORT_TYPE_FLOAT:
621
+ if (cmps1[i].val.f < cmps2[i].val.f) { all_equal = false; c = true; }
622
+ else if (cmps1[i].val.f == cmps2[i].val.f) { if (!c) all_equal = true; }
623
+ else { all_equal = false; }
624
+ break;
625
+ case FRT_SORT_TYPE_DOC:
626
+ if (fd1->hit.doc < fd2->hit.doc) { all_equal = false; c = true; }
627
+ break;
628
+ case FRT_SORT_TYPE_INTEGER:
629
+ if (cmps1[i].val.l < cmps2[i].val.l) { all_equal = false; c = true; }
630
+ else if (cmps1[i].val.l == cmps2[i].val.l) { if (!c) all_equal = true; }
631
+ else { all_equal = false; }
632
+ break;
633
+ case FRT_SORT_TYPE_BYTE:
634
+ if (cmps1[i].val.l > cmps2[i].val.l) { all_equal = false; c = true; }
635
+ else if (cmps1[i].val.l == cmps2[i].val.l) { if (!c) all_equal = true; }
636
+ else { all_equal = false; }
637
+ break;
638
+ case FRT_SORT_TYPE_STRING:
639
+ do {
640
+ char *s1 = cmps1[i].val.s;
641
+ char *s2 = cmps2[i].val.s;
642
+ if ((s1 == NULL) && s2) { all_equal = false; }
643
+ else if (s1 && (s2 == NULL)) { all_equal = false; c = true; }
644
+ else if (s1 && s2) {
645
+ #if defined POSH_OS_WIN32 || defined POSH_OS_WIN64
646
+ sc = strcmp(s1, s2);
647
+ #else
648
+ sc = strcoll(s1, s2);
649
+ #endif
650
+ if (sc < 0) { all_equal = false; c = true; }
651
+ else if (sc == 0) { if (!c) all_equal = true; }
652
+ else { all_equal = false; }
653
+ } else { all_equal = false; }
654
+ } while (0);
655
+ break;
656
+ default:
657
+ FRT_RAISE(FRT_ARG_ERROR, "Unknown sort type: %d.", type);
658
+ break;
659
+ }
660
+ } else {
661
+ switch (type) {
662
+ case FRT_SORT_TYPE_SCORE:
663
+ if (cmps1[i].val.f < cmps2[i].val.f) { all_equal = false; c = true; }
664
+ else if (cmps1[i].val.f == cmps2[i].val.f) { if (!c) all_equal = true; }
665
+ else { all_equal = false; }
666
+ break;
667
+ case FRT_SORT_TYPE_FLOAT:
668
+ if (cmps1[i].val.f > cmps2[i].val.f) { all_equal = false; c = true; }
669
+ else if (cmps1[i].val.f == cmps2[i].val.f) { if (!c) all_equal = true; }
670
+ else { all_equal = false; }
671
+ break;
672
+ case FRT_SORT_TYPE_DOC:
673
+ if (fd1->hit.doc > fd2->hit.doc) { all_equal = false; c = true; }
674
+ break;
675
+ case FRT_SORT_TYPE_INTEGER:
676
+ if (cmps1[i].val.l > cmps2[i].val.l) { all_equal = false; c = true; }
677
+ else if (cmps1[i].val.l == cmps2[i].val.l) { if (!c) all_equal = true; }
678
+ else { all_equal = false; }
679
+ break;
680
+ case FRT_SORT_TYPE_BYTE:
681
+ if (cmps1[i].val.l < cmps2[i].val.l) { all_equal = false; c = true; }
682
+ else if (cmps1[i].val.l == cmps2[i].val.l) { if (!c) all_equal = true; }
683
+ else { all_equal = false; }
684
+ break;
685
+ case FRT_SORT_TYPE_STRING:
686
+ do {
687
+ char *s1 = cmps1[i].val.s;
688
+ char *s2 = cmps2[i].val.s;
689
+ if (s1 && (s2 == NULL)) { if (!c) all_equal = false; }
690
+ else if ((s1 == NULL) && s2) { all_equal = false; c = true; }
691
+ else if (s1 && s2) {
692
+ #if defined POSH_OS_WIN32 || defined POSH_OS_WIN64
693
+ sc = strcmp(s1, s2);
694
+ #else
695
+ sc = strcoll(s1, s2);
696
+ #endif
697
+ if (sc > 0) { all_equal = false; c = true; }
698
+ else if (sc == 0) { if (!c) all_equal = true; }
699
+ else { all_equal = false; }
700
+ } else { all_equal = false; }
701
+ } while (0);
702
+ break;
703
+ default:
704
+ FRT_RAISE(FRT_ARG_ERROR, "Unknown sort type: %d.", type);
705
+ break;
706
+ }
707
+ }
708
+ if (!all_equal) break;
709
+ }
710
+ if (all_equal) {
711
+ if (cmps1[0].reverse) {
712
+ if (fd1->hit.doc > fd2->hit.doc) c = true;
713
+ } else {
714
+ if (fd1->hit.doc > fd2->hit.doc) c = true;
715
+ }
716
+ }
717
+ return c;
718
+ }
719
+
720
+ /***************************************************************************
721
+ *
722
+ * Sort
723
+ *
724
+ ***************************************************************************/
725
+
726
+ #define SORT_INIT_SIZE 4
727
+
728
+ FrtSort *frt_sort_new()
729
+ {
730
+ FrtSort *self = FRT_ALLOC(FrtSort);
731
+ self->size = 0;
732
+ self->capa = SORT_INIT_SIZE;
733
+ self->sort_fields = FRT_ALLOC_N(FrtSortField *, SORT_INIT_SIZE);
734
+ self->destroy_all = true;
735
+ self->start = 0;
736
+
737
+ return self;
738
+ }
739
+
740
+ void frt_sort_clear(FrtSort *self)
741
+ {
742
+ int i;
743
+ if (self->destroy_all) {
744
+ for (i = 0; i < self->size; i++) {
745
+ frt_sort_field_destroy(self->sort_fields[i]);
746
+ }
747
+ }
748
+ self->size = 0;
749
+ }
750
+
751
+ void frt_sort_destroy(void *p)
752
+ {
753
+ FrtSort *self = (FrtSort *)p;
754
+ frt_sort_clear(self);
755
+ free(self->sort_fields);
756
+ free(self);
757
+ }
758
+
759
+ void frt_sort_add_sort_field(FrtSort *self, FrtSortField *sf)
760
+ {
761
+ if (self->size == self->capa) {
762
+ self->capa <<= 1;
763
+ FRT_REALLOC_N(self->sort_fields, FrtSortField *, self->capa);
764
+ }
765
+
766
+ self->sort_fields[self->size] = sf;
767
+ self->size++;
768
+ }
769
+
770
+ char *frt_sort_to_s(FrtSort *self)
771
+ {
772
+ int i, len = 20;
773
+ char *s;
774
+ char *str;
775
+ char **sf_strs = FRT_ALLOC_N(char *, self->size);
776
+
777
+ for (i = 0; i < self->size; i++) {
778
+ sf_strs[i] = s = frt_sort_field_to_s(self->sort_fields[i]);
779
+ len += (int)strlen(s) + 2;
780
+ }
781
+
782
+ str = FRT_ALLOC_N(char, len);
783
+ s = frt_strapp(str, "Sort[");
784
+
785
+ for (i = 0; i < self->size; i++) {
786
+ s += sprintf(s, "%s, ", sf_strs[i]);
787
+ free(sf_strs[i]);
788
+ }
789
+ free(sf_strs);
790
+
791
+ if (self->size > 0) {
792
+ s -= 2;
793
+ }
794
+ sprintf(s, "]");
795
+ return str;
796
+ }