tree_node.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * \file tree_node.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 12-10-2010
  5. * \brief This file contains the definition of a virtual base class for a tree
  6. * node.
  7. */
  8. #ifndef _PVFMM_TREE_NODE_HPP_
  9. #define _PVFMM_TREE_NODE_HPP_
  10. #include <pvfmm_common.hpp>
  11. #include <assert.h>
  12. #include <cstring>
  13. namespace pvfmm{
  14. /**
  15. * \brief Virtual base class for tree node.
  16. */
  17. class TreeNode{
  18. public:
  19. /**
  20. * \brief Base class for node data. Contains initialization data for the node.
  21. */
  22. class NodeData{
  23. public:
  24. virtual ~NodeData(){};
  25. virtual void Clear(){}
  26. int max_depth;
  27. int dim;
  28. };
  29. /**
  30. * \brief Initialize pointers to NULL
  31. */
  32. TreeNode(): dim(0), depth(0), max_depth(MAX_DEPTH), parent(NULL), child(NULL), status(1) { }
  33. /**
  34. * \brief Virtual destructor
  35. */
  36. virtual ~TreeNode();
  37. /**
  38. * \brief Initialize the node by passing the relevant data.
  39. */
  40. virtual void Initialize(TreeNode* parent_, int path2node_, NodeData* data_) ;
  41. /**
  42. * \brief Clear node data.
  43. */
  44. virtual void ClearData(){}
  45. /**
  46. * \brief Returns the dimension of the tree.
  47. */
  48. int Dim(){return dim;}
  49. /**
  50. * \brief Returns the depth of this node. (Root has depth 0)
  51. */
  52. int Depth(){return depth;}
  53. /**
  54. * \brief Returns 'true' if this is a leaf node.
  55. */
  56. bool IsLeaf(){return child == NULL;}
  57. /**
  58. * \brief Returns the child corresponding to the input parameter.
  59. */
  60. TreeNode* Child(int id);
  61. /**
  62. * \brief Returns a pointer to the parent node.
  63. */
  64. TreeNode* Parent();
  65. /**
  66. * \brief Returns the index which corresponds to this node among its
  67. * siblings (parent's children).
  68. * this->Parent()->Child(this->Path2Node())==this
  69. */
  70. int Path2Node();
  71. /**
  72. * \brief Allocate a new object of the same type (as the derived class) and
  73. * return a pointer to it type cast as (TreeNode*).
  74. */
  75. virtual TreeNode* NewNode(TreeNode* n_=NULL);
  76. /**
  77. * \brief Evaluates and returns the subdivision condition for this node.
  78. * 'true' if node requires further subdivision.
  79. */
  80. virtual bool SubdivCond();
  81. /**
  82. * \brief Create child nodes and Initialize them.
  83. */
  84. virtual void Subdivide() ;
  85. /**
  86. * \brief Truncates the tree i.e. makes this a leaf node.
  87. */
  88. virtual void Truncate() ;
  89. /**
  90. * \brief Set the parent for this node.
  91. */
  92. void SetParent(TreeNode* p, int path2node_) ;
  93. /**
  94. * \brief Set a child for this node.
  95. */
  96. void SetChild(TreeNode* c, int id) ;
  97. /**
  98. * \brief Returns status.
  99. */
  100. int& GetStatus();
  101. /**
  102. * \brief Update status for all nodes up to the root node.
  103. */
  104. void SetStatus(int flag);
  105. protected:
  106. int dim; //Dimension of the tree
  107. int depth; //Depth of the node (root -> 0)
  108. int max_depth; //Maximum depth
  109. int path2node; //Identity among siblings
  110. TreeNode* parent; //Pointer to parent node
  111. TreeNode** child; //Pointer child nodes
  112. int status;
  113. };
  114. }//end namespace
  115. #endif //_PVFMM_TREE_NODE_HPP_