map to vector conversion inside class

J

John

I have a class which looks like this

template< typename K, typename I >
Class X {
typedef typename map<K,I> Tcontainer1;
typedef typename vector<K> Tcontainer2;

Tcontainer tmap;

public:
typedef typename Tcontainer1::iterator item;
typedef typename Tcontainer1::const_iterator citem;

public:
const K& key(item it);
void container_change(void);
....

}

What I would like to do is dynamically change the container
from the map to the vector when container_change() is initiated.
This means that I would have to change types at runtime! Any
idea how to do this? The problem is that the class is heavily
used and I would not like to change the interface of the class
if possible. Anything I could change internally to achieve this?

Thanks,
--j
 
V

Victor Bazarov

John said:
I have a class which looks like this

template< typename K, typename I >
Class X {
typedef typename map<K,I> Tcontainer1;
typedef typename vector<K> Tcontainer2;

Tcontainer tmap;

public:
typedef typename Tcontainer1::iterator item;
typedef typename Tcontainer1::const_iterator citem;

public:
const K& key(item it);
void container_change(void);
...

}

What I would like to do is dynamically change the container
from the map to the vector when container_change() is initiated.
This means that I would have to change types at runtime! Any
idea how to do this? The problem is that the class is heavily
used and I would not like to change the interface of the class
if possible. Anything I could change internally to achieve this?

Have two pointers, one to a 'map<>' and the other to a 'vector<>',
and keep one of them null to indicate that the other one is used.

The rest of the types (like the iterators and so on) are not so easy
to maintain, but 'key' function will work, you just need to branch
inside it depending on what container is "active".

V
 
J

John

What about the returned item types?
Dynamically I know what type to return.

Is this possible to do?
const K& key(tmap::iterator it);
 
V

Victor Bazarov

John said:
What about the returned item types?
Dynamically I know what type to return.

Is this possible to do?
const K& key(tmap::iterator it);

But isn't 'K' a template argument? You shouldn't have any problem
there, no? I am not sure what the 'iterator' here is for.
 
J

John

According to your suggestion

I have a class which would look like

template< typename K, typename I >
Class X {

typedef typename map<K,I> Tcontainer1;
typedef typename vector<K> Tcontainer2;

void *tmap;

public:
typedef typename Tcontainer1::iterator item;
typedef typename Tcontainer1::const_iterator citem;

public:
const K& key( tmap->iterator it);
void container_change(void);
....

}

But will this work? Even if tmap points to the vector or map depending
upon what it is pointing to? Can this be made to work?
 
V

Victor Bazarov

John said:
According to your suggestion

I have a class which would look like

template< typename K, typename I >
Class X {

That's not even going to compile!
typedef typename map<K,I> Tcontainer1;
typedef typename vector<K> Tcontainer2;

void *tmap;

public:
typedef typename Tcontainer1::iterator item;
typedef typename Tcontainer1::const_iterator citem;

public:
const K& key( tmap->iterator it);
void container_change(void);
...

}

But will this work?
No.

> Even if tmap points to the vector or map depending
upon what it is pointing to? Can this be made to work?

No, that's not what I suggested. Have two pointers, only one should
be "active", the non-null one:

template< typename K, typename I >
class X {
typedef std::map<K,I> Tcontainer1;
typedef std::vector<K> Tcontainer2;

Tcontainer1 *tmap; // one of these two pointers
Tcontainer2 *tvector; // is going to be null.

public:

X(); // should make one of the "containers" active

const K& key( ??? ); // think of changing the argument

void container_change(); // Change containers, how?
// No argument is supplied
// Do you just swap one with
// the other? I suppose it's OK...
};

You need to think more about it, I am guessing.

V
 
J

John

Cool

template< typename K, typename I >
class X {
typedef std::map<K,I> Tcontainer1;
typedef std::vector<K> Tcontainer2;

Tcontainer1 *tmap; // one of these two pointers
Tcontainer2 *tvector; // is going to be null.
bool container_is_map = true;

public:

X(); // should make one of the "containers" active

template< typename item>
const K& key( item x ); // think of changing the argument

void container_change(); // Change containers, how?
// No argument is supplied
// Do you just swap one with
// the other? I suppose it's OK...

};

Maybe something like this?
 
V

Victor Bazarov

John said:
Cool

template< typename K, typename I >
class X {
typedef std::map<K,I> Tcontainer1;
typedef std::vector<K> Tcontainer2;

Tcontainer1 *tmap; // one of these two pointers
Tcontainer2 *tvector; // is going to be null.
bool container_is_map = true;

public:

X(); // should make one of the "containers" active

template< typename item>
const K& key( item x ); // think of changing the argument

void container_change(); // Change containers, how?
// No argument is supplied
// Do you just swap one with
// the other? I suppose it's OK...

};

Maybe something like this?

Sure. You can even specialise the 'key' member template on map::iterator
and vector::iterator if you want.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top