/usr/include/hmat/tree.hpp is in libhmat-oss-dev 1.2.0-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | /*
HMat-OSS (HMatrix library, open source software)
Copyright (C) 2014-2015 Airbus Group SAS
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
http://github.com/jeromerobert/hmat-oss
*/
/*! \file
\ingroup HMatrix
\brief Templated Tree class used by ClusterTree and HMatrix.
*/
#ifndef _TREE_HPP
#define _TREE_HPP
#include <vector>
#include <list>
#include <cstddef>
#include <assert.h>
namespace hmat {
// Forward declaration
template <typename TreeNode> class Tree;
/* Visitor pattern
*/
enum Visit { tree_preorder, tree_postorder, tree_inorder, tree_leaf };
/** Class to recursively apply a given function to all nodes of a tree
*/
template <typename TreeNode>
class TreeProcedure {
public:
TreeProcedure() {}
virtual void visit(TreeNode* node, const Visit order) const = 0;
virtual ~TreeProcedure() {}
};
/*! \brief Templated tree class.
This class represents a tree of arity N, holding an instance of NodeData in
its nodes.
*/
template <typename TreeNode>
class Tree {
public:
/// depth of the current node in the tree
int depth;
protected:
/// empty for a leaf, pointeur on a vector of sons otherwise.
std::vector<TreeNode*> children;
public:
/// Pointer to the father, NULL if this node is the root
TreeNode* father;
public:
Tree(TreeNode* _father, int _depth = 0)
: depth(_depth), children(), father(_father) {}
virtual ~Tree() {
for (int i=0 ; i<nrChild() ; i++)
if (children[i])
delete children[i];
children.clear();
}
// https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
TreeNode* me() {
return static_cast<TreeNode*>(this);
}
const TreeNode* me() const {
return static_cast<const TreeNode*>(this);
}
/*! \brief Insert a child in the children vector.
If a child is already present, it is removed but not deleted.
\param index index in the children vector
\param child pointeur to the child
*/
void insertChild(int index, TreeNode *child) {
if (nrChild()<=index)
children.resize(index+1, (TreeNode*)NULL);
child->father = me();
children[index] = child;
child->depth = depth + 1;
}
/*! \brief Remove a child, and delete it if necessary.
*/
void removeChild(int index) {
assert(index>=0 && index<nrChild());
if (children[index])
delete children[index];
children[index] = (TreeNode*)NULL;
}
/*! \brief Return the number of nodes in the tree.
*/
int nodesCount() const {
int result = 1;
for (int i=0 ; i<nrChild() ; i++)
if (children[i])
result += children[i]->nodesCount();
return result;
}
/*! \brief Return the child of index, or NULL.
*/
inline TreeNode *getChild(int index) const {
assert(index>=0 && index<nrChild());
return children[index];
}
inline TreeNode *&getChild(int index) {
assert(index>=0 && index<nrChild());
return children[index];
}
inline TreeNode *getFather() const {
return father;
}
inline int nrChild() const {
return (int)children.size();
}
/*! \brief Return true if the node is a leaf (= it has no children).
*/
inline bool isLeaf() const {
return children.empty();
}
/*! \brief Return a list of nodes.
Not used anywhere.
*/
virtual std::list<const TreeNode*> listNodes() const {
std::list<const TreeNode*> result;
result.push_back(me());
for (int i=0 ; i<nrChild() ; i++)
if (children[i]) {
std::list<const TreeNode*> childNodes = children[i]->listNodes();
result.splice(result.end(), childNodes, childNodes.begin(), childNodes.end());
}
return result;
}
/*! \brief Return a list of leaves.
*/
void listAllLeaves(std::vector<const TreeNode*>& leaves) const {
if (!isLeaf()) {
for (int i=0 ; i<nrChild() ; i++)
if (children[i])
children[i]->listAllLeaves(leaves);
} else {
leaves.push_back(me());
}
}
void walk(const TreeProcedure<TreeNode> *proc) {
if (isLeaf()) {
proc->visit(me(), tree_leaf); // treatment on the leaves
} else {
proc->visit(me(), tree_preorder); // treatment on a non-leaf before recursion
bool first = true;
for (int i=0 ; i<nrChild() ; i++)
if (children[i]) {
if (!first)
proc->visit(me(), tree_inorder); // treatment on a non-leaf after 1st child (mainly usefull with 2 children)
first = false;
children[i]->walk(proc);
}
proc->visit(me(), tree_postorder); // treatment on a non-leaf after recursion
}
}
};
} // end namespace hmat
#endif // _TREE_HPP
|