Re: maps...

From: Ali Cehreli (acehreli_at_yahoo.com)
Date: 03/26/04


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



Relevant Pages

  • mapper - multiple loops to a single destination
    ... I am trying to find a way in the BizTalk 2004 mapper to do multiple ... certain child nodes - all to the same destination node group in the ... where a specific child node's value meets a condition, and then map one ... <B>Because the value of sibling node A is 'TheOne', ...
    (microsoft.public.biztalk.general)
  • Re: file copier with log
    ... and the destination directory. ... A colleague suggested that i map the source directory, too, and map ... need to log in an .mdb the source path, destination path, drive ... Sub xerox ...
    (microsoft.public.scripting.vbscript)
  • Re: Message appearence in a Trasformation Shape
    ... destination and when the i click ok it will open the map, just like you make it a drag Trasform shape and you specify every thing in the Transform Shape in the orchesteration view ... Have you selected an existing map? ... corresponds with the maps source schemas type and/or destination ... Private blog: http://blog.eliasen.dk ...
    (microsoft.public.biztalk.general)
  • Re: Paging TomTom Navigator peeps
    ... >>> destination (assuming I can browse the map by directional buttons and ... Select 'Set as destination' ... Tyger Burning Bright ... Black with extra black bits ...
    (uk.rec.motorcycles)
  • Re: GPS on a bike
    ... I'll often find out its GPS cor-ords and put the GPS on the bike ... Having a pointer telling how far and in what direction my ... destination is usually all I need. ... well with a map for the last 55 years, and being the old luddite I am, I ...
    (uk.rec.cycling)