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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/*
SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef KDEVPLATFORM_SETREPOSITORY_H
#define KDEVPLATFORM_SETREPOSITORY_H
#include "basicsetrepository.h"
#include <QMutex>
#include <list>
/**
* This header defines convenience-class that allow handling set-repositories using the represented higher-level objects instead
* of indices to them.
* */
namespace Utils {
/**
* Use this class to conveniently iterate over the items in a set.
* @tparam T The type the indices will be converted to
* @tparam Conversion Should be a class that has a toIndex member function that takes an object of type T as parameter, and returns an index,
* and a toItem member function that takes an index, and returns an item of type T.
* */
template <class T, class Conversion>
class ConvenientIterator
: public Conversion
{
public:
explicit ConvenientIterator(const Set::Iterator& it = Set::Iterator()) : m_it(it)
{
}
explicit ConvenientIterator(const Set& set) : m_it(set.iterator())
{
}
operator bool() const {
return m_it;
}
ConvenientIterator& operator++()
{
++m_it;
return *this;
}
T operator*() const
{
return Conversion::toItem(*m_it);
}
uint index() const
{
return *m_it;
}
private:
Set::Iterator m_it;
};
struct DummyLocker
{
};
template <class T>
struct IdentityConversion
{
static T toIndex(const T& t)
{
return t;
}
static T toItem(const T& t)
{
return t;
}
};
///This is a virtual set-node that allows conveniently iterating through the tree-structure,
///accessing the contained items directly, and accessing the ranges.
template <class T, class Conversion, class StaticRepository>
class VirtualSetNode
{
private:
using ClassType = VirtualSetNode<T, Conversion, StaticRepository>;
public:
inline explicit VirtualSetNode(const SetNodeData* data = nullptr) : m_data(data)
{
}
inline bool isValid() const
{
return ( bool )m_data;
}
///If this returns false, a left and a right node are available.
///If this returns true, this node represents a single item, that can be retrieved by calling item() or operator*.
inline bool isFinalNode() const
{
return m_data->leftNode() == 0;
}
inline T firstItem() const
{
return Conversion::toItem(start());
}
inline T lastItem() const
{
return Conversion::toItem(end() - 1);
}
inline T operator*() const
{
return Conversion::toItem(start());
}
inline ClassType leftChild() const
{
if (m_data->leftNode())
return ClassType(StaticRepository::repository()->nodeFromIndex(m_data->leftNode()));
else
return ClassType(nullptr);
}
inline ClassType rightChild() const
{
if (m_data->rightNode())
return ClassType(StaticRepository::repository()->nodeFromIndex(m_data->rightNode()));
else
return ClassType(nullptr);
}
///Returns the start of this node's range. If this is a final node, the length of the range is 1.
inline uint start() const
{
return m_data->start();
}
///Returns the end of this node's range.
inline uint end() const
{
return m_data->end();
}
private:
const SetNodeData* m_data;
};
template <class T, class Conversion, class StaticRepository, bool doReferenceCounting = false,
class StaticAccessLocker = DummyLocker>
class StorableSet
: public Conversion
{
public:
using Node = VirtualSetNode<T, Conversion, StaticRepository>;
StorableSet(const StorableSet& rhs) : m_setIndex(rhs.m_setIndex)
{
StaticAccessLocker lock;
Q_UNUSED(lock);
if (doReferenceCounting)
set().staticRef();
}
explicit StorableSet(const std::set<uint>& indices)
{
StaticAccessLocker lock;
Q_UNUSED(lock);
m_setIndex = StaticRepository::repository()->createSet(indices).setIndex();
if (doReferenceCounting)
set().staticRef();
}
StorableSet()
{
}
~StorableSet()
{
StaticAccessLocker lock;
Q_UNUSED(lock);
if (doReferenceCounting)
set().staticUnref();
}
void insert(const T& t)
{
insertIndex(Conversion::toIndex(t));
}
bool isEmpty() const
{
return m_setIndex == 0;
}
uint count() const
{
return set().count();
}
void insertIndex(uint index)
{
StaticAccessLocker lock;
Q_UNUSED(lock);
Set set(m_setIndex, StaticRepository::repository());
Set oldSet(set);
Set addedSet = StaticRepository::repository()->createSet(index);
if (doReferenceCounting)
addedSet.staticRef();
set += addedSet;
m_setIndex = set.setIndex();
if (doReferenceCounting) {
set.staticRef();
oldSet.staticUnref();
addedSet.staticUnref();
}
}
void remove(const T& t)
{
removeIndex(Conversion::toIndex(t));
}
void removeIndex(uint index)
{
StaticAccessLocker lock;
Q_UNUSED(lock);
Set set(m_setIndex, StaticRepository::repository());
Set oldSet(set);
Set removedSet = StaticRepository::repository()->createSet(index);
if (doReferenceCounting) {
removedSet.staticRef();
}
set -= removedSet;
m_setIndex = set.setIndex();
if (doReferenceCounting) {
set.staticRef();
oldSet.staticUnref();
removedSet.staticUnref();
}
}
Set set() const
{
return Set(m_setIndex, StaticRepository::repository());
}
bool contains(const T& item) const
{
return containsIndex(Conversion::toIndex(item));
}
bool containsIndex(uint index) const
{
StaticAccessLocker lock;
Q_UNUSED(lock);
Set set(m_setIndex, StaticRepository::repository());
return set.contains(index);
}
StorableSet& operator +=(const StorableSet& rhs)
{
StaticAccessLocker lock;
Q_UNUSED(lock);
Set set(m_setIndex, StaticRepository::repository());
Set oldSet(set);
Set otherSet(rhs.m_setIndex, StaticRepository::repository());
set += otherSet;
m_setIndex = set.setIndex();
if (doReferenceCounting) {
set.staticRef();
oldSet.staticUnref();
}
return *this;
}
StorableSet& operator -=(const StorableSet& rhs)
{
StaticAccessLocker lock;
Q_UNUSED(lock);
Set set(m_setIndex, StaticRepository::repository());
Set oldSet(set);
Set otherSet(rhs.m_setIndex, StaticRepository::repository());
set -= otherSet;
m_setIndex = set.setIndex();
if (doReferenceCounting) {
set.staticRef();
oldSet.staticUnref();
}
return *this;
}
StorableSet& operator &=(const StorableSet& rhs)
{
StaticAccessLocker lock;
Q_UNUSED(lock);
Set set(m_setIndex, StaticRepository::repository());
Set oldSet(set);
Set otherSet(rhs.m_setIndex, StaticRepository::repository());
set &= otherSet;
m_setIndex = set.setIndex();
if (doReferenceCounting) {
set.staticRef();
oldSet.staticUnref();
}
return *this;
}
StorableSet& operator=(const StorableSet& rhs)
{
StaticAccessLocker lock;
Q_UNUSED(lock);
if (doReferenceCounting)
set().staticUnref();
m_setIndex = rhs.m_setIndex;
if (doReferenceCounting)
set().staticRef();
return *this;
}
StorableSet operator +(const StorableSet& rhs) const
{
StorableSet ret(*this);
ret += rhs;
return ret;
}
StorableSet operator -(const StorableSet& rhs) const
{
StorableSet ret(*this);
ret -= rhs;
return ret;
}
StorableSet operator &(const StorableSet& rhs) const
{
StorableSet ret(*this);
ret &= rhs;
return ret;
}
bool operator==(const StorableSet& rhs) const
{
return m_setIndex == rhs.m_setIndex;
}
using Iterator = ConvenientIterator<T, Conversion>;
Iterator iterator() const
{
return ConvenientIterator<T, Conversion>(set());
}
Node node() const
{
return Node(StaticRepository::repository()->nodeFromIndex(m_setIndex));
}
uint setIndex() const
{
return m_setIndex;
}
private:
uint m_setIndex = 0;
};
template <class T, class Conversion, class StaticRepository, bool doReferenceCounting, class StaticAccessLocker>
uint qHash(const StorableSet<T, Conversion, StaticRepository, doReferenceCounting, StaticAccessLocker>& set)
{
return set.setIndex();
}
/** This is a helper-class that helps inserting a bunch of items into a set without caring about grouping them together.
*
* It creates a much better tree-structure if many items are inserted at one time, and this class helps doing that in
* cases where there is no better choice then storing a temporary list of items and inserting them all at once.
*
* This set will then care about really inserting them into the repository once the real set is requested.
*
* @todo eventually make this unnecessary
*
* @tparam T Should be the type that should be dealt
* @tparam Conversion Should be a class that has a toIndex member function that takes an object of type T as parameter, and returns an index,
* and a toItem member function that takes an index, and returns an item of type T.
**/
template <class T, class Conversion>
class LazySet
: public Conversion
{
public:
/** @param rep The repository the set should belong/belongs to
* @param lockBeforeAccess If this is nonzero, the given mutex will be locked before each modification to the repository.
* @param basicSet If this is explicitly given, the given set will be used as base. However it will not be changed.
*
* @warning Watch for deadlocks, never use this class while the mutex given through lockBeforeAccess is locked
*/
explicit LazySet(BasicSetRepository* rep, QMutex* lockBeforeAccess = nullptr, const Set& basicSet = Set()) : m_rep(
rep)
, m_set(basicSet)
, m_lockBeforeAccess(lockBeforeAccess)
{
}
void insert(const T& t)
{
if (!m_temporaryRemoveIndices.empty())
apply();
m_temporaryIndices.insert(Conversion::toIndex(t));
}
void insertIndex(uint index)
{
if (!m_temporaryRemoveIndices.empty())
apply();
m_temporaryIndices.insert(index);
}
void remove(const T& t)
{
if (!m_temporaryIndices.empty())
apply();
m_temporaryRemoveIndices.insert(Conversion::toIndex(t));
}
///Returns the set this LazySet represents. When this is called, the set is constructed in the repository.
Set set() const
{
apply();
return m_set;
}
///@warning this is expensive, because the set is constructed
bool contains(const T& item) const
{
QMutexLocker l(m_lockBeforeAccess);
uint index = Conversion::toIndex(item);
if (m_temporaryRemoveIndices.empty()) {
//Simplification without creating the set
if (m_temporaryIndices.find(index) != m_temporaryIndices.end())
return true;
return m_set.contains(index);
}
return set().contains(index);
}
LazySet& operator +=(const Set& set)
{
if (!m_temporaryRemoveIndices.empty())
apply();
QMutexLocker l(m_lockBeforeAccess);
m_set += set;
return *this;
}
LazySet& operator -=(const Set& set)
{
if (!m_temporaryIndices.empty())
apply();
QMutexLocker l(m_lockBeforeAccess);
m_set -= set;
return *this;
}
LazySet operator +(const Set& set) const
{
apply();
QMutexLocker l(m_lockBeforeAccess);
Set ret = m_set + set;
return LazySet(m_rep, m_lockBeforeAccess, ret);
}
LazySet operator -(const Set& set) const
{
apply();
QMutexLocker l(m_lockBeforeAccess);
Set ret = m_set - set;
return LazySet(m_rep, m_lockBeforeAccess, ret);
}
void clear()
{
QMutexLocker l(m_lockBeforeAccess);
m_set = Set();
m_temporaryIndices.clear();
m_temporaryRemoveIndices.clear();
}
ConvenientIterator<T, Conversion> iterator() const
{
apply();
return ConvenientIterator<T, Conversion>(set());
}
private:
void apply() const
{
if (!m_temporaryIndices.empty()) {
QMutexLocker l(m_lockBeforeAccess);
Set tempSet = m_rep->createSet(m_temporaryIndices);
m_temporaryIndices.clear();
m_set += tempSet;
}
if (!m_temporaryRemoveIndices.empty()) {
QMutexLocker l(m_lockBeforeAccess);
Set tempSet = m_rep->createSet(m_temporaryRemoveIndices);
m_temporaryRemoveIndices.clear();
m_set -= tempSet;
}
}
BasicSetRepository* m_rep;
mutable Set m_set;
QMutex* m_lockBeforeAccess;
using IndexList = std::set<Utils::BasicSetRepository::Index>;
mutable IndexList m_temporaryIndices;
mutable IndexList m_temporaryRemoveIndices;
};
///Persistent repository that manages string-sets, also correctly increasing the string reference-counts as needed
struct KDEVPLATFORMLANGUAGE_EXPORT StringSetRepository
: public Utils::BasicSetRepository
{
explicit StringSetRepository(const QString& name, QRecursiveMutex* mutex);
void itemRemovedFromSets(uint index) override;
void itemAddedToSets(uint index) override;
};
}
#endif
|