STL hash...

bridged with qdn.public.qnxrtp.porting
Post Reply
kc

STL hash...

Post by kc » Mon Jun 23, 2003 7:12 pm

Hi all,

I am trying to use the hash functions in STL, and I keep getting the hash
function is undeclared error message. I search the header files and find
they are defined in the stl_hash_fun.h file under ../include/g++-3 . Can I
use those SGI STL's implementation of hash functions? If not, do I need to
implement my own ?

Thanks
kc

Robert Rutherford

Re: STL hash...

Post by Robert Rutherford » Tue Jun 24, 2003 12:35 am

The hash functions are not part of the C++ standard.

It sounds like you are using SE or PE and you are getting the Dinkum C++
libraries by default (/usr/include/cpp). The Dinkum C++ libraries do no
support the non-standard hash functions <hash_map> etc.

If you want to use the GNU/GCC libraries instead then you can either
- use "QCC (or CC) -V gcc_ntox86_gpp"
- use "QCC (or CC) -Y _gpp"
- edit the entries in /etc/qcc/ to make GNU the default (not recommended)

This will get you
- the <hash_*> templates
- less compliance with the C++ Standard
- greater compatibiliy with Linux C++ code
- faster compiles
- smaller, faster code

It will also bring you under the gamut of the LGPL license - which may or
may not be of concern to you :-)

YMMV. Hope this helps,

Rob Rutherford
Ruzz Technology

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bd7hul$8li$1@inn.qnx.com...
Hi all,

I am trying to use the hash functions in STL, and I keep getting the hash
function is undeclared error message. I search the header files and find
they are defined in the stl_hash_fun.h file under ../include/g++-3 . Can I
use those SGI STL's implementation of hash functions? If not, do I need to
implement my own ?

Thanks
kc

kc

Re: STL hash...

Post by kc » Tue Jun 24, 2003 8:37 pm

Hi Rob,

I have tried your suggestion using compiler option, but it complaints about
the missing __G_config.h file. I guess I wont be using the hash functions.
Sigh.

Thanks for your help.
kc

"Robert Rutherford" <ruzz@NoSpamPlease.ruzz.com> wrote in message
news:bd84vu$rsg$1@inn.qnx.com...
The hash functions are not part of the C++ standard.

It sounds like you are using SE or PE and you are getting the Dinkum C++
libraries by default (/usr/include/cpp). The Dinkum C++ libraries do no
support the non-standard hash functions <hash_map> etc.

If you want to use the GNU/GCC libraries instead then you can either
- use "QCC (or CC) -V gcc_ntox86_gpp"
- use "QCC (or CC) -Y _gpp"
- edit the entries in /etc/qcc/ to make GNU the default (not recommended)

This will get you
- the <hash_*> templates
- less compliance with the C++ Standard
- greater compatibiliy with Linux C++ code
- faster compiles
- smaller, faster code

It will also bring you under the gamut of the LGPL license - which may or
may not be of concern to you :-)

YMMV. Hope this helps,

Rob Rutherford
Ruzz Technology

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bd7hul$8li$1@inn.qnx.com...
Hi all,

I am trying to use the hash functions in STL, and I keep getting the
hash
function is undeclared error message. I search the header files and find
they are defined in the stl_hash_fun.h file under ../include/g++-3 . Can
I
use those SGI STL's implementation of hash functions? If not, do I need
to
implement my own ?

Thanks
kc



Colin Burgess

Re: STL hash...

Post by Colin Burgess » Tue Jun 24, 2003 8:58 pm

Note that the gcc 3's libstc++ is trying really hard to be ISO compliant.
I don' know if they still have the hash_map though. Obviously it's best
to try and stick as closely to whatever standards C++ has though...

kc <kwlee@nortelnetworks.com> wrote:
Hi Rob,

I have tried your suggestion using compiler option, but it complaints about
the missing __G_config.h file. I guess I wont be using the hash functions.
Sigh.

Thanks for your help.
kc

"Robert Rutherford" <ruzz@NoSpamPlease.ruzz.com> wrote in message
news:bd84vu$rsg$1@inn.qnx.com...
The hash functions are not part of the C++ standard.

It sounds like you are using SE or PE and you are getting the Dinkum C++
libraries by default (/usr/include/cpp). The Dinkum C++ libraries do no
support the non-standard hash functions <hash_map> etc.

If you want to use the GNU/GCC libraries instead then you can either
- use "QCC (or CC) -V gcc_ntox86_gpp"
- use "QCC (or CC) -Y _gpp"
- edit the entries in /etc/qcc/ to make GNU the default (not recommended)

This will get you
- the <hash_*> templates
- less compliance with the C++ Standard
- greater compatibiliy with Linux C++ code
- faster compiles
- smaller, faster code

It will also bring you under the gamut of the LGPL license - which may or
may not be of concern to you :-)

YMMV. Hope this helps,

Rob Rutherford
Ruzz Technology

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bd7hul$8li$1@inn.qnx.com...
Hi all,

I am trying to use the hash functions in STL, and I keep getting the
hash
function is undeclared error message. I search the header files and find
they are defined in the stl_hash_fun.h file under ../include/g++-3 . Can
I
use those SGI STL's implementation of hash functions? If not, do I need
to
implement my own ?

Thanks
kc





--
cburgess@qnx.com

Robert Rutherford

Re: STL hash...

Post by Robert Rutherford » Wed Jun 25, 2003 12:58 am

The following works just fine for me:

neutrino2 /home/ruzz/hash_map:ruzz 129>cat hash_map.cc

#include <hash_map>
#include <iostream>

using namespace std;

int main () {

hash_map<int, int> Map;

Map[0] = 3;

}
neutrino2 /home/ruzz/hash_map:ruzz 130>CC -Y _gpp hash_map.cc -o hash_map
neutrino2 /home/ruzz/hash_map:ruzz 131>

Rob Rutherford

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bdab9a$cl3$1@inn.qnx.com...
Hi Rob,

I have tried your suggestion using compiler option, but it complaints
about
the missing __G_config.h file. I guess I wont be using the hash functions.
Sigh.

Thanks for your help.
kc

"Robert Rutherford" <ruzz@NoSpamPlease.ruzz.com> wrote in message
news:bd84vu$rsg$1@inn.qnx.com...
The hash functions are not part of the C++ standard.

It sounds like you are using SE or PE and you are getting the Dinkum C++
libraries by default (/usr/include/cpp). The Dinkum C++ libraries do no
support the non-standard hash functions <hash_map> etc.

If you want to use the GNU/GCC libraries instead then you can either
- use "QCC (or CC) -V gcc_ntox86_gpp"
- use "QCC (or CC) -Y _gpp"
- edit the entries in /etc/qcc/ to make GNU the default (not
recommended)

This will get you
- the <hash_*> templates
- less compliance with the C++ Standard
- greater compatibiliy with Linux C++ code
- faster compiles
- smaller, faster code

It will also bring you under the gamut of the LGPL license - which may
or
may not be of concern to you :-)

YMMV. Hope this helps,

Rob Rutherford
Ruzz Technology

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bd7hul$8li$1@inn.qnx.com...
Hi all,

I am trying to use the hash functions in STL, and I keep getting the
hash
function is undeclared error message. I search the header files and
find
they are defined in the stl_hash_fun.h file under ../include/g++-3 .
Can
I
use those SGI STL's implementation of hash functions? If not, do I
need
to
implement my own ?

Thanks
kc





Robert Rutherford

Re: STL hash...

Post by Robert Rutherford » Wed Jun 25, 2003 1:06 am

By the way this also works perfectly well with the Dinkum libs, which it
turns out do actually support <hash_map> and <hash_set>.

Doh!

So your problem is something else altogether. Sorry to send you down the
garden path.

Rob Rutherford

"Robert Rutherford" <ruzz@NoSpamPlease.ruzz.com> wrote in message
news:bdaqmk$s22$1@inn.qnx.com...
The following works just fine for me:

neutrino2 /home/ruzz/hash_map:ruzz 129>cat hash_map.cc

#include <hash_map
#include <iostream

using namespace std;

int main () {

hash_map<int, int> Map;

Map[0] = 3;

}
neutrino2 /home/ruzz/hash_map:ruzz 130>CC -Y _gpp hash_map.cc -o hash_map
neutrino2 /home/ruzz/hash_map:ruzz 131

Rob Rutherford

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bdab9a$cl3$1@inn.qnx.com...
Hi Rob,

I have tried your suggestion using compiler option, but it complaints
about
the missing __G_config.h file. I guess I wont be using the hash
functions.
Sigh.

Thanks for your help.
kc

"Robert Rutherford" <ruzz@NoSpamPlease.ruzz.com> wrote in message
news:bd84vu$rsg$1@inn.qnx.com...
The hash functions are not part of the C++ standard.

It sounds like you are using SE or PE and you are getting the Dinkum
C++
libraries by default (/usr/include/cpp). The Dinkum C++ libraries do
no
support the non-standard hash functions <hash_map> etc.

If you want to use the GNU/GCC libraries instead then you can either
- use "QCC (or CC) -V gcc_ntox86_gpp"
- use "QCC (or CC) -Y _gpp"
- edit the entries in /etc/qcc/ to make GNU the default (not
recommended)

This will get you
- the <hash_*> templates
- less compliance with the C++ Standard
- greater compatibiliy with Linux C++ code
- faster compiles
- smaller, faster code

It will also bring you under the gamut of the LGPL license - which
may
or
may not be of concern to you :-)

YMMV. Hope this helps,

Rob Rutherford
Ruzz Technology

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bd7hul$8li$1@inn.qnx.com...
Hi all,

I am trying to use the hash functions in STL, and I keep getting the
hash
function is undeclared error message. I search the header files and
find
they are defined in the stl_hash_fun.h file under ../include/g++-3 .
Can
I
use those SGI STL's implementation of hash functions? If not, do I
need
to
implement my own ?

Thanks
kc







kc

Re: STL hash...

Post by kc » Wed Jun 25, 2003 4:44 pm

Hi Rob,

My problem is to use the hash function. For instance,

#include <iostream>
#include <string>
#include <algorithm>
#include <hash_map>
#include <hash_set>
#include <iterator>
using namespace std;

struct eqStr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
int main()
{
hash_map<const char*, int, hash<const char*>, eqStr> Map;
return 0;
}

The compiler complaints 'hash' is undeclared. Is the hash function declared
in Dinkum libraries?

Thanks
kc

"Robert Rutherford" <ruzz@NoSpamPlease.ruzz.com> wrote in message
news:bdar52$sit$1@inn.qnx.com...
By the way this also works perfectly well with the Dinkum libs, which it
turns out do actually support <hash_map> and <hash_set>.

Doh!

So your problem is something else altogether. Sorry to send you down the
garden path.

Rob Rutherford

"Robert Rutherford" <ruzz@NoSpamPlease.ruzz.com> wrote in message
news:bdaqmk$s22$1@inn.qnx.com...
The following works just fine for me:

neutrino2 /home/ruzz/hash_map:ruzz 129>cat hash_map.cc

#include <hash_map
#include <iostream

using namespace std;

int main () {

hash_map<int, int> Map;

Map[0] = 3;

}
neutrino2 /home/ruzz/hash_map:ruzz 130>CC -Y _gpp hash_map.cc -o
hash_map
neutrino2 /home/ruzz/hash_map:ruzz 131

Rob Rutherford

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bdab9a$cl3$1@inn.qnx.com...
Hi Rob,

I have tried your suggestion using compiler option, but it complaints
about
the missing __G_config.h file. I guess I wont be using the hash
functions.
Sigh.

Thanks for your help.
kc

"Robert Rutherford" <ruzz@NoSpamPlease.ruzz.com> wrote in message
news:bd84vu$rsg$1@inn.qnx.com...
The hash functions are not part of the C++ standard.

It sounds like you are using SE or PE and you are getting the Dinkum
C++
libraries by default (/usr/include/cpp). The Dinkum C++ libraries do
no
support the non-standard hash functions <hash_map> etc.

If you want to use the GNU/GCC libraries instead then you can either
- use "QCC (or CC) -V gcc_ntox86_gpp"
- use "QCC (or CC) -Y _gpp"
- edit the entries in /etc/qcc/ to make GNU the default (not
recommended)

This will get you
- the <hash_*> templates
- less compliance with the C++ Standard
- greater compatibiliy with Linux C++ code
- faster compiles
- smaller, faster code

It will also bring you under the gamut of the LGPL license - which
may
or
may not be of concern to you :-)

YMMV. Hope this helps,

Rob Rutherford
Ruzz Technology

"kc" <kwlee@nortelnetworks.com> wrote in message
news:bd7hul$8li$1@inn.qnx.com...
Hi all,

I am trying to use the hash functions in STL, and I keep getting
the
hash
function is undeclared error message. I search the header files
and
find
they are defined in the stl_hash_fun.h file under ../include/g++-3
..
Can
I
use those SGI STL's implementation of hash functions? If not, do I
need
to
implement my own ?

Thanks
kc









Dale Sherwood @NYAB

Re: STL hash...

Post by Dale Sherwood @NYAB » Wed Jun 25, 2003 11:49 pm

int main()
{
hash_map<const char*, int, hash<const char*>, eqStr> Map;
return 0;
}

The compiler complaints 'hash' is undeclared. Is the hash function
declared in Dinkum libraries?
kc:

I, too, have an interest in this matter, so your question motivated me to
once again try to find the answers. The short answer to your question is:
No, Dinkum does not seem to supply a hash function for 'const char*' (or
string). I refer you to the Dinkum manpages, and the header files
<cpp/hash_map> and <cpp/xhash>.

The Dinkum hash_map uses hash_compare<Key, less<Key>> as a "hash traits"
function object. It includes (among other things) the hashing function
'size_t operator()(const Key& key)', which by default returns key
(implicitly casting it to size_t).

So, if you try this,
hash_map<const char*, int> Map;
it will compile. However, if you then try an insertion,
Map["0"] = 0;
it will not compile because the hash function returns 'const char*' as a
size_t without a cast. (Note that the Dinkum hash_map has a different
parameter signature than you are using--i.e., its fourth parameter is an
allocator, not an equality functor.)

As a first attempt, I got something like this to compile and run with the
Dinkum library:

#include <hash_map>
#include <iostream>
using namespace std;

typedef const char* CSTRING;

class cstringhash : public hash_compare< CSTRING >
{
public:
cstringhash();
size_t operator()(CSTRING key) const;
bool operator()(CSTRING const& key1, CSTRING const& key2) const;
};
inline cstringhash::cstringhash()
: hash_compare< CSTRING >()
{
}
inline size_t
cstringhash::operator()(CSTRING key) const
{
return (size_t)key;
}
inline bool
cstringhash::operator()(CSTRING const& key1, CSTRING const& key2) const
{
return hash_compare< CSTRING >::operator()(key1, key2);
}

int main()
{
hash_map<const char*, int, cstringhash> m;
m["0"] = 0;
m["99"] = 99;
cout << m["0"] << endl;
cout << m["99"] << endl;
return 0;
}

Of course, my hash function compiles and runs, but it is useless. I do not
know (or care) about hashing functions--I would just like to be able to use
the darn thing with a string index without all this hassle (Perl has spoiled
me)--but Stroustrup (The C++ Programming Language, 3rd edition, p. 503)
provides one which I adapt here:

inline size_t
cstringhash::operator()(CSTRING key) const
{
size_t res = 0;
while(*key) res = (res << 1) ^ *key++;
return res;
}

This also compiles and runs. I assume that it has better than linear time
lookups, unlike my first implementation.

This completes my investigation, for now.

->>>--Sherwood-->

kc

Re: STL hash...

Post by kc » Thu Jun 26, 2003 2:02 pm

Hey Dale,

Really appreciate your detail investigation.
I was looking at the header files, and I came to the conclusion myself that
Dinkum did not quite supply any hash function. However, it is always good to
have other opinions. In short, if I would like to use Dinkum libraries, I
will most likely have to implement my own hash function.

Thanks for your help again.
kc

"Dale Sherwood @NYAB" <Dale.Sherwood@nyab.com> wrote in message
news:bddb14$ogi$1@inn.qnx.com...
int main()
{
hash_map<const char*, int, hash<const char*>, eqStr> Map;
return 0;
}

The compiler complaints 'hash' is undeclared. Is the hash function
declared in Dinkum libraries?

kc:

I, too, have an interest in this matter, so your question motivated me to
once again try to find the answers. The short answer to your question is:
No, Dinkum does not seem to supply a hash function for 'const char*' (or
string). I refer you to the Dinkum manpages, and the header files
cpp/hash_map> and <cpp/xhash>.

The Dinkum hash_map uses hash_compare<Key, less<Key>> as a "hash traits"
function object. It includes (among other things) the hashing function
'size_t operator()(const Key& key)', which by default returns key
(implicitly casting it to size_t).

So, if you try this,
hash_map<const char*, int> Map;
it will compile. However, if you then try an insertion,
Map["0"] = 0;
it will not compile because the hash function returns 'const char*' as a
size_t without a cast. (Note that the Dinkum hash_map has a different
parameter signature than you are using--i.e., its fourth parameter is an
allocator, not an equality functor.)

As a first attempt, I got something like this to compile and run with the
Dinkum library:

#include <hash_map
#include <iostream
using namespace std;

typedef const char* CSTRING;

class cstringhash : public hash_compare< CSTRING
{
public:
cstringhash();
size_t operator()(CSTRING key) const;
bool operator()(CSTRING const& key1, CSTRING const& key2) const;
};
inline cstringhash::cstringhash()
: hash_compare< CSTRING >()
{
}
inline size_t
cstringhash::operator()(CSTRING key) const
{
return (size_t)key;
}
inline bool
cstringhash::operator()(CSTRING const& key1, CSTRING const& key2) const
{
return hash_compare< CSTRING >::operator()(key1, key2);
}

int main()
{
hash_map<const char*, int, cstringhash> m;
m["0"] = 0;
m["99"] = 99;
cout << m["0"] << endl;
cout << m["99"] << endl;
return 0;
}

Of course, my hash function compiles and runs, but it is useless. I do
not
know (or care) about hashing functions--I would just like to be able to
use
the darn thing with a string index without all this hassle (Perl has
spoiled
me)--but Stroustrup (The C++ Programming Language, 3rd edition, p. 503)
provides one which I adapt here:

inline size_t
cstringhash::operator()(CSTRING key) const
{
size_t res = 0;
while(*key) res = (res << 1) ^ *key++;
return res;
}

This also compiles and runs. I assume that it has better than linear time
lookups, unlike my first implementation.

This completes my investigation, for now.

->>>--Sherwood--

Dale Sherwood @NYAB

Re: STL hash...

Post by Dale Sherwood @NYAB » Thu Jun 26, 2003 7:34 pm

For the record, I submit the following corrections to my hash traits class
for use with the Dinkum hash_map, et. al.

"Dale Sherwood @NYAB" <Dale.Sherwood@nyab.com> wrote in message
news:bddb14$ogi$1@inn.qnx.com...
As a first attempt, I got something like this to compile and run with the
Dinkum library:

#include <hash_map
#include <iostream
using namespace std;

typedef const char* CSTRING;
Further testing shows that it works better if I use a 'string' instead of
'const char*'. Seems that the hash_map must actually be able to store the
keys :-)
class cstringhash : public hash_compare< CSTRING
{
public:
cstringhash();
size_t operator()(CSTRING key) const;
bool operator()(CSTRING const& key1, CSTRING const& key2) const;
};
inline cstringhash::cstringhash()
: hash_compare< CSTRING >()
{
}
[snip]
inline bool
cstringhash::operator()(CSTRING const& key1, CSTRING const& key2) const
{
return hash_compare< CSTRING >::operator()(key1, key2);
}

[snip]
inline size_t
cstringhash::operator()(CSTRING key) const
{
size_t res = 0;
while(*key) res = (res << 1) ^ *key++;
return res;
}
Renaming cstringhash to stringhash, this then becomes:

class stringhash : public hash_compare< string >
{
public:
stringhash();
size_t operator()(const string& key) const;
bool operator()(const string& key1, const string& key2) const;
};
inline stringhash::stringhash()
: hash_compare< string >()
{
}
inline bool
stringhash::operator()(const string& key1, const string& key2) const
{
return hash_compare< string >::operator()(key1, key2);
}
inline size_t
stringhash::operator()(const string& key) const
{
size_t res = 0;
// From "The C++ Programming Language", 3rd ed., p. 503.
typedef string::const_iterator CI;
CI p = key.begin();
CI end = key.end();
while(p != end) res = (res << 1) ^ (unsigned char)*p++;
return res;
}

->>>--Sherwood-->

Post Reply

Return to “qdn.public.qnxrtp.porting”