Created
May 25, 2014 12:47
-
-
Save ryanjdew/7484ed5b471cc2e9dda0 to your computer and use it in GitHub Desktop.
C++ encode/decode MarkLogic UDF functions
This file contains hidden or 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
/* | |
* Encode the map in a flattened state. | |
*/ | |
template<class T> | |
void Bucketeer<T>:: | |
encode(Encoder& e, Reporter& reporter) | |
{ | |
std::map<String, int> keys_w_count; | |
int key_count = 0; | |
// determine key count and collect unique keys | |
for ( | |
typename std::multimap<String, T>::iterator it = buckets.begin(); | |
it != buckets.end(); | |
// set the iterator to the upperbound of the current key | |
it = buckets.upper_bound(it->first) | |
) { | |
// store key with count of entries for that key | |
keys_w_count.insert(std::pair <String, int>(it->first,buckets.count(it->first))); | |
// increment key count | |
key_count++; | |
} | |
// encode key count | |
e.encode(key_count); | |
for (std::map<String, int>::iterator it = keys_w_count.begin(); it != keys_w_count.end(); ++it) { | |
// encode key value | |
e.encode((it->first)); | |
// encode count of unique values associated with key | |
e.encode(it->second); | |
std::pair <typename std::multimap<String,T>::iterator, typename std::multimap<String,T>::iterator> ret; | |
// get iterator for key range | |
ret = buckets.equal_range(it->first); | |
// encode each value for key | |
for (typename std::multimap<String, T>::iterator it2 = ret.first; it2 != ret.second; ++it2) { | |
e.encode(it2->second); | |
} | |
} | |
} | |
template<class T> | |
void Bucketeer<T>:: | |
decode(Decoder& d, Reporter& reporter) | |
{ | |
int key_count; | |
// decode key count | |
d.decode(key_count); | |
for (int i = 0; i < key_count;i++) { | |
String key; | |
int key_size; | |
// decode key | |
d.decode(key); | |
// decode count of key values | |
d.decode(key_size); | |
for (int j = 0; j < key_size; j++) { | |
T value; | |
// decode value for key | |
d.decode(value); | |
// insert key/value pair into our multimap | |
buckets.insert(typename std::pair <String, T> (key, value)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment