1
by brian
clean slate |
1 |
/************************************************************************
|
2 |
The hash table with external chains
|
|
3 |
||
4 |
(c) 1994-1997 Innobase Oy
|
|
5 |
||
6 |
Created 8/18/1994 Heikki Tuuri
|
|
7 |
*************************************************************************/
|
|
8 |
||
9 |
#include "ut0rnd.h" |
|
10 |
#include "mem0mem.h" |
|
11 |
||
12 |
/***************************************************************
|
|
13 |
Deletes a hash node. */
|
|
14 |
||
15 |
void
|
|
16 |
ha_delete_hash_node( |
|
17 |
/*================*/
|
|
18 |
hash_table_t* table, /* in: hash table */ |
|
19 |
ha_node_t* del_node); /* in: node to be deleted */ |
|
20 |
||
21 |
/**********************************************************************
|
|
22 |
Gets a hash node data. */
|
|
23 |
UNIV_INLINE
|
|
24 |
void* |
|
25 |
ha_node_get_data( |
|
26 |
/*=============*/
|
|
27 |
/* out: pointer to the data */
|
|
28 |
ha_node_t* node) /* in: hash chain node */ |
|
29 |
{
|
|
30 |
return(node->data); |
|
31 |
}
|
|
32 |
||
33 |
/**********************************************************************
|
|
34 |
Sets hash node data. */
|
|
35 |
UNIV_INLINE
|
|
36 |
void
|
|
37 |
ha_node_set_data( |
|
38 |
/*=============*/
|
|
39 |
ha_node_t* node, /* in: hash chain node */ |
|
40 |
void* data) /* in: pointer to the data */ |
|
41 |
{
|
|
42 |
node->data = data; |
|
43 |
}
|
|
44 |
||
45 |
/**********************************************************************
|
|
46 |
Gets the next node in a hash chain. */
|
|
47 |
UNIV_INLINE
|
|
48 |
ha_node_t* |
|
49 |
ha_chain_get_next( |
|
50 |
/*==============*/
|
|
51 |
/* out: next node, NULL if none */
|
|
52 |
ha_node_t* node) /* in: hash chain node */ |
|
53 |
{
|
|
54 |
return(node->next); |
|
55 |
}
|
|
56 |
||
57 |
/**********************************************************************
|
|
58 |
Gets the first node in a hash chain. */
|
|
59 |
UNIV_INLINE
|
|
60 |
ha_node_t* |
|
61 |
ha_chain_get_first( |
|
62 |
/*===============*/
|
|
63 |
/* out: first node, NULL if none */
|
|
64 |
hash_table_t* table, /* in: hash table */ |
|
65 |
ulint fold) /* in: fold value determining the chain */ |
|
66 |
{
|
|
67 |
return(hash_get_nth_cell(table, hash_calc_hash(fold, table))->node); |
|
68 |
}
|
|
69 |
||
70 |
/*****************************************************************
|
|
71 |
Looks for an element in a hash table. */
|
|
72 |
UNIV_INLINE
|
|
73 |
ha_node_t* |
|
74 |
ha_search( |
|
75 |
/*======*/
|
|
76 |
/* out: pointer to the first hash table node
|
|
77 |
in chain having the fold number, NULL if not
|
|
78 |
found */
|
|
79 |
hash_table_t* table, /* in: hash table */ |
|
80 |
ulint fold) /* in: folded value of the searched data */ |
|
81 |
{
|
|
82 |
ha_node_t* node; |
|
83 |
||
84 |
ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold))); |
|
85 |
||
86 |
node = ha_chain_get_first(table, fold); |
|
87 |
||
88 |
while (node) { |
|
89 |
if (node->fold == fold) { |
|
90 |
||
91 |
return(node); |
|
92 |
}
|
|
93 |
||
94 |
node = ha_chain_get_next(node); |
|
95 |
}
|
|
96 |
||
97 |
return(NULL); |
|
98 |
}
|
|
99 |
||
100 |
/*****************************************************************
|
|
101 |
Looks for an element in a hash table. */
|
|
102 |
UNIV_INLINE
|
|
103 |
void* |
|
104 |
ha_search_and_get_data( |
|
105 |
/*===================*/
|
|
106 |
/* out: pointer to the data of the first hash
|
|
107 |
table node in chain having the fold number,
|
|
108 |
NULL if not found */
|
|
109 |
hash_table_t* table, /* in: hash table */ |
|
110 |
ulint fold) /* in: folded value of the searched data */ |
|
111 |
{
|
|
112 |
ha_node_t* node; |
|
113 |
||
114 |
ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold))); |
|
115 |
||
116 |
node = ha_chain_get_first(table, fold); |
|
117 |
||
118 |
while (node) { |
|
119 |
if (node->fold == fold) { |
|
120 |
||
121 |
return(node->data); |
|
122 |
}
|
|
123 |
||
124 |
node = ha_chain_get_next(node); |
|
125 |
}
|
|
126 |
||
127 |
return(NULL); |
|
128 |
}
|
|
129 |
||
130 |
/*************************************************************
|
|
131 |
Looks for an element when we know the pointer to the data. */
|
|
132 |
UNIV_INLINE
|
|
133 |
ha_node_t* |
|
134 |
ha_search_with_data( |
|
135 |
/*================*/
|
|
136 |
/* out: pointer to the hash table node, NULL
|
|
137 |
if not found in the table */
|
|
138 |
hash_table_t* table, /* in: hash table */ |
|
139 |
ulint fold, /* in: folded value of the searched data */ |
|
140 |
void* data) /* in: pointer to the data */ |
|
141 |
{
|
|
142 |
ha_node_t* node; |
|
143 |
||
144 |
ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold))); |
|
145 |
||
146 |
node = ha_chain_get_first(table, fold); |
|
147 |
||
148 |
while (node) { |
|
149 |
if (node->data == data) { |
|
150 |
||
151 |
return(node); |
|
152 |
}
|
|
153 |
||
154 |
node = ha_chain_get_next(node); |
|
155 |
}
|
|
156 |
||
157 |
return(NULL); |
|
158 |
}
|
|
159 |
||
160 |
/*************************************************************
|
|
161 |
Looks for an element when we know the pointer to the data, and deletes
|
|
162 |
it from the hash table, if found. */
|
|
163 |
UNIV_INLINE
|
|
164 |
ibool
|
|
165 |
ha_search_and_delete_if_found( |
|
166 |
/*==========================*/
|
|
167 |
/* out: TRUE if found */
|
|
168 |
hash_table_t* table, /* in: hash table */ |
|
169 |
ulint fold, /* in: folded value of the searched data */ |
|
170 |
void* data) /* in: pointer to the data */ |
|
171 |
{
|
|
172 |
ha_node_t* node; |
|
173 |
||
174 |
ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold))); |
|
175 |
||
176 |
node = ha_search_with_data(table, fold, data); |
|
177 |
||
178 |
if (node) { |
|
179 |
ha_delete_hash_node(table, node); |
|
180 |
||
181 |
return(TRUE); |
|
182 |
}
|
|
183 |
||
184 |
return(FALSE); |
|
185 |
}
|