2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2013 enGits GmbH +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #ifndef SORTEDHASHSET_H
24 #define SORTEDHASHSET_H
26 template <class T
> class EgHashSet
;
34 private: // data types
42 private: // attributes
44 QVector
<QList
<entry_t
> > m_Buckets
;
46 QVector
<int> m_Old2NewIndex
;
53 void resize(int size
);
55 int insert(const T
&item
);
56 void getQVector(QVector
<T
> &qv
);
57 void updateIndexMap();
58 int newIndex(int old_index
) { return m_Old2NewIndex
[old_index
]; }
63 EgHashSet
<T
>::EgHashSet()
69 EgHashSet
<T
>::EgHashSet(int size
)
72 m_Buckets
.resize(size
);
76 void EgHashSet
<T
>::resize(int size
)
79 m_Buckets
.resize(size
);
83 void EgHashSet
<T
>::clear()
90 int EgHashSet
<T
>::insert(const T
&item
)
92 QList
<entry_t
> &list
= m_Buckets
[item
.hash()];
94 while (idx
< list
.size()) {
95 if (!(item
< list
[idx
].value
)) {
100 if (idx
< list
.size()) {
101 if (item
== list
[idx
].value
) {
102 return list
[idx
].index
;
107 entry
.index
= m_NumEntries
;
108 list
.insert(idx
, entry
);
114 void EgHashSet
<T
>::updateIndexMap()
117 m_Old2NewIndex
.clear();
118 m_Old2NewIndex
.fill(-1, m_NumEntries
);
119 for (int i
= 0; i
< m_Buckets
.size(); ++i
) {
120 foreach (entry_t entry
, m_Buckets
[i
]) {
121 m_Old2NewIndex
[entry
.index
] = idx
;
128 void EgHashSet
<T
>::getQVector(QVector
<T
> &qv
)
132 qv
.resize(m_NumEntries
);
133 QVector
<bool> entry_set(m_NumEntries
, false);
134 for (int i
= 0; i
< m_Buckets
.size(); ++i
) {
135 foreach (entry_t entry
, m_Buckets
[i
]) {
136 qv
[entry
.index
] = entry
.value
;
137 entry_set
[entry
.index
] = true;
140 foreach (bool is_set
, entry_set
) {
147 #endif // SORTEDHASHSET_H