[Qt-interest] "Double Sided Map"?
KC Jones
kc at asperasoft.com
Wed Feb 10 01:43:28 CET 2010
Both QMap and QHash have the method:
QList<Key> keys ( const T & value ) const
That you could use for the inverse access.
Neither class has any support for enforcing unique values, so the inverse
access will not be inherently unique. But you could conceivably add that in
a derived class. And the keys() method is guaranteed to be slow.
But if you do derive an invertible map class, I'd recommend using two QHash
collections internally. It would be easy enough to guarantee coherence of
the two collections if they were private to your two-way map. And you
wouldn't have to worry about the performance of the inverse access.
On Tue, Feb 9, 2010 at 3:37 PM, Jefferson Bandeira <jbsilva at ufrj.br> wrote:
> Hello guys, i hope you can answer my question.
> Actually in my application I need to map some values to Strings, however, I
> also need to make a "reverse mapping", i mean, i want to be able to see
> which QString is mapped to the number 3, but i also need to know which
> Number the QString "three" is mapped to.
> At the moment i'm doing something like this :
> ----------------------------------------------
> QMap<int, QString> i2string;
> QMap<QString, int> string2i;
> i2string[3] = QString("three");
> string2i["three"] = 3;
> ----------------------------------------------
> This indeed works for what i need, however, that's a bit messy, since any
> changes i make to one of the maps i must do to the other and this is kinda
> dangerous, since someone can easily forget about this, or the maps can be
> Desynched.
> So, My question is :
> Is there any Structure i can use in which i can insert the pair of things
> once, and i can query the structure to give me what i want, both ways?
> Something like :
> MagicStructure<int, QString> struct;
> struct.insert(3, QString("three"));
> struct.get(3); // Returns "three"
> struct.get("three"); // Returns 3
> PS : I can ensure that both values are unique through all structure, so
> that won't be a problem.
> Regards,
> Jefferson Bandeira
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100209/3f8b7bb6/attachment.html
More information about the Qt-interest-old
mailing list