C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to change f(t&) to f(t*) and not recode the function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
dima
Guest





PostPosted: Thu Oct 23, 2003 3:45 pm    Post subject: how to change f(t&) to f(t*) and not recode the function Reply with quote



I have something like this:

typedef std::map<std::string,T> MAP_TYPE;

void f(POSITION_MAP_TYPE::mapped_type t){
t.do();
}

now, i want to see if i can gain any performance by redefining my map
like so:
typedef std::map<std::string,T*> MAP_TYPE;

if i don't like it, i want to switch back.

my goal: not have to recode all function signatures and all code from
t.do()
to t->do() back and forth.

is there way to achive this? On a side note: I can do something like
typedef typename MAP_TYPE::pointer ptr; but i have not figured out how
to use it. In fact, i don't know how to use these
typedefs(pointer,reference etc) that containers define, if anybody can
show an example would be great.

thanks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Michael Tiomkin
Guest





PostPosted: Fri Oct 24, 2003 3:26 pm    Post subject: Re: how to change f(t&) to f(t*) and not recode the function Reply with quote



[email]dbabits (AT) hotmail (DOT) com[/email] (dima) wrote in message news:<5c72b580.0310222006.121e8538 (AT) posting (DOT) google.com>...
Quote:
I have something like this:

typedef std::map<std::string,T> MAP_TYPE;

void f(POSITION_MAP_TYPE::mapped_type t){
t.do();
}

now, i want to see if i can gain any performance by redefining my map
like so:
typedef std::map<std::string,T*> MAP_TYPE;

For every large type T you'll probably get huge performance benefits.
STL containers copy their data, and using pointers will make
adding/finding/deleting an item much faster. However,
you'll need a different memory management scheme
for containers of pointers. For example, you wouldn't like
to delete an object after adding a ptr to it to the map.
A good compromise could be using a ptr with refcount.
An object will contain only two words, and its copy constructor
and destructor will be relatively simple.

Quote:
if i don't like it, i want to switch back.

It wouldn't be very easy because of the different memory management.
You'll have to change your USE of the map.

Quote:
my goal: not have to recode all function signatures and all code from
t.do()
to t->do() back and forth.

You can use a map to T& instead of T*.
Most compilers would use a ptr as implementation of reference
anyway.

Quote:
is there way to achive this? On a side note: I can do something like
typedef typename MAP_TYPE::pointer ptr; but i have not figured out how
to use it. In fact, i don't know how to use these
typedefs(pointer,reference etc) that containers define, if anybody can
show an example would be great.

It's better to read a good C++ book. Look at the FAQ to see the references
to these books. I'm not sure you really need any typedefs from STL
containers beside the iterators.

Michael

Quote:
thanks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Turner
Guest





PostPosted: Fri Oct 24, 2003 7:22 pm    Post subject: Re: how to change f(t&) to f(t*) and not recode the function Reply with quote




"dima" <dbabits (AT) hotmail (DOT) com> wrote

Quote:
I have something like this:

typedef std::map<std::string,T> MAP_TYPE;

void f(POSITION_MAP_TYPE::mapped_type t){
t.do();
}

now, i want to see if i can gain any performance by redefining my map
like so:
typedef std::map<std::string,T*> MAP_TYPE;

if i don't like it, i want to switch back.

my goal: not have to recode all function signatures and all code from
t.do()
to t->do() back and forth.



You could try something like this:

template<typename T>
struct HereAndThere
{
T* operator ->() { return static_cast<T*>(this); }
const T* operator ->() const { return static_cast<const T*>(this); }
};

class MyType: public HereAndThere<MyType>
{
// etc
};

typedef std::map<std::string, MyType> ContainerMap;
typedef std::map<std::string, MyType*> ReferenceMap;

int main()
{
ContainerMap a;
ReferenceMap b;
a["Test"] = MyType();
b["Test"] = new MyType();
ContainerMap::iterator i = a.find("Test");
ReferenceMap::iterator j = b.find("Test");
(*i)->foo();
(*j)->foo();
return 0;
}


Regards
David Turner



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Fri Oct 24, 2003 7:29 pm    Post subject: Re: how to change f(t&) to f(t*) and not recode the function Reply with quote

dima wrote:
Quote:
typedef std::map<std::string,T> MAP_TYPE;

void f(POSITION_MAP_TYPE::mapped_type t){
t.do();
}

now, i want to see if i can gain any performance by redefining my map
like so:
typedef std::map<std::string,T*> MAP_TYPE;

if i don't like it, i want to switch back.

my goal: not have to recode all function signatures and all code from
t.do()
to t->do() back and forth.

You could code a set of inline forwarding functions that are overloads orf
the existing ones.

In addition to
void f(T& t);//def'd elsewhere
you add another
inline void f(T* t)
{ return f(*t); }
or vice versa.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
dima
Guest





PostPosted: Sat Oct 25, 2003 8:40 am    Post subject: Re: how to change f(t&) to f(t*) and not recode the function Reply with quote

"David Turner" <david (AT) firepro (DOT) co.za> wrote

Quote:
"dima" <dbabits (AT) hotmail (DOT) com> wrote in message
news:5c72b580.0310222006.121e8538 (AT) posting (DOT) google.com...
I have something like this:

typedef std::map<std::string,T> MAP_TYPE;

void f(POSITION_MAP_TYPE::mapped_type t){
t.do();
}

now, i want to see if i can gain any performance by redefining my map
like so:
typedef std::map<std::string,T*> MAP_TYPE;

if i don't like it, i want to switch back.

my goal: not have to recode all function signatures and all code from
t.do()
to t->do() back and forth.



You could try something like this:

template<typename T
struct HereAndThere
{
T* operator ->() { return static_cast<T*>(this); }
const T* operator ->() const { return static_cast<const T*>(this); }
};

class MyType: public HereAndThere<MyType
{
// etc
};

typedef std::map typedef std::map<std::string, MyType*> ReferenceMap;

int main()
{
ContainerMap a;
ReferenceMap b;
a["Test"] = MyType();
b["Test"] = new MyType();
ContainerMap::iterator i = a.find("Test");
ReferenceMap::iterator j = b.find("Test");
(*i)->foo();
(*j)->foo();
return 0;
}


thanks David, this is a very nice idea, except you have to call
i->second->foo()

Here's what i came up with in the mean time, critisism welcome:

template<class T> inline T* ptr(T& v) { return &v;}
template<class T> inline const T* ptr(const T& v){ return &v;}
template<class T> inline T* ptr( T* v){ return v; }
template<class T> inline const T* ptr(const T* v){ return v; }
template<class T> inline boost::shared_ptr<T>& ptr(shared_ptr<T>&
v){return v;}
template<class T> inline const boost::shared_ptr<T>& ptr(const
shared_ptr<T>& v){return v;}

template<class T> inline T& ref( T& v){ return v; }
template<class T> inline const T& ref(const T& v){ return v; }
template<class T> inline T& ref( T* v){ assert(v);return
*v;}
template<class T> inline const T& ref(const T* v){ assert(v);return
*v;}
template<class T> inline T& ref( boost::shared_ptr<T>&
v){return *v;}
template<class T> inline const T& ref(const boost::shared_ptr<T>&
v){return *v;}


And an example of usage:
void test_ptr(){
Foo* pe=new Foo;
Foo e;
boost::shared_ptr<Foo> sm(new Foo);

ptr<Foo>(pe)->fn();
ptr(e)->fn();
ptr<Foo>(sm)->fn();

ref<Foo>(pe).fn();
ref<Foo>(e).fn();
ref<Foo>(sm).fn();
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.