sortdict.h 17.8 KB
Newer Older
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1 2
/******************************************************************************
 *
3
 * 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
4 5
 *
 *
Dimitri van Heesch's avatar
Dimitri van Heesch committed
6
 * Copyright (C) 1997-2014 by Dimitri van Heesch.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */

#ifndef _SORTDICT_H
#define _SORTDICT_H

#include <qlist.h>
#include <qdict.h>
24
#include <qintdict.h>
Dimitri van Heesch's avatar
Dimitri van Heesch committed
25

Dimitri van Heesch's avatar
Dimitri van Heesch committed
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
#define AUTORESIZE 1

#if AUTORESIZE
const uint SDict_primes[] = 
{
  17,
  29,
  47,
  71,
  113,
  179,
  293,
  457,
  733,
  1171,
  1871,
  2999,
  4787,
  7669,
  12251,
  19603,
  31379,
  50177,
  80287,
  128449,
  205519,
  328829,
  526139,
  841801,
  1346881,
  2155007,
  3448033,
  5516827,
  8826919,
  14123059,
61 62 63 64 65 66 67 68
  23538433,
  39230771,
  65384537,
  108974231,
  181623707,
  302706181,
  504510283,
  840850487,
Dimitri van Heesch's avatar
Dimitri van Heesch committed
69 70 71 72
  0xffffffff
};
#endif

73
template<class T> class SDict;
74
template<class T> class SIntDict;
75

76
/** internal wrapper class that redirects compareValues() to the 
77 78 79 80 81 82 83
 *  dictionary 
 */
template<class T>
class SList : public QList<T>
{
  public:
    SList(SDict<T> *owner) : m_owner(owner) {}
84
    virtual ~SList() {}
85
    int compareValues(const T *item1,const T *item2) const
86
    {
87
      return m_owner->compareValues(item1,item2);
88 89 90 91 92
    }
  private:
    SDict<T> *m_owner;  
};

Dimitri van Heesch's avatar
Dimitri van Heesch committed
93
/** Ordered dictionary of elements of type T. 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
94 95 96 97 98 99
 *  Internally uses a QList<T> and a QDict<T>.
 */
template<class T>
class SDict 
{
  private:
100
    SList<T> *m_list;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
101
    QDict<T> *m_dict;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
102
    int m_sizeIndex;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
103 104 105 106 107
    
  public:
    /*! Create an ordered dictionary.
     *  \param size The size of the dictionary. Should be a prime number for
     *              best distribution of elements.
108 109
     *  \param caseSensitive indicated whether the keys should be sorted
     *         in a case sensitive way.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
110
     */
111
    SDict(int size=17,bool caseSensitive=TRUE) : m_sizeIndex(0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
112
    {
113
      m_list = new SList<T>(this);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
114 115
#if AUTORESIZE
      while ((uint)size>SDict_primes[m_sizeIndex]) m_sizeIndex++;
116
      m_dict = new QDict<T>(SDict_primes[m_sizeIndex],caseSensitive);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
117
#else
118
      m_dict = new QDict<T>(size,caseSensitive);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
119
#endif
Dimitri van Heesch's avatar
Dimitri van Heesch committed
120
    }
121

Dimitri van Heesch's avatar
Dimitri van Heesch committed
122 123 124 125 126 127
    /*! Destroys the dictionary */
    virtual ~SDict() 
    {
      delete m_list;
      delete m_dict;
    }
128 129

    /*! Appends an element to the dictionary. The element is owned by the
Dimitri van Heesch's avatar
Dimitri van Heesch committed
130 131 132 133 134 135 136 137 138
     *  dictionary.
     *  \param key The unique key to use to quicky find the item later on.
     *  \param d The compound to add.
     *  \sa find()
     */
    void append(const char *key,const T *d)
    {
      m_list->append(d);
      m_dict->insert(key,d);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
139 140 141 142 143 144
#if AUTORESIZE
      if (m_dict->size()>SDict_primes[m_sizeIndex])
      {
        m_dict->resize(SDict_primes[++m_sizeIndex]);
      }
#endif
Dimitri van Heesch's avatar
Dimitri van Heesch committed
145
    }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    /*! Prepends an element to the dictionary. The element is owned by the
     *  dictionary.
     *  \param key The unique key to use to quicky find the item later on.
     *  \param d The compound to add.
     *  \sa find()
     */
    void prepend(const char *key,const T *d)
    {
      m_list->prepend(d);
      m_dict->insert(key,d);
#if AUTORESIZE
      if (m_dict->size()>SDict_primes[m_sizeIndex])
      {
        m_dict->resize(SDict_primes[++m_sizeIndex]);
      }
#endif
    }

Dimitri van Heesch's avatar
Dimitri van Heesch committed
165 166 167 168 169 170
    /*! Remove an item from the dictionary */
    bool remove(const char *key)
    {
      T *item = m_dict->take(key);
      return item ? m_list->remove(item) : FALSE;
    }
171

Dimitri van Heesch's avatar
Dimitri van Heesch committed
172 173 174 175 176 177 178 179 180 181 182 183
    /*! Take an item out of the dictionary without deleting it */
    T *take(const char *key)
    {
      T *item = m_dict->take(key);
      if (item)
      {
        int i = m_list->find(item);
        m_list->take(i);
      }
      return item;
    }

Dimitri van Heesch's avatar
Dimitri van Heesch committed
184 185 186 187 188 189 190 191
    /*! Sorts the members of the dictionary. First appending a number
     *  of members and then sorting them is faster (O(NlogN) than using 
     *  inSort() for each member (O(N^2)).
     */
    void sort()
    {
      m_list->sort();
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
192 193 194 195 196 197 198 199 200
    /*! Inserts a compound into the dictionary in a sorted way.
     *  \param key The unique key to use to quicky find the item later on.
     *  \param d The compound to add.
     *  \sa find()
     */
    void inSort(const char *key,const T *d)
    {
      m_list->inSort(d);
      m_dict->insert(key,d);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
201 202 203 204 205 206
#if AUTORESIZE
      if (m_dict->size()>SDict_primes[m_sizeIndex])
      {
        m_dict->resize(SDict_primes[++m_sizeIndex]);
      }
#endif
Dimitri van Heesch's avatar
Dimitri van Heesch committed
207
    }
208

Dimitri van Heesch's avatar
Dimitri van Heesch committed
209 210 211 212 213 214 215 216 217 218 219 220
    void insertAt(int i,const char *key,const T *d)
    {
      m_list->insert(i,d);
      m_dict->insert(key,d);
#if AUTORESIZE
      if (m_dict->size()>SDict_primes[m_sizeIndex])
      {
        m_dict->resize(SDict_primes[++m_sizeIndex]);
      }
#endif
    }

Dimitri van Heesch's avatar
Dimitri van Heesch committed
221 222 223 224 225
    /*! Indicates whether or not the dictionary owns its elements */
    void setAutoDelete(bool val)
    {
      m_list->setAutoDelete(val);
    }
226

Dimitri van Heesch's avatar
Dimitri van Heesch committed
227 228 229 230 231 232 233 234 235
    /*! Looks up a compound given its key. 
     *  \param key The key to identify this element.
     *  \return The requested compound or zero if it cannot be found.
     *  \sa append() 
     */
    T *find(const char *key)
    {
      return m_dict->find(key);
    }
236 237 238 239 240 241 242 243
    T *find(const QCString &key)
    {
      return m_dict->find(key);
    }
    T *find(const QString &key)
    {
      return m_dict->find(key);
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
244 245 246 247 248 249
    int findAt(const QCString &key)
    {
      T *item = find(key);
      if (item==0) return -1;
      return m_list->find(item);
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
250

251
    /*! Equavalent to find(). */
Dimitri van Heesch's avatar
Dimitri van Heesch committed
252
    T *operator[](const char *key) const
253 254 255 256
    {
      return m_dict->find(key);
    }

Dimitri van Heesch's avatar
Dimitri van Heesch committed
257 258 259 260 261
    /*! Returns the item at position \a i in the sorted dictionary */
    T *at(uint i)
    {
      return m_list->at(i);
    }
262

Dimitri van Heesch's avatar
Dimitri van Heesch committed
263 264 265 266
    /*! Function that is used to compare two items when sorting.
     *  Overload this to properly sort items.
     *  \sa inSort()
     */
267
    virtual int compareValues(const T *item1,const T *item2) const
Dimitri van Heesch's avatar
Dimitri van Heesch committed
268 269 270
    {
      return item1!=item2;
    }
271

Dimitri van Heesch's avatar
Dimitri van Heesch committed
272 273 274 275 276 277 278 279 280
    /*! Clears the dictionary. Will delete items if setAutoDelete() was
     *  set to \c TRUE.
     *  \sa setAutoDelete
     */
    void clear()
    {
      m_list->clear();
      m_dict->clear();
    }
281
    
Dimitri van Heesch's avatar
Dimitri van Heesch committed
282 283
    /*! Returns the number of items stored in the dictionary
     */
284
    int count() const
Dimitri van Heesch's avatar
Dimitri van Heesch committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    {
      return m_list->count();
    }

    class Iterator;         // first forward declare
    friend class Iterator;  // then make it a friend
    /*! Simple iterator for SDict. It iterates in the order in which the
     *  elements are stored.
     */
    class Iterator
    {
      public:
        /*! Create an iterator given the dictionary. */
        Iterator(const SDict<T> &dict)
        {
          m_li = new QListIterator<T>(*dict.m_list);
        }
302

Dimitri van Heesch's avatar
Dimitri van Heesch committed
303 304 305 306 307
        /*! Destroys the dictionary */
        virtual ~Iterator()
        {
          delete m_li;
        }
308

Dimitri van Heesch's avatar
Dimitri van Heesch committed
309 310 311 312 313
        /*! Set the iterator to the first element in the list. 
         *  \return The first compound, or zero if the list was empty. 
         */
        T *toFirst() const
        {
314 315
          return m_li->toFirst();
        }
316

317 318 319 320 321 322 323
        /*! Set the iterator to the last element in the list. 
         *  \return The first compound, or zero if the list was empty. 
         */
        T *toLast() const
        {
          return m_li->toLast();
        }
324

325 326 327 328 329
        /*! Returns the current compound */
        T *current() const
        {
          return m_li->current();
        }
330
        
331 332 333 334 335 336 337 338
        /*! Moves the iterator to the next element.
         *  \return the new "current" element, or zero if the iterator was
         *          already pointing at the last element.
         */
        T *operator++()
        {
          return m_li->operator++();
        }
339

340 341 342 343 344 345 346 347 348 349 350 351 352
        /*! Moves the iterator to the previous element.
         *  \return the new "current" element, or zero if the iterator was
         *          already pointing at the first element.
         */
        T *operator--()
        {
          return m_li->operator--();
        }

      private:
        QListIterator<T> *m_li;
    };

353 354
    class IteratorDict;         // first forward declare
    friend class IteratorDict;  // then make it a friend
Dimitri van Heesch's avatar
Dimitri van Heesch committed
355
    /*! Simple iterator for SDict. It iterates over the dictionary elements
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
     *  in an unsorted way, but does provide information about the element's key.
     */
    class IteratorDict
    {
      public:
        /*! Create an iterator given the dictionary. */
        IteratorDict(const SDict<T> &dict)
        {
          m_di = new QDictIterator<T>(*dict.m_dict);
        }

        /*! Destroys the dictionary */
        virtual ~IteratorDict()
        {
          delete m_di;
        }

        /*! Set the iterator to the first element in the list. 
         *  \return The first compound, or zero if the list was empty. 
         */
        T *toFirst() const
        {
          return m_di->toFirst();
        }

        /*! Set the iterator to the last element in the list. 
         *  \return The first compound, or zero if the list was empty. 
         */
        T *toLast() const
        {
          return m_di->toLast();
        }

        /*! Returns the current compound */
        T *current() const
        {
          return m_di->current();
        }
        
        /*! Returns the current key */
        QCString currentKey() const
        {
          return m_di->currentKey();
        }
        
        /*! Moves the iterator to the next element.
         *  \return the new "current" element, or zero if the iterator was
         *          already pointing at the last element.
         */
        T *operator++()
        {
          return m_di->operator++();
        }

        /*! Moves the iterator to the previous element.
         *  \return the new "current" element, or zero if the iterator was
         *          already pointing at the first element.
         */
        T *operator--()
        {
          return m_di->operator--();
        }

      private:
        QDictIterator<T> *m_di;
    };
422 423
};

424
/** internal wrapper class that redirects compareValues() to the 
425 426 427 428 429 430 431
 *  dictionary 
 */
template<class T>
class SIntList : public QList<T>
{
  public:
    SIntList(SIntDict<T> *owner) : m_owner(owner) {}
432
    virtual ~SIntList() {}
433 434
  private:
    int compareValues(const T *item1,const T *item2) const
435
    {
436
      return m_owner->compareValues(item1,item2);
437 438 439 440
    }
    SIntDict<T> *m_owner;  
};

Dimitri van Heesch's avatar
Dimitri van Heesch committed
441
/** Ordered dictionary of elements of type T. 
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
 *  Internally uses a QList<T> and a QIntDict<T>.
 */
template<class T>
class SIntDict 
{
  private:
    SIntList<T> *m_list;
    QIntDict<T> *m_dict;
    int m_sizeIndex;
    
  public:
    /*! Create an ordered dictionary.
     *  \param size The size of the dictionary. Should be a prime number for
     *              best distribution of elements.
     */
457
    SIntDict(int size=17) : m_sizeIndex(0)
458 459 460 461 462 463 464 465 466
    {
      m_list = new SIntList<T>(this);
#if AUTORESIZE
      while ((uint)size>SDict_primes[m_sizeIndex]) m_sizeIndex++;
      m_dict = new QIntDict<T>(SDict_primes[m_sizeIndex]);
#else
      m_dict = new QIntDict<T>(size);
#endif
    }
467

468 469 470 471 472 473
    /*! Destroys the dictionary */
    virtual ~SIntDict() 
    {
      delete m_list;
      delete m_dict;
    }
474

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    /*! Appends a compound to the dictionary. The element is owned by the
     *  dictionary.
     *  \param key The unique key to use to quicky find the item later on.
     *  \param d The compound to add.
     *  \sa find()
     */
    void append(int key,const T *d)
    {
      m_list->append(d);
      m_dict->insert(key,d);
#if AUTORESIZE
      if (m_dict->size()>SDict_primes[m_sizeIndex])
      {
        m_dict->resize(SDict_primes[++m_sizeIndex]);
      }
#endif
    }
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

    /*! Prepend a compound to the dictionary. The element is owned by the
     *  dictionary.
     *  \param key The unique key to use to quicky find the item later on.
     *  \param d The compound to add.
     *  \sa find()
     */
    void prepend(int key,const T *d)
    {
      m_list->prepend(d);
      m_dict->insert(key,d);
#if AUTORESIZE
      if (m_dict->size()>SDict_primes[m_sizeIndex])
      {
        m_dict->resize(SDict_primes[++m_sizeIndex]);
      }
#endif
    }

511 512 513 514 515 516
    /*! Remove an item from the dictionary */
    bool remove(int key)
    {
      T *item = m_dict->take(key);
      return item ? m_list->remove(item) : FALSE;
    }
517

518 519 520 521 522 523 524 525
    /*! Sorts the members of the dictionary. First appending a number
     *  of members and then sorting them is faster (O(NlogN) than using 
     *  inSort() for each member (O(N^2)).
     */
    void sort()
    {
      m_list->sort();
    }
526

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    /*! Inserts a compound into the dictionary in a sorted way.
     *  \param key The unique key to use to quicky find the item later on.
     *  \param d The compound to add.
     *  \sa find()
     */
    void inSort(int key,const T *d)
    {
      m_list->inSort(d);
      m_dict->insert(key,d);
#if AUTORESIZE
      if (m_dict->size()>SDict_primes[m_sizeIndex])
      {
        m_dict->resize(SDict_primes[++m_sizeIndex]);
      }
#endif
    }
543

544 545 546 547 548
    /*! Indicates whether or not the dictionary owns its elements */
    void setAutoDelete(bool val)
    {
      m_list->setAutoDelete(val);
    }
549

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
    /*! Looks up a compound given its key. 
     *  \param key The key to identify this element.
     *  \return The requested compound or zero if it cannot be found.
     *  \sa append() 
     */
    T *find(int key)
    {
      return m_dict->find(key);
    }

    /*! Equavalent to find(). */
    T *operator[](int key) const
    {
      return m_dict->find(key);
    }

    /*! Returns the item at position \a i in the sorted dictionary */
    T *at(uint i)
    {
      return m_list->at(i);
    }
571

572 573 574 575
    /*! Function that is used to compare two items when sorting.
     *  Overload this to properly sort items.
     *  \sa inSort()
     */
576
    virtual int compareValues(const T *item1,const T *item2) const
577 578 579
    {
      return item1!=item2;
    }
580

581 582 583 584 585 586 587 588 589
    /*! Clears the dictionary. Will delete items if setAutoDelete() was
     *  set to \c TRUE.
     *  \sa setAutoDelete
     */
    void clear()
    {
      m_list->clear();
      m_dict->clear();
    }
590

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    /*! Returns the number of items stored in the dictionary
     */
    int count()
    {
      return m_list->count();
    }

    class Iterator;         // first forward declare
    friend class Iterator;  // then make it a friend
    /*! Simple iterator for SDict. It iterates in the order in which the
     *  elements are stored.
     */
    class Iterator
    {
      public:
        /*! Create an iterator given the dictionary. */
        Iterator(const SIntDict<T> &dict)
        {
          m_li = new QListIterator<T>(*dict.m_list);
        }
611
        
612 613 614 615 616
        /*! Destroys the dictionary */
        virtual ~Iterator()
        {
          delete m_li;
        }
617
        
618 619 620 621 622
        /*! Set the iterator to the first element in the list. 
         *  \return The first compound, or zero if the list was empty. 
         */
        T *toFirst() const
        {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
623 624
          return m_li->toFirst();
        }
625
        
Dimitri van Heesch's avatar
Dimitri van Heesch committed
626 627 628 629 630 631 632
        /*! Set the iterator to the last element in the list. 
         *  \return The first compound, or zero if the list was empty. 
         */
        T *toLast() const
        {
          return m_li->toLast();
        }
633
        
Dimitri van Heesch's avatar
Dimitri van Heesch committed
634 635 636 637 638
        /*! Returns the current compound */
        T *current() const
        {
          return m_li->current();
        }
639

Dimitri van Heesch's avatar
Dimitri van Heesch committed
640 641 642 643 644 645 646 647
        /*! Moves the iterator to the next element.
         *  \return the new "current" element, or zero if the iterator was
         *          already pointing at the last element.
         */
        T *operator++()
        {
          return m_li->operator++();
        }
648
        
Dimitri van Heesch's avatar
Dimitri van Heesch committed
649 650 651 652 653 654 655 656
        /*! Moves the iterator to the previous element.
         *  \return the new "current" element, or zero if the iterator was
         *          already pointing at the first element.
         */
        T *operator--()
        {
          return m_li->operator--();
        }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
657 658 659 660 661

      private:
        QListIterator<T> *m_li;
    };

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    class IteratorDict;         // first forward declare
    friend class IteratorDict;  // then make it a friend
    /*! Simple iterator for SDict. It iterates over the dictionary elements
     *  in an unsorted way, but does provide information about the element's key.
     */
    class IteratorDict
    {
      public:
        /*! Create an iterator given the dictionary. */
        IteratorDict(const SIntDict<T> &dict)
        {
          m_di = new QIntDictIterator<T>(*dict.m_dict);
        }

        /*! Destroys the dictionary */
        virtual ~IteratorDict()
        {
          delete m_di;
        }

        /*! Set the iterator to the first element in the list. 
         *  \return The first compound, or zero if the list was empty. 
         */
        T *toFirst() const
        {
          return m_di->toFirst();
        }

        /*! Set the iterator to the last element in the list. 
         *  \return The first compound, or zero if the list was empty. 
         */
        T *toLast() const
        {
          return m_di->toLast();
        }

        /*! Returns the current compound */
        T *current() const
        {
          return m_di->current();
        }
        
        /*! Returns the current key */
        int currentKey() const
        {
          return m_di->currentKey();
        }
        
        /*! Moves the iterator to the next element.
         *  \return the new "current" element, or zero if the iterator was
         *          already pointing at the last element.
         */
        T *operator++()
        {
          return m_di->operator++();
        }

        /*! Moves the iterator to the previous element.
         *  \return the new "current" element, or zero if the iterator was
         *          already pointing at the first element.
         */
        T *operator--()
        {
          return m_di->operator--();
        }

      private:
        QDictIterator<T> *m_di;
    };

Dimitri van Heesch's avatar
Dimitri van Heesch committed
732 733 734
};

#endif