sdsykes-ferret 0.11.6.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +102 -0
  4. data/Rakefile +338 -0
  5. data/TODO +17 -0
  6. data/TUTORIAL +231 -0
  7. data/bin/ferret-browser +79 -0
  8. data/ext/analysis.c +1555 -0
  9. data/ext/analysis.h +219 -0
  10. data/ext/api.c +69 -0
  11. data/ext/api.h +27 -0
  12. data/ext/array.c +123 -0
  13. data/ext/array.h +53 -0
  14. data/ext/bitvector.c +540 -0
  15. data/ext/bitvector.h +272 -0
  16. data/ext/compound_io.c +383 -0
  17. data/ext/config.h +42 -0
  18. data/ext/document.c +156 -0
  19. data/ext/document.h +53 -0
  20. data/ext/except.c +120 -0
  21. data/ext/except.h +168 -0
  22. data/ext/extconf.rb +14 -0
  23. data/ext/ferret.c +402 -0
  24. data/ext/ferret.h +91 -0
  25. data/ext/filter.c +156 -0
  26. data/ext/fs_store.c +483 -0
  27. data/ext/global.c +418 -0
  28. data/ext/global.h +117 -0
  29. data/ext/hash.c +567 -0
  30. data/ext/hash.h +473 -0
  31. data/ext/hashset.c +170 -0
  32. data/ext/hashset.h +187 -0
  33. data/ext/header.h +58 -0
  34. data/ext/helper.c +62 -0
  35. data/ext/helper.h +13 -0
  36. data/ext/inc/lang.h +48 -0
  37. data/ext/inc/threading.h +31 -0
  38. data/ext/index.c +6425 -0
  39. data/ext/index.h +961 -0
  40. data/ext/lang.h +66 -0
  41. data/ext/libstemmer.c +92 -0
  42. data/ext/libstemmer.h +79 -0
  43. data/ext/mempool.c +87 -0
  44. data/ext/mempool.h +35 -0
  45. data/ext/modules.h +162 -0
  46. data/ext/multimapper.c +310 -0
  47. data/ext/multimapper.h +51 -0
  48. data/ext/posh.c +1006 -0
  49. data/ext/posh.h +1007 -0
  50. data/ext/priorityqueue.c +151 -0
  51. data/ext/priorityqueue.h +143 -0
  52. data/ext/q_boolean.c +1608 -0
  53. data/ext/q_const_score.c +161 -0
  54. data/ext/q_filtered_query.c +209 -0
  55. data/ext/q_fuzzy.c +268 -0
  56. data/ext/q_match_all.c +148 -0
  57. data/ext/q_multi_term.c +677 -0
  58. data/ext/q_parser.c +2825 -0
  59. data/ext/q_phrase.c +1126 -0
  60. data/ext/q_prefix.c +100 -0
  61. data/ext/q_range.c +350 -0
  62. data/ext/q_span.c +2402 -0
  63. data/ext/q_term.c +337 -0
  64. data/ext/q_wildcard.c +171 -0
  65. data/ext/r_analysis.c +2575 -0
  66. data/ext/r_index.c +3472 -0
  67. data/ext/r_qparser.c +585 -0
  68. data/ext/r_search.c +4105 -0
  69. data/ext/r_store.c +513 -0
  70. data/ext/r_utils.c +963 -0
  71. data/ext/ram_store.c +471 -0
  72. data/ext/search.c +1741 -0
  73. data/ext/search.h +885 -0
  74. data/ext/similarity.c +150 -0
  75. data/ext/similarity.h +82 -0
  76. data/ext/sort.c +983 -0
  77. data/ext/stem_ISO_8859_1_danish.c +338 -0
  78. data/ext/stem_ISO_8859_1_danish.h +16 -0
  79. data/ext/stem_ISO_8859_1_dutch.c +635 -0
  80. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  81. data/ext/stem_ISO_8859_1_english.c +1156 -0
  82. data/ext/stem_ISO_8859_1_english.h +16 -0
  83. data/ext/stem_ISO_8859_1_finnish.c +792 -0
  84. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  85. data/ext/stem_ISO_8859_1_french.c +1276 -0
  86. data/ext/stem_ISO_8859_1_french.h +16 -0
  87. data/ext/stem_ISO_8859_1_german.c +512 -0
  88. data/ext/stem_ISO_8859_1_german.h +16 -0
  89. data/ext/stem_ISO_8859_1_italian.c +1091 -0
  90. data/ext/stem_ISO_8859_1_italian.h +16 -0
  91. data/ext/stem_ISO_8859_1_norwegian.c +296 -0
  92. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  93. data/ext/stem_ISO_8859_1_porter.c +776 -0
  94. data/ext/stem_ISO_8859_1_porter.h +16 -0
  95. data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
  96. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  97. data/ext/stem_ISO_8859_1_spanish.c +1119 -0
  98. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  99. data/ext/stem_ISO_8859_1_swedish.c +307 -0
  100. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  101. data/ext/stem_KOI8_R_russian.c +701 -0
  102. data/ext/stem_KOI8_R_russian.h +16 -0
  103. data/ext/stem_UTF_8_danish.c +344 -0
  104. data/ext/stem_UTF_8_danish.h +16 -0
  105. data/ext/stem_UTF_8_dutch.c +653 -0
  106. data/ext/stem_UTF_8_dutch.h +16 -0
  107. data/ext/stem_UTF_8_english.c +1176 -0
  108. data/ext/stem_UTF_8_english.h +16 -0
  109. data/ext/stem_UTF_8_finnish.c +808 -0
  110. data/ext/stem_UTF_8_finnish.h +16 -0
  111. data/ext/stem_UTF_8_french.c +1296 -0
  112. data/ext/stem_UTF_8_french.h +16 -0
  113. data/ext/stem_UTF_8_german.c +526 -0
  114. data/ext/stem_UTF_8_german.h +16 -0
  115. data/ext/stem_UTF_8_italian.c +1113 -0
  116. data/ext/stem_UTF_8_italian.h +16 -0
  117. data/ext/stem_UTF_8_norwegian.c +302 -0
  118. data/ext/stem_UTF_8_norwegian.h +16 -0
  119. data/ext/stem_UTF_8_porter.c +794 -0
  120. data/ext/stem_UTF_8_porter.h +16 -0
  121. data/ext/stem_UTF_8_portuguese.c +1055 -0
  122. data/ext/stem_UTF_8_portuguese.h +16 -0
  123. data/ext/stem_UTF_8_russian.c +709 -0
  124. data/ext/stem_UTF_8_russian.h +16 -0
  125. data/ext/stem_UTF_8_spanish.c +1137 -0
  126. data/ext/stem_UTF_8_spanish.h +16 -0
  127. data/ext/stem_UTF_8_swedish.c +313 -0
  128. data/ext/stem_UTF_8_swedish.h +16 -0
  129. data/ext/stopwords.c +401 -0
  130. data/ext/store.c +692 -0
  131. data/ext/store.h +777 -0
  132. data/ext/term_vectors.c +352 -0
  133. data/ext/threading.h +31 -0
  134. data/ext/utilities.c +446 -0
  135. data/ext/win32.h +54 -0
  136. data/lib/ferret.rb +29 -0
  137. data/lib/ferret/browser.rb +246 -0
  138. data/lib/ferret/browser/s/global.js +192 -0
  139. data/lib/ferret/browser/s/style.css +148 -0
  140. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  141. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  142. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  143. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  144. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  145. data/lib/ferret/browser/views/layout.rhtml +22 -0
  146. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  147. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  148. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  149. data/lib/ferret/browser/webrick.rb +14 -0
  150. data/lib/ferret/document.rb +130 -0
  151. data/lib/ferret/field_infos.rb +44 -0
  152. data/lib/ferret/index.rb +786 -0
  153. data/lib/ferret/number_tools.rb +157 -0
  154. data/lib/ferret_version.rb +3 -0
  155. data/setup.rb +1555 -0
  156. data/test/test_all.rb +5 -0
  157. data/test/test_helper.rb +24 -0
  158. data/test/threading/number_to_spoken.rb +132 -0
  159. data/test/threading/thread_safety_index_test.rb +79 -0
  160. data/test/threading/thread_safety_read_write_test.rb +76 -0
  161. data/test/threading/thread_safety_test.rb +133 -0
  162. data/test/unit/analysis/tc_analyzer.rb +548 -0
  163. data/test/unit/analysis/tc_token_stream.rb +646 -0
  164. data/test/unit/index/tc_index.rb +762 -0
  165. data/test/unit/index/tc_index_reader.rb +699 -0
  166. data/test/unit/index/tc_index_writer.rb +437 -0
  167. data/test/unit/index/th_doc.rb +315 -0
  168. data/test/unit/largefile/tc_largefile.rb +46 -0
  169. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  170. data/test/unit/search/tc_filter.rb +135 -0
  171. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  172. data/test/unit/search/tc_index_searcher.rb +61 -0
  173. data/test/unit/search/tc_multi_searcher.rb +128 -0
  174. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  175. data/test/unit/search/tc_search_and_sort.rb +179 -0
  176. data/test/unit/search/tc_sort.rb +49 -0
  177. data/test/unit/search/tc_sort_field.rb +27 -0
  178. data/test/unit/search/tc_spans.rb +190 -0
  179. data/test/unit/search/tm_searcher.rb +384 -0
  180. data/test/unit/store/tc_fs_store.rb +77 -0
  181. data/test/unit/store/tc_ram_store.rb +35 -0
  182. data/test/unit/store/tm_store.rb +34 -0
  183. data/test/unit/store/tm_store_lock.rb +68 -0
  184. data/test/unit/tc_document.rb +81 -0
  185. data/test/unit/ts_analysis.rb +2 -0
  186. data/test/unit/ts_index.rb +2 -0
  187. data/test/unit/ts_largefile.rb +4 -0
  188. data/test/unit/ts_query_parser.rb +2 -0
  189. data/test/unit/ts_search.rb +2 -0
  190. data/test/unit/ts_store.rb +2 -0
  191. data/test/unit/ts_utils.rb +2 -0
  192. data/test/unit/utils/tc_bit_vector.rb +295 -0
  193. data/test/unit/utils/tc_number_tools.rb +117 -0
  194. data/test/unit/utils/tc_priority_queue.rb +106 -0
  195. metadata +285 -0
@@ -0,0 +1,219 @@
1
+ #ifndef FRT_ANALYSIS_H
2
+ #define FRT_ANALYSIS_H
3
+
4
+ #include "global.h"
5
+ #include "hash.h"
6
+ #include "multimapper.h"
7
+ #include <wchar.h>
8
+
9
+ /****************************************************************************
10
+ *
11
+ * Token
12
+ *
13
+ ****************************************************************************/
14
+
15
+ typedef struct Token
16
+ {
17
+ char text[MAX_WORD_SIZE];
18
+ int len;
19
+ off_t start;
20
+ off_t end;
21
+ int pos_inc;
22
+ } Token;
23
+
24
+ extern Token *tk_new();
25
+ extern void tk_destroy(void *p);
26
+ extern Token *tk_set(Token *tk, char *text, int tlen, off_t start, off_t end,
27
+ int pos_inc);
28
+ extern Token *tk_set_no_len(Token *tk, char *text, off_t start, off_t end,
29
+ int pos_inc);
30
+ extern int tk_eq(Token *tk1, Token *tk2);
31
+ extern int tk_cmp(Token *tk1, Token *tk2);
32
+
33
+ /****************************************************************************
34
+ *
35
+ * TokenStream
36
+ *
37
+ ****************************************************************************/
38
+
39
+
40
+ typedef struct TokenStream TokenStream;
41
+ struct TokenStream
42
+ {
43
+ char *t; /* ptr used to scan text */
44
+ char *text;
45
+ Token *(*next)(TokenStream *ts);
46
+ TokenStream *(*reset)(TokenStream *ts, char *text);
47
+ TokenStream *(*clone_i)(TokenStream *ts);
48
+ void (*destroy_i)(TokenStream *ts);
49
+ int ref_cnt;
50
+ };
51
+
52
+ #define ts_new(type) ts_new_i(sizeof(type))
53
+ extern TokenStream *ts_new_i(size_t size);
54
+ extern TokenStream *ts_clone_size(TokenStream *orig_ts, size_t size);
55
+
56
+ typedef struct CachedTokenStream
57
+ {
58
+ TokenStream super;
59
+ Token token;
60
+ } CachedTokenStream;
61
+
62
+ typedef struct MultiByteTokenStream
63
+ {
64
+ CachedTokenStream super;
65
+ mbstate_t state;
66
+ } MultiByteTokenStream;
67
+
68
+ typedef struct StandardTokenizer
69
+ {
70
+ CachedTokenStream super;
71
+ bool (*advance_to_start)(TokenStream *ts);
72
+ bool (*is_tok_char)(char *c);
73
+ int (*get_alpha)(TokenStream *ts, char *token);
74
+ int (*get_apostrophe)(char *input);
75
+ } StandardTokenizer;
76
+
77
+ typedef struct TokenFilter
78
+ {
79
+ TokenStream super;
80
+ TokenStream *sub_ts;
81
+ } TokenFilter;
82
+
83
+ extern TokenStream *filter_clone_size(TokenStream *ts, size_t size);
84
+ #define tf_new(type, sub) tf_new_i(sizeof(type), sub)
85
+ extern TokenStream *tf_new_i(size_t size, TokenStream *sub_ts);
86
+
87
+ typedef struct StopFilter
88
+ {
89
+ TokenFilter super;
90
+ HashTable *words;
91
+ } StopFilter;
92
+
93
+ typedef struct MappingFilter
94
+ {
95
+ TokenFilter super;
96
+ MultiMapper *mapper;
97
+ } MappingFilter;
98
+
99
+ typedef struct HyphenFilter
100
+ {
101
+ TokenFilter super;
102
+ char text[MAX_WORD_SIZE];
103
+ int start;
104
+ int pos;
105
+ int len;
106
+ Token *tk;
107
+ } HyphenFilter;
108
+
109
+ typedef struct StemFilter
110
+ {
111
+ TokenFilter super;
112
+ struct sb_stemmer *stemmer;
113
+ char *algorithm;
114
+ char *charenc;
115
+ } StemFilter;
116
+
117
+ #define ts_next(mts) mts->next(mts)
118
+ #define ts_clone(mts) mts->clone_i(mts)
119
+
120
+ extern void ts_deref(TokenStream *ts);
121
+
122
+ extern TokenStream *non_tokenizer_new();
123
+
124
+ extern TokenStream *whitespace_tokenizer_new();
125
+ extern TokenStream *mb_whitespace_tokenizer_new(bool lowercase);
126
+
127
+ extern TokenStream *letter_tokenizer_new();
128
+ extern TokenStream *mb_letter_tokenizer_new(bool lowercase);
129
+
130
+ extern TokenStream *standard_tokenizer_new();
131
+ extern TokenStream *mb_standard_tokenizer_new();
132
+
133
+ extern TokenStream *hyphen_filter_new(TokenStream *ts);
134
+ extern TokenStream *lowercase_filter_new(TokenStream *ts);
135
+ extern TokenStream *mb_lowercase_filter_new(TokenStream *ts);
136
+
137
+ extern const char *ENGLISH_STOP_WORDS[];
138
+ extern const char *FULL_ENGLISH_STOP_WORDS[];
139
+ extern const char *EXTENDED_ENGLISH_STOP_WORDS[];
140
+ extern const char *FULL_FRENCH_STOP_WORDS[];
141
+ extern const char *FULL_SPANISH_STOP_WORDS[];
142
+ extern const char *FULL_PORTUGUESE_STOP_WORDS[];
143
+ extern const char *FULL_ITALIAN_STOP_WORDS[];
144
+ extern const char *FULL_GERMAN_STOP_WORDS[];
145
+ extern const char *FULL_DUTCH_STOP_WORDS[];
146
+ extern const char *FULL_SWEDISH_STOP_WORDS[];
147
+ extern const char *FULL_NORWEGIAN_STOP_WORDS[];
148
+ extern const char *FULL_DANISH_STOP_WORDS[];
149
+ extern const char *FULL_RUSSIAN_STOP_WORDS[];
150
+ extern const char *FULL_FINNISH_STOP_WORDS[];
151
+
152
+ extern TokenStream *stop_filter_new_with_words_len(TokenStream *ts,
153
+ const char **words, int len);
154
+ extern TokenStream *stop_filter_new_with_words(TokenStream *ts,
155
+ const char **words);
156
+ extern TokenStream *stop_filter_new(TokenStream *ts);
157
+ extern TokenStream *stem_filter_new(TokenStream *ts, const char *algorithm,
158
+ const char *charenc);
159
+
160
+ extern TokenStream *mapping_filter_new(TokenStream *ts);
161
+ extern TokenStream *mapping_filter_add(TokenStream *ts, const char *pattern,
162
+ const char *replacement);
163
+
164
+ /****************************************************************************
165
+ *
166
+ * Analyzer
167
+ *
168
+ ****************************************************************************/
169
+
170
+ typedef struct Analyzer
171
+ {
172
+ TokenStream *current_ts;
173
+ TokenStream *(*get_ts)(struct Analyzer *a, char *field, char *text);
174
+ void (*destroy_i)(struct Analyzer *a);
175
+ int ref_cnt;
176
+ } Analyzer;
177
+
178
+ extern void a_deref(Analyzer *a);
179
+
180
+ #define a_get_ts(ma, field, text) ma->get_ts(ma, field, text)
181
+
182
+ extern Analyzer *analyzer_new(TokenStream *ts,
183
+ void (*destroy)(Analyzer *a),
184
+ TokenStream *(*get_ts)(Analyzer *a,
185
+ char *field,
186
+ char *text));
187
+ extern void a_standard_destroy(Analyzer *a);
188
+ extern Analyzer *non_analyzer_new();
189
+
190
+ extern Analyzer *whitespace_analyzer_new(bool lowercase);
191
+ extern Analyzer *mb_whitespace_analyzer_new(bool lowercase);
192
+
193
+ extern Analyzer *letter_analyzer_new(bool lowercase);
194
+ extern Analyzer *mb_letter_analyzer_new(bool lowercase);
195
+
196
+ extern Analyzer *standard_analyzer_new(bool lowercase);
197
+ extern Analyzer *mb_standard_analyzer_new(bool lowercase);
198
+
199
+ extern Analyzer *standard_analyzer_new_with_words(const char **words,
200
+ bool lowercase);
201
+ extern Analyzer *standard_analyzer_new_with_words_len(const char **words, int len,
202
+ bool lowercase);
203
+ extern Analyzer *mb_standard_analyzer_new_with_words(const char **words,
204
+ bool lowercase);
205
+ extern Analyzer *mb_standard_analyzer_new_with_words_len(const char **words,
206
+ int len, bool lowercase);
207
+
208
+ #define PFA(analyzer) ((PerFieldAnalyzer *)(analyzer))
209
+ typedef struct PerFieldAnalyzer
210
+ {
211
+ Analyzer super;
212
+ HashTable *dict;
213
+ Analyzer *default_a;
214
+ } PerFieldAnalyzer;
215
+
216
+ extern Analyzer *per_field_analyzer_new(Analyzer *a);
217
+ extern void pfa_add_field(Analyzer *self, char *field, Analyzer *analyzer);
218
+
219
+ #endif
@@ -0,0 +1,69 @@
1
+
2
+ #include <stdlib.h> /* for calloc, free */
3
+ #include "header.h"
4
+
5
+ extern struct SN_env * SN_create_env(int S_size, int I_size, int B_size)
6
+ {
7
+ struct SN_env * z = (struct SN_env *) calloc(1, sizeof(struct SN_env));
8
+ if (z == NULL) return NULL;
9
+ z->p = create_s();
10
+ if (z->p == NULL) goto error;
11
+ if (S_size)
12
+ {
13
+ int i;
14
+ z->S = (symbol * *) calloc(S_size, sizeof(symbol *));
15
+ if (z->S == NULL) goto error;
16
+
17
+ for (i = 0; i < S_size; i++)
18
+ {
19
+ z->S[i] = create_s();
20
+ if (z->S[i] == NULL) goto error;
21
+ }
22
+ z->S_size = S_size;
23
+ }
24
+
25
+ if (I_size)
26
+ {
27
+ z->I = (int *) calloc(I_size, sizeof(int));
28
+ if (z->I == NULL) goto error;
29
+ z->I_size = I_size;
30
+ }
31
+
32
+ if (B_size)
33
+ {
34
+ z->B = (symbol *) calloc(B_size, sizeof(symbol));
35
+ if (z->B == NULL) goto error;
36
+ z->B_size = B_size;
37
+ }
38
+
39
+ return z;
40
+ error:
41
+ SN_close_env(z);
42
+ return NULL;
43
+ }
44
+
45
+ extern void SN_close_env(struct SN_env * z)
46
+ {
47
+ if (z == NULL) return;
48
+ if (z->S_size)
49
+ {
50
+ int i;
51
+ for (i = 0; i < z->S_size; i++)
52
+ {
53
+ lose_s(z->S[i]);
54
+ }
55
+ free(z->S);
56
+ }
57
+ if (z->I_size) free(z->I);
58
+ if (z->B_size) free(z->B);
59
+ if (z->p) lose_s(z->p);
60
+ free(z);
61
+ }
62
+
63
+ extern int SN_set_current(struct SN_env * z, int size, const symbol * s)
64
+ {
65
+ int err = replace_s(z, 0, z->l, size, s, NULL);
66
+ z->c = 0;
67
+ return err;
68
+ }
69
+
@@ -0,0 +1,27 @@
1
+
2
+ typedef unsigned char symbol;
3
+
4
+ /* Or replace 'char' above with 'short' for 16 bit characters.
5
+
6
+ More precisely, replace 'char' with whatever type guarantees the
7
+ character width you need. Note however that sizeof(symbol) should divide
8
+ HEAD, defined in header.h as 2*sizeof(int), without remainder, otherwise
9
+ there is an alignment problem. In the unlikely event of a problem here,
10
+ consult Martin Porter.
11
+
12
+ */
13
+
14
+ struct SN_env {
15
+ symbol * p;
16
+ int c; int a; int l; int lb; int bra; int ket;
17
+ int S_size; int I_size; int B_size;
18
+ symbol * * S;
19
+ int * I;
20
+ symbol * B;
21
+ };
22
+
23
+ extern struct SN_env * SN_create_env(int S_size, int I_size, int B_size);
24
+ extern void SN_close_env(struct SN_env * z);
25
+
26
+ extern int SN_set_current(struct SN_env * z, int size, const symbol * s);
27
+
@@ -0,0 +1,123 @@
1
+ #include "array.h"
2
+ #include <string.h>
3
+
4
+ #define META_CNT ARY_META_CNT
5
+ #define DATA_SZ sizeof(int) * META_CNT
6
+
7
+ void **ary_new_i(int type_size, int init_capa)
8
+ {
9
+ void **ary;
10
+ if (init_capa <= 0) {
11
+ init_capa = ARY_INIT_CAPA;
12
+ }
13
+ ary = (void **)&(((int *)ecalloc(DATA_SZ + init_capa * type_size))[META_CNT]);
14
+ ary_type_size(ary) = type_size;
15
+ ary_capa(ary) = init_capa;
16
+ return ary;
17
+ }
18
+
19
+ INLINE void ary_resize_i(void ***ary, int size)
20
+ {
21
+ size++;
22
+ if (size >= ary_sz(*ary)) {
23
+ int capa = ary_capa(*ary);
24
+ if (size >= capa) {
25
+ int *ary_start = &((int *)*ary)[-META_CNT];
26
+ while (size >= capa) {
27
+ capa <<= 1;
28
+ }
29
+
30
+ ary_start = (int *)erealloc(ary_start,
31
+ DATA_SZ + capa * ary_type_size(*ary));
32
+ *ary = (void **)&(ary_start[META_CNT]);
33
+ memset(((char *)*ary) + ary_type_size(*ary) * ary_sz(*ary), 0,
34
+ (capa - ary_sz(*ary)) * ary_type_size(*ary));
35
+ ary_capa(*ary) = capa;
36
+ }
37
+ ary_sz(*ary) = size;
38
+ }
39
+ }
40
+
41
+ void ary_set_i(void ***ary, int index, void *value)
42
+ {
43
+ if (index < 0) {
44
+ index += ary_sz(*ary);
45
+ if (index < 0) {
46
+ RAISE(INDEX_ERROR, "index %d out array", index);
47
+ }
48
+ }
49
+ ary_resize_i(ary, index);
50
+ (*ary)[index] = value;
51
+ }
52
+
53
+ void *ary_get_i(void **ary, int index)
54
+ {
55
+ if (index < 0) {
56
+ index += ary_sz(ary);
57
+ }
58
+ if (index >= 0 && index < ary_sz(ary)) {
59
+ return ary[index];
60
+ }
61
+ else {
62
+ return NULL;
63
+ }
64
+ }
65
+
66
+ void ary_push_i(void ***ary, void *value)
67
+ {
68
+ int size = ary_sz(*ary);
69
+ ary_resize_i(ary, size);
70
+ (*ary)[size] = value;
71
+ }
72
+
73
+ void *ary_pop_i(void **ary)
74
+ {
75
+ void *val = ary[--ary_sz(ary)];
76
+ ary[ary_sz(ary)] = NULL;
77
+ return val;
78
+ }
79
+
80
+ void ary_unshift_i(void ***ary, void *value)
81
+ {
82
+ int size = ary_sz(*ary);
83
+ ary_resize_i(ary, size);
84
+ memmove(*ary, *ary + 1, size * sizeof(void *));
85
+ (*ary)[0] = value;
86
+ }
87
+
88
+ void *ary_shift_i(void **ary)
89
+ {
90
+ void *val = ary[0];
91
+ int size = --ary_sz(ary);
92
+ memmove(ary, ary + 1, size * sizeof(void *));
93
+ ary[size] = NULL;
94
+ return val;
95
+ }
96
+
97
+ void *ary_remove_i(void **ary, int index)
98
+ {
99
+ if (index >= 0 && index < ary_sz(ary)) {
100
+ void *val = ary[index];
101
+ memmove(ary + index, ary + index + 1,
102
+ (ary_sz(ary) - index + 1) * sizeof(void *));
103
+ ary_sz(ary)--;
104
+ return val;
105
+ }
106
+ else {
107
+ return NULL;
108
+ }
109
+ }
110
+
111
+ void ary_delete_i(void **ary, int index, void (*free_elem)(void *p))
112
+ {
113
+ free_elem(ary_remove(ary, index));
114
+ }
115
+
116
+ void ary_destroy_i(void **ary, void (*free_elem)(void *p))
117
+ {
118
+ int i;
119
+ for (i = ary_sz(ary) - 1; i >= 0; i--) {
120
+ free_elem(ary[i]);
121
+ }
122
+ ary_free(ary);
123
+ }
@@ -0,0 +1,53 @@
1
+ #ifndef FRT_ARRAY_H
2
+ #define FRT_ARRAY_H
3
+ #include "global.h"
4
+
5
+ #if defined POSH_OS_SOLARIS || defined POSH_OS_SUNOS
6
+ # define ARY_META_CNT 4
7
+ #else
8
+ # define ARY_META_CNT 3
9
+ #endif
10
+
11
+ #define ARY_INIT_CAPA 8
12
+ #define ary_size(ary) ary_sz(ary)
13
+ #define ary_sz(ary) (((int *)ary)[-1])
14
+ #define ary_capa(ary) (((int *)ary)[-2])
15
+ #define ary_type_size(ary) (((int *)ary)[-3])
16
+ #define ary_start(ary) ((void **)&(((int *)ary)[-ARY_META_CNT]))
17
+ #define ary_free(ary) free(ary_start(ary))
18
+
19
+ #define ary_new_type_capa(type, init_capa)\
20
+ (type *)ary_new_i(sizeof(type), init_capa)
21
+ #define ary_new_type(type) (type *)ary_new_i(sizeof(type), 0)
22
+ #define ary_new_capa(init_capa) ary_new_i(sizeof(void *), init_capa)
23
+ #define ary_new() ary_new_i(sizeof(void *), 0)
24
+ #define ary_resize(ary, size) ary_resize_i(((void ***)(void *)&ary), size)
25
+ #define ary_set(ary, i, val) ary_set_i(((void ***)(void *)&ary), i, val)
26
+ #define ary_get(ary, i) ary_get_i(((void **)ary), i)
27
+ #define ary_push(ary, val) ary_push_i(((void ***)(void *)&ary), val)
28
+ #define ary_pop(ary) ary_pop_i(((void **)ary))
29
+ #define ary_unshift(ary, val) ary_unshift_i(((void ***)(void *)&ary), val)
30
+ #define ary_shift(ary) ary_shift_i(((void **)ary))
31
+ #define ary_remove(ary, i) ary_remove_i(((void **)ary), i)
32
+ #define ary_delete(ary, i, f) ary_delete_i(((void **)ary), i, (free_ft)f)
33
+ #define ary_destroy(ary, f) ary_destroy_i(((void **)ary), (free_ft)f)
34
+ #define ary_rsz(ary, size) ary_resize(ary, size)
35
+ #define ary_grow(ary) ary_resize(ary, ary_sz(ary))
36
+ #define ary_last(ary) ary[ary_sz(ary) - 1]
37
+ #define ary_sort(ary, cmp) qsort(ary, ary_size(ary), ary_type_size(ary), cmp)
38
+ #define ary_each_rev(ary, i) for (i = ary_size(ary) - 1; i >= 0; i--)
39
+ #define ary_each(ary, i) for (i = 0; i < ary_size(ary); i++)
40
+
41
+ extern void ary_resize_i(void ***ary, int size);
42
+ extern void **ary_new_i(int type_size, int init_capa);
43
+ extern void ary_set_i(void ***ary, int index, void *value);
44
+ extern void *ary_get_i(void **ary, int index);
45
+ extern void ary_push_i(void ***ary, void *value);
46
+ extern void *ary_pop_i(void **ary);
47
+ extern void ary_unshift_i(void ***ary, void *value);
48
+ extern void *ary_shift_i(void **ary);
49
+ extern void *ary_remove_i(void **ary, int index);
50
+ extern void ary_delete_i(void **ary, int index, void (*free_elem)(void *p));
51
+ extern void ary_destroy_i(void **ary, void (*free_elem)(void *p));
52
+
53
+ #endif