bpp-phyl3 3.0.0
AwareNode.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#ifndef BPP_PHYL_LEGACY_TREE_AWARENODE_H
6#define BPP_PHYL_LEGACY_TREE_AWARENODE_H
7
8#include <Bpp/BppString.h>
9#include <Bpp/Clonable.h>
10#include <Bpp/Numeric/Number.h>
11#include <Bpp/Utils/MapTools.h>
12
13#include "../../Tree/TreeExceptions.h"
14
15// From the STL:
16#include <string>
17#include <vector>
18#include <map>
19#include <iostream>
20#include <algorithm>
21#include <cstddef>
22#include <memory>
23
25
26namespace bpp
27{
41class AwareNode // :
42 // public std::enable_shared_from_this<AwareNode>
43{
44protected:
45 unsigned int id_;
46 std::vector<AwareNode* > sons_;
49
50public:
55 id_(0),
56 sons_(),
57 father_(0),
59 {}
60
64 AwareNode(unsigned int id) :
65 id_(id),
66 sons_(),
67 father_(0),
69 {}
70
81 AwareNode(const AwareNode& node);
82
94 AwareNode& operator=(const AwareNode& node);
95
96 AwareNode* clone() const { return new AwareNode(*this); }
97
98 virtual ~AwareNode()
99 {}
100
101public:
106 template<class N, class E, class I>
107 void updateTree(AssociationTreeGraphImplObserver<N, E, I>* tree, unsigned int index);
108
109
121 virtual unsigned int getId() const { return id_; }
122
128 virtual void setId(unsigned int id) { id_ = id; }
129
130 virtual std::vector<unsigned int> getSonsId() const
131 {
132 std::vector<unsigned int> sonsId(sons_.size());
133 for (size_t i = 0; i < sons_.size(); i++)
134 {
135 sonsId[i] = sons_[i]->getId();
136 }
137 return sonsId;
138 }
139
154 virtual double getDistanceToFather() const
155 {
156 return distanceToFather_;
157 }
158
168 virtual void setDistanceToFather(double distance)
169 {
170 distanceToFather_ = distance;
171 }
172
186 virtual const AwareNode* getFather() const { return father_; }
187
193 virtual AwareNode* getFather() { return father_; }
194
195 // virtual int getFatherId() const { return father_->getId(); }
196
202 virtual void setFather(AwareNode* node)
203 {
204 father_ = node;
205 }
206
210 virtual void removeFather()
211 {
212 father_ = 0;
213 }
214
218 virtual bool hasFather() const { return father_ != 0; }
219
227 virtual size_t getNumberOfSons() const { return sons_.size(); }
228
229 bool isLeaf() const
230 {
231 return getNumberOfSons() == 0;
232 }
233
234 virtual std::vector<AwareNode*>& getSons()
235 {
236 return sons_;
237 }
238
239 virtual const AwareNode* getSon(size_t pos) const
240 {
241 if (pos >= sons_.size()) throw IndexOutOfBoundsException("AwareNode::getSon().", pos, 0, sons_.size() - 1);
242 return sons_[pos];
243 }
244
245 virtual AwareNode* getSon(size_t pos)
246 {
247 if (pos >= sons_.size()) throw IndexOutOfBoundsException("AwareNode::getSon().", pos, 0, sons_.size() - 1);
248 return sons_[pos];
249 }
250
251 virtual void addSon(size_t pos, AwareNode* node)
252 {
253 if (!node)
254 throw NullPointerException("AwareNode::addSon(). Empty node given as input.");
255 if (find(sons_.begin(), sons_.end(), node) == sons_.end())
256 sons_.insert(sons_.begin() + static_cast<ptrdiff_t>(pos), node);
257 else // Otherwise node is already present.
258 std::cerr << "DEVEL warning: AwareNode::addSon. Son node already registered! No pb here, but could be a bug in your implementation..." << std::endl;
259 }
260
261 virtual void addSon(AwareNode* node)
262 {
263 if (!node)
264 throw NullPointerException("AwareNode::addSon(). Empty node given as input.");
265 if (find(sons_.begin(), sons_.end(), node) == sons_.end())
266 sons_.push_back(node);
267 else // Otherwise node is already present.
268 throw Exception("AwareNode::addSon. Trying to add a node which is already present.");
269 }
270
271 virtual void setSon(size_t pos, AwareNode* node)
272 {
273 if (!node)
274 throw NullPointerException("AwareNode::setSon(). Empty node given as input.");
275 if (pos >= sons_.size())
276 throw IndexOutOfBoundsException("AwareNode::setSon(). Invalid node position.", pos, 0, sons_.size() - 1);
277 std::vector<AwareNode*>::iterator search = find(sons_.begin(), sons_.end(), node);
278 if (search == sons_.end() || search == sons_.begin() + static_cast<ptrdiff_t>(pos))
279 sons_[pos] = node;
280 else
281 throw Exception("AwareNode::setSon. Trying to set a node which is already present.");
282 }
283
284 virtual void removeSon(size_t pos)
285 {
286 if (pos >= sons_.size())
287 throw IndexOutOfBoundsException("AwareNode::removeSon(). Invalid node position.", pos, 0, sons_.size() - 1);
288 sons_.erase(sons_.begin() + static_cast<ptrdiff_t>(pos));
289 }
290
291 virtual void removeSon(AwareNode* node)
292 {
293 if (!node)
294 throw NullPointerException("AwareNode::removeSon(). Empty node given as input.");
295 for (size_t i = 0; i < sons_.size(); i++)
296 {
297 if (sons_[i] == node)
298 {
299 sons_.erase(sons_.begin() + static_cast<ptrdiff_t>(i));
300 return;
301 }
302 }
303 throw Exception("AwareNode::removeSon, not found node : " + TextTools::toString(node->getId()));
304 }
305
306 virtual void removeSons()
307 {
308 sons_.clear();
309 }
310
311 virtual void swap(size_t branch1, size_t branch2);
312
313 virtual size_t getSonPosition(const AwareNode* son) const;
314
325 AwareNode* operator[](int i) { return (i < 0) ? father_ : sons_[static_cast<std::size_t>(i)]; }
326
327 const AwareNode* operator[](int i) const { return (i < 0) ? father_ : sons_[static_cast<std::size_t>(i)]; }
328
331 virtual bool hasNoSon() const { return getNumberOfSons() == 0; }
332};
333
334template<class N, class E, class I>
336{
337 id_ = index;
338
339 std::shared_ptr<N> thisN = tree->getNode(index);
340 father_ = tree->hasFather(thisN) ? dynamic_cast<AwareNode*>(tree->getFatherOfNode(thisN).get()) : 0;
341
342 sons_.clear();
343 std::vector< std::shared_ptr<N>> vS = tree->getSons(thisN);
344
345 for (typename std::vector<std::shared_ptr<N>>::iterator it = vS.begin(); it != vS.end(); it++)
346 {
347 sons_.push_back(dynamic_cast<AwareNode*>(it->get()));
348 }
349}
350} // end of namespace bpp.
351#endif // BPP_PHYL_LEGACY_TREE_AWARENODE_H
virtual std::shared_ptr< N > getNode(NodeIndex nodeIndex) const=0
std::vector< std::shared_ptr< N > > getSons(const std::shared_ptr< N > node) const
bool hasFather(const std::shared_ptr< N > nodeObject) const
std::shared_ptr< N > getFatherOfNode(const std::shared_ptr< N > nodeObject) const
A node class aware of its neighbours.
Definition: AwareNode.h:43
virtual ~AwareNode()
Definition: AwareNode.h:98
virtual AwareNode * getFather()
Get the father of this node is there is one.
Definition: AwareNode.h:193
AwareNode()
Build a new void Node object.
Definition: AwareNode.h:54
AwareNode & operator=(const AwareNode &node)
Assignation operator.
Definition: AwareNode.cpp:23
virtual bool hasNoSon() const
Definition: AwareNode.h:331
virtual size_t getNumberOfSons() const
Definition: AwareNode.h:227
virtual const AwareNode * getFather() const
Get the father of this node is there is one.
Definition: AwareNode.h:186
AwareNode * clone() const
Definition: AwareNode.h:96
AwareNode * father_
Definition: AwareNode.h:47
virtual const AwareNode * getSon(size_t pos) const
Definition: AwareNode.h:239
virtual size_t getSonPosition(const AwareNode *son) const
Definition: AwareNode.cpp:51
virtual AwareNode * getSon(size_t pos)
Definition: AwareNode.h:245
AwareNode * operator[](int i)
Definition: AwareNode.h:325
unsigned int id_
Definition: AwareNode.h:45
const AwareNode * operator[](int i) const
Definition: AwareNode.h:327
AwareNode(unsigned int id)
Build a new Node with specified id.
Definition: AwareNode.h:64
virtual bool hasFather() const
Tell if this node has a father node.
Definition: AwareNode.h:218
virtual void setId(unsigned int id)
Set this node's id.
Definition: AwareNode.h:128
virtual std::vector< unsigned int > getSonsId() const
Definition: AwareNode.h:130
virtual void removeSon(size_t pos)
Definition: AwareNode.h:284
virtual void setSon(size_t pos, AwareNode *node)
Definition: AwareNode.h:271
virtual void removeSons()
Definition: AwareNode.h:306
virtual void swap(size_t branch1, size_t branch2)
Definition: AwareNode.cpp:35
virtual void removeSon(AwareNode *node)
Definition: AwareNode.h:291
virtual void addSon(AwareNode *node)
Definition: AwareNode.h:261
bool isLeaf() const
Definition: AwareNode.h:229
virtual void removeFather()
Remove the father of this node.
Definition: AwareNode.h:210
double distanceToFather_
Definition: AwareNode.h:48
virtual std::vector< AwareNode * > & getSons()
Definition: AwareNode.h:234
std::vector< AwareNode * > sons_
Definition: AwareNode.h:46
virtual unsigned int getId() const
Get the node's id.
Definition: AwareNode.h:121
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
Definition: AwareNode.h:168
virtual void setFather(AwareNode *node)
Set the father node of this node.
Definition: AwareNode.h:202
void updateTree(AssociationTreeGraphImplObserver< N, E, I > *tree, unsigned int index)
update information from TreeObserver
Definition: AwareNode.h:335
virtual void addSon(size_t pos, AwareNode *node)
Definition: AwareNode.h:251
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: AwareNode.h:154
std::string toString(T t)
Defines the basic types of data flow nodes.