Key commonKeyName (Key key1, Key key2) { // do not let removed namespaces escape key1 = key1.dup(); key2 = key2.dup(); if (key1.isBelowOrSame(key2)) return key2; if (key2.isBelowOrSame(key1)) return key1; if (key1.getNamespace() != key2.getNamespace()) { removeNamespace(key1); removeNamespace(key2); } Key ret(key1.getNamespace(), KEY_END); for (auto it1 = ++key1.begin(), it2 = ++key2.begin(); it1 != key1.end() && it2 != key2.end(); ++it1, ++it2) { if (*it1 != *it2) break; ret.addBaseName(*it1); } return ret; }
int main () { using namespace kdb; Key k ("user/name/a/very/long\\/name/to\\\\/iterate\\with\\/some\\\\\\/escaping", KEY_END); std::cout << "begin: " << *k.begin () << std::endl; std::cout << "end: " << *k.end () << std::endl; std::cout << "rbegin: " << *k.rbegin () << std::endl; std::cout << "rend: " << *k.rend () << std::endl; std::cout << "forward iterator: "; for (auto && elem : k) { std::cout << elem << " "; } std::cout << std::endl; std::cout << "output reverse except first: "; for (Key::iterator i = --k.end (); i != k.begin (); --i) { std::cout << *i << " "; } std::cout << std::endl; std::cout << "reverse iterator: "; for (Key::reverse_iterator i = k.rbegin (); i != k.rend (); ++i) { std::cout << *i << " "; } std::cout << std::endl; std::cout << "output except first: "; for (Key::reverse_iterator i = --k.rend (); i != k.rbegin (); --i) { std::cout << *i << " "; } std::cout << std::endl; }
int RegionState::KeyCmp::operator() (const Key& lhs, const Key& rhs) const { Region::const_iterator lit = lhs.begin(); const Region::const_iterator lend = rhs.end(); Region::const_iterator rit = rhs.begin(); const Region::const_iterator rend = rhs.end(); while (lit != lend && rit != rend) { const ClosureState* lstate = *lit; const ClosureState* rstate = *rit; const int cmp = m_cmp(ClosureState::getKey(lstate), ClosureState::getKey(rstate)); if (cmp < 0) return -1; if (cmp > 0) return 1; ++lit; ++rit; } if (lit != lend) return 1; if (rit != rend) return -1; return 0; }
int LsCommand::getDepth (Key const & key) { return std::distance (key.begin (), key.end ()); }