2 * $Id: reclists.c,v 1.1 2006-11-24 20:29:07 quinn Exp $
7 #include <yaz/yaz-util.h>
14 struct record *record;
15 struct reclist_bucket *next;
20 struct reclist_bucket **hashtable;
24 struct record **flatlist;
32 struct record *reclist_read_record(struct reclist *l)
34 if (l->pointer < l->num_records)
35 return l->flatlist[l->pointer++];
40 void reclist_rewind(struct reclist *l)
45 // Jenkins one-at-a-time hash (from wikipedia)
46 static unsigned int hash(const unsigned char *key)
48 unsigned int hash = 0;
62 struct reclist *reclist_create(NMEM nmem, int numrecs)
68 while (hashsize < numrecs)
70 res = nmem_malloc(nmem, sizeof(struct reclist));
71 res->hashtable = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
72 bzero(res->hashtable, hashsize * sizeof(struct reclist_bucket*));
73 res->hashtable_size = hashsize;
75 res->hashmask = hashsize - 1; // Creates a bitmask
78 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record*));
79 res->flatlist_size = numrecs;
84 void reclist_insert(struct reclist *l, struct record *record)
87 struct reclist_bucket **p;
89 bucket = hash(record->merge_key) & l->hashmask;
90 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
92 // We found a matching record. Merge them
93 if (!strcmp(record->merge_key, (*p)->record->merge_key))
95 struct record *existing = (*p)->record;
96 yaz_log(YLOG_LOG, "Found a matching record: %s", record->merge_key);
97 record->next_cluster = existing->next_cluster;
98 existing->next_cluster = record;
102 if (!*p) // We made it to the end of the bucket without finding match
104 yaz_log(YLOG_DEBUG, "Added a new record: %s", record->merge_key);
105 struct reclist_bucket *new = nmem_malloc(l->nmem,
106 sizeof(struct reclist_bucket));
107 new->record = record;
108 record->next_cluster = 0;
111 l->flatlist[l->num_records++] = record;
119 * indent-tabs-mode: nil
121 * vim: shiftwidth=4 tabstop=8 expandtab