- If the data structure is an array it's relatively easy - just refer to everything by index and keep a second array with the extra data (although even this gets hairy if the items ever move).
- Usually, the data structure I have this problem with is some kind of graph (often more constrained than that, say a DAG or a tree).
- Often, the main data structure will contain only static data (it gets loaded and then doesn't change after that), although I'd prefer solutions that don't rely on this.
How to non-intrusively attach extra data to a data structure?
This is a problem that I have hit several times, and I've never found a solution that I really like, so I'm wondering how other people deal with this situation.
It's quite a general problem: I have some data structure, and I would like to attach some extra information about the items in the data structure, without (visibly) affecting the original structure.
To make it slightly more concrete:
As a fallback, you can always make a hashmap that stores an association between the address of your existing data structure, and whatever extra data you want to record.
It might look vaguely like this:
It might look vaguely like this:
// these are the existing data structures, which you don't modifyPerson* Joe;Person* Henry;struct ExtraPersonData { bool isCool; ExtraPersonData(bool b) : isCool(b) {}};std::map<Person*, ExtraPersonData> extraDataPerPerson;extraDataPerPerson[Joe] = ExtraPersonData(true);extraDataPerPerson[Henry] = ExtraPersonData(false);
Quote:Original post by pinacolada
As a fallback, you can always make a hashmap that stores an association between the address of your existing data structure.
I've been wondering about that, but I was put off by the weight of the association data structure. However, I suspect a carefully implemented hash table is actually likely to be the best way to go.
Do you happen to know of any hash functions that are optimized for hashing pointers?
John B
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement