YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
paramGroupTree.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024-2024 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "generator.h"
7#include <iostream>
8#include <stack>
9
11{
12 insertRecursive(rootnode, param.getListOfGroups(), param, 0);
13}
14
15void SectionHandler::insertRecursive(SectionNode& node, const std::deque<std::string>& groups, const Parameter& param, size_t levelOfNesting)
16{
17 // Case 1
18 // This is a leaf. Insert the parameter at the beginning of the deque.
19 // In this way the deque contains at the beginning all the parameters which do not belong to a further subsection
20 if (groups.empty())
21 {
22 auto newNode = std::make_unique<SectionNode>();
23 newNode->name = param.getParamOnly();
24 newNode->param = param;
25 newNode->nestingLevel = levelOfNesting;
26 node.nextNode.push_front(std::move(newNode));
27 return;
28 }
29
30 const std::string& currentGroup = groups.front();
31 // Case 2
32 // Search if it already exists a subsection with the same name then...
33 for (const auto& next : node.nextNode)
34 {
35 if (next->name == currentGroup)
36 {
37 //Case 2a
38 //...if it exists, go deeper in the tree
39 insertRecursive(*next, std::deque<std::string>(groups.begin() + 1, groups.end()), param, levelOfNesting+1);
40 return;
41 }
42 }
43
44 //Case 2b
45 //...if it does not exist, than create the new subsection at the end of the deque.
46 // In this way the deque contains at the beginning all the parameters which do not belong to a further subsection
47 auto newNode = std::make_unique<SectionNode>();
48 newNode->name = currentGroup;
49 // newNode->paramPointer = nullptr; //this is a group, not an actual parameter
50 newNode->nestingLevel = levelOfNesting;
51 node.nextNode.push_back(std::move(newNode));
52 // then go deeper in the tree
53 insertRecursive(*node.nextNode.back(), std::deque<std::string>(groups.begin() + 1, groups.end()), param, levelOfNesting+1);
54}
55
56SectionHandler::SectionNode* SectionHandler::iterator_start()
57{
58 computePreOrderTraversal();
59 auto it = prePreOrderTraversal.begin();
60 iterator_ptr = it;
61 return *iterator_ptr;
62}
63
64void SectionHandler::computePreOrderTraversal()
65{
66 std::vector<SectionHandler::SectionNode*> result;
67
68 std::stack<SectionHandler::SectionNode*> nodeStack;
69 nodeStack.push(&rootnode);
70
71 while (!nodeStack.empty()) {
72 SectionHandler::SectionNode* node = nodeStack.top();
73 nodeStack.pop();
74
75 // Do not insert the root node, it is not a real parameter, it is just a container for all other parameters
76 if (node->name!="")
77 { result.push_back(node); }
78
79 // Insert children in reverse order to perform a pre-order traversal
80 for (auto it = node->nextNode.rbegin(); it != node->nextNode.rend(); ++it)
81 {
82 nodeStack.push(it->get());
83 }
84 }
85
86 prePreOrderTraversal = result;
87}
88
89SectionHandler::SectionNode* SectionHandler::iterator_next()
90{
91 if (iterator_ptr == prePreOrderTraversal.end()) { return nullptr; }
92
93 iterator_ptr++;
94 if (iterator_ptr == prePreOrderTraversal.end()) { return nullptr; }
95
96 return *iterator_ptr;
97}
98
99SectionHandler::SectionNode* SectionHandler::iterator_get()
100{
101 return *iterator_ptr;
102}
103
104bool SectionHandler::SectionNode::isParameter()
105{
106 return nextNode.size() == 0;
107}
108
109bool SectionHandler::SectionNode::isGroupOfParameters()
110{
111 return nextNode.size() != 0;
112}
std::string getParamOnly() const
Definition parameter.cpp:89
std::deque< std::string > getListOfGroups() const
Definition parameter.cpp:94
SectionNode * iterator_next()
SectionNode * iterator_start()
SectionNode * iterator_get()
void insert(const Parameter &param)