summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/common/strhash.h
blob: 41b3d1135b68498069ac81b39100c3abe59d84bf (plain) (blame)
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
#ifndef STRHASH_H
#define STRHASH_H

#include <QHash>
#include <QString>
#include <QStringList>
#include <QDebug>

template <class T>
class StrHash
{
    public:
        StrHash() {}
        StrHash(std::initializer_list<std::pair<QString, T>> list) : hash(QHash<QString, T>(list))
        {
            initLower();
        }

        StrHash(const QHash<QString, T>& other) : hash(QHash<QString, T>(other))
        {
            initLower();
        }

        void insert(const QString& key, const T& value)
        {
            if (lowerCaseHash.contains(key.toLower()))
                remove(key, Qt::CaseInsensitive);

            hash.insert(key, value);
            lowerCaseHash.insert(key.toLower(), key);
        }

        bool contains(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
        {
            if (cs == Qt::CaseSensitive)
                return hash.contains(key);

            return lowerCaseHash.contains(key.toLower());
        }

        int	remove(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive)
        {
            if (cs == Qt::CaseSensitive)
            {
                int res = hash.remove(key);
                if (res > 0)
                    lowerCaseHash.remove(key.toLower());

                return res;
            }

            // Case insensitive
            QString lowerKey = key.toLower();
            if (lowerCaseHash.contains(lowerKey))
            {
                int res = hash.remove(lowerCaseHash.value(lowerKey));
                lowerCaseHash.remove(lowerKey);
                return res;
            }

            return 0;
        }

        T take(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive)
        {
            if (cs == Qt::CaseSensitive)
            {
                lowerCaseHash.remove(key.toLower());
                return hash.take(key);
            }

            // Case insensitive
            QString lowerKey = key.toLower();
            if (lowerCaseHash.contains(lowerKey))
            {
                QString theKey = lowerCaseHash.value(lowerKey);
                lowerCaseHash.remove(lowerKey);
                return hash.take(theKey);
            }

            return QString();
        }

        StrHash<T>& unite(const StrHash<T>& other)
        {
            unite(other.hash);
            return *this;
        }

        StrHash<T>& unite(const QHash<QString, T>& other)
        {
            QHashIterator<QString, T> it(other);
            while (it.hasNext())
            {
                it.next();
                insert(it.key(), it.value());
            }

            return *this;
        }

        T value(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
        {
            if (cs == Qt::CaseSensitive)
                return hash.value(key);

            return hash.value(lowerCaseHash.value(key.toLower()));
        }

        QList<T> values() const
        {
            return hash.values();
        }

        QStringList keys() const
        {
            return hash.keys();
        }

        QHashIterator<QString, T> iterator() const
        {
            return QHashIterator<QString, T>(hash);
        }

        void clear()
        {
            hash.clear();
            lowerCaseHash.clear();
        }

        int count() const
        {
            return hash.count();
        }

        int count(const QString& key, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
        {
            if (cs == Qt::CaseSensitive)
                return hash.count(key);

            return lowerCaseHash.count(key.toLower());
        }

        bool isEmpty() const
        {
            return hash.isEmpty();
        }

        QHash<QString, T> toQHash() const
        {
            return hash;
        }

        T& operator[](const QString& key)
        {
            if (lowerCaseHash.contains(key.toLower()) && !hash.contains(key))
            {
                T value = hash[lowerCaseHash[key.toLower()]];
                remove(key, Qt::CaseInsensitive);
                hash[key] = value;
            }

            lowerCaseHash[key.toLower()] = key;
            return hash[key];
        }

        const T operator[](const QString& key) const
        {
            return hash[lowerCaseHash[key.toLower()]];
        }

    private:
        void initLower()
        {
            QHashIterator<QString, T> it(hash);
            while (it.hasNext())
            {
                it.next();
                lowerCaseHash[it.key().toLower()] = it.key();
            }
        }

        QHash<QString, QString> lowerCaseHash;
        QHash<QString, T> hash;
};

#endif // STRHASH_H