1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Quick & light hash implementation for tab completion purposes
18
* by Andi Gutmans <andi@zend.com>
19
* and Zeev Suraski <zeev@zend.com>
20
* Small portability changes by Monty.
23
#include "client_priv.h"
24
#include <mystrings/m_string.h>
25
#include "completion_hash.h"
27
uint hashpjw(const char *arKey, uint nKeyLength)
31
for (i = 0; i < nKeyLength; i++) {
32
h = (h << 4) + arKey[i];
33
if ((g = (h & 0xF0000000))) {
41
int completion_hash_init(HashTable *ht, uint nSize)
43
ht->arBuckets = (Bucket **) malloc(nSize* sizeof(Bucket *));
44
memset(ht->arBuckets, 0, nSize* sizeof(Bucket *));
51
init_alloc_root(&ht->mem_root, 8192, 0);
52
ht->pHashFunction = hashpjw;
53
ht->nTableSize = nSize;
59
int completion_hash_update(HashTable *ht, char *arKey, uint nKeyLength,
66
h = ht->pHashFunction(arKey, nKeyLength);
67
nIndex = h % ht->nTableSize;
69
if (nKeyLength <= 0) {
72
p = ht->arBuckets[nIndex];
75
if ((p->h == h) && (p->nKeyLength == nKeyLength)) {
76
if (!memcmp(p->arKey, arKey, nKeyLength)) {
79
if (!(n = (entry *) alloc_root(&ht->mem_root,sizeof(entry))))
92
if (!(p = (Bucket *) alloc_root(&ht->mem_root, sizeof(Bucket))))
96
p->nKeyLength = nKeyLength;
99
if (!(p->pData = (entry*) alloc_root(&ht->mem_root, sizeof(entry))))
106
p->pNext = ht->arBuckets[nIndex];
107
ht->arBuckets[nIndex] = p;
112
static Bucket *completion_hash_find(HashTable *ht, const char *arKey,
118
h = ht->pHashFunction(arKey, nKeyLength);
119
nIndex = h % ht->nTableSize;
121
p = ht->arBuckets[nIndex];
124
if ((p->h == h) && (p->nKeyLength == nKeyLength)) {
125
if (!memcmp(p->arKey, arKey, nKeyLength)) {
135
int completion_hash_exists(HashTable *ht, char *arKey, uint nKeyLength)
140
h = ht->pHashFunction(arKey, nKeyLength);
141
nIndex = h % ht->nTableSize;
143
p = ht->arBuckets[nIndex];
146
if ((p->h == h) && (p->nKeyLength == nKeyLength))
148
if (!strcmp(p->arKey, arKey)) {
157
Bucket *find_all_matches(HashTable *ht, const char *str, uint length,
162
b = completion_hash_find(ht,str,length);
167
*res_length = length;
172
Bucket *find_longest_match(HashTable *ht, char *str, uint length,
180
b = completion_hash_find(ht,str,length);
191
while (s[lm]!=0 && (b=completion_hash_find(ht,s,lm+1))) {
192
if (b->count<count) {
204
void completion_hash_clean(HashTable *ht)
206
free_root(&ht->mem_root,MYF(0));
207
memset(ht->arBuckets, 0, ht->nTableSize*sizeof(Bucket *));
211
void completion_hash_free(HashTable *ht)
213
completion_hash_clean(ht);
218
void add_word(HashTable *ht,char *str)
222
for (i=1; *pos; i++, pos++)
223
completion_hash_update(ht, str, i, str);