[Qt-interest] New container class needed in Qt

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Wed Oct 19 10:21:20 CEST 2011


> Why don't you just follow Scotts suggestion?

Who said I wasn't? :) My original question was whether Qt had some
some built-in class already which did what I wanted (maybe I
overlooked some container class).

>
>  U at(int i)
>  {
>    T t = m_list.at(i);
>    return m_hash.value(t);
>  }
>

The caller is going to use this class much like a map/hash. So to get
a value he/she will be using a 'key' and not an index.

A very rough sketch of an ordered map (I haven't taken care of other
map api's like deletion etc)

template <typename K, typename V>
class OrderedMap
{
public:
void insert(K key, V value)
{
    m_keyValues.insert(key, value);
    m_keyOrder << key;
}

V value(K key)
{
    return m_keyValues.value(key);
}

QList<K> keys()
{
    return m_keyOrder;
}

private:
QHash<K, V> m_keyValues;
QList<K> m_keyOrder;
}

So we keep track of insertion order of keys with the QList. A lot of
optimizations can be done on top of this (like what Roop suggested to
make it work efficiently with large data sets).

Thanks again for your suggestion.

-mandeep

>
> In other words: Create the container you crave by wrapping other containers.
> Don't litter your code with keeping the lists and hashes in sync all over
> the place. Do it in an encapsulation, just like you're looking for.
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>



More information about the Qt-interest-old mailing list