stringhash.h 6.64 KB
Newer Older
1 2
/***************************************************************************
    begin                : Sun Feb 2 2003
3
    copyright            : (C) 2003 - 2004 by Scott Wheeler
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    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>
20 21

#include "filehandle.h"
22

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

27
template <class T> class Hash
28
{
29
    friend class Iterator;
30
public:
31

32 33 34
    Hash() : m_table(m_tableSize) {}
    ~Hash();

35 36 37 38 39
    /**
     * 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.
     */
40
    bool insert(T value);
41 42 43 44

    /**
     * Returns true if the set contains the item \a value.
     */
45
    bool contains(T value) const;
46 47 48 49

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

52 53
    QValueList<T> values() const;

54
    int hash(T key) const;
55

56
    static inline int tableSize() { return m_tableSize; }
57

58
protected:
59 60 61 62 63 64 65 66

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

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
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:

109
    void deleteNode(Node *n);
110
    
111
    QPtrVector<Node> m_table;
112 113 114
    static const int m_tableSize = 5003;
};

115 116 117 118 119
////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

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

124
inline char hashStringAccess(const QCString &in, int index)
125 126 127 128
{
    return in[index];
}

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

template <typename StringType>
inline int hashString(const StringType &s)
133 134 135
{
    uint h = 0;
    uint g;
136
    for(uint i = 0; i < s.length(); i++)
137
    {
138 139
        h = (h << 4) + hashStringAccess(s, i);
        if((g = h & 0xf0000000))
140 141 142 143
            h ^= g >> 24;
        h &= ~g;
    }
    int index = h;
144
    if(index < 0)
145 146 147 148
        index = -index;
    return index;        
}

149 150 151
////////////////////////////////////////////////////////////////////////////////
// specializations
////////////////////////////////////////////////////////////////////////////////
152

153 154 155 156 157 158
// StringHash

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

161 162 163 164 165 166
// PtrHash

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

169 170 171 172 173 174 175 176 177 178 179 180
// FileHandleHash

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

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

Dirk Mueller's avatar
Dirk Mueller committed
181
    FileHandle value(const QString &key) const
182 183 184 185 186 187 188 189 190 191 192 193
    {
        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();
    }
};


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
////////////////////////////////////////////////////////////////////////////////
// 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);
261
        else
262 263 264 265 266 267 268 269
            m_table.remove(h);
    }

    delete i;

    return true;
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
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;
}

288 289 290 291 292 293 294 295 296
template <class T>
void Hash<T>::deleteNode(Node *n)
{
    if(n) {
        deleteNode(n->next);
        delete n;
    }
}

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
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;
}


312
#endif