2 * $Id: reclists.c,v 1.4 2006-12-08 21:40:58 quinn Exp $
7 #include <yaz/yaz-util.h>
14 struct record *record;
15 struct reclist_bucket *next;
18 struct record *reclist_read_record(struct reclist *l)
20 if (l->pointer < l->num_records)
21 return l->flatlist[l->pointer++];
26 void reclist_rewind(struct reclist *l)
31 // Jenkins one-at-a-time hash (from wikipedia)
32 static unsigned int hash(const unsigned char *key)
34 unsigned int hash = 0;
48 struct reclist *reclist_create(NMEM nmem, int numrecs)
54 while (hashsize < numrecs)
56 res = nmem_malloc(nmem, sizeof(struct reclist));
57 res->hashtable = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
58 bzero(res->hashtable, hashsize * sizeof(struct reclist_bucket*));
59 res->hashtable_size = hashsize;
61 res->hashmask = hashsize - 1; // Creates a bitmask
64 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record*));
65 res->flatlist_size = numrecs;
70 struct record *reclist_insert(struct reclist *l, struct record *record)
73 struct reclist_bucket **p;
76 bucket = hash((unsigned char*) record->merge_key) & l->hashmask;
77 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
79 // We found a matching record. Merge them
80 if (!strcmp(record->merge_key, (*p)->record->merge_key))
82 struct record *existing = (*p)->record;
83 record->next_cluster = existing->next_cluster;
84 existing->next_cluster = record;
89 if (!*p) // We made it to the end of the bucket without finding match
91 struct reclist_bucket *new = nmem_malloc(l->nmem,
92 sizeof(struct reclist_bucket));
94 record->next_cluster = 0;
97 l->flatlist[l->num_records++] = record;
107 * indent-tabs-mode: nil
109 * vim: shiftwidth=4 tabstop=8 expandtab