/src/Algorithms_NTree/NTreeDynamicOrdered.h

http://github.com/Akranar/daguerreo · C Header · 169 lines · 141 code · 14 blank · 14 comment · 10 complexity · 48bdda6108f2c1120198e19720850037 MD5 · raw file

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