Skip to content

Instantly share code, notes, and snippets.

@rdp
Created December 21, 2009 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdp/261041 to your computer and use it in GitHub Desktop.
Save rdp/261041 to your computer and use it in GitHub Desktop.
// google hash with ruby interactions
#include <iostream>
#include <google/dense_hash_map>
#include <ruby.h>
using google::dense_hash_map; // namespace where class lives by default
using std::cout;
using std::endl;
using __gnu_cxx::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);
}
};
struct eqrb
{
bool operator()(const VALUE s1, const VALUE s2) const
{
if(s1 == s2) {
return true;
}
return rb_eql(s1, s2); // this is eql? on them, basically
// return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
// ugh
}
};
struct hashrb
{
size_t operator()(const VALUE hash_me) const
{
return rb_hash(hash_me);
}
};
int main()
{
dense_hash_map<VALUE, VALUE, hashrb, eqrb> months;
months.set_empty_key(NULL);
months[rb_str_new2("abc")] = 31;
cout << "jan -> " << months[rb_str_new2("abc")] << endl;
/*
cout << "april -> " << months["april"] << endl;
cout << "june -> " << months["june"] << endl;
cout << "november -> " << months["november"] << endl;
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment