wang1986one / sparsehash

Automatically exported from code.google.com/p/sparsehash
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Sparse hash for user defined strings #79

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am trying to run the following example, but I can not retrieve the values.I 
tried the same example with string instead of const char*.

I would appreciate if the example given with the code is explained if the 
strings available are not literal strings.

#include <iostream>
#include <string>
#include <cstring>
#include <google/dense_hash_map>
#include <fstream>
#include <vector>
#include <boost/algorithm/string.hpp>

using google::dense_hash_map;      // namespace where class lives by default
using std::cout;
using std::endl;
using std::string;
using std::tr1::hash;  // or __gnu_cxx::hash, or maybe tr1::hash, depending on 
your OS

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0);
  }
};

int main()
{

  std::string line;
  std::ifstream myfile("benchmark.txt");
  std::vector< string> strs;
  dense_hash_map<const char*, string, hash< const char*>, eqstr> PoS;

  PoS.set_empty_key(NULL);

  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline(myfile,line);
      boost::split(strs, line, boost::is_any_of(","));
      if (strs.size() > 1){
        if (!strs[0].empty() and !strs[1].empty()){
          PoS.insert(std::make_pair< const char*, const char* >(strs[0].c_str(), strs[1].c_str() ));          
      }
      }
      strs.clear();
    }
    myfile.close();
  }

  std::ifstream words("words.txt");
  if (words.is_open())
  {
    while (std::getline(words, line)){
     getline(words,line);
     cout<<line<< PoS[line.c_str()]<<endl;
    }
    words.close();
  }
}

Original issue reported on code.google.com by rmyeid on 17 Jan 2012 at 11:05

GoogleCodeExporter commented 9 years ago
}       strs.clear();

After this, your hash keys and values are dangling pointers.

If your hash_map is from string to string, you shouldn't have this problem.

Original comment by csilv...@gmail.com on 17 Jan 2012 at 11:10

GoogleCodeExporter commented 9 years ago
Thanks for the timely response.
I changed all the const char* to string and I get this error when I leave 
PoS.set_empty_key(NULL);
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid

When i change the above line to PoS.set_empty_key(string()); I get the 
following error
dict: /usr/include/google/sparsehash/densehashtable.h:930: 
std::pair<google::dense_hashtable_iterator<V, K, HF, ExK, SetK, EqK, A>, bool> 
google::dense_hashtable<Value, Key, HashFcn, ExtractKey, SetKey, EqualKey, 
Alloc>::insert_noresize(google::dense_hashtable<Value, Key, HashFcn, 
ExtractKey, SetKey, EqualKey, Alloc>::const_reference) [with Value = 
std::pair<const std::basic_string<char>, std::basic_string<char> >, Key = 
std::basic_string<char>, HashFcn = std::tr1::hash<std::basic_string<char> >, 
ExtractKey = google::dense_hash_map<std::basic_string<char>, 
std::basic_string<char>, std::tr1::hash<std::basic_string<char> >, 
eqstr>::SelectKey, SetKey = google::dense_hash_map<std::basic_string<char>, 
std::basic_string<char>, std::tr1::hash<std::basic_string<char> >, 
eqstr>::SetKey, EqualKey = eqstr, Alloc = 
google::libc_allocator_with_realloc<std::pair<const std::basic_string<char>, 
std::basic_string<char> > >, google::dense_hashtable<Value, Key, HashFcn, 
ExtractKey, SetKey, EqualKey, Alloc>::const_reference = const std::pair<const 
std::basic_string<char>, std::basic_string<char> >&]: Assertion 
`(!settings.use_empty() || !equals(get_key(obj), get_key(val_info.emptyval))) 
&& "Inserting the empty key"' failed.

Original comment by rmyeid on 17 Jan 2012 at 11:20

GoogleCodeExporter commented 9 years ago
Yes, NULL is not a valid value for a string.

Original comment by csilv...@gmail.com on 17 Jan 2012 at 11:39

GoogleCodeExporter commented 9 years ago
What about the second error when I change the above line to 
PoS.set_empty_key(string());

Original comment by rmyeid on 17 Jan 2012 at 11:42

GoogleCodeExporter commented 9 years ago
} EqualKey = eqstr

You still have this, which is wrong when the key is a string.

Original comment by csilv...@gmail.com on 17 Jan 2012 at 11:51

GoogleCodeExporter commented 9 years ago
I am attaching the working code, just as a reference for anyone who could be 
stuck with the same issue.

Original comment by rmyeid on 20 Jan 2012 at 9:27

Attachments: