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