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
// Hash map class template | |
template <typename K, typename V, typename F = KeyHash<K>> | |
class HashMap { | |
public: | |
HashMap() { | |
// construct zero initialized hash table of size | |
table = new HashNode<K, V> *[TABLE_SIZE](); | |
} | |
~HashMap() { |
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
struct MyKeyHash { | |
unsigned long operator()(const int& k) const | |
{ | |
return k % 10; | |
} | |
}; | |
HashMap<int, string, MyKeyHash> hmap; | |
hmap.put(1, "val1"); | |
hmap.put(2, "val2"); |
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
// Hash node class template | |
template <typename K, typename V> | |
class HashNode { | |
public: | |
HashNode(const K &key, const V &value) : | |
key(key), value(value), next(NULL) { | |
} | |
K getKey() const { | |
return key; |
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
// Default hash function class | |
template <typename K> | |
struct KeyHash { | |
unsigned long operator()(const K& key) const | |
{ | |
return reinterpret_cast<unsigned long>(key) % TABLE_SIZE; | |
} | |
}; |
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
final class MyTask implements Runnable { | |
@Override | |
public void run() { | |
try { | |
System.out.println("My task is started running..."); | |
// ... | |
anotherMethod(); | |
// ... | |
} catch (Throwable t) { | |
System.err.println("Uncaught exception is detected! " + t |
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
namespace AMS { | |
class IService { | |
public: | |
// Create or just return a singleton instance | |
static IService& instance(); | |
// Destroy the singleton instance | |
static void destroy(); | |
// Create a messaging domain restricted for communication |
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
void pub() { | |
// creates or gets the singleton AMS service | |
AMS::IService& service = AMS::IService::instance(); | |
// enables exhaustive logging (i.e. message received/sent) | |
service.debug_mode(); | |
// allows using global logger of AMS service | |
service.logger().information("publisher side running..."); | |
// either creates a new domain or joins an existing one |
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
// defines a handler class for the message first | |
class TestMsgHandler : public AMS::IHandler { | |
public: | |
virtual void handle(AMS::IMsgObj *baseMsg) { | |
TestMsg *msg = dynamic_cast<TestMsg *>(baseMsg); | |
if (msg != 0) { | |
// process message here | |
} | |
} | |
}; |
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
class TestMsg : public AMS::IMsgObj { | |
public: | |
// give unique id to each message | |
TestMsg() : IMsgObj(/*msg id*/1) {} | |
// primitive types and string can be used | |
std::string name; | |
double value; | |
// other class objects can be composed | |
AdditionalInfo info; |
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
class TwoPartComparator : public rocksdb::Comparator { | |
public: | |
// Three-way comparison function: | |
// if a < b: negative result | |
// if a > b: positive result | |
// else: zero result | |
int Compare(const rocksdb::Slice& a, const rocksdb::Slice& b) const { | |
int a1, a2, b1, b2; | |
ParseKey(a, &a1, &a2); | |
ParseKey(b, &b1, &b2); |
NewerOlder