Thread: RE: [Dev-C++] Reverse Map lookup.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2003-06-30 17:52:44
|
Cool.
The only thing I was planning on using was the [] so I
could access the function pointers in the first map
easily
(will do this more often, the reverse look-up will only
be
used by admin functions setting up the connection between
command name and function pointer)
--- Ioannis Vranos <iv...@em...> wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On
> Behalf
> > Of David McKen
> > Sent: Monday, June 30, 2003 6:27 PM
> > To: Dev-C Mailing List
> > Subject: [Dev-C++] Reverse Map lookup.
> >
> >
> > Ok, here is a question for all you STL users out
> there.
> >
> > I have a map which uses a string as the key and the
> data
> > object is a function pointer (I am yet to implement
> this so
> > don't ask me how I did it). What I would need (please
> don't
> > ask me why, it is the request of another member of
>our
> > team) but I also need to do a reverse-lookup (for a
> given
> > function pointer address what strings are assigned).
> >
> > For the curious this is for a dynamic command system.
> When
> > the user enters a command we look up what function is
> > assigned to that command. For now you can assume that
> it is
> > a 1:1 relationship, no more than one command is
> mapped
> to a
> > particular function. This might change in the future
> > (should we start implementing aliases) so a solution
> that
> > would solve this as well would be quite helpful.
>
>
> multimap for more than 1:1.
>
>
>
> > So far the only solutions I see is to set up 2 maps
> (2nd
> > one for the reverse lookup would have to be a
> multimap
> if
> > more than one command can be mapped to a particular
> > function)
>
>
> Yes. There is a std::multimap.
>
>
> >. To make sure everything is kept in sync I would
> > probably have to encapsulate it and over-ride some
> > operators.
>
>
> If you are not very time concerned (I am using maps in
> this style myself and
> haven't found it to be very slow) you can use a loop to
> check each one of
> the addresses. Look what I am doing in a program of
> mine:
>
>
> > map<unsigned, double> al;
> >
> > // Ls sorted
> > vector<double>L;
> >
> > // ...
> >
> >
> > The double are floating point numbers, the same in both
> > cases. In L they are
> > sorted though:
> >
> >
> > for(vector<unsigned>::size_type i=0; i<E.size(); ++i)
> > {
> > for(vector<unsigned>::size_type j=0; j<A[i].size();
> > ++j)
> > {
> > al[ A[i][j] ]= something;
> >
> > L.push_back(al[ A[i][j] ]);
> > }
> > }
> >
> >
> >
> > sort(L.begin(), L.end());
> >
> >
> > system("cls");
> >
> >
> >
> > for(map<unsigned, double>::size_type i=1;
> i<=al.size();
> > ++i)
> > {
> > cout<<i<<": "<<al[i]<<"\t\t\t";
> >
> > cout<<L[i-1]<<": ";
> >
> > for(map<unsigned, double>::size_type j=1;
> > j<=al.size(); ++j)
> > {
> > if(L[i-1]==al[j])
> > {
> > cout<<j<<endl;
> > break;
> > }
> > }
> >
> > }
> >
> >
> >
> > Check the last for loop inside the same for loop. I
> > search the value L[i-1]
> > (sorted vector) inside the map and display the unsigned
> > corresponding to it.
> > It doesn't take long.
> >
> >
> > If you can't afford it, then you will have to use a
> > multimap and a map and
> > keep them synchronized. I do not see any need to
> overload
> > anything though.
=====
Signed
David Mcken
Life Sucks
Live with it
__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com
|
|
From: Ioannis V. <iv...@em...> - 2003-07-01 05:21:30
|
> -----Original Message-----
> From: Ioannis Vranos [mailto:iv...@em...]=20
> Sent: Monday, June 30, 2003 7:55 PM
> To: 'Dev-C Mailing List'
> Subject: RE: [Dev-C++] Reverse Map lookup.
>=20
>=20
> > -----Original Message-----
> > From: dev...@li...=20
> > [mailto:dev...@li...] On Behalf=20
> > Of David McKen
> > Sent: Monday, June 30, 2003 6:27 PM
> > To: Dev-C Mailing List
> > Subject: [Dev-C++] Reverse Map lookup.
I had better tell you how you will do it:
// Assuming you have stored function addresses in void *.
map<string, void *>whatever;
Let's assume you are looking for a specific address to find the string.
A map iterator consists of a pair<R,T> which is a struct having 2 =
pointers,
first and last, first potining to the first elemenet (key) and last=20
whichh points to the second.
// Target value
void *tval=3Dsomething;
for(map<string, void *>const_iterator p=3Dwhatever.begin(); =
p!=3Dwhatever.end();
++p)
{
// You found the val
if(p->last=3D=3Dtval)
//do whatever you want to do with p->first
}
I hope this helps David.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|
|
From: David M. <ci...@ya...> - 2003-07-01 13:29:07
|
Thanks but I already did that part.
I am relatively new to the STL (I understand most of the
concepts) and am teaching myself by using the refrence
pages on SGI's web site. This works for the containers but
is not so good for the algorithms. I was more looking for
something in the STL that would have made this job a little
easier.
Thanks for the Code Ioannis, however I finished writing
that section of the code up about half-an-hour after I
recieved your first e-mail. this is the layout of the class
that I am building.
typedef int (*func_ptr) (PC_CHAR *, string);
class CMD_TABLE {
public:
CMD_TABLE();
~CMD_TABLE();
void insert(string, func_ptr);
func_ptr lookup(string);
private:
//Was hoping to upgrade this to a hash_map.
//Seems Visual C++ lacks the hash_map header
//Until I can convert the library we are using to
//g++ I hav to use VC. :(
map <string, func_ptr, str_lessthan> cmd_table;
multimap <string, func_ptr, str_lessthan>
reverse_cmd_table;
};
--- Ioannis Vranos <iv...@em...> wrote:
> > -----Original Message-----
> > From: Ioannis Vranos [mailto:iv...@em...]
> > Sent: Monday, June 30, 2003 7:55 PM
> > To: 'Dev-C Mailing List'
> > Subject: RE: [Dev-C++] Reverse Map lookup.
> >
> >
> > > -----Original Message-----
> > > From: dev...@li...
> > > [mailto:dev...@li...] On
> Behalf
> > > Of David McKen
> > > Sent: Monday, June 30, 2003 6:27 PM
> > > To: Dev-C Mailing List
> > > Subject: [Dev-C++] Reverse Map lookup.
>
>
> I had better tell you how you will do it:
>
>
> // Assuming you have stored function addresses in void *.
> map<string, void *>whatever;
>
>
> Let's assume you are looking for a specific address to
> find the string.
>
>
> A map iterator consists of a pair<R,T> which is a struct
> having 2 pointers,
> first and last, first potining to the first elemenet
> (key) and last
> whichh points to the second.
>
>
>
> // Target value
> void *tval=something;
>
>
> for(map<string, void *>const_iterator p=whatever.begin();
> p!=whatever.end();
> ++p)
> {
> // You found the val
> if(p->last==tval)
> //do whatever you want to do with p->first
> }
>
>
>
> I hope this helps David.
>
>
>
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL 1: http://run.to/noicys
> * Alternative URL 2: http://www.noicys.cjb.net
>
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET
> sites including
> Data Reports, E-commerce, Portals, and Forums are
> available now.
> Download today and enter to win an XBOX or Visual Studio
> .NET.
>
http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
=====
Signed
David Mcken
Life Sucks
Live with it
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
|
|
From: Ioannis V. <iv...@em...> - 2003-06-30 19:18:29
|
One thing to not be confused in my example. The unsigned keys i am storing are sequential, that is 1,2,3,... that's why the map<unsigned,double>::size_type is used from 1 to <=.size(). I could have used an unsigned counter too. I am telling this so as to avoid possible newbie confusion, there is no separate access through a size_type. size_types are unsigned integer values used to describe the size (at least for usual containers, one can always define his own size_type and size() to describe what he imagines as "size" in his own classes. :-) ). Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of David McKen > Sent: Monday, June 30, 2003 8:53 PM > To: Dev-C Mailing List > Subject: RE: [Dev-C++] Reverse Map lookup. > > > Cool. > > The only thing I was planning on using was the [] so I > could access the function pointers in the first map > easily > (will do this more often, the reverse look-up will only > be > used by admin functions setting up the connection between > command name and function pointer) > > --- Ioannis Vranos <iv...@em...> wrote: > > > -----Original Message----- > > > From: dev...@li... > > > [mailto:dev...@li...] On > > Behalf > > > Of David McKen > > > Sent: Monday, June 30, 2003 6:27 PM > > > To: Dev-C Mailing List > > > Subject: [Dev-C++] Reverse Map lookup. > > > > > > > > > Ok, here is a question for all you STL users out > > there. > > > > > > I have a map which uses a string as the key and the > > data > > > object is a function pointer (I am yet to implement > > this so > > > don't ask me how I did it). What I would need (please > > don't > > > ask me why, it is the request of another member of > >our > > > team) but I also need to do a reverse-lookup (for a > > given > > > function pointer address what strings are assigned). > > > > > > For the curious this is for a dynamic command system. > > When > > > the user enters a command we look up what function is > > > assigned to that command. For now you can assume that > > it is > > > a 1:1 relationship, no more than one command is > > mapped > > to a > > > particular function. This might change in the future > > > (should we start implementing aliases) so a solution > > that > > > would solve this as well would be quite helpful. > > > > > > multimap for more than 1:1. > > > > > > > > > So far the only solutions I see is to set up 2 maps > > (2nd > > > one for the reverse lookup would have to be a > > multimap > > if > > > more than one command can be mapped to a particular > > > function) > > > > > > Yes. There is a std::multimap. > > > > > > >. To make sure everything is kept in sync I would > > > probably have to encapsulate it and over-ride some > > > operators. > > > > > > If you are not very time concerned (I am using maps in > > this style myself and > > haven't found it to be very slow) you can use a loop to > > check each one of > > the addresses. Look what I am doing in a program of > > mine: > > > > > > > map<unsigned, double> al; > > > > > > // Ls sorted > > > vector<double>L; > > > > > > // ... > > > > > > > > > The double are floating point numbers, the same in both > > > cases. In L they are > > > sorted though: > > > > > > > > > for(vector<unsigned>::size_type i=0; i<E.size(); ++i) > > > { > > > for(vector<unsigned>::size_type j=0; j<A[i].size(); > > > ++j) > > > { > > > al[ A[i][j] ]= something; > > > > > > L.push_back(al[ A[i][j] ]); > > > } > > > } > > > > > > > > > > > > sort(L.begin(), L.end()); > > > > > > > > > system("cls"); > > > > > > > > > > > > for(map<unsigned, double>::size_type i=1; > > i<=al.size(); > > > ++i) > > > { > > > cout<<i<<": "<<al[i]<<"\t\t\t"; > > > > > > cout<<L[i-1]<<": "; > > > > > > for(map<unsigned, double>::size_type j=1; > > > j<=al.size(); ++j) > > > { > > > if(L[i-1]==al[j]) > > > { > > > cout<<j<<endl; > > > break; > > > } > > > } > > > > > > } > > > > > > > > > > > > Check the last for loop inside the same for loop. I > > > search the value L[i-1] > > > (sorted vector) inside the map and display the unsigned > > > corresponding to it. > > > It doesn't take long. > > > > > > > > > If you can't afford it, then you will have to use a > > > multimap and a map and > > > keep them synchronized. I do not see any need to > > overload > > > anything though. > > > ===== > Signed > David Mcken > > Life Sucks > Live with it > > __________________________________________________ > Do you Yahoo!? > The New Yahoo! Search - Faster. Easier. Bingo > http://search.yahoo.com > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_06 1203_01/01 _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |