/*
- * Copyright (C) 1994-1995, Index Data I/S
+ * Copyright (C) 1994-1996, Index Data I/S
* All rights reserved.
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: rssbool.c,v $
- * Revision 1.1 1995-12-11 09:15:27 adam
+ * Revision 1.5 1997-09-09 13:38:16 adam
+ * Partial port to WIN95/NT.
+ *
+ * Revision 1.4 1996/10/29 13:55:27 adam
+ * Include of zebrautl.h instead of alexutil.h.
+ *
+ * Revision 1.3 1996/10/08 13:00:41 adam
+ * Bug fix: result sets with ranked operands in boolean operations weren't
+ * sorted.
+ *
+ * Revision 1.2 1996/05/15 18:35:17 adam
+ * Implemented snot operation.
+ *
+ * Revision 1.1 1995/12/11 09:15:27 adam
* New set types: sand/sor/snot - ranked versions of and/or/not in
* ranked/semi-ranked result sets.
* Note: the snot not finished yet.
*
*/
-#include <stdio.h>
#include <assert.h>
+#include <stdio.h>
+#include <string.h>
#include <rsbool.h>
-#include <alexutil.h>
+#include <zebrautl.h>
static void *r_create_and(const struct rset_control *sel, void *parms,
int *flags);
RSET rset_r;
char *key_buf;
int *score_buf;
+ int *score_idx;
int key_no;
int key_max;
int (*cmp)(const void *p1, const void *p2);
struct rset_bool_info *info;
int position;
int last_pos;
- int open_flag;
+ int flag;
};
static void *r_create_common (const struct rset_control *sel,
rset_bool_parms *bool_parms, int *flags);
-
+
+static struct rset_bool_info *qsort_info;
+
+static int qcomp (const void *p1, const void *p2)
+{
+ int i1 = *(int*) p1;
+ int i2 = *(int*) p2;
+ return qsort_info->score_buf[i2] - qsort_info->score_buf[i1];
+}
+
static void key_add (struct rset_bool_info *info,
char *buf, int score)
{
memcpy (info->key_buf + info->key_size * info->key_no,
buf, info->key_size);
info->score_buf[info->key_no] = score;
+ info->score_idx[info->key_no] = info->key_no;
(info->key_no)++;
}
more_r = rset_read (info->rset_r, fd_r, buf_r);
}
else if (cmp > 1)
- {
- rset_score (info->rset_r, fd_r, &score_r);
- if (score_r != -1)
- key_add (info, buf_r, 1);
more_r = rset_read (info->rset_r, fd_r, buf_r);
- }
else
- {
- rset_score (info->rset_l, fd_l, &score_l);
- if (score_l != -1)
- key_add (info, buf_l, 1);
more_l = rset_read (info->rset_l, fd_l, buf_l);
- }
}
rset_close (info->rset_l, fd_l);
rset_close (info->rset_r, fd_r);
rset_delete (info->rset_r);
xfree (buf_l);
xfree (buf_r);
+ qsort_info = info;
+ qsort (info->score_idx, info->key_no, sizeof(*info->score_idx), qcomp);
return info;
}
rset_delete (info->rset_r);
xfree (buf_l);
xfree (buf_r);
+ qsort_info = info;
+ qsort (info->score_idx, info->key_no, sizeof(*info->score_idx), qcomp);
return info;
}
int *flags)
{
char *buf_l, *buf_r;
+ int more_l, more_r;
+ RSFD fd_l, fd_r;
struct rset_bool_info *info;
info = r_create_common (sel, parms, flags);
buf_l = xmalloc (info->key_size);
buf_r = xmalloc (info->key_size);
+
+ fd_l = rset_open (info->rset_l, RSETF_SORT_SYSNO|RSETF_READ);
+ fd_r = rset_open (info->rset_r, RSETF_SORT_SYSNO|RSETF_READ);
+
+ more_l = rset_read(info->rset_l, fd_l, buf_l);
+ more_r = rset_read(info->rset_r, fd_r, buf_r);
+
+ while (more_l || more_r)
+ {
+ int cmp;
+ int score;
+
+ if (more_l && more_r)
+ cmp = (*info->cmp)(buf_l, buf_r);
+ else if (more_r)
+ cmp = 2;
+ else
+ cmp = -2;
+
+ if (cmp >= -1 && cmp <= 1)
+ more_l = rset_read (info->rset_l, fd_l, buf_l);
+ else if (cmp > 1)
+ {
+ more_r = rset_read (info->rset_r, fd_r, buf_r);
+ }
+ else
+ {
+ rset_score (info->rset_l, fd_l, &score);
+ key_add (info, buf_l, score == -1 ? 1 : score);
+ more_l = rset_read (info->rset_l, fd_l, buf_l);
+ }
+ }
+ rset_close (info->rset_l, fd_l);
+ rset_close (info->rset_r, fd_r);
+
rset_delete (info->rset_l);
rset_delete (info->rset_r);
xfree (buf_l);
xfree (buf_r);
+ qsort_info = info;
+ qsort (info->score_idx, info->key_no, sizeof(*info->score_idx), qcomp);
return info;
}
info->key_max = 1000;
info->key_buf = xmalloc (info->key_size * info->key_max);
info->score_buf = xmalloc (info->key_max * sizeof(*info->score_buf));
+ info->score_idx = xmalloc (info->key_max * sizeof(*info->score_idx));
info->key_no = 0;
return info;
rfd->position = 0;
rfd->last_pos = 0;
- rfd->open_flag = flag;
+ rfd->flag = flag;
return rfd;
}
assert (info->rfd_list == NULL);
xfree (info->score_buf);
+ xfree (info->score_idx);
xfree (info->key_buf);
xfree (info);
}
if (p->position >= info->key_no)
return 0;
- p->last_pos = (p->position)++;
+ if (p->flag & RSETF_SORT_RANK)
+ p->last_pos = info->score_idx[(p->position)++];
+ else
+ p->last_pos = (p->position)++;
memcpy (buf, info->key_buf + info->key_size * p->last_pos,
info->key_size);
return 1;