+static void add_rec (struct rset_rel_info *info, double score, void *key)
+{
+ int idx, i, j;
+
+ for (i = 0; i<info->no_rec; i++)
+ {
+ idx = info->sort_idx[i];
+ if (score <= info->score_buf[idx])
+ break;
+ }
+ if (info->no_rec < info->max_rec)
+ { /* there is room for this entry */
+ for (j = info->no_rec; j > i; --j)
+ info->sort_idx[j] = info->sort_idx[j-1];
+ idx = info->sort_idx[j] = info->no_rec;
+ ++(info->no_rec);
+ }
+ else if (i == 0)
+ return; /* score too low */
+ else
+ {
+ idx = info->sort_idx[0]; /* remove this entry */
+
+ --i;
+ for (j = 0; j < i; ++j) /* make room */
+ info->sort_idx[j] = info->sort_idx[j+1];
+ info->sort_idx[j] = idx; /* allocate sort entry */
+ }
+ memcpy (info->key_buf + idx*info->key_size, key, info->key_size);
+ info->score_buf[idx] = score;
+}
+
+
+static struct rset_rel_info *qsort_info;
+
+static int qcomp (const void *p1, const void *p2)
+{
+ int i1 = *(int*) p1;
+ int i2 = *(int*) p2;
+
+ return qsort_info->cmp (qsort_info->key_buf + i1*qsort_info->key_size,
+ qsort_info->key_buf + i2*qsort_info->key_size);
+}
+
+#define SCORE_SHOW 0.0 /* base score for showing up */
+#define SCORE_COOC 0.3 /* component dependent on co-oc */
+#define SCORE_DYN (1-(SCORE_SHOW+SCORE_COOC)) /* dynamic component of score */
+