How to use Google Sparse Hash

Notes on how to use the Google Sparse Hash, hopefully a much better hash map than STL unordered_map. 🙂

  1. Checkout the github repository
    1. https://github.com/sparsehash/sparsehash
  2. Configure && Make && Make install (standard auto config routines)
    1. use configure –prefix if you want to build it locally
    2. once it finished building, it should have /include, /lib in the build directory
    3. make sure to add the include directory to the include path
  3. Find an example program
    1. http://goog-sparsehash.sourceforge.net/
    2. Change hash to std::hash
    3. More example programs in html can be found in docs directory of the github repository. But then you need to convert it from HTML.
    4. Use the following command
      1. g++ -I install_dir/include/ test.cc -o test.o

This program works on Mac (test.cc)

#include <iostream>
#include <google/sparse_hash_map>

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

int main()
{
google::sparse_hash_map<const char*, int, std::hash<const char*>, eqstr> months\
;

months.set_deleted_key(NULL);
months[“january”] = 31;
months[“february”] = 28;
months[“march”] = 31;
months[“april”] = 30;
months[“may”] = 31;
months[“june”] = 30;
months[“july”] = 31;
months[“august”] = 31;
months[“september”] = 30;
months[“october”] = 31;
months[“november”] = 30;
months[“december”] = 31;

std::cout << “september -> ” << months[“september”] << std::endl;
std::cout << “april -> ” << months[“april”] << std::endl;
std::cout << “june -> ” << months[“june”] << std::endl;
std::cout << “november -> ” << months[“november”] << std::endl;
}

A good link to look at

https://groups.google.com/forum/#!topic/google-sparsehash/mdb0eF3d7IM

Cheers!

 

 

Advertisements
This entry was posted in Tools, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s