2

I'd like to have two structures which point to eachother. Specifically, I'd like to have the following:

template<typename Key, typename Value> 
class MyStructure {
public:
  typedef map<Key, list<pair<Value, typename ListType::iterator>>> MapType;
  typedef list<typename MapType::element_type::iterator> ListType;
private:
  MapType map_;
  ListType list_;
}

Obviously this will not work since ListType is not declared previously as a type. How could I do this? As you can see I am using the iterator types as pointers to these two structures' elements.

I was thinking of using the curiously recurring template pattern, but could not get anywhere with that. Someone on ##c++ also suggested using template aliases, but that failed as well (at least, I don't know how to use this idea).

Am I doing something wrong, conceptually? Or perhaps "not in line with C++ concepts"? I could certainly do this with void*s, but I'm trying to make things The Right Way :)

Thanks!

Federico Lebrón
  • 1,772
  • 1
  • 11
  • 10
  • 5
    That data structure is nonsensical to me. And I mean more nonsensical than using `list` is most of the time already. (What are you actually trying to accomplish with this data structure?) – Billy ONeal Mar 08 '12 at 07:59
  • 4
    Is this a [duplicate](http://stackoverflow.com/q/9595420/777186)? – jogojapan Mar 08 '12 at 08:05
  • I am trying to have a map that, given a key, has a list of values. These values also have a representation in a list, which I use for other purposes. The purpose is to be able to iterate all of the map's elements, in insertion order, as opposed to the order the map stores them natively. EDIT: Billy: I've made a small change, perhaps the structure makes a bit more sense now? What a ListType element point to, now, is an element in some list in the map. – Federico Lebrón Mar 08 '12 at 08:11
  • You can refer the link provided by @jogojapan. What you ask is not possible. – iammilind Mar 08 '12 at 08:15
  • 3
    I'm not sure that this is a duplicate. It depends on exactly what @Frederico is trying to achieve, as opposed to how he thinks it could be technically achieved. – Cheers and hth. - Alf Mar 08 '12 at 08:16
  • What I was asked to attempt, was this: A multi-valued map (that is, ordinarily a map>), such that it can be iterated in insertion order. That is, if I set m[1].push_back(1), m[2].push_back(2), m[1].push_back(2), the iteration order should be [(1, 1), (2, 2), (1, 2)], not [(1, 1), (1, 2), (2, 2)], as it would be if I simply iterated each list in the map. The list is simply to remember the insertion order: I always push_back to it, and it holds pointers to the map's lists' elements. – Federico Lebrón Mar 08 '12 at 08:19
  • @iammilind: While interleaving the templates is not possible, starting from the problem and working out a new solution certainly is. All that matters is the complexity of said new solution. – Matthieu M. Mar 08 '12 at 08:27

2 Answers2

3

Although I suspected this might be a duplicate (and in many ways it is), "Cheers and hth. - Alf" is right in pointing out that the potential duplicate question was specifically about the use of typedef for this kind of thing.

However, in the present question, the OP would like to know how generally to deal with mutual inclusion in the scenario described in the question.

Here is a suggestion:

#include <list>
#include <map>

template <typename Key, typename Value>
class MyElement;

template <typename Key, typename Value>
class MyStructure
{
public:
  typedef std::map<Key,MyElement<Key,Value> > MapType;
  typedef std::list<MyElement<Key,Value> >    ListType;
};

template <typename Key, typename Value>
class MyElement {
public:
  typename MyStructure<Key,Value>::MapType::iterator  map_iterator;
  typename MyStructure<Key,Value>::ListType::iterator list_iterator;
};

As you can see, I introduced a new data type MyElement, which contains the list iterator as well as the map iterator. Because that is a class rather than a typedef, it can be forward-declared.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
  • 1
    @FedericoLebrón, if you find this answer useful, then you can accept it by clicking the right mark below it. – iammilind Mar 08 '12 at 09:00
0

This is unfortunately not possible, in the way you express it.

On the other hand, it seems to me that (given your requirements) you could reverse the storage.

What if you used a list of the Values, and then had the map point to this list ?

This would break the cyclic dependency.

typedef std::list< std::pair<Key, Value> > ListType;
typedef std::multiset<typename ListType::iterator, CmpFirst> MapType;

(Not sure if I really understood what you were trying to achieve though...)

Another solution you should probably consider is to see if you can express this in terms of Boost.MultiIndex. When several iterations/lookup patterns are necessary, Boost.MultiIndex is often a better alternative than handmade solution, as it interleaves the indexes within the values so as to minimize storage. Plus consistency (ie, having all indexes referencing the same set of elements) is a given.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722