59 template <
typename T,
62 typename Items >
class Node;
64 template <
typename T,
74 NodeItem(
const T &endSymbol, T
const &key)
75 : mEndSymbol(endSymbol),
84 return Cmp()(this->mKey, oth.mKey);
87 bool operator<(T
const &key)
const {
88 return Cmp()(this->mKey, key);
91 bool operator==(T
const &key)
const {
92 return !Cmp()(this->mKey, key) && !Cmp()(key, this->mKey);
96 return !Cmp()(this->mKey, oth.mKey) && !Cmp()(oth.mKey, this->mKey);
99 bool operator!=(T
const &key)
const {
100 return !((*this) == key);
104 return !((*this) == oth);
107 void set(T
const &key) {
111 const T &
get()
const {
124 createChilds(parent);
131 mChilds =
new NodeClass(mEndSymbol, parent);
144 template <
typename T,
156 EndNodeItem(
const T &endSymbol, T
const &key, V
const &value)
160 void set(T
const &key, V
const &value) {
161 ParentClass::set(key);
165 const V &getValue()
const {
181 template <
typename T,
195 template <
typename T,
198 typename Items >
class Node
205 typedef typename Items::iterator ItemsContainerIter;
206 typedef typename Items::const_iterator ItemsContainerConstIter;
213 typedef std::pair<const T *, const V *> KeyValuePair;
214 typedef typename NodeClass::ItemsContainerConstIter ItemsContainerConstIter;
220 mCheckKeyLeft(
false),
221 mCheckKeyRight(
true),
226 pushNode(node, key, mooveToEnd);
233 : mRootNode(oth.mRootNode),
234 mCurrentNode(oth.mCurrentNode),
235 mCurrentPos(oth.mCurrentPos),
236 mKeyStack(oth.mKeyStack),
237 mCheckKeyLeft(oth.mCheckKeyLeft),
238 mCheckKeyRight(oth.mCheckKeyRight),
239 mEndReached(oth.mEndReached) {
240 if (!mKeyStack.empty()) {
241 mKeyValuePair = std::make_pair(&mKeyStack[0], oth.mKeyValuePair.second);
247 mRootNode = oth.mRootNode;
248 mCurrentNode = oth.mCurrentNode;
249 mCurrentPos = oth.mCurrentPos;
250 mKeyStack = oth.mKeyStack;
251 if (!mKeyStack.empty()) {
252 mKeyValuePair = std::make_pair(&mKeyStack[0], oth.mKeyValuePair.second);
254 mCheckKeyLeft = oth.mCheckKeyLeft;
255 mCheckKeyRight = oth.mCheckKeyRight;
256 mEndReached = oth.mEndReached;
261 const KeyValuePair &operator*()
const {
262 return this->mKeyValuePair;
265 const KeyValuePair *operator->()
const {
266 return &this->mKeyValuePair;
270 return this->equals(oth);
274 return !(*
this == oth);
300 friend class Node<T, V, Cmp, Items>;
304 ItemsContainerConstIter mCurrentPos;
305 std::vector<T> mKeyStack;
306 KeyValuePair mKeyValuePair;
313 if (!mCurrentNode->mItems.empty() && mCurrentPos == mCurrentNode->mItems.end()) {
317 bool newNode =
false;
318 bool oldNode =
false;
320 while (mEndReached || !isLeftEnd()) {
322 ItemsContainerConstIter iterBegin = mCurrentNode->mItems.begin();
323 if (!mKeyStack.empty()) {
324 if (mKeyStack.back() == mCurrentNode->endSymbol()) {
325 mKeyStack.pop_back();
326 mCurrentPos = mCurrentNode->mItems.begin();
333 if (!newNode && !mKeyStack.empty()) {
335 mCurrentNode = mCurrentNode->parent();
336 mCurrentPos = mCurrentNode->mItems.find(mKeyStack.back());
337 iterBegin = mCurrentNode->mItems.begin();
338 mKeyStack.pop_back();
339 mCheckKeyLeft =
false;
340 if (mCurrentPos != iterBegin) {
347 ItemsContainerConstIter iter = mCurrentNode->mItems.find(mCurrentNode->endSymbol());
348 if (iter != mCurrentNode->mItems.end()) {
351 mKeyStack.push_back(item.get());
352 mKeyValuePair.first = &(mKeyStack[0]);
354 mCheckKeyLeft =
true;
357 mCurrentNode = mCurrentNode->parent();
358 mCurrentPos = mCurrentNode->mItems.find(mKeyStack.back());
359 iterBegin = mCurrentNode->mItems.begin();
360 mKeyStack.pop_back();
361 mCheckKeyLeft =
false;
362 if (mCurrentPos != iterBegin) {
373 for (; mCurrentPos != iterBegin; --mCurrentPos) {
376 if (item != mCurrentNode->endSymbol()) {
377 mKeyStack.push_back(item.get());
378 pushNode(item.getChilds(), 0,
true);
386 if (!newNode && !oldNode) {
389 if (item != mCurrentNode->endSymbol()) {
390 mKeyStack.push_back(item.get());
391 pushNode(item.getChilds(), 0,
true);
399 mCurrentPos = mCurrentNode->mItems.end();
405 ItemsContainerConstIter iterEnd = mCurrentNode->mItems.end();
407 if (!mKeyStack.empty()) {
408 if (mKeyStack.back() == mCurrentNode->endSymbol()) {
409 mKeyStack.pop_back();
410 mCurrentPos = mCurrentNode->mItems.begin();
414 if (mCurrentPos == iterEnd && !mKeyStack.empty()) {
415 mCurrentNode = mCurrentNode->parent();
416 mCurrentPos = mCurrentNode->mItems.find(mKeyStack.back());
418 iterEnd = mCurrentNode->mItems.end();
419 mKeyStack.pop_back();
422 for (; mCurrentPos != iterEnd; ++mCurrentPos) {
423 if (mCheckKeyRight) {
424 mCheckKeyRight =
false;
425 ItemsContainerConstIter iter = mCurrentNode->mItems.find(mCurrentNode->endSymbol());
426 if (iter != iterEnd) {
429 mKeyStack.push_back(item.get());
430 mKeyValuePair.first = &(mKeyStack[0]);
432 mCheckKeyLeft =
true;
439 if (item != mCurrentNode->endSymbol()) {
440 mKeyStack.push_back(item.get());
441 pushNode(item.getChilds());
451 if (this->mRootNode == this->mCurrentNode &&
452 this->mCurrentPos == this->mCurrentNode->mItems.end()) {
458 bool isLeftEnd()
const {
459 if (this->mRootNode == this->mCurrentNode &&
460 this->mCurrentPos == this->mCurrentNode->mItems.begin()) {
467 if (this->isEnd() && oth.isEnd()) {
471 if (this->mCurrentNode == oth.mCurrentNode &&
472 this->mCurrentPos == oth.mCurrentPos) {
479 void pushNode(
const NodeClass *node,
const T * key = 0,
bool mooveToEnd =
false) {
481 mCheckKeyLeft =
false;
483 mCurrentPos = mCurrentNode->mItems.end();
485 mCheckKeyRight =
false;
488 for (
int i = 0; key[i] != node->endSymbol(); ++i) {
489 mKeyStack.push_back(key[i]);
492 mCurrentPos = node->mItems.begin();
493 mCheckKeyRight =
true;
501 typedef std::pair<const T *, V *> MutableKeyValuePair;
502 MutableKeyValuePair mMutableKeyValuePair;
505 MutableKeyValuePair & getPair() {
506 mMutableKeyValuePair.first = this->mKeyValuePair.first;
507 mMutableKeyValuePair.second =
const_cast<V *
>(this->mKeyValuePair.second);
508 return this->mMutableKeyValuePair;
515 MutableKeyValuePair &operator*() {
519 MutableKeyValuePair *operator->() {
555 NodeClass * nodeWithKey(
const T *key) {
556 return const_cast<NodeClass *
>(
const_cast<const NodeClass *
>(
this)->nodeWithKey(key));
559 const NodeClass * nodeWithKey(
const T *key)
const {
560 const NodeClass * node = nodeWithPrefix(key);
562 if (node->mItems.getItem(mEndSymbol)) {
569 const NodeClass * nodeWithPrefix(
const T *prefix)
const {
571 const NodeClass * node =
this;
574 if (prefix[i] == mEndSymbol) {
577 const NodeItemClass *item = node->mItems.getItem(prefix[i]);
582 node = item->getChilds();
588 bool erase(NodeClass * node,
const T * key) {
593 bool finished =
false;
598 while (key[keyIndex] != node->endSymbol()) {
603 while (node && !finished && erased) {
604 ItemsContainerIter iterEnd = node->mItems.end();
607 for (ItemsContainerIter iter = node->mItems.begin(); iter != iterEnd; ++iter) {
614 erased = node->mItems.eraseItem(key[keyIndex]);
616 }
else if (count == 1) {
617 erased = node->mItems.eraseItem(key[keyIndex]);
621 node = node->parent();
632 Node(
const T &eSymbol, NodeClass * parent = 0)
642 T endSymbol()
const {
647 std::for_each(mItems.begin(), mItems.end(), deleteItem);
656 unsigned int size()
const {
660 std::pair<iterator, bool> insert(
const T *key, V
const &value) {
661 std::pair<iterator, bool> result(end(),
false);
663 NodeClass * node =
this;
666 std::pair<typename Items::Item *, bool> itemPair = node->mItems.insertItem(key[i]);
667 NodeItemClass *item = itemPair.first;
668 if (itemPair.second) {
669 result.first = iterator(node,
this, key);
674 }
else if (key[i] == mEndSymbol) {
675 ((EndNodeItemClass *)item)->set(key[i], value);
676 result.first = iterator(node,
this, key);
677 result.second =
true;
681 node = item->getOrCreateChilds(node);
689 bool erase(iterator pos) {
690 if (pos.mCurrentNode && pos.mCurrentPos != pos.mCurrentNode->mItems.end()) {
691 return erase(const_cast<NodeClass *>(pos.mCurrentNode), pos->first);
696 bool erase(
const T *key) {
697 NodeClass * node = nodeWithKey(key);
699 return erase(node, key);
704 const V *
get(
const T *key)
const {
705 return const_cast<NodeClass *
>(
this)->
get(key);
708 V *
get(
const T *key) {
709 NodeClass * node = nodeWithKey(key);
711 NodeItemClass *item = node->mItems.getItem(mEndSymbol);
712 return &(((EndNodeItemClass *)item)->getValue());
718 bool hasKey(
const T *key)
const {
719 return get(key) != (V *)0;
722 const NodeClass * parent()
const {
726 NodeClass * parent() {
730 const_iterator begin()
const {
731 return const_iterator(
this,
this);
734 const_iterator end()
const {
735 return const_iterator(
this,
this, 0,
true);
739 return iterator(
this,
this);
743 return iterator(
this,
this, 0,
true);
746 const_iterator find(
const T *key)
const {
747 NodeClass * node =
const_cast<NodeClass *
>(
this)->nodeWithKey(key);
749 return const_iterator(
this,
this, 0,
true);
751 return const_iterator(node,
this, key);
755 iterator find(
const T *key) {
756 NodeClass * node = this->nodeWithKey(key);
758 return iterator(
this,
this, 0,
true);
760 return iterator(node,
this, key);
764 iterator startsWith(
const T *prefix) {
765 const NodeClass * node =
const_cast<const NodeClass *
>(
this)->nodeWithPrefix(prefix);
767 return iterator(
this,
this, 0,
true);
769 return iterator(const_cast<NodeClass *>(node), const_cast<NodeClass *>(node), prefix);
773 const_iterator startsWith(
const T *prefix)
const {
774 const NodeClass * node = nodeWithPrefix(prefix);
776 return const_iterator(
this,
this, 0,
true);
778 return const_iterator(node, node, prefix);
795 unsigned int operator()(
const T & c)
const {
796 return static_cast<unsigned int>(c);
812 template <
typename T,
820 typedef std::vector<Item *> Items;
821 typedef typename Items::iterator iterator;
822 typedef typename Items::const_iterator const_iterator;
829 : mEndSymbol(endSymbol),
830 mItems(Max, (
Item *)0) {}
832 const_iterator find(
const T & k)
const {
833 const Item * item = getItem(k);
835 return mItems.begin() + mSymolToIndex(k);
840 iterator find(
const T & k) {
841 const Item * item = getItem(k);
843 return mItems.begin() + mSymolToIndex(k);
849 return mItems.begin();
852 const_iterator begin()
const {
853 return mItems.begin();
860 const_iterator end()
const {
865 std::fill(mItems.begin(), mItems.end(), (
Item *)0);
869 return mItems.empty();
872 std::pair<Item *, bool> insertItem(T
const &k) {
873 std::pair<Item *, bool> ret((
Item *)0,
false);
875 assignItem(k, createNodeItem(k));
876 ret.first = getItem(k);
878 ret.first = getItem(k);
879 if (k == mEndSymbol) {
886 bool eraseItem(T
const &k) {
887 Item * item = getItem(k);
890 assignItem(k, (
Item *)0);
897 Item *getItem(T
const &k) {
898 return mItems[mSymolToIndex(k)];
901 const Item *getItem(T
const &k)
const {
902 return mItems[mSymolToIndex(k)];
905 void assignItem(T k,
Item *i) {
906 mItems[mSymolToIndex(k)] = i;
910 if (k == mEndSymbol) {
934 template <
typename T,
940 typedef std::set<Item *, NodeItemPtrCompare<T, V, Cmp, SetItems<T, V, Cmp> > > Items;
941 typedef typename Items::iterator iterator;
942 typedef typename Items::const_iterator const_iterator;
949 : mEndSymbol(endSymbol) {}
951 const_iterator find(
const T & k)
const {
955 iterator find(
const T & k) {
956 Item tmp(mEndSymbol, k);
957 return mItems.find(&tmp);
961 return mItems.begin();
964 const_iterator begin()
const {
965 return mItems.begin();
972 const_iterator end()
const {
977 return mItems.empty();
984 std::pair<Item *, bool> insertItem(T
const &k) {
985 std::pair<Item *, bool> ret((
Item*)0,
false);
986 Item tmp(mEndSymbol, k);
987 iterator iter = mItems.find(&tmp);
988 if (iter == mItems.end()) {
989 Item *v = createNodeItem(k);
993 ret.first = (
Item *) * iter;
994 if (k == mEndSymbol) {
1001 bool eraseItem(T
const &k) {
1002 Item tmp(mEndSymbol, k);
1003 iterator iter = mItems.find(&tmp);
1004 if (iter != mItems.end()) {
1013 const Item *getItem(T
const &k)
const {
1014 return const_cast<SetItems *
>(
this)->getItem(k);
1017 Item *getItem(T
const &k) {
1018 Item tmp(mEndSymbol, k);
1020 iterator iter = mItems.find(&tmp);
1021 if (iter == mItems.end()) {
1024 return (
Item *)(*iter);
1028 if (k == mEndSymbol) {
1284 template <
typename T,
1286 typename Cmp = std::less<T>,
1298 : mRoot(endSymbol) {}
1306 std::pair<iterator, bool>
insert(
const T *key, V
const &value) {
1307 return mRoot.insert(key, value);
1316 std::pair<iterator, bool>
insert(
const std::vector<T>& key, V
const &value) {
1318 return this->
insert(&key[0], value);
1327 return mRoot.erase(key);
1337 return this->
erase(&key[0]);
1346 return mRoot.erase(pos);
1354 const V *
get(
const T *key)
const {
1355 return mRoot.get(key);
1363 const V *
get(
const std::vector<T>& key)
const {
1365 return this->
get(&key[0]);
1373 V *
get(
const T *key) {
1374 return mRoot.get(key);
1382 V *
get(
const std::vector<T>& key) {
1384 return this->
get(&key[0]);
1395 return *(
insert(key, V()).first->second);
1416 return mRoot.hasKey(key);
1424 bool hasKey(
const std::vector<T>& key)
const {
1426 return this->
hasKey(&key[0]);
1434 return mRoot.empty();
1442 return mRoot.size();
1458 return mRoot.startsWith(prefix);
1477 return mRoot.startsWith(prefix);
1495 return mRoot.endSymbol();
1503 return mRoot.begin();
1520 return mRoot.find(key);
1528 iterator
find(
const std::vector<T>& key) {
1530 return this->
find(&key[0]);
1538 const_iterator
find(
const T *key)
const {
1539 return mRoot.find(key);
1547 const_iterator
find(
const std::vector<T>& key)
const {
1548 return this->
find(&key[0]);
1556 return mRoot.begin();