2 * $Id: termlists.c,v 1.1 2006-11-24 20:29:07 quinn Exp $
7 #include <yaz/yaz-util.h>
12 // As terms are found in incoming records, they are added to (or updated in) a
13 // Hash table. When term records are updated, a frequency value is updated. At
14 // the same time, a highscore is maintained for the most frequent terms.
16 struct termlist_bucket
18 struct termlist_score term;
19 struct termlist_bucket *next;
24 struct termlist_bucket **hashtable;
28 struct termlist_score **highscore;
37 // Jenkins one-at-a-time hash (from wikipedia)
38 static unsigned int hash(const unsigned char *key)
40 unsigned int hash = 0;
54 struct termlist *termlist_create(NMEM nmem, int numterms, int highscore_size)
60 // Calculate a hash size smallest power of 2 larger than 50% of expected numterms
61 halfnumterms = numterms >> 1;
64 while (hashsize < halfnumterms)
66 res = nmem_malloc(nmem, sizeof(struct termlist));
67 res->hashtable = nmem_malloc(nmem, hashsize * sizeof(struct termlist_bucket*));
68 bzero(res->hashtable, hashsize * sizeof(struct termlist_bucket*));
69 res->hashtable_size = hashsize;
71 res->hashmask = hashsize - 1; // Creates a bitmask
73 res->highscore = nmem_malloc(nmem, highscore_size * sizeof(struct termlist_score *));
74 res->highscore_size = highscore_size;
75 res->highscore_num = 0;
76 res->highscore_min = 0;
81 static void update_highscore(struct termlist *tl, struct termlist_score *t)
87 if (t->frequency < tl->highscore_min)
91 for (i = 0; i < tl->highscore_num; i++)
93 if (tl->highscore[i]->frequency < tl->highscore[smallest]->frequency)
95 if (tl->highscore[i] == t)
98 if (tl->highscore_num)
99 tl->highscore_min = tl->highscore[smallest]->frequency;
102 if (tl->highscore_num < tl->highscore_size)
104 tl->highscore[tl->highscore_num++] = t;
105 if (t->frequency < tl->highscore_min)
106 tl->highscore_min = t->frequency;
110 if (t->frequency > tl->highscore[smallest]->frequency)
112 tl->highscore[smallest] = t;
117 void termlist_insert(struct termlist *tl, const char *term)
120 struct termlist_bucket **p;
122 bucket = hash(term) & tl->hashmask;
123 for (p = &tl->hashtable[bucket]; *p; p = &(*p)->next)
125 if (!strcmp(term, (*p)->term.term))
127 yaz_log(YLOG_LOG, "Found a matching term: %s", term);
128 (*p)->term.frequency++;
129 update_highscore(tl, &((*p)->term));
133 if (!*p) // We made it to the end of the bucket without finding match
135 yaz_log(YLOG_DEBUG, "Added a new term: %s", term);
136 struct termlist_bucket *new = nmem_malloc(tl->nmem,
137 sizeof(struct termlist_bucket));
138 new->term.term = nmem_strdup(tl->nmem, term);
139 new->term.frequency = 1;
142 update_highscore(tl, &new->term);
146 static int compare(const void *s1, const void *s2)
148 struct termlist_score **p1 = (struct termlist_score**) s1, **p2 = (struct termlist_score **) s2;
149 return (*p2)->frequency - (*p1)->frequency;
152 struct termlist_score **termlist_highscore(struct termlist *tl, int *len)
154 qsort(tl->highscore, tl->highscore_num, sizeof(struct termlist_score*), compare);
155 *len = tl->highscore_num;
156 return tl->highscore;
162 * indent-tabs-mode: nil
164 * vim: shiftwidth=4 tabstop=8 expandtab