File: sortedhash.h

package info (click to toggle)
sqlitestudio 3.4.21%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 54,880 kB
  • sloc: ansic: 406,208; cpp: 123,872; yacc: 2,692; tcl: 497; sh: 462; xml: 426; makefile: 19
file content (164 lines) | stat: -rw-r--r-- 4,135 bytes parent folder | download
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
#ifndef SORTEDHASH_H
#define SORTEDHASH_H

#include <QHash>

/**
 * @brief Partially implemented sorted hash.
 *
 * This is kind of a sorted QHash, except it doesn't act as sorted with iterators,
 * just with keys. It also doesn't work with multiple values for single key.
 *
 * The complete sorted hash might be implemented later on.
 */
template <class Key, class T>
class SortedHash : public QHash<Key, T>
{
    public:
        SortedHash(std::initializer_list<std::pair<Key, T>> list) : QHash<Key, T>(list)
        {
            sortedKeys = keys();
        }

        SortedHash(const QHash<Key, T>& other) : QHash<Key, T>(other)
        {
            sortedKeys = keys();
        }

        SortedHash(QHash<Key, T>&& other) : QHash<Key, T>(other)
        {
            sortedKeys = keys();
        }

        SortedHash() : QHash<Key, T>()
        {
        }

        typename QHash<Key, T>::iterator insert(const Key& key, const T& value)
        {
            if (!sortedKeys.contains(key))
                sortedKeys << key;

            return QHash<Key, T>::insert(key, value);
        }

        int remove(const Key& key)
        {
            sortedKeys.removeOne(key);
            return QHash<Key, T>::remove(key);
        }

        void swap(QHash<Key, T>& other)
        {
            QHash<Key, T>::swap(other);
            sortedKeys = keys();
        }

        void swap(SortedHash<Key, T>& other)
        {
            QHash<Key, T>::swap(other);
            sortedKeys = other.sortedKeys;
        }

        T take(const Key& key)
        {
            sortedKeys.removeOne(key);
            return QHash<Key, T>::take(key);
        }

        QList<Key> keys() const
        {
            return sortedKeys;
        }

        QList<Key> keys(const T& value) const
        {
            QList<Key> results;
            for (const Key& k : sortedKeys)
                if (value(k) == value)
                    results << k;

            return results;
        }

        SortedHash<Key, T>& unite(const QHash<Key, T>& other)
        {
            QHash<Key, T>::unite(other);
            sortedKeys += other.keys();
            return *this;
        }

        SortedHash<Key, T>& unite(const SortedHash<Key, T>& other)
        {
            QHash<Key, T>::unite(other);
            sortedKeys += other.sortedKeys;
            return *this;
        }

        QList<T> values() const
        {
            QList<T> results;
            for (const Key& k : sortedKeys)
                results << value(k);

            return results;
        }

        bool operator!=(const SortedHash<Key, T>& other) const
        {
            return !operator==(other);
        }

        SortedHash<Key, T>& operator=(const QHash<Key, T>& other)
        {
            QHash<Key, T>::operator=(other);
            sortedKeys = other.keys();
            return *this;
        }

        SortedHash<Key, T>& operator=(const SortedHash<Key, T>& other)
        {
            QHash<Key, T>::operator=(other);
            sortedKeys = other.sortedKeys;
            return *this;
        }

        bool operator==(const SortedHash<Key, T>& other) const
        {
            return QHash<Key, T>::operator==(other) && sortedKeys == other.sortedKeys;
        }

        T &	operator[](const Key& key)
        {
            if (!sortedKeys.contains(key))
                sortedKeys << key;

            return QHash<Key, T>::operator[](key);
        }

        const T	operator[](const Key& key) const
        {
            return QHash<Key, T>::operator[](key);
        }

        Key firstKey() const
        {
            if (sortedKeys.size() == 0)
                return Key();

            return sortedKeys.first();
        }

        Key lastKey() const
        {
            if (sortedKeys.size() == 0)
                return Key();

            return sortedKeys.last();
        }

    private:
        QList<Key> sortedKeys;
};

#endif // SORTEDHASH_H