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,147 @@
1
+ #include <string.h>
2
+ #include "frt_priorityqueue.h"
3
+
4
+ #define START_CAPA 127
5
+
6
+ FrtPriorityQueue *frt_pq_new(int capa, frt_lt_ft less_than, frt_free_ft free_elem)
7
+ {
8
+ FrtPriorityQueue *pq = FRT_ALLOC(FrtPriorityQueue);
9
+ pq->size = 0;
10
+ pq->capa = capa;
11
+ pq->mem_capa = (START_CAPA > capa ? capa : START_CAPA) + 1;
12
+ pq->heap = FRT_ALLOC_N(void *, pq->mem_capa);
13
+ memset(pq->heap, 0, (sizeof(void *))*(pq->mem_capa));
14
+ pq->less_than_i = less_than;
15
+
16
+ /* need to set this yourself if you want to change it */
17
+ pq->free_elem_i = free_elem ? free_elem : &frt_dummy_free;
18
+ return pq;
19
+ }
20
+
21
+ FrtPriorityQueue *frt_pq_clone(FrtPriorityQueue *pq)
22
+ {
23
+ FrtPriorityQueue *new_pq = FRT_ALLOC(FrtPriorityQueue);
24
+ memcpy(new_pq, pq, sizeof(FrtPriorityQueue));
25
+ new_pq->heap = FRT_ALLOC_N(void *, new_pq->mem_capa);
26
+ memcpy(new_pq->heap, pq->heap, sizeof(void *) * (new_pq->size + 1));
27
+
28
+ return new_pq;
29
+ }
30
+
31
+ void frt_pq_clear(FrtPriorityQueue *pq)
32
+ {
33
+ int i;
34
+ for (i = 1; i <= pq->size; i++) {
35
+ pq->free_elem_i(pq->heap[i]);
36
+ pq->heap[i] = NULL;
37
+ }
38
+ pq->size = 0;
39
+ }
40
+
41
+ void frt_pq_free(FrtPriorityQueue *pq)
42
+ {
43
+ free(pq->heap);
44
+ free(pq);
45
+ }
46
+
47
+ void frt_pq_destroy(FrtPriorityQueue *pq)
48
+ {
49
+ frt_pq_clear(pq);
50
+ frt_pq_free(pq);
51
+ }
52
+
53
+ /**
54
+ * This method is used internally by frt_pq_push. It is similar to frt_pq_down except
55
+ * that where frt_pq_down reorders the elements from the top, pq_up reorders from
56
+ * the bottom.
57
+ *
58
+ * @param pq the PriorityQueue to reorder
59
+ */
60
+ static void frt_pq_up(FrtPriorityQueue *pq)
61
+ {
62
+ void **heap = pq->heap;
63
+ void *node;
64
+ int i = pq->size;
65
+ int j = i >> 1;
66
+
67
+ node = heap[i];
68
+
69
+ while ((j > 0) && pq->less_than_i(node, heap[j])) {
70
+ heap[i] = heap[j];
71
+ i = j;
72
+ j = j >> 1;
73
+ }
74
+ heap[i] = node;
75
+ }
76
+
77
+ void frt_pq_down(FrtPriorityQueue *pq)
78
+ {
79
+ register int i = 1;
80
+ register int j = 2; /* i << 1; */
81
+ register int k = 3; /* j + 1; */
82
+ register int size = pq->size;
83
+ void **heap = pq->heap;
84
+ void *node = heap[i]; /* save top node */
85
+
86
+ if ((k <= size) && (pq->less_than_i(heap[k], heap[j]))) {
87
+ j = k;
88
+ }
89
+
90
+ while ((j <= size) && pq->less_than_i(heap[j], node)) {
91
+ heap[i] = heap[j]; /* shift up child */
92
+ i = j;
93
+ j = i << 1;
94
+ k = j + 1;
95
+ if ((k <= size) && pq->less_than_i(heap[k], heap[j])) {
96
+ j = k;
97
+ }
98
+ }
99
+ heap[i] = node;
100
+ }
101
+
102
+ void frt_pq_push(FrtPriorityQueue *pq, void *elem)
103
+ {
104
+ pq->size++;
105
+ if (pq->size >= pq->mem_capa) {
106
+ pq->mem_capa <<= 1;
107
+ FRT_REALLOC_N(pq->heap, void *, pq->mem_capa);
108
+ }
109
+ pq->heap[pq->size] = elem;
110
+ frt_pq_up(pq);
111
+ }
112
+
113
+ FrtPriorityQueueInsertEnum frt_pq_insert(FrtPriorityQueue *pq, void *elem)
114
+ {
115
+ if (pq->size < pq->capa) {
116
+ frt_pq_push(pq, elem);
117
+ return FRT_PQ_ADDED;
118
+ }
119
+
120
+ if (pq->size > 0 && pq->less_than_i(pq->heap[1], elem)) {
121
+ pq->free_elem_i(pq->heap[1]);
122
+ pq->heap[1] = elem;
123
+ frt_pq_down(pq);
124
+ return FRT_PQ_INSERTED;
125
+ }
126
+
127
+ pq->free_elem_i(elem);
128
+ return FRT_PQ_DROPPED;
129
+ }
130
+
131
+ void *frt_pq_top(FrtPriorityQueue *pq)
132
+ {
133
+ return pq->size ? pq->heap[1] : NULL;
134
+ }
135
+
136
+ void *frt_pq_pop(FrtPriorityQueue *pq)
137
+ {
138
+ if (pq->size > 0) {
139
+ void *result = pq->heap[1]; /* save first value */
140
+ pq->heap[1] = pq->heap[pq->size]; /* move last to first */
141
+ pq->heap[pq->size] = NULL;
142
+ pq->size--;
143
+ frt_pq_down(pq); /* adjust heap */
144
+ return result;
145
+ }
146
+ return NULL;
147
+ }
@@ -0,0 +1,147 @@
1
+ #ifndef FRT_PRIORITYQUEUE_H
2
+ #define FRT_PRIORITYQUEUE_H
3
+
4
+ #include "frt_global.h"
5
+
6
+ typedef bool (*frt_lt_ft)(const void *p1, const void *p2);
7
+
8
+ /**
9
+ * A PriorityQueue has a fixed size and contains a less_than function and a
10
+ * free_elem function specific to the data type to be stored in the queue.
11
+ */
12
+ typedef struct FrtPriorityQueue
13
+ {
14
+ int size;
15
+ int capa;
16
+ int mem_capa;
17
+ void **heap;
18
+ frt_lt_ft less_than_i;
19
+ frt_free_ft free_elem_i;
20
+ } FrtPriorityQueue;
21
+
22
+ /**
23
+ * Create a new PriorityQueue setting the less_than and free_elem for this
24
+ * specific PriorityQueue.
25
+ *
26
+ * @param capa the capacity of the PriorityQueue. As more than the capacity is
27
+ * added to the queue the least valued elements drop off the bottom.
28
+ * @param less_than the function to determine whether one value is less than
29
+ * another for this particular PriorityQueue
30
+ * @param free_elem the function to free the elements in the PriorityQueue
31
+ * when it is destroyed or there is insertion overflow
32
+ * @return a newly allocated PriorityQueue
33
+ */
34
+ extern FrtPriorityQueue *frt_pq_new(int capa,
35
+ frt_lt_ft less_than,
36
+ frt_free_ft free_elem);
37
+
38
+ /**
39
+ * Allocate a clone of the PriorityQueue. This can be used if you want to scan
40
+ * through all elements of the PriorityQueue but you don't want to have to
41
+ * remove the all and add them all again.
42
+ *
43
+ * @param pq the priority queue to clone
44
+ * @return a clone of the original priority queue
45
+ */
46
+ extern FrtPriorityQueue *frt_pq_clone(FrtPriorityQueue *pq);
47
+
48
+ /**
49
+ * Clear all elements from the PriorityQueue and reset the size to 0. When
50
+ * the elements are removed from the PriorityQueue, free_elem is used to free
51
+ * them, unless it was set to NULL in which case nothing will happen to them.
52
+ *
53
+ * @param self the PriorityQueue to clear
54
+ */
55
+ extern void frt_pq_clear(FrtPriorityQueue *self);
56
+
57
+ /**
58
+ * Free the memory allocated to the PriorityQueue. This function does nothing
59
+ * to the elements in the PriorityQueue itself. To destroy them also, use
60
+ * frt_pq_destroy.
61
+ *
62
+ * @param self the PriorityQueue to free
63
+ */
64
+ extern void frt_pq_free(FrtPriorityQueue *self);
65
+
66
+ /**
67
+ * Destroy the PriorityQueue, freeing all memory allocated to it and also
68
+ * destroying all the elements contained by it. This method is equivalent to
69
+ * calling frt_pq_clear followed by frt_pq_free.
70
+ *
71
+ * @param the PriorityQueue to destroy
72
+ */
73
+ extern void frt_pq_destroy(FrtPriorityQueue *self);
74
+
75
+ /**
76
+ * Reorder the PriorityQueue after the top element has been modified. This
77
+ * method is used especially when the PriorityQueue contains a queue of
78
+ * iterators. When the top iterator is incremented you should call this
79
+ * method.
80
+ *
81
+ * @param self the PriorityQueue to reorder
82
+ */
83
+ extern void frt_pq_down(FrtPriorityQueue *self);
84
+
85
+ /**
86
+ * Add another element to the PriorityQueue. This method should only be used
87
+ * when the PriorityQueue has enough space allocated to hold all elements
88
+ * added. If there is a chance that you will add more than the amount you have
89
+ * allocated then you should use frt_pq_insert. frt_pq_insert will handle insertion
90
+ * overflow.
91
+ *
92
+ * @param self the PriorityQueue to add the element to
93
+ * @param elem the element to add to the PriorityQueue
94
+ */
95
+ extern void frt_pq_push(FrtPriorityQueue *self, void *elem);
96
+
97
+ typedef enum {
98
+ FRT_PQ_DROPPED = 0,
99
+ FRT_PQ_ADDED,
100
+ FRT_PQ_INSERTED
101
+ } FrtPriorityQueueInsertEnum;
102
+
103
+ /**
104
+ * Add another element to the PriorityQueue. Unlike frt_pq_push, this method
105
+ * handles insertion overflow. That is, when you insert more elements than the
106
+ * capacity of the PriorityQueue, the elements are dropped off the bottom and
107
+ * freed using the free_elem function.
108
+ *
109
+ * @param self the PriorityQueue to add the element to
110
+ * @param elem the element to add to the PriorityQueue
111
+ * @returns one of three values;
112
+ * <pre>
113
+ * 0 == FRT_PQ_DROPPED the element was too small (according to the less_than
114
+ * function) so it was destroyed
115
+ * 1 == FRT_PQ_ADDED the element was successfully added
116
+ * 2 == FRT_PQ_INSERTED the element was successfully added after another
117
+ * element was dropped and destroyed
118
+ * </pre>
119
+ */
120
+ extern FrtPriorityQueueInsertEnum frt_pq_insert(FrtPriorityQueue *self,
121
+ void *elem);
122
+
123
+ /**
124
+ * Get the top element in the PriorityQueue.
125
+ *
126
+ * @param self the PriorityQueue to get the top from
127
+ * @return the top element in the PriorityQueue
128
+ */
129
+ extern void *frt_pq_top(FrtPriorityQueue *self);
130
+
131
+ /**
132
+ * Remove and return the top element in the PriorityQueue.
133
+ *
134
+ * @param self the PriorityQueue to get the top from
135
+ * @return the top element in the PriorityQueue
136
+ */
137
+ extern void *frt_pq_pop(FrtPriorityQueue *self);
138
+
139
+ /**
140
+ * Return true if the PriorityQueue is full.
141
+ *
142
+ * @param self the PriorityQueue to test
143
+ * @return true if the PriorityQueue is full.
144
+ */
145
+ #define frt_pq_full(pq) ((pq)->size == (pq)->capa)
146
+
147
+ #endif