Debian Codegraph @

Source/Utilities/Container/CQtManagedRestrictedModificationHash.h

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * This file incorporates work covered by the following copyright and
20 * permission notice (to be compatible with LGPLv2.1,
21 * this code is available/released under GPLv3,
22 * see https://www.gnu.org/licenses/gpl-faq.html#AllCompatibility):
23 *
24 *
25 * Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
26 * Contact: http://www.qt-project.org/legal
27 *
28 * This file is part of the QtCore module of the Qt Toolkit.
29 *
30 * $QT_BEGIN_LICENSE:LGPL$
31 * Commercial License Usage
32 * Licensees holding valid commercial Qt licenses may use this file in
33 * accordance with the commercial license agreement provided with the
34 * Software or, alternatively, in accordance with the terms contained in
35 * a written agreement between you and Digia. For licensing terms and
36 * conditions see http://qt.digia.com/licensing. For further information
37 * use the contact form at http://qt.digia.com/contact-us.
38 *
39 * GNU Lesser General Public License Usage
40 * Alternatively, this file may be used under the terms of the GNU Lesser
41 * General Public License version 2.1 as published by the Free Software
42 * Foundation and appearing in the file LICENSE.LGPL included in the
43 * packaging of this file. Please review the following information to
44 * ensure the GNU Lesser General Public License version 2.1 requirements
45 * will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
46 *
47 * In addition, as a special exception, Digia gives you certain additional
48 * rights. These rights are described in the Digia Qt LGPL Exception
49 * version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
50 *
51 * GNU General Public License Usage
52 * Alternatively, this file may be used under the terms of the GNU
53 * General Public License version 3.0 as published by the Free Software
54 * Foundation and appearing in the file LICENSE.GPL included in the
55 * packaging of this file. Please review the following information to
56 * ensure the GNU General Public License version 3.0 requirements will be
57 * met: http://www.gnu.org/copyleft/gpl.html.
58 *
59 *
60 * $QT_END_LICENSE$
61 *
62 *
63 */
64
65 #ifndef KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONHASH_H
66 #define KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONHASH_H
67
68 // Namespace includes
69 #include "ContainerSettings.h"
70 #include "CQtManagedRestrictedModificationHashBase.h"
71 #include "CQtManagedRestrictedModificationHashData.h"
72 #include "CQtManagedRestrictedModificationList.h"
73
74 #ifdef KONCLUDE_EMPLOY_QT_MANAGED_RESTRICTED_MODIFICATION_HASH_CONTAINER
75
76 // Library includes
77 #include <QHash>
78
79 // Other includes
80 #include "Context/CContext.h"
81
82 // Logger includes
83 #include "Logger/CLogger.h"
84
85
86 namespace Konclude {
87
88 using namespace Context;
89
90 namespace Utilities {
91
92 namespace Container {
93
94
95 /*!
96 *
97 * \class CQtManagedRestrictedModificationHash
98 * \author Andreas Steigmiller
99 * \version 0.1
100 * \brief TODO
101 *
102 */
103
104
105 template <class Key, class T>
106 struct CQtManagedRestrictedModificationHashNode
107 {
108 CQtManagedRestrictedModificationHashNode *next;
109 uint h;
110 Key key;
111 T value;
112
113 inline CQtManagedRestrictedModificationHashNode(const Key &key0) : key(key0) {} // ### remove in 5.0
114 inline CQtManagedRestrictedModificationHashNode(const Key &key0, const T &value0) : key(key0), value(value0) {}
115 inline bool same_key(uint h0, const Key &key0) { return h0 == h && key0 == key; }
116 };
117
118
119 template <class Key, class T>
120 struct CQtManagedRestrictedModificationHashDummyNode
121 {
122 CQtManagedRestrictedModificationHashDummyNode *next;
123 uint h;
124 Key key;
125
126 inline CQtManagedRestrictedModificationHashDummyNode(const Key &key0) : key(key0) {}
127 };
128
129 struct CQtManagedRestrictedModificationHashDummyValue
130 {
131 };
132
133 inline bool operator==(const CQtManagedRestrictedModificationHashDummyValue & /* v1 */, const CQtManagedRestrictedModificationHashDummyValue & /* v2 */)
134 {
135 return true;
136 }
137
138 template <class Key, class T>
139 class CQtManagedRestrictedModificationHash : public CQtManagedRestrictedModificationHashBase
140 {
141 typedef CQtManagedRestrictedModificationHashDummyNode<Key, T> DummyNode;
142 typedef CQtManagedRestrictedModificationHashNode<Key, T> Node;
143
144 union {
145 CQtManagedRestrictedModificationHashData *d;
146 CQtManagedRestrictedModificationHashNode<Key, T> *e;
147 };
148 CContext* mContext;
149 CMemoryAllocationManager* mMemMan;
150
151 static inline Node *concrete(CQtManagedRestrictedModificationHashData::Node *node) {
152 return reinterpret_cast<Node *>(node);
153 }
154
155 #ifdef Q_ALIGNOF
156 static inline int alignOfNode() { return qMax<int>(sizeof(void*), Q_ALIGNOF(Node)); }
157 static inline int alignOfDummyNode() { return qMax<int>(sizeof(void*), Q_ALIGNOF(DummyNode)); }
158 #else
159 static inline int alignOfNode() { return 0; }
160 static inline int alignOfDummyNode() { return 0; }
161 #endif
162
163 public:
164 inline CQtManagedRestrictedModificationHash() : d(&CQtManagedRestrictedModificationHashData::shared_null),mContext(nullptr) {
165 mMemMan = CContext::getMemoryAllocationManager(mContext);
166 /*d->ref.ref();*/
167 }
168 inline CQtManagedRestrictedModificationHash(CContext* context) : d(&CQtManagedRestrictedModificationHashData::shared_null),mContext(context) {
169 mMemMan = CContext::getMemoryAllocationManager(mContext);
170 /*d->ref.ref();*/
171 }
172 inline CQtManagedRestrictedModificationHash(const CQtManagedRestrictedModificationHash<Key, T> &other) : d(other.d),mContext(nullptr) {
173 mMemMan = CContext::getMemoryAllocationManager(mContext);
174 /* d->ref.ref();*/ if (!d->sharable) detach();
175 }
176 inline CQtManagedRestrictedModificationHash(const CQtManagedRestrictedModificationHash<Key, T> &other, CContext* context) : d(other.d),mContext(context) {
177 mMemMan = CContext::getMemoryAllocationManager(mContext);
178 /* d->ref.ref();*/ if (!d->sharable) detach();
179 }
180 inline ~CQtManagedRestrictedModificationHash() {
181 if (/*!d->ref.deref()*/ d->refHash == this) freeData(d);
182 }
183
184
185 CQtManagedRestrictedModificationHash<Key, T>* init(CContext* context = nullptr);
186 CQtManagedRestrictedModificationHash<Key, T>* init(const CQtManagedRestrictedModificationHash<Key, T> &other, CContext* context = nullptr);
187 CQtManagedRestrictedModificationHash<Key, T>* init(CQtManagedRestrictedModificationHash<Key, T> *other, CContext* context = nullptr);
188
189
190 CQtManagedRestrictedModificationHash<Key, T> &operator=(const CQtManagedRestrictedModificationHash<Key, T> &other);
191
192 bool operator==(const CQtManagedRestrictedModificationHash<Key, T> &other) const;
193 inline bool operator!=(const CQtManagedRestrictedModificationHash<Key, T> &other) const { return !(*this == other); }
194
195 inline int size() const { return d->size; }
196
197 inline bool isEmpty() const { return d->size == 0; }
198
199 inline int capacity() const { return d->numBuckets; }
200 void reserve(int size);
201 inline void squeeze() { reserve(1); }
202
203 inline void detach() { if (/*d->ref != 1*/d->refHash != this) detach_helper(); }
204 inline void detachRestrictedCopy(void* assistValue) { if (/*d->ref != 1*/d->refHash != this) detach_helper_RestrictedCopy(assistValue); }
205 inline bool isDetached() const { return /*d->ref == 1;*/ d->refHash == this; }
206 inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; }
207
208 void clear();
209
210 int remove(const Key &key);
211 T take(const Key &key);
212
213 bool contains(const Key &key) const;
214 const Key key(const T &value) const;
215 const Key key(const T &value, const Key &defaultKey) const;
216 const T value(const Key &key) const;
217 const T value(const Key &key, const T &defaultValue) const;
218 T &operator[](const Key &key);
219 const T operator[](const Key &key) const;
220
221 bool tryGetValuePointer(const Key& key, T*& valuePointer) const;
222 bool tryGetValue(const Key& key, T* value = nullptr) const;
223 T* valuePointer(const Key& key) const;
224
225 QList<Key> uniqueKeys() const;
226 QList<Key> keys() const;
227 QList<Key> keys(const T &value) const;
228 QList<T> values() const;
229 QList<T> values(const Key &key) const;
230
231 CQtManagedRestrictedModificationList<Key> uniqueKeysInManagedRestrictedModificationList(CContext* context = nullptr) const;
232 CQtManagedRestrictedModificationList<Key> keysInManagedRestrictedModificationList(CContext* context = nullptr) const;
233 CQtManagedRestrictedModificationList<Key> keysInManagedRestrictedModificationList(const T &value, CContext* context = nullptr) const;
234 CQtManagedRestrictedModificationList<T> valuesInManagedRestrictedModificationList(CContext* context = nullptr) const;
235 CQtManagedRestrictedModificationList<T> valuesInManagedRestrictedModificationList(const Key &key, CContext* context = nullptr) const;
236
237 int count(const Key &key) const;
238
239 class const_iterator;
240
241 class iterator
242 {
243 friend class const_iterator;
244 CQtManagedRestrictedModificationHashData::Node *i;
245
246 public:
247 typedef std::bidirectional_iterator_tag iterator_category;
248 typedef ptrdiff_t difference_type;
249 typedef T value_type;
250 typedef T *pointer;
251 typedef T &reference;
252
253 // ### Qt 5: get rid of 'operator Node *'
254 inline operator Node *() const { return concrete(i); }
255 inline iterator() : i(0) { }
256 explicit inline iterator(void *node) : i(reinterpret_cast<CQtManagedRestrictedModificationHashData::Node *>(node)) { }
257
258 inline const Key &key() const { return concrete(i)->key; }
259 inline T &value() const { return concrete(i)->value; }
260 inline T &operator*() const { return concrete(i)->value; }
261 inline T *operator->() const { return &concrete(i)->value; }
262 inline bool operator==(const iterator &o) const { return i == o.i; }
263 inline bool operator!=(const iterator &o) const { return i != o.i; }
264
265 inline iterator &operator++() {
266 i = CQtManagedRestrictedModificationHashData::nextNode(i);
267 return *this;
268 }
269 inline iterator operator++(int) {
270 iterator r = *this;
271 i = CQtManagedRestrictedModificationHashData::nextNode(i);
272 return r;
273 }
274 inline iterator &operator--() {
275 i = CQtManagedRestrictedModificationHashData::previousNode(i);
276 return *this;
277 }
278 inline iterator operator--(int) {
279 iterator r = *this;
280 i = CQtManagedRestrictedModificationHashData::previousNode(i);
281 return r;
282 }
283 inline iterator operator+(int j) const
284 { iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; }
285 inline iterator operator-(int j) const { return operator+(-j); }
286 inline iterator &operator+=(int j) { return *this = *this + j; }
287 inline iterator &operator-=(int j) { return *this = *this - j; }
288
289 // ### Qt 5: not sure this is necessary anymore
290 #ifdef QT_STRICT_ITERATORS
291 private:
292 #else
293 public:
294 #endif
295 inline bool operator==(const const_iterator &o) const
296 { return i == o.i; }
297 inline bool operator!=(const const_iterator &o) const
298 { return i != o.i; }
299
300 private:
301 // ### Qt 5: remove
302 inline operator bool() const { return false; }
303 };
304 friend class iterator;
305
306 class const_iterator
307 {
308 friend class iterator;
309 CQtManagedRestrictedModificationHashData::Node *i;
310
311 public:
312 typedef std::bidirectional_iterator_tag iterator_category;
313 typedef ptrdiff_t difference_type;
314 typedef T value_type;
315 typedef const T *pointer;
316 typedef const T &reference;
317
318 // ### Qt 5: get rid of 'operator Node *'
319 inline operator Node *() const { return concrete(i); }
320 inline const_iterator() : i(0) { }
321 explicit inline const_iterator(void *node)
322 : i(reinterpret_cast<CQtManagedRestrictedModificationHashData::Node *>(node)) { }
323 #ifdef QT_STRICT_ITERATORS
324 explicit inline const_iterator(const iterator &o)
325 #else
326 inline const_iterator(const iterator &o)
327 #endif
328 { i = o.i; }
329
330 inline const Key &key() const { return concrete(i)->key; }
331 inline const T &value() const { return concrete(i)->value; }
332 inline const T &operator*() const { return concrete(i)->value; }
333 inline const T *operator->() const { return &concrete(i)->value; }
334 inline bool operator==(const const_iterator &o) const { return i == o.i; }
335 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
336
337 inline const_iterator &operator++() {
338 i = CQtManagedRestrictedModificationHashData::nextNode(i);
339 return *this;
340 }
341 inline const_iterator operator++(int) {
342 const_iterator r = *this;
343 i = CQtManagedRestrictedModificationHashData::nextNode(i);
344 return r;
345 }
346 inline const_iterator &operator--() {
347 i = CQtManagedRestrictedModificationHashData::previousNode(i);
348 return *this;
349 }
350 inline const_iterator operator--(int) {
351 const_iterator r = *this;
352 i = CQtManagedRestrictedModificationHashData::previousNode(i);
353 return r;
354 }
355 inline const_iterator operator+(int j) const
356 { const_iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; }
357 inline const_iterator operator-(int j) const { return operator+(-j); }
358 inline const_iterator &operator+=(int j) { return *this = *this + j; }
359 inline const_iterator &operator-=(int j) { return *this = *this - j; }
360
361 // ### Qt 5: not sure this is necessary anymore
362 #ifdef QT_STRICT_ITERATORS
363 private:
364 inline bool operator==(const iterator &o) const { return operator==(const_iterator(o)); }
365 inline bool operator!=(const iterator &o) const { return operator!=(const_iterator(o)); }
366 #endif
367
368 private:
369 // ### Qt 5: remove
370 inline operator bool() const { return false; }
371 };
372 friend class const_iterator;
373
374 // STL style
375 inline iterator begin() { detach(); return iterator(d->firstNode()); }
376 inline const_iterator begin() const { return const_iterator(d->firstNode()); }
377 inline const_iterator constBegin() const { return const_iterator(d->firstNode()); }
378 inline iterator end() { detach(); return iterator(e); }
379 inline const_iterator end() const { return const_iterator(e); }
380 inline const_iterator constEnd() const { return const_iterator(e); }
381 iterator erase(iterator it);
382
383 // more Qt
384 typedef iterator Iterator;
385 typedef const_iterator ConstIterator;
386 inline int count() const { return d->size; }
387 iterator find(const Key &key);
388 const_iterator find(const Key &key) const;
389 const_iterator constFind(const Key &key) const;
390 iterator insert(const Key &key, const T &value);
391 iterator insertMulti(const Key &key, const T &value);
392 CQtManagedRestrictedModificationHash<Key, T> &unite(const CQtManagedRestrictedModificationHash<Key, T> &other);
393
394 // STL compatibility
395 typedef T mapped_type;
396 typedef Key key_type;
397 typedef ptrdiff_t difference_type;
398 typedef int size_type;
399
400 inline bool empty() const { return isEmpty(); }
401
402 #ifdef QT_CQTMANAGEDONEWAYMUTATIONHASH_DEBUG
403 inline void dump() const { d->dump(); }
404 inline void checkSanity() const { d->checkSanity(); }
405 #endif
406
407 private:
408 void detach_helper();
409 void detach_helper_RestrictedCopy(void* assistValue);
410 void freeData(CQtManagedRestrictedModificationHashData *d);
411 Node **findNode(const Key &key, uint *hp = 0) const;
412 Node *createNode(uint h, const Key &key, const T &value, Node **nextNode);
413 void deleteNode(Node *node);
414 static void deleteNode2(CQtManagedRestrictedModificationHashData::Node *node);
415
416 static void duplicateNode(CQtManagedRestrictedModificationHashData::Node *originalNode, void *newNode);
417 static bool copyRestricted_helper(CQtManagedRestrictedModificationHashData::Node *originalNode, void* assistValue);
418 };
419
420
421 }; // end namespace Container
422
423 }; // end namespace Utilities
424
425 }; // end namespace Konclude
426
427 #include "CQtManagedRestrictedModificationHash.cpp"
428
429
430 #endif // KONCLUDE_EMPLOY_QT_MANAGED_RESTRICTED_MODIFICATION_HASH_CONTAINER
431
432 #endif // KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONHASH_H