2 * $Id: reclists.c,v 1.3 2007-01-08 12:43:41 adam Exp $
11 #include <yaz/yaz-util.h>
18 struct record *record;
19 struct reclist_bucket *next;
22 struct record *reclist_read_record(struct reclist *l)
24 if (l->pointer < l->num_records)
25 return l->flatlist[l->pointer++];
30 void reclist_rewind(struct reclist *l)
35 // Jenkins one-at-a-time hash (from wikipedia)
36 static unsigned int hash(const unsigned char *key)
38 unsigned int hash = 0;
52 struct reclist *reclist_create(NMEM nmem, int numrecs)
58 while (hashsize < numrecs)
60 res = nmem_malloc(nmem, sizeof(struct reclist));
61 res->hashtable = nmem_malloc(nmem, hashsize * sizeof(struct reclist_bucket*));
62 bzero(res->hashtable, hashsize * sizeof(struct reclist_bucket*));
63 res->hashtable_size = hashsize;
65 res->hashmask = hashsize - 1; // Creates a bitmask
68 res->flatlist = nmem_malloc(nmem, numrecs * sizeof(struct record*));
69 res->flatlist_size = numrecs;
74 struct record *reclist_insert(struct reclist *l, struct record *record)
77 struct reclist_bucket **p;
78 struct record *head = 0;
80 bucket = hash((unsigned char*) record->merge_key) & l->hashmask;
81 for (p = &l->hashtable[bucket]; *p; p = &(*p)->next)
83 // We found a matching record. Merge them
84 if (!strcmp(record->merge_key, (*p)->record->merge_key))
86 struct record *existing = (*p)->record;
87 record->next_cluster = existing->next_cluster;
88 existing->next_cluster = record;
93 if (!head && l->num_records < l->flatlist_size)
95 struct reclist_bucket *new =
96 nmem_malloc(l->nmem, sizeof(struct reclist_bucket));
100 new->record = record;
101 record->next_cluster = 0;
104 assert(l->num_records < l->flatlist_size);
105 l->flatlist[l->num_records++] = record;
115 * indent-tabs-mode: nil
117 * vim: shiftwidth=4 tabstop=8 expandtab