Re: maps...
From: Ali Cehreli (acehreli_at_yahoo.com)
Date: 03/26/04
- Next message: TheEngine: "Timers?"
- Previous message: Disposable Svenkiller: "overloads across templates and namespaces"
- In reply to: grahamo: "maps..."
- Next in thread: David Harmon: "Re: maps..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 26 Mar 2004 09:27:59 -0800
graham_walsh50@hotmail.com (grahamo) wrote in message news:<79528aa8.0403260349.480bcdc7@posting.google.com>...
> Hi,
>
> for reasons that we wont discuss now I need to make a copy of a map
> but with the following setup;
>
> // assume this already exists and is populated.
> map<thread_id, some_object> source;
>
>
> // this is where the above is to be copied to.
> map<some_object, thread_id> destination;
This conversion works only if you are sure that there is a one-to-one
mapping between the thread_ids and some_objects. Otherwise, you may
need to use multimap for destination.
> Right now I see a loop in the existing codebase (I'm maintaining it)
> that looks something like this;
>
>
> map<thread_id, someobject>::iterator i;
> for (i = source.begin(), i!=source.end(); ++i)
> {
> destination[source->second] = source->first;
> }
>
>
>
> You can see that the second map is simply the first map with the key,
> value fields swapped.
>
> Whats the most efficient way to do this? I would like to use the range
> form of a contrstuctor to get the benfits that come with same, however
> I ain't sure how to do it. Any ideas?
I don't think the constructor of map can do anything special in this
case. The underlying tree structure of the source is unrelated to the
tree structure of the destination. The constructor must iterate over
all the objects in the source to build the destination tree anyway.
> Or must I iterate over the map
> manually as illustrated above.
You can make the code "cooler" :) by using a standard algorithm that
does the iteration. You still need to write the swapping of the pair
members though:
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
typedef int thread_id;
typedef char some_object;
template <class F, class S>
pair<S, F> reversePair(pair<F, S> const & object)
{
return pair<S, F>(object.second, object.first);
}
int main()
{
map<thread_id, some_object> source;
source[1] = 'a';
source[2] = 'b';
map<some_object, thread_id> destination;
transform(source.begin(), source.end(),
inserter(destination, destination.begin()),
reversePair<thread_id, some_object>);
cout << destination['a'] << '\n';
}
>
>
> thanks a million,
>
>
> GrahamO
There must be other solutions to this problem. The simplest that I can
think of, if possible, you could store thread_id in each some_object.
Then the search would be unnecessary.
I can think of an optimization that may work in your program: If
source is going to live as long as destination, and some_objects are
expensive to copy, you can define destination as
map<some_object_proxy,
thread_id,
some_object_proxy_comparator> destination;
some_object_proxy: a class that keeps a pointer to the actual
some_object in the source
some_object_proxy_comparator: a predicate that does the comparison
through that pointer
Ali
- Next message: TheEngine: "Timers?"
- Previous message: Disposable Svenkiller: "overloads across templates and namespaces"
- In reply to: grahamo: "maps..."
- Next in thread: David Harmon: "Re: maps..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|