61 template <
typename T,
64 typename Items >
class Node;
66 template <
typename T,
69 typename Items >
class NodeItem
72 typedef Node<T, V, Cmp, Items> NodeClass;
73 typedef NodeItem<T, V, Cmp, Items> NodeItemClass;
76 NodeItem(
const T &endSymbol, T
const &key)
77 : mEndSymbol(endSymbol),
85 bool operator<(NodeItemClass
const &oth)
const {
86 return Cmp()(this->mKey, oth.mKey);
89 bool operator<(T
const &key)
const {
90 return Cmp()(this->mKey, key);
93 bool operator==(T
const &key)
const {
94 return !Cmp()(this->mKey, key) && !Cmp()(key, this->mKey);
97 bool operator==(NodeItemClass
const &oth)
const {
98 return !Cmp()(this->mKey, oth.mKey) && !Cmp()(oth.mKey, this->mKey);
101 bool operator!=(T
const &key)
const {
102 return !((*this) == key);
105 bool operator!=(NodeItemClass
const &oth)
const {
106 return !((*this) == oth);
109 void set(T
const &key) {
113 const T &
get()
const {
117 const NodeClass *getChilds()
const {
121 NodeClass *getChilds() {
125 NodeClass *getOrCreateChilds(NodeClass * parent) {
126 createChilds(parent);
131 void createChilds(NodeClass * parent) {
133 mChilds =
new NodeClass(mEndSymbol, parent);
137 NodeItem(NodeItem
const &);
138 NodeItem &operator=(NodeItem
const &);
146 template <
typename T,
149 typename Items >
class EndNodeItem:
public NodeItem<T, V, Cmp, Items>
152 typedef NodeItem<T, V, Cmp, Items> ParentClass;
155 EndNodeItem(
const T &endSymbol, T
const &key)
156 : ParentClass(endSymbol, key) {}
158 EndNodeItem(
const T &endSymbol, T
const &key, V
const &value)
159 : ParentClass(endSymbol, key),
162 void set(T
const &key, V
const &value) {
163 ParentClass::set(key);
167 const V &getValue()
const {
176 EndNodeItem(EndNodeItem
const &);
177 EndNodeItem &operator=(EndNodeItem
const &);
183 template <
typename T,
186 typename Items >
class NodeItemPtrCompare
189 typedef NodeItem<T, V, Cmp, Items> NodeItemClass;
192 bool operator()(
const NodeItemClass *v1,
const NodeItemClass *v2) {
197 template <
typename T,
200 typename Items >
class Node
203 typedef NodeItem<T, V, Cmp, Items> NodeItemClass;
204 typedef EndNodeItem<T, V, Cmp, Items> EndNodeItemClass;
205 typedef Node<T, V, Cmp, Items> NodeClass;
207 typedef typename Items::iterator ItemsContainerIter;
208 typedef typename Items::const_iterator ItemsContainerConstIter;
215 typedef std::pair<const T *, const V *> KeyValuePair;
216 typedef typename NodeClass::ItemsContainerConstIter ItemsContainerConstIter;
219 const_iterator(
const NodeClass *node,
const NodeClass * root,
const T * key = 0,
bool mooveToEnd =
false)
222 mCheckKeyLeft(false),
223 mCheckKeyRight(true),
228 pushNode(node, key, mooveToEnd);
234 const_iterator(
const const_iterator & oth)
235 : mRootNode(oth.mRootNode),
236 mCurrentNode(oth.mCurrentNode),
237 mCurrentPos(oth.mCurrentPos),
238 mKeyStack(oth.mKeyStack),
239 mCheckKeyLeft(oth.mCheckKeyLeft),
240 mCheckKeyRight(oth.mCheckKeyRight),
241 mEndReached(oth.mEndReached) {
242 if (!mKeyStack.empty()) {
243 mKeyValuePair = std::make_pair(&mKeyStack[0], oth.mKeyValuePair.second);
247 const_iterator & operator=(
const const_iterator & oth) {
249 mRootNode = oth.mRootNode;
250 mCurrentNode = oth.mCurrentNode;
251 mCurrentPos = oth.mCurrentPos;
252 mKeyStack = oth.mKeyStack;
253 if (!mKeyStack.empty()) {
254 mKeyValuePair = std::make_pair(&mKeyStack[0], oth.mKeyValuePair.second);
256 mCheckKeyLeft = oth.mCheckKeyLeft;
257 mCheckKeyRight = oth.mCheckKeyRight;
258 mEndReached = oth.mEndReached;
264 return this->mKeyValuePair;
267 const KeyValuePair *operator->()
const {
268 return &this->mKeyValuePair;
271 bool operator==(const_iterator
const &oth)
const {
272 return this->equals(oth);
275 bool operator!=(const_iterator
const &oth)
const {
276 return !(*
this == oth);
279 const_iterator operator++(
int) {
280 const_iterator iter = *
this;
285 const_iterator &operator++() {
290 const_iterator operator--(
int) {
291 const_iterator iter = *
this;
296 const_iterator &operator--() {
302 friend class Node<T, V, Cmp, Items>;
304 const NodeClass * mRootNode;
305 const NodeClass * mCurrentNode;
306 ItemsContainerConstIter mCurrentPos;
307 std::vector<T> mKeyStack;
308 KeyValuePair mKeyValuePair;
315 if (!mCurrentNode->mItems.empty() && mCurrentPos == mCurrentNode->mItems.end()) {
319 bool newNode =
false;
320 bool oldNode =
false;
322 while (mEndReached || !isLeftEnd()) {
324 ItemsContainerConstIter iterBegin = mCurrentNode->mItems.begin();
325 if (!mKeyStack.empty()) {
326 if (mKeyStack.back() == mCurrentNode->endSymbol()) {
327 mKeyStack.pop_back();
328 mCurrentPos = mCurrentNode->mItems.begin();
335 if (!newNode && !mKeyStack.empty()) {
337 mCurrentNode = mCurrentNode->parent();
338 mCurrentPos = mCurrentNode->mItems.find(mKeyStack.back());
339 iterBegin = mCurrentNode->mItems.begin();
340 mKeyStack.pop_back();
341 mCheckKeyLeft =
false;
342 if (mCurrentPos != iterBegin) {
349 ItemsContainerConstIter iter = mCurrentNode->mItems.find(mCurrentNode->endSymbol());
350 if (iter != mCurrentNode->mItems.end()) {
352 const NodeItemClass & item = *(
const NodeItemClass *) * mCurrentPos;
353 mKeyStack.push_back(item.get());
354 mKeyValuePair.first = &(mKeyStack[0]);
355 mKeyValuePair.second = &(((
const EndNodeItemClass &)item).getValue());
356 mCheckKeyLeft =
true;
359 mCurrentNode = mCurrentNode->parent();
360 mCurrentPos = mCurrentNode->mItems.find(mKeyStack.back());
361 iterBegin = mCurrentNode->mItems.begin();
362 mKeyStack.pop_back();
363 mCheckKeyLeft =
false;
364 if (mCurrentPos != iterBegin) {
375 for (; mCurrentPos != iterBegin; --mCurrentPos) {
377 const NodeItemClass &item = *(
const NodeItemClass *) * mCurrentPos;
378 if (item != mCurrentNode->endSymbol()) {
379 mKeyStack.push_back(item.get());
380 pushNode(item.getChilds(), 0,
true);
388 if (!newNode && !oldNode) {
390 const NodeItemClass &item = *(
const NodeItemClass *) * mCurrentPos;
391 if (item != mCurrentNode->endSymbol()) {
392 mKeyStack.push_back(item.get());
393 pushNode(item.getChilds(), 0,
true);
401 mCurrentPos = mCurrentNode->mItems.end();
407 ItemsContainerConstIter iterEnd = mCurrentNode->mItems.end();
409 if (!mKeyStack.empty()) {
410 if (mKeyStack.back() == mCurrentNode->endSymbol()) {
411 mKeyStack.pop_back();
412 mCurrentPos = mCurrentNode->mItems.begin();
416 if (mCurrentPos == iterEnd && !mKeyStack.empty()) {
417 mCurrentNode = mCurrentNode->parent();
418 mCurrentPos = mCurrentNode->mItems.find(mKeyStack.back());
420 iterEnd = mCurrentNode->mItems.end();
421 mKeyStack.pop_back();
424 for (; mCurrentPos != iterEnd; ++mCurrentPos) {
425 if (mCheckKeyRight) {
426 mCheckKeyRight =
false;
427 ItemsContainerConstIter iter = mCurrentNode->mItems.find(mCurrentNode->endSymbol());
428 if (iter != iterEnd) {
430 const NodeItemClass & item = *(
const NodeItemClass *) * mCurrentPos;
431 mKeyStack.push_back(item.get());
432 mKeyValuePair.first = &(mKeyStack[0]);
433 mKeyValuePair.second = &(((
const EndNodeItemClass &)item).getValue());
434 mCheckKeyLeft =
true;
440 const NodeItemClass &item = *(
const NodeItemClass *) * mCurrentPos;
441 if (item != mCurrentNode->endSymbol()) {
442 mKeyStack.push_back(item.get());
443 pushNode(item.getChilds());
453 if (this->mRootNode == this->mCurrentNode &&
454 this->mCurrentPos == this->mCurrentNode->mItems.end()) {
460 bool isLeftEnd()
const {
461 if (this->mRootNode == this->mCurrentNode &&
462 this->mCurrentPos == this->mCurrentNode->mItems.begin()) {
468 bool equals(
const const_iterator & oth)
const {
469 if (this->isEnd() && oth.isEnd()) {
473 if (this->mCurrentNode == oth.mCurrentNode &&
474 this->mCurrentPos == oth.mCurrentPos) {
481 void pushNode(
const NodeClass *node,
const T * key = 0,
bool mooveToEnd =
false) {
483 mCheckKeyLeft =
false;
485 mCurrentPos = mCurrentNode->mItems.end();
487 mCheckKeyRight =
false;
490 for (
int i = 0; key[i] != node->endSymbol(); ++i) {
491 mKeyStack.push_back(key[i]);
494 mCurrentPos = node->mItems.begin();
495 mCheckKeyRight =
true;
500 class iterator :
public const_iterator
503 typedef std::pair<const T *, V *> MutableKeyValuePair;
504 MutableKeyValuePair mMutableKeyValuePair;
507 MutableKeyValuePair & getPair() {
508 mMutableKeyValuePair.first = this->mKeyValuePair.first;
509 mMutableKeyValuePair.second =
const_cast<V *
>(this->mKeyValuePair.second);
510 return this->mMutableKeyValuePair;
514 iterator(NodeClass *node, NodeClass *root,
const T * key = 0,
bool mooveToEnd =
false)
515 : const_iterator(node, root, key, mooveToEnd) {}
521 MutableKeyValuePair *operator->() {
525 iterator operator++(
int) {
526 iterator iter = *
this;
531 iterator &operator++() {
536 iterator operator--(
int) {
537 iterator iter = *
this;
542 iterator &operator--() {
550 Node &operator=(Node
const &);
552 static void deleteItem(NodeItemClass *item) {
557 NodeClass * nodeWithKey(
const T *key) {
558 return const_cast<NodeClass *
>(
const_cast<const NodeClass *
>(
this)->nodeWithKey(key));
561 const NodeClass * nodeWithKey(
const T *key)
const {
562 const NodeClass * node = nodeWithPrefix(key);
564 if (node->mItems.getItem(mEndSymbol)) {
571 const NodeClass * nodeWithPrefix(
const T *prefix)
const {
573 const NodeClass * node =
this;
576 if (prefix[i] == mEndSymbol) {
579 const NodeItemClass *item = node->mItems.getItem(prefix[i]);
584 node = item->getChilds();
590 bool erase(NodeClass * node,
const T * key) {
595 bool finished =
false;
600 while (key[keyIndex] != node->endSymbol()) {
605 while (node && !finished && erased) {
606 ItemsContainerIter iterEnd = node->mItems.end();
609 for (ItemsContainerIter iter = node->mItems.begin(); iter != iterEnd; ++iter) {
616 erased = node->mItems.eraseItem(key[keyIndex]);
618 }
else if (count == 1) {
619 erased = node->mItems.eraseItem(key[keyIndex]);
623 node = node->parent();
634 Node(
const T &eSymbol, NodeClass * parent = 0)
644 T endSymbol()
const {
649 std::for_each(mItems.begin(), mItems.end(), deleteItem);
658 unsigned int size()
const {
662 std::pair<iterator, bool> insert(
const T *key, V
const &value) {
663 std::pair<iterator, bool> result(end(),
false);
665 NodeClass * node =
this;
668 std::pair<typename Items::Item *, bool> itemPair = node->mItems.insertItem(key[i]);
669 NodeItemClass *item = itemPair.first;
670 if (itemPair.second) {
671 result.first = iterator(node,
this, key);
676 }
else if (key[i] == mEndSymbol) {
677 ((EndNodeItemClass *)item)->set(key[i], value);
678 result.first = iterator(node,
this, key);
679 result.second =
true;
683 node = item->getOrCreateChilds(node);
691 bool erase(iterator pos) {
692 if (pos.mCurrentNode && pos.mCurrentPos != pos.mCurrentNode->mItems.end()) {
693 return erase(const_cast<NodeClass *>(pos.mCurrentNode), pos->first);
698 bool erase(
const T *key) {
699 NodeClass * node = nodeWithKey(key);
701 return erase(node, key);
706 const V *
get(
const T *key)
const {
707 return const_cast<NodeClass *
>(
this)->
get(key);
710 V *
get(
const T *key) {
711 NodeClass * node = nodeWithKey(key);
713 NodeItemClass *item = node->mItems.getItem(mEndSymbol);
714 return &(((EndNodeItemClass *)item)->getValue());
720 bool hasKey(
const T *key)
const {
721 return get(key) != (V *)0;
724 const NodeClass * parent()
const {
728 NodeClass * parent() {
732 const_iterator begin()
const {
733 return const_iterator(
this,
this);
736 const_iterator end()
const {
737 return const_iterator(
this,
this, 0,
true);
741 return iterator(
this,
this);
745 return iterator(
this,
this, 0,
true);
748 const_iterator find(
const T *key)
const {
749 NodeClass * node =
const_cast<NodeClass *
>(
this)->nodeWithKey(key);
751 return const_iterator(
this,
this, 0,
true);
753 return const_iterator(node,
this, key);
757 iterator find(
const T *key) {
758 NodeClass * node = this->nodeWithKey(key);
760 return iterator(
this,
this, 0,
true);
762 return iterator(node,
this, key);
766 iterator startsWith(
const T *prefix) {
767 const NodeClass * node =
const_cast<const NodeClass *
>(
this)->nodeWithPrefix(prefix);
769 return iterator(
this,
this, 0,
true);
771 return iterator(const_cast<NodeClass *>(node), const_cast<NodeClass *>(node), prefix);
775 const_iterator startsWith(
const T *prefix)
const {
776 const NodeClass * node = nodeWithPrefix(prefix);
778 return const_iterator(
this,
this, 0,
true);
780 return const_iterator(node, node, prefix);
794 template <
typename T>
class SymbolToIndexMapper
797 unsigned int operator()(
const T & c)
const {
798 return static_cast<unsigned int>(c);
814 template <
typename T,
818 typename M = SymbolToIndexMapper<T> >
class VectorItems
821 typedef NodeItem<T, V, Cmp, VectorItems<T, V, Cmp, Max, M> > Item;
822 typedef std::vector<Item *> Items;
823 typedef typename Items::iterator iterator;
824 typedef typename Items::const_iterator const_iterator;
825 typedef Node<T, V, Cmp, VectorItems<T, V, Cmp, Max, M> > NodeClass;
826 typedef typename NodeClass::NodeItemClass NodeItemClass;
827 typedef typename NodeClass::EndNodeItemClass EndNodeItemClass;
830 VectorItems(T
const &endSymbol)
831 : mEndSymbol(endSymbol),
832 mItems(Max, (Item *)0) {}
834 const_iterator find(
const T & k)
const {
835 const Item * item = getItem(k);
837 return mItems.begin() + mSymolToIndex(k);
842 iterator find(
const T & k) {
843 const Item * item = getItem(k);
845 return mItems.begin() + mSymolToIndex(k);
851 return mItems.begin();
854 const_iterator begin()
const {
855 return mItems.begin();
862 const_iterator end()
const {
867 std::fill(mItems.begin(), mItems.end(), (Item *)0);
871 return mItems.empty();
874 std::pair<Item *, bool> insertItem(T
const &k) {
875 std::pair<Item *, bool> ret((Item *)0,
false);
877 assignItem(k, createNodeItem(k));
878 ret.first = getItem(k);
880 ret.first = getItem(k);
881 if (k == mEndSymbol) {
888 bool eraseItem(T
const &k) {
889 Item * item = getItem(k);
892 assignItem(k, (Item *)0);
899 Item *getItem(T
const &k) {
900 return mItems[mSymolToIndex(k)];
903 const Item *getItem(T
const &k)
const {
904 return mItems[mSymolToIndex(k)];
907 void assignItem(T k, Item *i) {
908 mItems[mSymolToIndex(k)] = i;
911 NodeItemClass *createNodeItem(T
const &k) {
912 if (k == mEndSymbol) {
913 return new EndNodeItemClass(mEndSymbol, k);
915 return new NodeItemClass(mEndSymbol, k);
936 template <
typename T,
938 typename Cmp >
class SetItems
941 typedef NodeItem<T, V, Cmp, SetItems<T, V, Cmp> > Item;
942 typedef std::set<Item *, NodeItemPtrCompare<T, V, Cmp, SetItems<T, V, Cmp> > > Items;
943 typedef typename Items::iterator iterator;
944 typedef typename Items::const_iterator const_iterator;
945 typedef Node<T, V, Cmp, SetItems<T, V, Cmp> > NodeClass;
946 typedef typename NodeClass::NodeItemClass NodeItemClass;
947 typedef typename NodeClass::EndNodeItemClass EndNodeItemClass;
950 SetItems(T
const &endSymbol)
951 : mEndSymbol(endSymbol) {}
953 const_iterator find(
const T & k)
const {
954 return const_cast<SetItems<T, V, Cmp> *
>(
this)->find(k);
957 iterator find(
const T & k) {
958 Item tmp(mEndSymbol, k);
959 return mItems.find(&tmp);
963 return mItems.begin();
966 const_iterator begin()
const {
967 return mItems.begin();
974 const_iterator end()
const {
979 return mItems.empty();
986 std::pair<Item *, bool> insertItem(T
const &k) {
987 std::pair<Item *, bool> ret((Item*)0,
false);
988 Item tmp(mEndSymbol, k);
989 iterator iter = mItems.find(&tmp);
990 if (iter == mItems.end()) {
991 Item *v = createNodeItem(k);
995 ret.first = (Item *) * iter;
996 if (k == mEndSymbol) {
1003 bool eraseItem(T
const &k) {
1004 Item tmp(mEndSymbol, k);
1005 iterator iter = mItems.find(&tmp);
1006 if (iter != mItems.end()) {
1015 const Item *getItem(T
const &k)
const {
1016 return const_cast<SetItems *
>(
this)->getItem(k);
1019 Item *getItem(T
const &k) {
1020 Item tmp(mEndSymbol, k);
1022 iterator iter = mItems.find(&tmp);
1023 if (iter == mItems.end()) {
1026 return (Item *)(*iter);
1029 NodeItemClass *createNodeItem(T
const &k) {
1030 if (k == mEndSymbol) {
1031 return new EndNodeItemClass(mEndSymbol, k);
1033 return new NodeItemClass(mEndSymbol, k);
1286 template <
typename T,
1288 typename Cmp = std::less<T>,
1289 typename Items = SetItems<T, V, Cmp> >
class Trie
1292 typedef typename Node<T, V, Cmp, Items>::iterator iterator;
1293 typedef typename Node<T, V, Cmp, Items>::const_iterator const_iterator;
1299 Trie(
const T &endSymbol)
1300 : mRoot(endSymbol) {}
1308 std::pair<iterator, bool> insert(
const T *key, V
const &value) {
1309 return mRoot.insert(key, value);
1318 std::pair<iterator, bool> insert(
const std::vector<T>& key, V
const &value) {
1320 return this->insert(&key[0], value);
1328 bool erase(
const T *key) {
1329 return mRoot.erase(key);
1337 bool erase(
const std::vector<T>& key) {
1339 return this->erase(&key[0]);
1347 bool erase(iterator pos) {
1348 return mRoot.erase(pos);
1356 const V *
get(
const T *key)
const {
1357 return mRoot.get(key);
1365 const V *
get(
const std::vector<T>& key)
const {
1367 return this->
get(&key[0]);
1375 V *
get(
const T *key) {
1376 return mRoot.get(key);
1384 V *
get(
const std::vector<T>& key) {
1386 return this->
get(&key[0]);
1396 V &operator[](
const T *key) {
1397 return *(insert(key, V()).first->second);
1407 V &operator[](
const std::vector<T>& key) {
1409 return this->operator[](&key[0]);
1417 bool hasKey(
const T *key)
const {
1418 return mRoot.hasKey(key);
1426 bool hasKey(
const std::vector<T>& key)
const {
1428 return this->hasKey(&key[0]);
1435 bool empty()
const {
1436 return mRoot.empty();
1443 unsigned int size()
const {
1444 return mRoot.size();
1459 iterator startsWith(
const T *prefix) {
1460 return mRoot.startsWith(prefix);
1468 iterator startsWith(
const std::vector<T>& prefix) {
1470 return this->startsWith(&prefix[0]);
1478 const_iterator startsWith(
const T *prefix)
const {
1479 return mRoot.startsWith(prefix);
1487 const_iterator startsWith(
const std::vector<T>& prefix)
const {
1489 return this->startsWith(&prefix[0]);
1496 T endSymbol()
const {
1497 return mRoot.endSymbol();
1505 return mRoot.begin();
1521 iterator find(
const T *key) {
1522 return mRoot.find(key);
1530 iterator find(
const std::vector<T>& key) {
1532 return this->find(&key[0]);
1540 const_iterator find(
const T *key)
const {
1541 return mRoot.find(key);
1549 const_iterator find(
const std::vector<T>& key)
const {
1550 return this->find(&key[0]);
1557 const_iterator begin()
const {
1558 return mRoot.begin();
1565 const_iterator end()
const {
1571 Trie &operator=(Trie
const &);
1574 Node<T, V, Cmp, Items> mRoot;
All the definitions for datatypes as well as some basic conversion functions are defined here...
Mezzanine::Vector3 operator*(const btVector3 &Vec, const Mezzanine::Vector3 &lhs)
Right Hand Multiplication Operator for Bullet Vectors with a Mezzanine::Vector3.
The bulk of the engine components go in this namspace.