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 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
/*
Copyright (C) 2010 Klarälvdalens Datakonsult AB,
a KDAB Group company, info@kdab.net,
author Stephen Kelly <stephen@kdab.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef KBIHASH_P_H
#define KBIHASH_P_H
#include <QHash>
#include <QMap>
#include <QDebug>
template<typename LeftContainer, typename RightContainer>
class KBiAssociativeContainer;
template<typename LeftContainer, typename RightContainer>
QDebug operator<<(QDebug out, const KBiAssociativeContainer<LeftContainer, RightContainer> &container);
template<typename LeftContainer, typename RightContainer>
QDataStream &operator<<(QDataStream &out, const KBiAssociativeContainer<LeftContainer, RightContainer> &container);
template<typename LeftContainer, typename RightContainer>
QDataStream &operator>>(QDataStream &in, KBiAssociativeContainer<LeftContainer, RightContainer> &container);
template<typename LeftContainer, typename RightContainer>
class KBiAssociativeContainer
{
// We need to convert from a QHash::iterator or QMap::iterator
// to a KBiAssociativeContainer::iterator (left or right)
// Do do that we use this implicit ctor. We partially specialize
// it for QHash and QMap types.
// Our iterator inherits from this struct to get the implicit ctor,
// and this struct must inherit from the QHash or QMap iterator.
template<typename Container, typename T, typename U>
struct _iterator_impl_ctor : public Container::iterator
{
_iterator_impl_ctor(typename Container::iterator it);
};
template<typename T, typename U>
struct _iterator_impl_ctor<QHash<T, U>, T, U> : public QHash<T, U>::iterator
{
/* implicit */ _iterator_impl_ctor(const typename QHash<T, U>::iterator it)
// Using internals here because I was too lazy to write my own iterator.
#if QT_VERSION < 0x050000
: QHash<T, U>::iterator(reinterpret_cast<void *>(static_cast<QHashNode<T, U> *>(it)))
#else
: QHash<T, U>::iterator(it)
#endif
{
}
};
template<typename T, typename U>
struct _iterator_impl_ctor<QMap<T, U>, T, U> : public QMap<T, U>::iterator
{
/* implicit */ _iterator_impl_ctor(const typename QMap<T, U>::iterator it)
// Using internals here because I was too lazy to write my own iterator.
#if QT_VERSION < 0x050000
: QMap<T, U>::iterator(static_cast<QMapData::Node*>(it))
#else
: QMap<T, U>::iterator(it)
#endif
{
}
};
public:
typedef typename RightContainer::mapped_type left_type;
typedef typename LeftContainer::mapped_type right_type;
template <typename Container>
class _iterator : public _iterator_impl_ctor<Container, typename Container::key_type, typename Container::mapped_type>
{
public:
explicit inline _iterator(void *data) : Container::iterator(data) {}
/* implicit */ _iterator(const typename Container::iterator it)
: _iterator_impl_ctor<Container, typename Container::key_type, typename Container::mapped_type>(it)
{
}
inline const typename Container::mapped_type &value() const {
return Container::iterator::value();
}
inline const typename Container::mapped_type &operator*() const {
return Container::iterator::operator*();
}
inline const typename Container::mapped_type *operator->() const {
return Container::iterator::operator->();
}
private:
#ifndef Q_CC_MSVC
using Container::iterator::operator*;
using Container::iterator::operator->;
using Container::iterator::value;
#endif
};
typedef _iterator<LeftContainer> left_iterator;
typedef typename LeftContainer::const_iterator left_const_iterator;
typedef _iterator<RightContainer> right_iterator;
typedef typename RightContainer::const_iterator right_const_iterator;
inline KBiAssociativeContainer() {}
inline KBiAssociativeContainer(const KBiAssociativeContainer<LeftContainer, RightContainer> &other) {
*this = other;
}
const KBiAssociativeContainer<LeftContainer, RightContainer> &operator=(const KBiAssociativeContainer<LeftContainer, RightContainer> &other) {
_leftToRight = other._leftToRight; _rightToLeft = other._rightToLeft; return *this;
}
inline bool removeLeft(left_type t) {
const right_type u = _leftToRight.take(t);
return _rightToLeft.remove(u) != 0;
}
inline bool removeRight(right_type u) {
const left_type t = _rightToLeft.take(u);
return _leftToRight.remove(t) != 0;
}
inline right_type takeLeft(left_type t) {
const right_type u = _leftToRight.take(t);
_rightToLeft.remove(u);
return u;
}
inline left_type takeRight(right_type u) {
const left_type t = _rightToLeft.take(u);
_leftToRight.remove(t);
return t;
}
inline left_type rightToLeft(right_type u) const {
return _rightToLeft.value(u);
}
inline right_type leftToRight(left_type t) const {
return _leftToRight.value(t);
}
inline bool leftContains(left_type t) const {
return _leftToRight.contains(t);
}
inline bool rightContains(right_type u) const {
return _rightToLeft.contains(u);
}
inline int size() const {
return _leftToRight.size();
}
inline int count() const {
return _leftToRight.count();
}
inline int capacity() const {
return _leftToRight.capacity();
}
void reserve(int size) {
_leftToRight.reserve(size); _rightToLeft.reserve(size);
}
inline void squeeze() {
_leftToRight.squeeze(); _rightToLeft.squeeze();
}
inline void detach() {
_leftToRight.detach(); _rightToLeft.detach();
}
inline bool isDetached() const {
return _leftToRight.isDetached();
}
inline void setSharable(bool sharable) {
_leftToRight.setSharable(sharable); _rightToLeft.setSharable(sharable);
}
inline bool isSharedWith(const KBiAssociativeContainer<RightContainer, LeftContainer> &other) const {
return _leftToRight.isSharedWith(other._leftToRight) && _rightToLeft.isSharedWith(other._leftToRight);
}
void clear() {
_leftToRight.clear(); _rightToLeft.clear();
}
QList<left_type> leftValues() const {
return _leftToRight.keys();
}
QList<right_type> rightValues() const {
return _rightToLeft.keys();
}
right_iterator eraseRight(right_iterator it) {
Q_ASSERT(it != rightEnd());
_leftToRight.remove(it.value());
return _rightToLeft.erase(it);
}
left_iterator eraseLeft(left_iterator it) {
Q_ASSERT(it != leftEnd());
_rightToLeft.remove(it.value());
return _leftToRight.erase(it);
}
left_iterator findLeft(left_type t) {
return _leftToRight.find(t);
}
left_const_iterator findLeft(left_type t) const {
return _leftToRight.find(t);
}
left_const_iterator constFindLeft(left_type t) const {
return _leftToRight.constFind(t);
}
right_iterator findRight(right_type u) {
return _rightToLeft.find(u);
}
right_const_iterator findRight(right_type u) const {
return _rightToLeft.find(u);
}
right_const_iterator constFindRight(right_type u) const {
return _rightToLeft.find(u);
}
left_iterator insert(left_type t, right_type u) {
// biHash.insert(5, 7); // creates 5->7 in _leftToRight and 7->5 in _rightToLeft
// biHash.insert(5, 9); // replaces 5->7 with 5->9 in _leftToRight and inserts 9->5 in _rightToLeft.
// The 7->5 in _rightToLeft would be dangling, so we remove it before insertion.
// This means we need to hash u and t up to twice each. Could probably be done better using QHashNode.
if (_leftToRight.contains(t))
_rightToLeft.remove(_leftToRight.take(t));
if (_rightToLeft.contains(u))
_leftToRight.remove(_rightToLeft.take(u));
_rightToLeft.insert(u, t);
return _leftToRight.insert(t, u);
}
KBiAssociativeContainer<LeftContainer, RightContainer> &intersect(const KBiAssociativeContainer<LeftContainer, RightContainer> &other) {
typename KBiAssociativeContainer<RightContainer, LeftContainer>::left_iterator it = leftBegin();
while (it != leftEnd()) {
if (!other.leftContains(it.key()))
it = eraseLeft(it);
else
++it;
}
return *this;
}
KBiAssociativeContainer<LeftContainer, RightContainer> &subtract(const KBiAssociativeContainer<LeftContainer, RightContainer> &other) {
typename KBiAssociativeContainer<RightContainer, LeftContainer>::left_iterator it = leftBegin();
while (it != leftEnd()) {
if (other._leftToRight.contains(it.key()))
it = eraseLeft(it);
else
++it;
}
return *this;
}
KBiAssociativeContainer<LeftContainer, RightContainer> &unite(const KBiAssociativeContainer<LeftContainer, RightContainer> &other) {
typename LeftContainer::const_iterator it = other._leftToRight.constBegin();
const typename LeftContainer::const_iterator end = other._leftToRight.constEnd();
while (it != end) {
const left_type key = it.key();
if (!_leftToRight.contains(key))
insert(key, it.value());
++it;
}
return *this;
}
void updateRight(left_iterator it, right_type u) {
Q_ASSERT(it != leftEnd());
const left_type key = it.key();
_rightToLeft.remove(_leftToRight.value(key));
_leftToRight[key] = u;
_rightToLeft[u] = key;
}
void updateLeft(right_iterator it, left_type t) {
Q_ASSERT(it != rightEnd());
const right_type key = it.key();
_leftToRight.remove(_rightToLeft.value(key));
_rightToLeft[key] = t;
_leftToRight[t] = key;
}
inline bool isEmpty() const {
return _leftToRight.isEmpty();
}
const right_type operator[](const left_type &t) const {
return _leftToRight.operator[](t);
}
bool operator==(const KBiAssociativeContainer<LeftContainer, RightContainer> &other) const {
return _leftToRight.operator == (other._leftToRight);
}
bool operator!=(const KBiAssociativeContainer<LeftContainer, RightContainer> &other) const {
return _leftToRight.operator != (other._leftToRight);
}
left_iterator toLeftIterator(right_iterator it) const {
Q_ASSERT(it != rightEnd());
return _leftToRight.find(it.value());
}
right_iterator toRightIterator(left_iterator it) const {
Q_ASSERT(it != leftEnd());
return _rightToLeft.find(it.value());
}
inline left_iterator leftBegin() {
return _leftToRight.begin();
}
inline left_iterator leftEnd() {
return _leftToRight.end();
}
inline left_const_iterator leftBegin() const {
return _leftToRight.begin();
}
inline left_const_iterator leftEnd() const {
return _leftToRight.end();
}
inline left_const_iterator leftConstBegin() const {
return _leftToRight.constBegin();
}
inline left_const_iterator leftConstEnd() const {
return _leftToRight.constEnd();
}
inline right_iterator rightBegin() {
return _rightToLeft.begin();
}
inline right_iterator rightEnd() {
return _rightToLeft.end();
}
inline right_const_iterator rightBegin() const {
return _rightToLeft.begin();
}
inline right_const_iterator rightEnd() const {
return _rightToLeft.end();
}
inline right_const_iterator rightConstBegin() const {
return _rightToLeft.constBegin();
}
inline right_const_iterator rightConstEnd() const {
return _rightToLeft.constEnd();
}
static KBiAssociativeContainer<LeftContainer, RightContainer> fromHash(const QHash<left_type, right_type> &hash) {
KBiAssociativeContainer<LeftContainer, RightContainer> container;
typename QHash<left_type, right_type>::const_iterator it = hash.constBegin();
const typename QHash<left_type, right_type>::const_iterator end = hash.constEnd();
for ( ; it != end; ++it)
container.insert(it.key(), it.value());
return container;
}
static KBiAssociativeContainer<LeftContainer, RightContainer> fromMap(const QMap<left_type, right_type> &hash) {
KBiAssociativeContainer<LeftContainer, RightContainer> container;
typename QMap<left_type, right_type>::const_iterator it = hash.constBegin();
const typename QMap<left_type, right_type>::const_iterator end = hash.constEnd();
for ( ; it != end; ++it)
container.insert(it.key(), it.value());
return container;
}
friend QDataStream &operator<< <LeftContainer, RightContainer>(QDataStream &out, const KBiAssociativeContainer<LeftContainer, RightContainer> &bihash);
friend QDataStream &operator>> <LeftContainer, RightContainer>(QDataStream &in, KBiAssociativeContainer<LeftContainer, RightContainer> &biHash);
friend QDebug operator<< <LeftContainer, RightContainer>(QDebug out, const KBiAssociativeContainer<LeftContainer, RightContainer> &biHash);
protected:
LeftContainer _leftToRight;
RightContainer _rightToLeft;
};
template<typename LeftContainer, typename RightContainer>
QDataStream &operator<<(QDataStream &out, const KBiAssociativeContainer<LeftContainer, RightContainer> &container)
{
return out << container._leftToRight;
}
template<typename LeftContainer, typename RightContainer>
QDataStream &operator>>(QDataStream &in, KBiAssociativeContainer<LeftContainer, RightContainer> &container)
{
LeftContainer leftToRight;
in >> leftToRight;
typename LeftContainer::const_iterator it = leftToRight.constBegin();
const typename LeftContainer::const_iterator end = leftToRight.constEnd();
for (; it != end; ++it)
container.insert(it.key(), it.value());
return in;
}
template<typename Container, typename T, typename U>
struct _containerType
{
operator const char *();
};
template<typename T, typename U>
struct _containerType<QHash<T, U>, T, U>
{
operator const char *()
{
return "QHash";
}
};
template<typename T, typename U>
struct _containerType<QMap<T, U>, T, U>
{
operator const char *()
{
return "QMap";
}
};
template<typename Container>
static const char * containerType()
{
return _containerType<Container, typename Container::key_type, typename Container::mapped_type>();
}
template<typename LeftContainer, typename RightContainer>
QDebug operator<<(QDebug out, const KBiAssociativeContainer<LeftContainer, RightContainer> &container)
{
typename KBiAssociativeContainer<LeftContainer, RightContainer>::left_const_iterator it = container.leftConstBegin();
const typename KBiAssociativeContainer<LeftContainer, RightContainer>::left_const_iterator end = container.leftConstEnd();
out.nospace() << "KBiAssociativeContainer<" << containerType<LeftContainer>() << ", " << containerType<RightContainer>() << ">" << "(";
for (; it != end; ++it)
out << "(" << it.key() << " <=> " << it.value() << ") ";
out << ")";
return out;
}
/**
* @brief KBiHash provides a bi-directional hash container
*
* @note This class is designed to make mapping easier in proxy model implementations.
*
* @todo Figure out whether to discard this and use boost::bimap instead, submit it Qt or keep it here and make more direct use of QHashNode.
*/
template <typename T, typename U>
struct KBiHash : public KBiAssociativeContainer<QHash<T, U>, QHash<U, T> >
{
KBiHash()
: KBiAssociativeContainer<QHash<T, U>, QHash<U, T> > ()
{
}
KBiHash(const KBiAssociativeContainer<QHash<T, U>, QHash<U, T> > &container)
: KBiAssociativeContainer<QHash<T, U>, QHash<U, T> > (container)
{
}
};
template<typename T, typename U>
QDebug operator<<(QDebug out, const KBiHash<T, U> &biHash)
{
typename KBiHash<T, U>::left_const_iterator it = biHash.leftConstBegin();
const typename KBiHash<T, U>::left_const_iterator end = biHash.leftConstEnd();
out.nospace() << "KBiHash(";
for (; it != end; ++it)
out << "(" << it.key() << " <=> " << it.value() << ") ";
out << ")";
return out;
}
template <typename T, typename U>
struct KHash2Map : public KBiAssociativeContainer<QHash<T, U>, QMap<U, T> >
{
KHash2Map()
: KBiAssociativeContainer<QHash<T, U>, QMap<U, T> > ()
{
}
KHash2Map(const KBiAssociativeContainer<QHash<T, U>, QMap<U, T> > &container)
: KBiAssociativeContainer<QHash<T, U>, QMap<U, T> > (container)
{
}
typename KBiAssociativeContainer<QHash<T, U>, QMap<U, T> >::right_iterator rightLowerBound(const U &key)
{
return this->_rightToLeft.lowerBound(key);
}
typename KBiAssociativeContainer<QHash<T, U>, QMap<U, T> >::right_const_iterator rightLowerBound(const U &key) const
{
return this->_rightToLeft.lowerBound(key);
}
typename KBiAssociativeContainer<QHash<T, U>, QMap<U, T> >::right_iterator rightUpperBound(const U &key)
{
return this->_rightToLeft.upperBound(key);
}
typename KBiAssociativeContainer<QHash<T, U>, QMap<U, T> >::right_const_iterator rightUpperBound(const U &key) const
{
return this->_rightToLeft.upperBound(key);
}
};
template<typename T, typename U>
QDebug operator<<(QDebug out, const KHash2Map<T, U> &container)
{
typename KHash2Map<T, U>::left_const_iterator it = container.leftConstBegin();
const typename KHash2Map<T, U>::left_const_iterator end = container.leftConstEnd();
out.nospace() << "KHash2Map(";
for (; it != end; ++it)
out << "(" << it.key() << " <=> " << it.value() << ") ";
out << ")";
return out;
}
#endif
|