|
Prev: deriving a class with a subset of base class methods?
Next: template class specialization trouble
From: sjefet on 7 Apr 2008 01:13 Hi, I am new to boost, and to templates (feeling very comfortable with C). I am trying to use boost hash tables where the keys are char *, like: unordered_map<const char *, CMyclass *> myclass_map_t ; 1. Couldnt figure so far, how do i set a hash function that works on the string (and not on the pointer) ? 2. Whenever i get a new key i want to check first if it is already in the hash table, if yes - get its value, and if not have the opportunity to create a new value for it, but be able to do it with one operation. Is there a way to achieve that ? TIA, jehs. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 7 Apr 2008 07:37 On 7 Apr., 18:13, sje...(a)gmail.com wrote: > I am new to boost, and to templates (feeling very comfortable with C). > I am trying to use boost hash tables where the keys are char *, like: > > unordered_map<const char *, CMyclass *> myclass_map_t ; Note: I have no experience with this new facility of the boost trunk, but it seems that it reflects the current specification of upcoming C++0x std::unordered_map (given current compiler technologies, i.e. modulo rvalue references where not available). > 1. Couldnt figure so far, how do i set a hash function that works on > the string (and not on the pointer) ? IMO hash functions on string-like entities belong to the most popular use-cases of hashers. A simple way would be to have a look at boost's implementation for std::string, which effectively calls boost::hash_range. So you could provide something like #include <string.h> struct CharArrayHashEqual : std::unary_function<const char*, std::size_t> { std::size_t operator()(const char* s) const { return boost::hash_range(s, strlen(s)); } bool operator(const char* s1, const char* s2) const { return strcmp(s1, s2) == 0; } }; and use type CharArrayHash as the third (Hash) *and* forth (EqualTo) template argument of boost::unordered_map. Also very popular are the FVN algorithm, http://www.isthe.com/chongo/tech/comp/fnv/ or the Jenkins hash, http://burtleburtle.net/bob/hash/doobs.html (instead of the "boost::hash_range(s, strlen(s))" part) > 2. Whenever i get a new key i want to check first if it is already in > the hash table, if yes - get its value, and if not have the > opportunity to create a new value for it, but be able to do it with > one operation. Is there a way to achieve that ? You will probably write a helper function which does this, which e.g. uses the member function insert that returns a pair<iterator, bool>. Depending on the bool part of the return value you can modify the value accessible via the iterator result value or not. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 7 Apr 2008 23:06 On 8 Apr., 00:37, Daniel Kr�gler <daniel.krueg...(a)googlemail.com> wrote: > #include <string.h> > > struct CharArrayHashEqual : std::unary_function<const char*, > std::size_t> > { > std::size_t operator()(const char* s) const { > return boost::hash_range(s, strlen(s)); This should be return boost::hash_range(s, s + strlen(s)); of course. Sorry for the noise, Daniel -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 7 Apr 2008 23:06 On Apr 8, 12:37 am, Daniel Kr�gler <daniel.krueg...(a)googlemail.com> wrote: > struct CharArrayHashEqual : std::unary_function<const char*, > std::size_t> > { > [...] > bool operator(const char* s1, const char* s2) const What is that supposed to declare? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 7 Apr 2008 23:18 On 8 Apr., 00:37, Daniel Kr�gler <daniel.krueg...(a)googlemail.com> wrote: > On 7 Apr., 18:13, sje...(a)gmail.com wrote: > > > I am new to boost, and to templates (feeling very comfortable with C). > > I am trying to use boost hash tables where the keys are char *, like: > > > unordered_map<const char *, CMyclass *> myclass_map_t ; > > Note: I have no experience with this new facility of the > boost trunk, but it seems that it reflects the current > specification of upcoming C++0x std::unordered_map (given > current compiler technologies, i.e. modulo rvalue references > where not available). > > > 1. Couldnt figure so far, how do i set a hash function that works on > > the string (and not on the pointer) ? > > IMO hash functions on string-like entities belong to > the most popular use-cases of hashers. A simple way would > be to have a look at boost's implementation for std::string, > which effectively calls boost::hash_range. So you could provide > something like > > #include <string.h> > > struct CharArrayHashEqual : std::unary_function<const char*, > std::size_t> > { > std::size_t operator()(const char* s) const { > return boost::hash_range(s, strlen(s)); > } > > bool operator(const char* s1, const char* s2) const { > return strcmp(s1, s2) == 0; > } Hastiness doesn't pay well: std::size_t operator()(const char* s) const { return boost::hash_range(s, s + strlen(s)); } bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) == 0; } Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 Prev: deriving a class with a subset of base class methods? Next: template class specialization trouble |