Blame src/sortdict.h

Packit Service 50c9f2
/******************************************************************************
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * 
Packit Service 50c9f2
 *
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Permission to use, copy, modify, and distribute this software and its
Packit Service 50c9f2
 * documentation under the terms of the GNU General Public License is hereby 
Packit Service 50c9f2
 * granted. No representations are made about the suitability of this software 
Packit Service 50c9f2
 * for any purpose. It is provided "as is" without express or implied warranty.
Packit Service 50c9f2
 * See the GNU General Public License for more details.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Documents produced by Doxygen are derivative works derived from the
Packit Service 50c9f2
 * input used in their production; they are not affected by this license.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 */
Packit Service 50c9f2
Packit Service 50c9f2
#ifndef _SORTDICT_H
Packit Service 50c9f2
#define _SORTDICT_H
Packit Service 50c9f2
Packit Service 50c9f2
#include <qlist.h>
Packit Service 50c9f2
#include <qdict.h>
Packit Service 50c9f2
#include <qintdict.h>
Packit Service 50c9f2
Packit Service 50c9f2
#define AUTORESIZE 1
Packit Service 50c9f2
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
const uint SDict_primes[] = 
Packit Service 50c9f2
{
Packit Service 50c9f2
  17,
Packit Service 50c9f2
  29,
Packit Service 50c9f2
  47,
Packit Service 50c9f2
  71,
Packit Service 50c9f2
  113,
Packit Service 50c9f2
  179,
Packit Service 50c9f2
  293,
Packit Service 50c9f2
  457,
Packit Service 50c9f2
  733,
Packit Service 50c9f2
  1171,
Packit Service 50c9f2
  1871,
Packit Service 50c9f2
  2999,
Packit Service 50c9f2
  4787,
Packit Service 50c9f2
  7669,
Packit Service 50c9f2
  12251,
Packit Service 50c9f2
  19603,
Packit Service 50c9f2
  31379,
Packit Service 50c9f2
  50177,
Packit Service 50c9f2
  80287,
Packit Service 50c9f2
  128449,
Packit Service 50c9f2
  205519,
Packit Service 50c9f2
  328829,
Packit Service 50c9f2
  526139,
Packit Service 50c9f2
  841801,
Packit Service 50c9f2
  1346881,
Packit Service 50c9f2
  2155007,
Packit Service 50c9f2
  3448033,
Packit Service 50c9f2
  5516827,
Packit Service 50c9f2
  8826919,
Packit Service 50c9f2
  14123059,
Packit Service 50c9f2
  23538433,
Packit Service 50c9f2
  39230771,
Packit Service 50c9f2
  65384537,
Packit Service 50c9f2
  108974231,
Packit Service 50c9f2
  181623707,
Packit Service 50c9f2
  302706181,
Packit Service 50c9f2
  504510283,
Packit Service 50c9f2
  840850487,
Packit Service 50c9f2
  0xffffffff
Packit Service 50c9f2
};
Packit Service 50c9f2
#endif
Packit Service 50c9f2
Packit Service 50c9f2
template<class T> class SDict;
Packit Service 50c9f2
template<class T> class SIntDict;
Packit Service 50c9f2
Packit Service 50c9f2
/** internal wrapper class that redirects compareValues() to the 
Packit Service 50c9f2
 *  dictionary 
Packit Service 50c9f2
 */
Packit Service 50c9f2
template<class T>
Packit Service 50c9f2
class SList : public QList<T>
Packit Service 50c9f2
{
Packit Service 50c9f2
  public:
Packit Service 50c9f2
    SList(SDict<T> *owner) : m_owner(owner) {}
Packit Service 50c9f2
    virtual ~SList() {}
Packit Service 50c9f2
    int compareValues(const T *item1,const T *item2) const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_owner->compareValues(item1,item2);
Packit Service 50c9f2
    }
Packit Service 50c9f2
  private:
Packit Service 50c9f2
    SDict<T> *m_owner;  
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
/** Ordered dictionary of elements of type T. 
Packit Service 50c9f2
 *  Internally uses a QList<T> and a QDict<T>.
Packit Service 50c9f2
 */
Packit Service 50c9f2
template<class T>
Packit Service 50c9f2
class SDict 
Packit Service 50c9f2
{
Packit Service 50c9f2
  private:
Packit Service 50c9f2
    SList<T> *m_list;
Packit Service 50c9f2
    QDict<T> *m_dict;
Packit Service 50c9f2
    int m_sizeIndex;
Packit Service 50c9f2
    
Packit Service 50c9f2
  public:
Packit Service 50c9f2
    /*! Create an ordered dictionary.
Packit Service 50c9f2
     *  \param size The size of the dictionary. Should be a prime number for
Packit Service 50c9f2
     *              best distribution of elements.
Packit Service 50c9f2
     *  \param caseSensitive indicated whether the keys should be sorted
Packit Service 50c9f2
     *         in a case sensitive way.
Packit Service 50c9f2
     */
Packit Service 50c9f2
    SDict(int size=17,bool caseSensitive=TRUE) : m_sizeIndex(0)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list = new SList<T>(this);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      while ((uint)size>SDict_primes[m_sizeIndex]) m_sizeIndex++;
Packit Service 50c9f2
      m_dict = new QDict<T>(SDict_primes[m_sizeIndex],caseSensitive);
Packit Service 50c9f2
#else
Packit Service 50c9f2
      m_dict = new QDict<T>(size,caseSensitive);
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Destroys the dictionary */
Packit Service 50c9f2
    virtual ~SDict() 
Packit Service 50c9f2
    {
Packit Service 50c9f2
      delete m_list;
Packit Service 50c9f2
      delete m_dict;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Appends an element to the dictionary. The element is owned by the
Packit Service 50c9f2
     *  dictionary.
Packit Service 50c9f2
     *  \param key The unique key to use to quicky find the item later on.
Packit Service 50c9f2
     *  \param d The compound to add.
Packit Service 50c9f2
     *  \sa find()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void append(const char *key,const T *d)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->append(d);
Packit Service 50c9f2
      m_dict->insert(key,d);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      if (m_dict->size()>SDict_primes[m_sizeIndex])
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_dict->resize(SDict_primes[++m_sizeIndex]);
Packit Service 50c9f2
      }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Prepends an element to the dictionary. The element is owned by the
Packit Service 50c9f2
     *  dictionary.
Packit Service 50c9f2
     *  \param key The unique key to use to quicky find the item later on.
Packit Service 50c9f2
     *  \param d The compound to add.
Packit Service 50c9f2
     *  \sa find()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void prepend(const char *key,const T *d)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->prepend(d);
Packit Service 50c9f2
      m_dict->insert(key,d);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      if (m_dict->size()>SDict_primes[m_sizeIndex])
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_dict->resize(SDict_primes[++m_sizeIndex]);
Packit Service 50c9f2
      }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Remove an item from the dictionary */
Packit Service 50c9f2
    bool remove(const char *key)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      T *item = m_dict->take(key);
Packit Service 50c9f2
      return item ? m_list->remove(item) : FALSE;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Take an item out of the dictionary without deleting it */
Packit Service 50c9f2
    T *take(const char *key)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      T *item = m_dict->take(key);
Packit Service 50c9f2
      if (item)
Packit Service 50c9f2
      {
Packit Service 50c9f2
        int i = m_list->find(item);
Packit Service 50c9f2
        m_list->take(i);
Packit Service 50c9f2
      }
Packit Service 50c9f2
      return item;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Sorts the members of the dictionary. First appending a number
Packit Service 50c9f2
     *  of members and then sorting them is faster (O(NlogN) than using 
Packit Service 50c9f2
     *  inSort() for each member (O(N^2)).
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void sort()
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->sort();
Packit Service 50c9f2
    }
Packit Service 50c9f2
    /*! Inserts a compound into the dictionary in a sorted way.
Packit Service 50c9f2
     *  \param key The unique key to use to quicky find the item later on.
Packit Service 50c9f2
     *  \param d The compound to add.
Packit Service 50c9f2
     *  \sa find()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void inSort(const char *key,const T *d)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->inSort(d);
Packit Service 50c9f2
      m_dict->insert(key,d);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      if (m_dict->size()>SDict_primes[m_sizeIndex])
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_dict->resize(SDict_primes[++m_sizeIndex]);
Packit Service 50c9f2
      }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    void insertAt(int i,const char *key,const T *d)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->insert(i,d);
Packit Service 50c9f2
      m_dict->insert(key,d);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      if (m_dict->size()>SDict_primes[m_sizeIndex])
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_dict->resize(SDict_primes[++m_sizeIndex]);
Packit Service 50c9f2
      }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Indicates whether or not the dictionary owns its elements */
Packit Service 50c9f2
    void setAutoDelete(bool val)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->setAutoDelete(val);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Looks up a compound given its key. 
Packit Service 50c9f2
     *  \param key The key to identify this element.
Packit Service 50c9f2
     *  \return The requested compound or zero if it cannot be found.
Packit Service 50c9f2
     *  \sa append() 
Packit Service 50c9f2
     */
Packit Service 50c9f2
    T *find(const char *key)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_dict->find(key);
Packit Service 50c9f2
    }
Packit Service 50c9f2
    T *find(const QCString &key)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_dict->find(key);
Packit Service 50c9f2
    }
Packit Service 50c9f2
    T *find(const QString &key)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_dict->find(key);
Packit Service 50c9f2
    }
Packit Service 50c9f2
    int findAt(const QCString &key)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      T *item = find(key);
Packit Service 50c9f2
      if (item==0) return -1;
Packit Service 50c9f2
      return m_list->find(item);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Equavalent to find(). */
Packit Service 50c9f2
    T *operator[](const char *key) const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_dict->find(key);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Returns the item at position \a i in the sorted dictionary */
Packit Service 50c9f2
    T *at(uint i)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_list->at(i);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Function that is used to compare two items when sorting.
Packit Service 50c9f2
     *  Overload this to properly sort items.
Packit Service 50c9f2
     *  \sa inSort()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    virtual int compareValues(const T *item1,const T *item2) const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return item1!=item2;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Clears the dictionary. Will delete items if setAutoDelete() was
Packit Service 50c9f2
     *  set to \c TRUE.
Packit Service 50c9f2
     *  \sa setAutoDelete
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void clear()
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->clear();
Packit Service 50c9f2
      m_dict->clear();
Packit Service 50c9f2
    }
Packit Service 50c9f2
    
Packit Service 50c9f2
    /*! Returns the number of items stored in the dictionary
Packit Service 50c9f2
     */
Packit Service 50c9f2
    int count() const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_list->count();
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    class Iterator;         // first forward declare
Packit Service 50c9f2
    friend class Iterator;  // then make it a friend
Packit Service 50c9f2
    /*! Simple iterator for SDict. It iterates in the order in which the
Packit Service 50c9f2
     *  elements are stored.
Packit Service 50c9f2
     */
Packit Service 50c9f2
    class Iterator
Packit Service 50c9f2
    {
Packit Service 50c9f2
      public:
Packit Service 50c9f2
        /*! Create an iterator given the dictionary. */
Packit Service 50c9f2
        Iterator(const SDict<T> &dict)
Packit Service 50c9f2
        {
Packit Service 50c9f2
          m_li = new QListIterator<T>(*dict.m_list);
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Destroys the dictionary */
Packit Service 50c9f2
        virtual ~Iterator()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          delete m_li;
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Set the iterator to the first element in the list. 
Packit Service 50c9f2
         *  \return The first compound, or zero if the list was empty. 
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *toFirst() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->toFirst();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Set the iterator to the last element in the list. 
Packit Service 50c9f2
         *  \return The first compound, or zero if the list was empty. 
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *toLast() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->toLast();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Returns the current compound */
Packit Service 50c9f2
        T *current() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->current();
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Moves the iterator to the next element.
Packit Service 50c9f2
         *  \return the new "current" element, or zero if the iterator was
Packit Service 50c9f2
         *          already pointing at the last element.
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *operator++()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->operator++();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Moves the iterator to the previous element.
Packit Service 50c9f2
         *  \return the new "current" element, or zero if the iterator was
Packit Service 50c9f2
         *          already pointing at the first element.
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *operator--()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->operator--();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
      private:
Packit Service 50c9f2
        QListIterator<T> *m_li;
Packit Service 50c9f2
    };
Packit Service 50c9f2
Packit Service 50c9f2
    class IteratorDict;         // first forward declare
Packit Service 50c9f2
    friend class IteratorDict;  // then make it a friend
Packit Service 50c9f2
    /*! Simple iterator for SDict. It iterates over the dictionary elements
Packit Service 50c9f2
     *  in an unsorted way, but does provide information about the element's key.
Packit Service 50c9f2
     */
Packit Service 50c9f2
    class IteratorDict
Packit Service 50c9f2
    {
Packit Service 50c9f2
      public:
Packit Service 50c9f2
        /*! Create an iterator given the dictionary. */
Packit Service 50c9f2
        IteratorDict(const SDict<T> &dict)
Packit Service 50c9f2
        {
Packit Service 50c9f2
          m_di = new QDictIterator<T>(*dict.m_dict);
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Destroys the dictionary */
Packit Service 50c9f2
        virtual ~IteratorDict()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          delete m_di;
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Set the iterator to the first element in the list. 
Packit Service 50c9f2
         *  \return The first compound, or zero if the list was empty. 
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *toFirst() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->toFirst();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Set the iterator to the last element in the list. 
Packit Service 50c9f2
         *  \return The first compound, or zero if the list was empty. 
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *toLast() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->toLast();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Returns the current compound */
Packit Service 50c9f2
        T *current() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->current();
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Returns the current key */
Packit Service 50c9f2
        QCString currentKey() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->currentKey();
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Moves the iterator to the next element.
Packit Service 50c9f2
         *  \return the new "current" element, or zero if the iterator was
Packit Service 50c9f2
         *          already pointing at the last element.
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *operator++()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->operator++();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Moves the iterator to the previous element.
Packit Service 50c9f2
         *  \return the new "current" element, or zero if the iterator was
Packit Service 50c9f2
         *          already pointing at the first element.
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *operator--()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->operator--();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
      private:
Packit Service 50c9f2
        QDictIterator<T> *m_di;
Packit Service 50c9f2
    };
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
/** internal wrapper class that redirects compareValues() to the 
Packit Service 50c9f2
 *  dictionary 
Packit Service 50c9f2
 */
Packit Service 50c9f2
template<class T>
Packit Service 50c9f2
class SIntList : public QList<T>
Packit Service 50c9f2
{
Packit Service 50c9f2
  public:
Packit Service 50c9f2
    SIntList(SIntDict<T> *owner) : m_owner(owner) {}
Packit Service 50c9f2
    virtual ~SIntList() {}
Packit Service 50c9f2
  private:
Packit Service 50c9f2
    int compareValues(const T *item1,const T *item2) const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_owner->compareValues(item1,item2);
Packit Service 50c9f2
    }
Packit Service 50c9f2
    SIntDict<T> *m_owner;  
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
/** Ordered dictionary of elements of type T. 
Packit Service 50c9f2
 *  Internally uses a QList<T> and a QIntDict<T>.
Packit Service 50c9f2
 */
Packit Service 50c9f2
template<class T>
Packit Service 50c9f2
class SIntDict 
Packit Service 50c9f2
{
Packit Service 50c9f2
  private:
Packit Service 50c9f2
    SIntList<T> *m_list;
Packit Service 50c9f2
    QIntDict<T> *m_dict;
Packit Service 50c9f2
    int m_sizeIndex;
Packit Service 50c9f2
    
Packit Service 50c9f2
  public:
Packit Service 50c9f2
    /*! Create an ordered dictionary.
Packit Service 50c9f2
     *  \param size The size of the dictionary. Should be a prime number for
Packit Service 50c9f2
     *              best distribution of elements.
Packit Service 50c9f2
     */
Packit Service 50c9f2
    SIntDict(int size=17) : m_sizeIndex(0)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list = new SIntList<T>(this);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      while ((uint)size>SDict_primes[m_sizeIndex]) m_sizeIndex++;
Packit Service 50c9f2
      m_dict = new QIntDict<T>(SDict_primes[m_sizeIndex]);
Packit Service 50c9f2
#else
Packit Service 50c9f2
      m_dict = new QIntDict<T>(size);
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Destroys the dictionary */
Packit Service 50c9f2
    virtual ~SIntDict() 
Packit Service 50c9f2
    {
Packit Service 50c9f2
      delete m_list;
Packit Service 50c9f2
      delete m_dict;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Appends a compound to the dictionary. The element is owned by the
Packit Service 50c9f2
     *  dictionary.
Packit Service 50c9f2
     *  \param key The unique key to use to quicky find the item later on.
Packit Service 50c9f2
     *  \param d The compound to add.
Packit Service 50c9f2
     *  \sa find()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void append(int key,const T *d)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->append(d);
Packit Service 50c9f2
      m_dict->insert(key,d);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      if (m_dict->size()>SDict_primes[m_sizeIndex])
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_dict->resize(SDict_primes[++m_sizeIndex]);
Packit Service 50c9f2
      }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Prepend a compound to the dictionary. The element is owned by the
Packit Service 50c9f2
     *  dictionary.
Packit Service 50c9f2
     *  \param key The unique key to use to quicky find the item later on.
Packit Service 50c9f2
     *  \param d The compound to add.
Packit Service 50c9f2
     *  \sa find()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void prepend(int key,const T *d)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->prepend(d);
Packit Service 50c9f2
      m_dict->insert(key,d);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      if (m_dict->size()>SDict_primes[m_sizeIndex])
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_dict->resize(SDict_primes[++m_sizeIndex]);
Packit Service 50c9f2
      }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Remove an item from the dictionary */
Packit Service 50c9f2
    bool remove(int key)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      T *item = m_dict->take(key);
Packit Service 50c9f2
      return item ? m_list->remove(item) : FALSE;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Sorts the members of the dictionary. First appending a number
Packit Service 50c9f2
     *  of members and then sorting them is faster (O(NlogN) than using 
Packit Service 50c9f2
     *  inSort() for each member (O(N^2)).
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void sort()
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->sort();
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Inserts a compound into the dictionary in a sorted way.
Packit Service 50c9f2
     *  \param key The unique key to use to quicky find the item later on.
Packit Service 50c9f2
     *  \param d The compound to add.
Packit Service 50c9f2
     *  \sa find()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void inSort(int key,const T *d)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->inSort(d);
Packit Service 50c9f2
      m_dict->insert(key,d);
Packit Service 50c9f2
#if AUTORESIZE
Packit Service 50c9f2
      if (m_dict->size()>SDict_primes[m_sizeIndex])
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_dict->resize(SDict_primes[++m_sizeIndex]);
Packit Service 50c9f2
      }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Indicates whether or not the dictionary owns its elements */
Packit Service 50c9f2
    void setAutoDelete(bool val)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->setAutoDelete(val);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Looks up a compound given its key. 
Packit Service 50c9f2
     *  \param key The key to identify this element.
Packit Service 50c9f2
     *  \return The requested compound or zero if it cannot be found.
Packit Service 50c9f2
     *  \sa append() 
Packit Service 50c9f2
     */
Packit Service 50c9f2
    T *find(int key)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_dict->find(key);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Equavalent to find(). */
Packit Service 50c9f2
    T *operator[](int key) const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_dict->find(key);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Returns the item at position \a i in the sorted dictionary */
Packit Service 50c9f2
    T *at(uint i)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_list->at(i);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Function that is used to compare two items when sorting.
Packit Service 50c9f2
     *  Overload this to properly sort items.
Packit Service 50c9f2
     *  \sa inSort()
Packit Service 50c9f2
     */
Packit Service 50c9f2
    virtual int compareValues(const T *item1,const T *item2) const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return item1!=item2;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Clears the dictionary. Will delete items if setAutoDelete() was
Packit Service 50c9f2
     *  set to \c TRUE.
Packit Service 50c9f2
     *  \sa setAutoDelete
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void clear()
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_list->clear();
Packit Service 50c9f2
      m_dict->clear();
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /*! Returns the number of items stored in the dictionary
Packit Service 50c9f2
     */
Packit Service 50c9f2
    int count() const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_list->count();
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    class Iterator;         // first forward declare
Packit Service 50c9f2
    friend class Iterator;  // then make it a friend
Packit Service 50c9f2
    /*! Simple iterator for SDict. It iterates in the order in which the
Packit Service 50c9f2
     *  elements are stored.
Packit Service 50c9f2
     */
Packit Service 50c9f2
    class Iterator
Packit Service 50c9f2
    {
Packit Service 50c9f2
      public:
Packit Service 50c9f2
        /*! Create an iterator given the dictionary. */
Packit Service 50c9f2
        Iterator(const SIntDict<T> &dict)
Packit Service 50c9f2
        {
Packit Service 50c9f2
          m_li = new QListIterator<T>(*dict.m_list);
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Destroys the dictionary */
Packit Service 50c9f2
        virtual ~Iterator()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          delete m_li;
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Set the iterator to the first element in the list. 
Packit Service 50c9f2
         *  \return The first compound, or zero if the list was empty. 
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *toFirst() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->toFirst();
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Set the iterator to the last element in the list. 
Packit Service 50c9f2
         *  \return The first compound, or zero if the list was empty. 
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *toLast() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->toLast();
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Returns the current compound */
Packit Service 50c9f2
        T *current() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->current();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Moves the iterator to the next element.
Packit Service 50c9f2
         *  \return the new "current" element, or zero if the iterator was
Packit Service 50c9f2
         *          already pointing at the last element.
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *operator++()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->operator++();
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Moves the iterator to the previous element.
Packit Service 50c9f2
         *  \return the new "current" element, or zero if the iterator was
Packit Service 50c9f2
         *          already pointing at the first element.
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *operator--()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_li->operator--();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
      private:
Packit Service 50c9f2
        QListIterator<T> *m_li;
Packit Service 50c9f2
    };
Packit Service 50c9f2
Packit Service 50c9f2
    class IteratorDict;         // first forward declare
Packit Service 50c9f2
    friend class IteratorDict;  // then make it a friend
Packit Service 50c9f2
    /*! Simple iterator for SDict. It iterates over the dictionary elements
Packit Service 50c9f2
     *  in an unsorted way, but does provide information about the element's key.
Packit Service 50c9f2
     */
Packit Service 50c9f2
    class IteratorDict
Packit Service 50c9f2
    {
Packit Service 50c9f2
      public:
Packit Service 50c9f2
        /*! Create an iterator given the dictionary. */
Packit Service 50c9f2
        IteratorDict(const SIntDict<T> &dict)
Packit Service 50c9f2
        {
Packit Service 50c9f2
          m_di = new QIntDictIterator<T>(*dict.m_dict);
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Destroys the dictionary */
Packit Service 50c9f2
        virtual ~IteratorDict()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          delete m_di;
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Set the iterator to the first element in the list. 
Packit Service 50c9f2
         *  \return The first compound, or zero if the list was empty. 
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *toFirst() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->toFirst();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Set the iterator to the last element in the list. 
Packit Service 50c9f2
         *  \return The first compound, or zero if the list was empty. 
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *toLast() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->toLast();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Returns the current compound */
Packit Service 50c9f2
        T *current() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->current();
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Returns the current key */
Packit Service 50c9f2
        int currentKey() const
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->currentKey();
Packit Service 50c9f2
        }
Packit Service 50c9f2
        
Packit Service 50c9f2
        /*! Moves the iterator to the next element.
Packit Service 50c9f2
         *  \return the new "current" element, or zero if the iterator was
Packit Service 50c9f2
         *          already pointing at the last element.
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *operator++()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->operator++();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
        /*! Moves the iterator to the previous element.
Packit Service 50c9f2
         *  \return the new "current" element, or zero if the iterator was
Packit Service 50c9f2
         *          already pointing at the first element.
Packit Service 50c9f2
         */
Packit Service 50c9f2
        T *operator--()
Packit Service 50c9f2
        {
Packit Service 50c9f2
          return m_di->operator--();
Packit Service 50c9f2
        }
Packit Service 50c9f2
Packit Service 50c9f2
      private:
Packit Service 50c9f2
        QDictIterator<T> *m_di;
Packit Service 50c9f2
    };
Packit Service 50c9f2
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
#endif