summaryrefslogtreecommitdiffstats
path: root/juk/stringhash.h
blob: 75992eec3df2c5369940a898f4e88251b7d2054b (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/***************************************************************************
    begin                : Sun Feb 2 2003
    copyright            : (C) 2003 - 2004 by Scott Wheeler
    email                : wheeler@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef STRINGHASH_H
#define STRINGHASH_H

#include <qptrvector.h>

#include "filehandle.h"

/**
 * A simple hash representing an (un-mapped) set of data.
 */

template <class T> class Hash
{
    friend class Iterator;
public:

    Hash() : m_table(m_tableSize) {}
    ~Hash();

    /**
     * To combine two operations into one (that takes the same amount as each
     * independantly) this inserts an item and returns true if the item was
     * already in the set or false if it did not.
     */
    bool insert(T value);

    /**
     * Returns true if the set contains the item \a value.
     */
    bool contains(T value) const;

    /**
     * Removes an item.  Returns true if the item was present and false if not.
     */
    bool remove(T value);

    QValueList<T> values() const;

    int hash(T key) const;

    static inline int tableSize() { return m_tableSize; }

protected:

    struct Node
    {
        Node(T value) : key(value), next(0) {}
        T key;
        Node *next;
    };

public:

    class Iterator
    {
    friend class Hash<T>;
    public:
        Iterator(const Hash<T> &hash) : m_hash(hash), m_index(0), m_node(hash.m_table[0]) {}
        const T &operator*() const { return m_node->key; }
        T &operator*() { return m_node->key; }

        bool operator==(const Iterator &it) const { return m_index == it.m_index && m_node == it.m_node; }
        bool operator!=(const Iterator &it) const { return !(it == *this); }

        T &operator++();

    private:
        const Hash<T> &m_hash;
        int m_index;
        Node *m_node;
    };

    Iterator begin() const
    {
        Iterator it(*this);
        while(!it.m_node && it.m_index < m_tableSize - 1) {
            it.m_index++;
            it.m_node = m_table[it.m_index];
        }

        return it;
    }

    Iterator end() const
    {
        Iterator it(*this);
        it.m_node = 0;
        it.m_index = m_tableSize - 1;
        return it;
    }

protected:

    void deleteNode(Node *n);
    
    QPtrVector<Node> m_table;
    static const int m_tableSize = 5003;
};

////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

inline char hashStringAccess(const QString &in, int index)
{
    return in.unicode()[index].cell();
}

inline char hashStringAccess(const QCString &in, int index)
{
    return in[index];
}

// Based on QGDict's hash functions, Copyright (C) 1992-2000 Trolltech AS

template <typename StringType>
inline int hashString(const StringType &s)
{
    uint h = 0;
    uint g;
    for(uint i = 0; i < s.length(); i++)
    {
        h = (h << 4) + hashStringAccess(s, i);
        if((g = h & 0xf0000000))
            h ^= g >> 24;
        h &= ~g;
    }
    int index = h;
    if(index < 0)
        index = -index;
    return index;        
}

////////////////////////////////////////////////////////////////////////////////
// specializations
////////////////////////////////////////////////////////////////////////////////

// StringHash

template<> inline int Hash<QString>::hash(QString key) const
{
    return hashString(key) % tableSize();
}
typedef Hash<QString> StringHash;

// PtrHash

template<> inline int Hash<void *>::hash(void *key) const
{
    return long(key) % tableSize();
}
typedef Hash<void *> PtrHash;

// FileHandleHash

template<> inline int Hash<FileHandle>::hash(FileHandle key) const
{
    return hashString(key.absFilePath()) % tableSize();
}

class FileHandleHash : public Hash<FileHandle>
{
public:
    FileHandleHash() : Hash<FileHandle>() {}

    FileHandle value(const QString &key) const
    {
        int h = hashString(key) % tableSize();
        Node *i = m_table[h];

        while(i && i->key.absFilePath() != key)
            i = i->next;

        return i ? i->key : FileHandle::null();
    }
};


////////////////////////////////////////////////////////////////////////////////
// template method implementations
////////////////////////////////////////////////////////////////////////////////

template <class T>
Hash<T>::~Hash()
{
    for(int i = 0; i < m_tableSize; i++)
        deleteNode(m_table[i]);
}

template <class T>
bool Hash<T>::insert(T value)
{
    int h = hash(value);
    Node *i = m_table[h];
    Node *j = 0;
    
    while(i) {
        if(i->key == value)
            return true;
        else {
            j = i;
            i = i->next;
        }
    }

    if(j)
        j->next = new Node(value);
    else
        m_table.insert(h, new Node(value));

    return false;
}

template <class T>
bool Hash<T>::contains(T value) const
{
    int h = hash(value);
    Node *i = m_table[h];

    while(i && i->key != value)
        i = i->next;

    return bool(i);
}

template <class T>
bool Hash<T>::remove(T value)
{
    int h = hash(value);
    Node *previous = 0;
    Node *i = m_table[h];

    while(i && i->key != value) {
        previous = i;
        i = i->next;
    }

    if(!i)
        return false;

    if(previous)
        previous->next = i->next;
    else {
        if(i->next)
            m_table.insert(h, i->next);
        else
            m_table.remove(h);
    }

    delete i;

    return true;
}

template <class T>
QValueList<T> Hash<T>::values() const
{
    QValueList<T> l;

    Node *n;

    for(int i = 0; i < tableSize(); i++) {
        n = m_table[i];
        while(n) {
            l.append(n->key);
            n = n->next;
        }
    }

    return l;
}

template <class T>
void Hash<T>::deleteNode(Node *n)
{
    if(n) {
        deleteNode(n->next);
        delete n;
    }
}

template <class T>
T &Hash<T>::Iterator::operator++()
{
    if(m_node)
        m_node = m_node->next;

    while(!m_node && m_index < m_tableSize - 1) {
        m_index++;
        m_node = m_hash.m_table[m_index];
    }

    return m_node->key;
}


#endif