C++ compilation.
[idzebra-moved-to-github.git] / rset / rsbool.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsbool.c,v $
7  * Revision 1.16  1999-05-26 07:49:14  adam
8  * C++ compilation.
9  *
10  * Revision 1.15  1999/02/02 14:51:32  adam
11  * Updated WIN32 code specific sections. Changed header.
12  *
13  * Revision 1.14  1998/03/05 08:36:27  adam
14  * New result set model.
15  *
16  * Revision 1.13  1997/12/18 10:54:24  adam
17  * New method result set method rs_hits that returns the number of
18  * hits in result-set (if known). The ranked result set returns real
19  * number of hits but only when not combined with other operands.
20  *
21  * Revision 1.12  1997/10/31 12:37:01  adam
22  * Code calls xfree() instead of free().
23  *
24  * Revision 1.11  1997/09/09 13:38:15  adam
25  * Partial port to WIN95/NT.
26  *
27  * Revision 1.10  1996/10/29 13:55:20  adam
28  * Include of zebrautl.h instead of alexutil.h.
29  *
30  * Revision 1.9  1995/12/11 09:15:22  adam
31  * New set types: sand/sor/snot - ranked versions of and/or/not in
32  * ranked/semi-ranked result sets.
33  * Note: the snot not finished yet.
34  * New rset member: flag.
35  * Bug fix: r_delete in rsrel.c did free bad memory block.
36  *
37  * Revision 1.8  1995/10/12  12:41:55  adam
38  * Private info (buf) moved from struct rset_control to struct rset.
39  * Bug fixes in relevance.
40  *
41  * Revision 1.7  1995/10/10  14:00:03  adam
42  * Function rset_open changed its wflag parameter to general flags.
43  *
44  * Revision 1.6  1995/10/06  14:38:05  adam
45  * New result set method: r_score.
46  * Local no (sysno) and score is transferred to retrieveCtrl.
47  *
48  * Revision 1.5  1995/09/08  14:52:41  adam
49  * Work on relevance feedback.
50  *
51  * Revision 1.4  1995/09/08  08:54:04  adam
52  * More efficient and operation.
53  *
54  * Revision 1.3  1995/09/07  13:58:43  adam
55  * New parameter: result-set file descriptor (RSFD) to support multiple
56  * positions within the same result-set.
57  * Boolean operators: and, or, not implemented.
58  *
59  * Revision 1.2  1995/09/06  16:11:55  adam
60  * More work on boolean sets.
61  *
62  * Revision 1.1  1995/09/06  13:27:15  adam
63  * New set type: bool. Not finished yet.
64  *
65  */
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <assert.h>
71
72 #include <rsbool.h>
73 #include <zebrautl.h>
74
75 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
76 static RSFD r_open (RSET ct, int flag);
77 static void r_close (RSFD rfd);
78 static void r_delete (RSET ct);
79 static void r_rewind (RSFD rfd);
80 static int r_count (RSET ct);
81 static int r_read_and (RSFD rfd, void *buf, int *term_index);
82 static int r_read_or (RSFD rfd, void *buf, int *term_index);
83 static int r_read_not (RSFD rfd, void *buf, int *term_index);
84 static int r_write (RSFD rfd, const void *buf);
85
86 static const struct rset_control control_and = 
87 {
88     "and",
89     r_create,
90     r_open,
91     r_close,
92     r_delete,
93     r_rewind,
94     r_count,
95     r_read_and,
96     r_write,
97 };
98
99 static const struct rset_control control_or = 
100 {
101     "or",
102     r_create,
103     r_open,
104     r_close,
105     r_delete,
106     r_rewind,
107     r_count,
108     r_read_or,
109     r_write,
110 };
111
112 static const struct rset_control control_not = 
113 {
114     "not",
115     r_create,
116     r_open,
117     r_close,
118     r_delete,
119     r_rewind,
120     r_count,
121     r_read_not,
122     r_write,
123 };
124
125
126 const struct rset_control *rset_kind_and = &control_and;
127 const struct rset_control *rset_kind_or = &control_or;
128 const struct rset_control *rset_kind_not = &control_not;
129
130 struct rset_bool_info {
131     int key_size;
132     RSET rset_l;
133     RSET rset_r;
134     int term_index_s;
135     int (*cmp)(const void *p1, const void *p2);
136     struct rset_bool_rfd *rfd_list;
137 };
138
139 struct rset_bool_rfd {
140     RSFD rfd_l;
141     RSFD rfd_r;
142     int  more_l;
143     int  more_r;
144     int term_index_l;
145     int term_index_r;
146     void *buf_l;
147     void *buf_r;
148     struct rset_bool_rfd *next;
149     struct rset_bool_info *info;
150 };    
151
152 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
153 {
154     rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
155     struct rset_bool_info *info;
156
157     info = (struct rset_bool_info *) xmalloc (sizeof(*info));
158     info->key_size = bool_parms->key_size;
159     info->rset_l = bool_parms->rset_l;
160     info->rset_r = bool_parms->rset_r;
161     if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
162         ct->flags |= RSET_FLAG_VOLATILE;
163     info->cmp = bool_parms->cmp;
164     info->rfd_list = NULL;
165     
166     info->term_index_s = info->rset_l->no_rset_terms;
167     ct->no_rset_terms =
168         info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
169     ct->rset_terms = (RSET_TERM *)
170         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
171
172     memcpy (ct->rset_terms, info->rset_l->rset_terms,
173             info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
174     memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
175             info->rset_r->rset_terms,
176             info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
177     return info;
178 }
179
180 static RSFD r_open (RSET ct, int flag)
181 {
182     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
183     struct rset_bool_rfd *rfd;
184
185     if (flag & RSETF_WRITE)
186     {
187         logf (LOG_FATAL, "bool set type is read-only");
188         return NULL;
189     }
190     rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
191     rfd->next = info->rfd_list;
192     info->rfd_list = rfd;
193     rfd->info = info;
194
195     rfd->buf_l = xmalloc (info->key_size);
196     rfd->buf_r = xmalloc (info->key_size);
197     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
198     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
199     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
200                              &rfd->term_index_l);
201     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
202                              &rfd->term_index_r);
203     return rfd;
204 }
205
206 static void r_close (RSFD rfd)
207 {
208     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
209     struct rset_bool_rfd **rfdp;
210     
211     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
212         if (*rfdp == rfd)
213         {
214             xfree ((*rfdp)->buf_l);
215             xfree ((*rfdp)->buf_r);
216             rset_close (info->rset_l, (*rfdp)->rfd_l);
217             rset_close (info->rset_r, (*rfdp)->rfd_r);
218             *rfdp = (*rfdp)->next;
219             xfree (rfd);
220             return;
221         }
222     logf (LOG_FATAL, "r_close but no rfd match!");
223     assert (0);
224 }
225
226 static void r_delete (RSET ct)
227 {
228     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
229
230     assert (info->rfd_list == NULL);
231     xfree (ct->rset_terms);
232     rset_delete (info->rset_l);
233     rset_delete (info->rset_r);
234     xfree (info);
235 }
236
237 static void r_rewind (RSFD rfd)
238 {
239     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
240     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
241
242     logf (LOG_DEBUG, "rsbool_rewind");
243     rset_rewind (info->rset_l, p->rfd_l);
244     rset_rewind (info->rset_r, p->rfd_r);
245     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
246     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
247 }
248
249 static int r_count (RSET ct)
250 {
251     return 0;
252 }
253
254 static int r_read_and (RSFD rfd, void *buf, int *term_index)
255 {
256     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
257     struct rset_bool_info *info = p->info;
258
259     while (p->more_l && p->more_r)
260     {
261         int cmp;
262
263         cmp = (*info->cmp)(p->buf_l, p->buf_r);
264         if (!cmp)
265         {
266             memcpy (buf, p->buf_l, info->key_size);
267             *term_index = p->term_index_l;
268             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
269                                    &p->term_index_l);
270             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
271                                    &p->term_index_r);
272             return 1;
273         }
274         else if (cmp == 1)
275         {
276             memcpy (buf, p->buf_r, info->key_size);
277
278             *term_index = p->term_index_r + info->term_index_s;
279             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
280                                    &p->term_index_r);
281             return 1;
282         }
283         else if (cmp == -1)
284         {
285             memcpy (buf, p->buf_l, info->key_size);
286             *term_index = p->term_index_l;
287             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
288                                    &p->term_index_l);
289             return 1;
290         }
291         else if (cmp > 1)
292             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
293                                    &p->term_index_r);
294         else
295             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
296                                    &p->term_index_l);
297     }
298     return 0;
299 }
300
301 static int r_read_or (RSFD rfd, void *buf, int *term_index)
302 {
303     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
304     struct rset_bool_info *info = p->info;
305
306     while (p->more_l || p->more_r)
307     {
308         int cmp;
309
310         if (p->more_l && p->more_r)
311             cmp = (*info->cmp)(p->buf_l, p->buf_r);
312         else if (p->more_r)
313             cmp = 2;
314         else
315             cmp = -2;
316         if (!cmp)
317         {
318             memcpy (buf, p->buf_l, info->key_size);
319             *term_index = p->term_index_l;
320             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
321                                    &p->term_index_l);
322             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
323                                    &p->term_index_r);
324             return 1;
325         }
326         else if (cmp > 0)
327         {
328             memcpy (buf, p->buf_r, info->key_size);
329             *term_index = p->term_index_r + info->term_index_s;
330             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
331                                    &p->term_index_r);
332             return 1;
333         }
334         else
335         {
336             memcpy (buf, p->buf_l, info->key_size);
337             *term_index = p->term_index_l;
338             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
339                                    &p->term_index_l);
340             return 1;
341         }
342     }
343     return 0;
344 }
345
346 static int r_read_not (RSFD rfd, void *buf, int *term_index)
347 {
348     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
349     struct rset_bool_info *info = p->info;
350
351     while (p->more_l || p->more_r)
352     {
353         int cmp;
354
355         if (p->more_l && p->more_r)
356             cmp = (*info->cmp)(p->buf_l, p->buf_r);
357         else if (p->more_r)
358             cmp = 2;
359         else
360             cmp = -2;
361         if (cmp < -1)
362         {
363             memcpy (buf, p->buf_l, info->key_size);
364             *term_index = p->term_index_l;
365             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
366                                    &p->term_index_l);
367             return 1;
368         }
369         else if (cmp > 1)
370             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
371                                    &p->term_index_r);
372         else
373         {
374             memcpy (buf, p->buf_l, info->key_size);
375             do
376             {
377                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
378                                        &p->term_index_l);
379                 if (!p->more_l)
380                     break;
381                 cmp = (*info->cmp)(p->buf_l, buf);
382             } while (cmp >= -1 && cmp <= 1);
383             do
384             {
385                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
386                                        &p->term_index_r);
387                 if (!p->more_r)
388                     break;
389                 cmp = (*info->cmp)(p->buf_r, buf);
390             } while (cmp >= -1 && cmp <= 1);
391         }
392     }
393     return 0;
394 }
395
396
397 static int r_write (RSFD rfd, const void *buf)
398 {
399     logf (LOG_FATAL, "bool set type is read-only");
400     return -1;
401 }
402