1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #ifndef SORTEDHASHSET_H
22 #define SORTEDHASHSET_H
24 template <class T
> class EgHashSet
;
32 private: // data types
40 private: // attributes
42 QVector
<QList
<entry_t
> > m_Buckets
;
44 QVector
<int> m_Old2NewIndex
;
51 void resize(int size
);
53 int insert(const T
&item
);
54 void getQVector(QVector
<T
> &qv
);
55 void updateIndexMap();
56 int newIndex(int old_index
) { return m_Old2NewIndex
[old_index
]; }
61 EgHashSet
<T
>::EgHashSet()
67 EgHashSet
<T
>::EgHashSet(int size
)
70 m_Buckets
.resize(size
);
74 void EgHashSet
<T
>::resize(int size
)
77 m_Buckets
.resize(size
);
81 void EgHashSet
<T
>::clear()
88 int EgHashSet
<T
>::insert(const T
&item
)
90 QList
<entry_t
> &list
= m_Buckets
[item
.hash()];
92 while (idx
< list
.size()) {
93 if (!(item
< list
[idx
].value
)) {
98 if (idx
< list
.size()) {
99 if (item
== list
[idx
].value
) {
100 return list
[idx
].index
;
105 entry
.index
= m_NumEntries
;
106 list
.insert(idx
, entry
);
112 void EgHashSet
<T
>::updateIndexMap()
115 m_Old2NewIndex
.clear();
116 m_Old2NewIndex
.fill(-1, m_NumEntries
);
117 for (int i
= 0; i
< m_Buckets
.size(); ++i
) {
118 foreach (entry_t entry
, m_Buckets
[i
]) {
119 m_Old2NewIndex
[entry
.index
] = idx
;
126 void EgHashSet
<T
>::getQVector(QVector
<T
> &qv
)
130 qv
.resize(m_NumEntries
);
131 QVector
<bool> entry_set(m_NumEntries
, false);
132 for (int i
= 0; i
< m_Buckets
.size(); ++i
) {
133 foreach (entry_t entry
, m_Buckets
[i
]) {
134 qv
[entry
.index
] = entry
.value
;
135 entry_set
[entry
.index
] = true;
138 foreach (bool is_set
, entry_set
) {
145 #endif // SORTEDHASHSET_H