1da2e3ebdSchin /*********************************************************************** 2da2e3ebdSchin * * 3da2e3ebdSchin * This software is part of the ast package * 4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property * 5da2e3ebdSchin * and is licensed under the * 6da2e3ebdSchin * Common Public License, Version 1.0 * 77c2fbfb3SApril Chin * by AT&T Intellectual Property * 8da2e3ebdSchin * * 9da2e3ebdSchin * A copy of the License is available at * 10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt * 11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12da2e3ebdSchin * * 13da2e3ebdSchin * Information and Software Systems Research * 14da2e3ebdSchin * AT&T Research * 15da2e3ebdSchin * Florham Park NJ * 16da2e3ebdSchin * * 17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> * 18da2e3ebdSchin * David Korn <dgk@research.att.com> * 19da2e3ebdSchin * Phong Vo <kpv@research.att.com> * 20da2e3ebdSchin * * 21da2e3ebdSchin ***********************************************************************/ 22da2e3ebdSchin #pragma prototyped 23da2e3ebdSchin /* 24da2e3ebdSchin * Glenn Fowler 25da2e3ebdSchin * AT&T Research 26da2e3ebdSchin * 27da2e3ebdSchin * hash table library 28da2e3ebdSchin */ 29da2e3ebdSchin 30da2e3ebdSchin #include "hashlib.h" 31da2e3ebdSchin 32da2e3ebdSchin /* 33da2e3ebdSchin * hash table sequential scan 34da2e3ebdSchin * 35da2e3ebdSchin * Hash_position_t* pos; 36da2e3ebdSchin * Hash_bucket_t* b; 37da2e3ebdSchin * pos = hashscan(tab, flags); 38da2e3ebdSchin * while (b = hashnext(&pos)) ...; 39da2e3ebdSchin * hashdone(pos); 40da2e3ebdSchin */ 41da2e3ebdSchin 42da2e3ebdSchin /* 43da2e3ebdSchin * return pos for scan on table 44da2e3ebdSchin */ 45da2e3ebdSchin 46da2e3ebdSchin Hash_position_t* hashscan(register Hash_table_t * tab,register int flags)47da2e3ebdSchinhashscan(register Hash_table_t* tab, register int flags) 48da2e3ebdSchin { 49da2e3ebdSchin register Hash_position_t* pos; 50da2e3ebdSchin 51da2e3ebdSchin static Hash_bucket_t empty; 52da2e3ebdSchin 53da2e3ebdSchin if (!(pos = newof(0, Hash_position_t, 1, 0))) return(0); 54da2e3ebdSchin pos->tab = tab->root->last.table = tab; 55da2e3ebdSchin pos->bucket = ∅ 56da2e3ebdSchin pos->slot = tab->table - 1; 57da2e3ebdSchin pos->limit = tab->table + tab->size; 58da2e3ebdSchin if (tab->scope && !(flags & HASH_NOSCOPE)) 59da2e3ebdSchin { 60da2e3ebdSchin pos->flags = HASH_SCOPE; 61da2e3ebdSchin do 62da2e3ebdSchin { 63da2e3ebdSchin register Hash_bucket_t* b; 64da2e3ebdSchin 65da2e3ebdSchin if (tab->frozen) 66da2e3ebdSchin { 67da2e3ebdSchin register Hash_bucket_t** sp = tab->table; 68da2e3ebdSchin register Hash_bucket_t** sx = tab->table + tab->size; 69da2e3ebdSchin 70da2e3ebdSchin while (sp < sx) 71da2e3ebdSchin for (b = *sp++; b; b = b->next) 72da2e3ebdSchin b->hash &= ~HASH_HIDDEN; 73da2e3ebdSchin } 74da2e3ebdSchin } while (tab = tab->scope); 75da2e3ebdSchin tab = pos->tab; 76da2e3ebdSchin } 77da2e3ebdSchin else pos->flags = 0; 78da2e3ebdSchin tab->frozen++; 79da2e3ebdSchin return(pos); 80da2e3ebdSchin } 81da2e3ebdSchin 82da2e3ebdSchin /* 83da2e3ebdSchin * return next scan element 84da2e3ebdSchin */ 85da2e3ebdSchin 86da2e3ebdSchin Hash_bucket_t* hashnext(register Hash_position_t * pos)87da2e3ebdSchinhashnext(register Hash_position_t* pos) 88da2e3ebdSchin { 89da2e3ebdSchin register Hash_bucket_t* b; 90da2e3ebdSchin 91da2e3ebdSchin if (!pos) return(pos->tab->root->last.bucket = 0); 92da2e3ebdSchin b = pos->bucket; 93da2e3ebdSchin for (;;) 94da2e3ebdSchin { 95da2e3ebdSchin if (!(b = b->next)) 96da2e3ebdSchin { 97da2e3ebdSchin do 98da2e3ebdSchin { 99da2e3ebdSchin if (++pos->slot >= pos->limit) 100da2e3ebdSchin { 101da2e3ebdSchin pos->tab->frozen--; 102da2e3ebdSchin if (!pos->flags || !pos->tab->scope) return(0); 103da2e3ebdSchin pos->tab = pos->tab->scope; 104da2e3ebdSchin pos->tab->root->last.table = pos->tab; 105da2e3ebdSchin pos->limit = (pos->slot = pos->tab->table) + pos->tab->size; 106da2e3ebdSchin pos->tab->frozen++; 107da2e3ebdSchin } 108da2e3ebdSchin } while (!(b = *pos->slot)); 109da2e3ebdSchin } 110da2e3ebdSchin if (!(b->hash & HASH_DELETED) && (!(pos->tab->flags & HASH_VALUE) || b->value) && (!pos->flags || !(b->hash & (HASH_HIDDEN|HASH_HIDES)))) break; 111da2e3ebdSchin if (b->hash & HASH_HIDES) 112da2e3ebdSchin { 113da2e3ebdSchin register Hash_bucket_t* h = (Hash_bucket_t*)b->name; 114da2e3ebdSchin 115da2e3ebdSchin if (!(h->hash & HASH_HIDDEN)) 116da2e3ebdSchin { 117da2e3ebdSchin h->hash |= HASH_HIDDEN; 118da2e3ebdSchin if (!(b->hash & HASH_DELETED)) break; 119da2e3ebdSchin } 120da2e3ebdSchin } 121da2e3ebdSchin else b->hash &= ~HASH_HIDDEN; 122da2e3ebdSchin } 123da2e3ebdSchin return(pos->tab->root->last.bucket = pos->bucket = b); 124da2e3ebdSchin } 125da2e3ebdSchin 126da2e3ebdSchin /* 127da2e3ebdSchin * terminate scan 128da2e3ebdSchin */ 129da2e3ebdSchin 130da2e3ebdSchin void hashdone(register Hash_position_t * pos)131da2e3ebdSchinhashdone(register Hash_position_t* pos) 132da2e3ebdSchin { 133da2e3ebdSchin if (pos) 134da2e3ebdSchin { 135da2e3ebdSchin if (pos->tab->frozen) 136da2e3ebdSchin pos->tab->frozen--; 137da2e3ebdSchin free(pos); 138da2e3ebdSchin } 139da2e3ebdSchin } 140