Created
May 3, 2012 12:11
-
-
Save donovanhide/2585252 to your computer and use it in GitHub Desktop.
DHT for Karim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <utility> | |
#include <iostream> | |
#include <google/dense_hash_map> | |
class mLong { | |
private: | |
int64_t value; | |
public: | |
mLong(): | |
value(0) | |
{} | |
mLong(const int64_t value): | |
value(value) | |
{} | |
int64_t getValue(){ | |
return value; | |
} | |
inline bool operator() (const mLong& lhs, const mLong &rhs) const { | |
// Uncomment to see when used | |
// std::cout << "Equality function called" << std::endl; | |
return (lhs.value==rhs.value); | |
} | |
inline size_t operator() (const mLong& v) const { | |
// Uncomment to see when used | |
// std::cout << "Hash function called" << std::endl; | |
return (size_t)v.value; | |
} | |
}; | |
template<class K> | |
class DHT { | |
typedef google::dense_hash_map<K,int64_t,K,K> map_t; | |
typedef typename map_t::iterator iterator_t; | |
map_t local_dht; | |
public: | |
DHT() | |
{ | |
local_dht.set_empty_key(NULL); | |
} | |
bool insert(K e1, int v1) { | |
return local_dht.insert(std::make_pair(e1, v1)).second; | |
} | |
int64_t retrieve(K e1) | |
{ | |
iterator_t element_itr = local_dht.find(e1); | |
return (element_itr == local_dht.end())?-1:element_itr->second; | |
} | |
}; | |
int main(int argc, char** argv) { | |
DHT<mLong> test; | |
std::cout << ((test.insert(mLong(12),1)==true)?"PASS":"FAIL") << std::endl; | |
std::cout << ((test.insert(mLong(14),5)==true)?"PASS":"FAIL") << std::endl; | |
std::cout << ((test.insert(mLong(12),1)==false)?"PASS":"FAIL") << std::endl; | |
std::cout << ((test.insert(mLong(14),5)==false)?"PASS":"FAIL") << std::endl; | |
std::cout << ((test.retrieve(mLong(12))==1)?"PASS":"FAIL") << std::endl; | |
std::cout << ((test.retrieve(mLong(14))==5)?"PASS":"FAIL") << std::endl; | |
std::cout << ((test.retrieve(mLong(13))==-1)?"PASS":"FAIL") << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gcc dht.cc -lstdc++ -o dht && ./dht | |
PASS | |
PASS | |
PASS | |
PASS | |
PASS | |
PASS | |
PASS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment