/src/Algorithms_NTree/NTreeDynamicOrdered.h
C Header | 169 lines | 141 code | 14 blank | 14 comment | 10 complexity | 48bdda6108f2c1120198e19720850037 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, LGPL-3.0, GPL-2.0
1#ifndef _N_TREE_DYNAMIC_ORDERED_H_ 2#define _N_TREE_DYNAMIC_ORDERED_H_ 3 4#include "NTree.h" 5#include "HasSetParent.h" 6#include "ExLib_MetaProgramming.h" 7#include <vector> 8 9template <class BASE = NTree, bool HAS_PARENT = TypeHasSetParent<BASE>::value> 10class NTreeDynamicOrdered: public BASE 11{ 12 typedef BASE CHILDREN; 13protected: 14 std::vector<CHILDREN *> children; 15public: 16 virtual ~NTreeDynamicOrdered() 17 { 18 /* 19 for (unsigned int i = 0; i < children.size(); ++i) 20 { 21 delete children[i]; 22 } 23 children.clear(); 24 */ 25 } 26 virtual inline CHILDREN * GetChildAt(unsigned int index) const 27 { 28 return children[index]; 29 } 30 virtual inline unsigned int GetOrder() const 31 { 32 return children.size(); 33 } 34 35 template<class CAST_TYPE> 36 CAST_TYPE * GetChildAtAs(unsigned int index) const 37 { 38 return static_cast<CAST_TYPE *>(GetChildAt(index)); 39 } 40 41 virtual inline CHILDREN * AttachChildAt(unsigned int index, CHILDREN * child) 42 { 43 CHILDREN * rtn = children[index]; 44 children[index] = child; 45 return rtn; 46 } 47 virtual inline int AttachChild(CHILDREN * child) 48 { 49 children.push_back(child); 50 return children.size() - 1; 51 } 52 virtual inline CHILDREN * DetachChildAt(unsigned int index) 53 { 54 CHILDREN * rtn = children[index]; 55 children[index] = 0; 56 return rtn; 57 } 58 virtual inline CHILDREN * DetachChild(CHILDREN * child) 59 { 60 for (unsigned int i = 0; i < children.size(); ++i) 61 { 62 if (children[i] == child) 63 { 64 CHILDREN * rtn = children[i]; 65 if (i == children.size()-1) 66 { 67 children.pop_back(); 68 } 69 else 70 { 71 children[i] = 0; 72 } 73 return rtn; 74 } 75 } 76 return 0; 77 } 78 79 virtual inline void DetachAll() 80 { 81 children.clear(); 82 } 83 84}; 85 86 87template <class BASE> 88class NTreeDynamicOrdered<BASE, true>: public BASE 89{ 90 typedef BASE CHILDREN; 91protected: 92 std::vector<CHILDREN *> children; 93public: 94 virtual ~NTreeDynamicOrdered() 95 { 96 /* 97 for (unsigned int i = 0; i < children.size(); ++i) 98 { 99 delete children[i]; 100 } 101 children.clear(); 102 */ 103 } 104 virtual inline CHILDREN * GetChildAt(unsigned int index) const 105 { 106 return children[index]; 107 } 108 virtual inline unsigned int GetOrder() const 109 { 110 return children.size(); 111 } 112 113 template<class CAST_TYPE> 114 CAST_TYPE * GetChildAtAs(unsigned int index) const 115 { 116 return static_cast<CAST_TYPE *>(GetChildAt(index)); 117 } 118 119 virtual inline CHILDREN * AttachChildAt(unsigned int index, CHILDREN * child) 120 { 121 CHILDREN * rtn = children[index]; 122 children[index] = child; 123 child->SetParent(this); 124 return rtn; 125 } 126 virtual inline int AttachChild(CHILDREN * child) 127 { 128 children.push_back(child); 129 child->SetParent(this); 130 return children.size() - 1; 131 } 132 virtual inline CHILDREN * DetachChildAt(unsigned int index) 133 { 134 CHILDREN * rtn = children[index]; 135 rtn->SetParent(0); 136 children[index] = 0; 137 return rtn; 138 } 139 virtual inline CHILDREN * DetachChild(CHILDREN * child) 140 { 141 for (unsigned int i = 0; i < children.size(); ++i) 142 { 143 if (children[i] == child) 144 { 145 CHILDREN * rtn = children[i]; 146 if (i == children.size()-1) 147 { 148 children.pop_back(); 149 } 150 else 151 { 152 children[i] = 0; 153 } 154 child->SetParent(0); 155 return rtn; 156 } 157 } 158 return 0; 159 } 160 161 virtual inline void DetachAll() 162 { 163 children.clear(); 164 } 165 166}; 167 168 169#endif