fixed edge display for volume cells
[engrid-github.git] / src / libengrid / eghashset.h
blobef1dee3b9cc3b178f5cc1a7707ff9f8b21625a10
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
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. +
11 // + +
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. +
16 // + +
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/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #ifndef SORTEDHASHSET_H
22 #define SORTEDHASHSET_H
24 template <class T> class EgHashSet;
26 #include <QList>
28 template <class T>
29 class EgHashSet
32 private: // data types
34 struct entry_t
36 T value;
37 int index;
40 private: // attributes
42 QVector<QList<entry_t> > m_Buckets;
43 int m_NumEntries;
44 QVector<int> m_Old2NewIndex;
46 public: // methods
48 EgHashSet();
49 EgHashSet(int size);
51 void resize(int size);
52 void clear();
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]; }
60 template <class T>
61 EgHashSet<T>::EgHashSet()
63 clear();
66 template <class T>
67 EgHashSet<T>::EgHashSet(int size)
69 clear();
70 m_Buckets.resize(size);
73 template <class T>
74 void EgHashSet<T>::resize(int size)
76 clear();
77 m_Buckets.resize(size);
80 template <class T>
81 void EgHashSet<T>::clear()
83 m_Buckets.clear();
84 m_NumEntries = 0;
87 template <class T>
88 int EgHashSet<T>::insert(const T &item)
90 QList<entry_t> &list = m_Buckets[item.hash()];
91 int idx = 0;
92 while (idx < list.size()) {
93 if (!(item < list[idx].value)) {
94 break;
96 ++ idx;
98 if (idx < list.size()) {
99 if (item == list[idx].value) {
100 return list[idx].index;
103 entry_t entry;
104 entry.value = item;
105 entry.index = m_NumEntries;
106 list.insert(idx, entry);
107 ++m_NumEntries;
108 return entry.index;
111 template <class T>
112 void EgHashSet<T>::updateIndexMap()
114 int idx = 0;
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;
120 ++idx;
125 template <class T>
126 void EgHashSet<T>::getQVector(QVector<T> &qv)
128 updateIndexMap();
129 qv.clear();
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) {
139 if (!is_set) {
140 EG_BUG;
145 #endif // SORTEDHASHSET_H