2 * $Id: termlists.c,v 1.3 2007-01-08 18:32:35 quinn Exp $
7 #include <yaz/yaz-util.h>
13 #include "termlists.h"
16 // As terms are found in incoming records, they are added to (or updated in) a
17 // Hash table. When term records are updated, a frequency value is updated. At
18 // the same time, a highscore is maintained for the most frequent terms.
20 struct termlist_bucket
22 struct termlist_score term;
23 struct termlist_bucket *next;
28 struct termlist_bucket **hashtable;
32 struct termlist_score **highscore;
41 // Jenkins one-at-a-time hash (from wikipedia)
42 static unsigned int hash(const unsigned char *key)
44 unsigned int hash = 0;
58 struct termlist *termlist_create(NMEM nmem, int numterms, int highscore_size)
64 // Calculate a hash size smallest power of 2 larger than 50% of expected numterms
65 halfnumterms = numterms >> 1;
68 while (hashsize < halfnumterms)
70 res = nmem_malloc(nmem, sizeof(struct termlist));
71 res->hashtable = nmem_malloc(nmem, hashsize * sizeof(struct termlist_bucket*));
72 bzero(res->hashtable, hashsize * sizeof(struct termlist_bucket*));
73 res->hashtable_size = hashsize;
75 res->hashmask = hashsize - 1; // Creates a bitmask
77 res->highscore = nmem_malloc(nmem, highscore_size * sizeof(struct termlist_score *));
78 res->highscore_size = highscore_size;
79 res->highscore_num = 0;
80 res->highscore_min = 0;
85 static void update_highscore(struct termlist *tl, struct termlist_score *t)
91 if (tl->highscore_num > tl->highscore_size && t->frequency < tl->highscore_min)
95 for (i = 0; i < tl->highscore_num; i++)
97 if (tl->highscore[i]->frequency < tl->highscore[smallest]->frequency)
99 if (tl->highscore[i] == t)
102 if (tl->highscore_num)
103 tl->highscore_min = tl->highscore[smallest]->frequency;
104 if (t->frequency < tl->highscore_min)
105 tl->highscore_min = t->frequency;
108 if (tl->highscore_num < tl->highscore_size)
110 tl->highscore[tl->highscore_num++] = t;
111 if (t->frequency < tl->highscore_min)
112 tl->highscore_min = t->frequency;
116 if (t->frequency > tl->highscore[smallest]->frequency)
118 tl->highscore[smallest] = t;
123 void termlist_insert(struct termlist *tl, const char *term)
126 struct termlist_bucket **p;
128 bucket = hash((unsigned char *)term) & tl->hashmask;
129 for (p = &tl->hashtable[bucket]; *p; p = &(*p)->next)
131 if (!strcmp(term, (*p)->term.term))
133 (*p)->term.frequency++;
134 update_highscore(tl, &((*p)->term));
138 if (!*p) // We made it to the end of the bucket without finding match
140 struct termlist_bucket *new = nmem_malloc(tl->nmem,
141 sizeof(struct termlist_bucket));
142 new->term.term = nmem_strdup(tl->nmem, term);
143 new->term.frequency = 1;
146 update_highscore(tl, &new->term);
150 static int compare(const void *s1, const void *s2)
152 struct termlist_score **p1 = (struct termlist_score**) s1, **p2 = (struct termlist_score **) s2;
153 return (*p2)->frequency - (*p1)->frequency;
156 struct termlist_score **termlist_highscore(struct termlist *tl, int *len)
158 qsort(tl->highscore, tl->highscore_num, sizeof(struct termlist_score*), compare);
159 *len = tl->highscore_num;
160 return tl->highscore;
166 * indent-tabs-mode: nil
168 * vim: shiftwidth=4 tabstop=8 expandtab