Skip to content

Commit 932abda

Browse files
authored
Implement Tree class (#30)
* Implement Tree class Signed-off-by: jparisu <javierparis@eprosima.com> * Add comment to classes Signed-off-by: jparisu <javierparis@eprosima.com> * uncrustify Signed-off-by: jparisu <javierparis@eprosima.com> Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent e896974 commit 932abda

3 files changed

Lines changed: 210 additions & 2 deletions

File tree

cpp_utils/include/cpp_utils/macros/macros.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ namespace utils {
6666
*/
6767
#define TYPE_NAME(x) typeid(x).name()
6868

69+
#define MAX(x, y) x < y ? y : x
70+
#define MIN(x, y) x > y ? y : x
71+
6972
} /* namespace utils */
7073
} /* namespace eprosima */
71-
72-
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file Tree.hpp
17+
*
18+
* This file contains class Tree definition.
19+
*/
20+
21+
#pragma once
22+
23+
#include <list>
24+
25+
namespace eprosima {
26+
namespace utils {
27+
28+
/**
29+
* Generic data struct of with an internal value of type \c Info .
30+
*/
31+
template <typename Info>
32+
struct Node
33+
{
34+
public:
35+
36+
Node();
37+
Node(
38+
const Info& info);
39+
Node(
40+
Info&& info);
41+
42+
template<typename ... Args>
43+
Node(
44+
Args... args);
45+
46+
Info info;
47+
};
48+
49+
/**
50+
* Class that represents each of the Nodes that form a Tree, including the source and the leaves.
51+
*/
52+
template <typename Info>
53+
class TreeNode : public Node<Info>
54+
{
55+
public:
56+
57+
using Node<Info>::Node;
58+
59+
void add_branch(
60+
const Info& info);
61+
void add_branch(
62+
Info&& info);
63+
void add_branch(
64+
const TreeNode& node);
65+
void add_branch(
66+
TreeNode&& node);
67+
68+
bool leaf() const noexcept;
69+
70+
unsigned int depth() const noexcept;
71+
72+
const std::list<TreeNode>& branches() const noexcept;
73+
74+
std::list<TreeNode> all_nodes() const noexcept;
75+
76+
protected:
77+
78+
std::list<TreeNode> branches_;
79+
};
80+
81+
} /* namespace utils */
82+
} /* namespace eprosima */
83+
84+
// Include implementation template file
85+
#include <cpp_utils/types/impl/Tree.ipp>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file Tree.ipp
17+
*
18+
* This file contains class Tree implementation.
19+
*/
20+
21+
#pragma once
22+
23+
namespace eprosima {
24+
namespace utils {
25+
26+
template <typename Info>
27+
Node<Info>::Node()
28+
{
29+
// Do nothing
30+
}
31+
32+
template <typename Info>
33+
Node<Info>::Node(
34+
const Info& info)
35+
: info(info)
36+
{
37+
// Do nothing
38+
}
39+
40+
template <typename Info>
41+
Node<Info>::Node(
42+
Info&& info)
43+
: info(std::move(info))
44+
{
45+
// Do nothing
46+
}
47+
48+
template <typename Info>
49+
template<typename ... Args>
50+
Node<Info>::Node(
51+
Args... args)
52+
: info(args ...)
53+
{
54+
// Do nothing
55+
}
56+
57+
template <typename Info>
58+
void TreeNode<Info>::add_branch(
59+
const Info& info)
60+
{
61+
branches_.push_back(TreeNode(info));
62+
}
63+
64+
template <typename Info>
65+
void TreeNode<Info>::add_branch(
66+
Info&& info)
67+
{
68+
branches_.push_back(TreeNode(std::move(info)));
69+
}
70+
71+
template <typename Info>
72+
void TreeNode<Info>::add_branch(
73+
const TreeNode& node)
74+
{
75+
branches_.push_back(node);
76+
}
77+
78+
template <typename Info>
79+
void TreeNode<Info>::add_branch(
80+
TreeNode&& node)
81+
{
82+
branches_.push_back(std::move(node));
83+
}
84+
85+
template <typename Info>
86+
bool TreeNode<Info>::leaf() const noexcept
87+
{
88+
return branches_.empty();
89+
}
90+
91+
template <typename Info>
92+
unsigned int TreeNode<Info>::depth() const noexcept
93+
{
94+
unsigned int max_value = 0;
95+
for (const auto& b : branches_)
96+
{
97+
auto child_value = b.depth();
98+
max_value = MAX(max_value, child_value);
99+
}
100+
return max_value;
101+
}
102+
103+
template <typename Info>
104+
const std::list<TreeNode<Info>>& TreeNode<Info>::branches() const noexcept
105+
{
106+
return branches_;
107+
}
108+
109+
template <typename Info>
110+
std::list<TreeNode<Info>> TreeNode<Info>::all_nodes() const noexcept
111+
{
112+
std::list<TreeNode<Info>> result(branches_);
113+
for (const auto& b : branches_)
114+
{
115+
auto b_branches = b.all_nodes();
116+
result.splice(result.end(), b_branches);
117+
}
118+
return result;
119+
}
120+
121+
} /* namespace utils */
122+
} /* namespace eprosima */

0 commit comments

Comments
 (0)