bpp-core  2.2.0
BasicTNode.cpp
Go to the documentation of this file.
1 //
2 // File: BasicTNode.cpp
3 // Author: Sylvain Gaillard
4 // Created: 14/01/2011 14:59:07
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (January 12, 2011)
9 
10 This software is a computer program whose purpose is to provide utilitary
11 classes. This file belongs to the Bio++ Project.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "BasicTNode.h"
41 
42 using namespace bpp;
43 
44 #include <cstddef>
45 
46 using namespace std;
47 
49  if (father_) {
50  father_->removeSon(this);
51  }
52  for (size_t i = 0 ; i < sons_.size() ; i++) {
53  sons_[i]->removeFather();
54  }
55 }
56 
58  sons_(node.sons_),
59  father_(node.father_)
60 {};
61 
63  sons_ = node.sons_;
64  father_ = node.father_;
65  return * this;
66 }
67 
68 // Neighbors
69 
70 const BasicTNode* BasicTNode::getNeighbor(int pos) const {
71  if (pos < 0 || pos > static_cast<int>(sons_.size())) {
72  throw IndexOutOfBoundsException("BasicTNode::getNeighbor() pos is out of bounds", static_cast<size_t>(pos), 0, sons_.size());
73  }
74  if (pos == 0)
75  return father_;
76  else
77  return sons_[static_cast<size_t>(pos - 1)];
78 }
79 
81  if (pos < 0 || pos > static_cast<int>(sons_.size())) {
82  throw IndexOutOfBoundsException("BasicTNode::getNeighbor() pos is out of bounds", static_cast<size_t>(pos), 0, sons_.size());
83  }
84  if (pos == 0)
85  return father_;
86  else
87  return sons_[static_cast<size_t>(pos - 1)];
88 }
89 
90 const BasicTNode* BasicTNode::operator[](int i) const {
91  if (i < 0) {
92  return father_;
93  } else {
94  return sons_[static_cast<size_t>(i)];
95  }
96 }
97 
99  if (i < 0) {
100  return father_;
101  } else {
102  return sons_[static_cast<size_t>(i)];
103  }
104 }
105 
106 // Fathers
107 
108 const BasicTNode* BasicTNode::getFather(int pos) const {
109  if (pos != 0) {
110  throw IndexOutOfBoundsException("BasicTNode::getFather() pos must be 0 for TNode", static_cast<size_t>(pos), 0, 0);
111  }
112  return getFather();
113 }
114 
116  if (pos != 0) {
117  throw IndexOutOfBoundsException("BasicTNode::getFather() pos must be 0 for TNode", static_cast<size_t>(pos), 0, 0);
118  }
119  return getFather();
120 }
121 
123  return father_;
124 }
125 
127  return father_;
128 }
129 
130 bool BasicTNode::isFather(const BasicTNode* node) const {
131  if (father_ == node)
132  return true;
133  return false;
134 }
135 
137  if (!node)
138  throw NullPointerException("BasicTNode::addFather() Empty node given as input");
139  if (father_)
140  throw Exception("BasicTNode::addFather() This node already has a father.");
141  if (!isFather(node))
142  father_ = node;
143  if (!node->isSon(this))
144  node->addSon(this);
145 }
146 
148  if (hasFathers()) {
149  BasicTNode* father = father_;
150  father_ = 0;
151  father->removeSon(this);
152  return father;
153  }
154  return 0;
155 }
156 
157 // Sons
158 
159 const BasicTNode* BasicTNode::getSon(int pos) const {
160  if (pos < 0 || pos > static_cast<int>(sons_.size()) - 1) {
161  throw IndexOutOfBoundsException("BasicTNode::getSon() pos out of range", static_cast<size_t>(pos), 0, sons_.size() - 1);
162  }
163  return sons_[static_cast<size_t>(pos)];
164 }
165 
167  if (pos < 0 || pos > static_cast<int>(sons_.size()) - 1) {
168  throw IndexOutOfBoundsException("BasicTNode::getSon() pos out of range", static_cast<size_t>(pos), 0, sons_.size() - 1);
169  }
170  return sons_[static_cast<size_t>(pos)];
171 }
172 
173 bool BasicTNode::isSon(const BasicTNode* node) const {
174  for (size_t i = 0 ; i < sons_.size() ; i++) {
175  if (sons_[i] == node)
176  return true;
177  }
178  return false;
179 }
180 
182  if (!node)
183  throw NullPointerException("BasicTNode::addSon() Empty node given as input.");
184  if (!isSon(node))
185  sons_.push_back(node);
186  if (!node->isFather(this))
187  node->addFather(this);
188 }
189 
191  if (!node)
192  throw NullPointerException("BasicTNode::removeSon() Empty node given as input.");
193  for (size_t i = 0 ; i < sons_.size() ; i++) {
194  if (sons_[i] == node) {
195  sons_.erase(sons_.begin() + static_cast<ptrdiff_t>(i));
196  node->removeFather();
197  }
198  }
199 }
200 
202  if (pos < 0 || pos > static_cast<int>(sons_.size() - 1))
203  throw IndexOutOfBoundsException("BasicTNode::removeSon() pos out of bound", static_cast<size_t>(pos), 0, sons_.size() - 1);
204  BasicTNode* node = sons_[static_cast<size_t>(pos)];
205  sons_.erase(sons_.begin() + pos);
206  node->removeFather();
207  return node;
208 }
std::vector< BasicTNode *> sons_
Definition: BasicTNode.h:56
virtual void addFather(BasicTNode *node)
Add a father to this node.
Definition: BasicTNode.cpp:136
virtual void addSon(BasicTNode *node)
Add a son to this node.
Definition: BasicTNode.cpp:181
This class allows to perform a correspondence analysis.
virtual bool isSon(const BasicTNode *node) const
Tell if a node is son of this node.
Definition: BasicTNode.cpp:173
virtual BasicTNode * removeFather()
Remove the father of this node.
Definition: BasicTNode.cpp:147
STL namespace.
BasicTNode * father_
Definition: BasicTNode.h:57
virtual bool isFather(const BasicTNode *node) const
Tell if the node is a father of this node.
Definition: BasicTNode.cpp:130
const BasicTNode * getNeighbor(int pos) const
Get a neighbor of this node in const context.
Definition: BasicTNode.cpp:70
Simple implementation of TNode.
Definition: BasicTNode.h:54
BasicTNode & operator=(const BasicTNode &node)
Assignation operator.
Definition: BasicTNode.cpp:62
BasicTNode()
Simple constructor.
Definition: BasicTNode.h:63
The base class exception for NULL pointer error.
Definition: Exceptions.h:123
Exception base class.
Definition: Exceptions.h:57
const BasicTNode * getSon(int pos) const
Get a particular son in const environment.
Definition: BasicTNode.cpp:159
Index out of bounds exception class.
Definition: Exceptions.h:298
virtual void removeSon(BasicTNode *son)
Remove a son of this node.
Definition: BasicTNode.cpp:190
const BasicTNode * getFather() const
Get the father in const environment.
Definition: BasicTNode.cpp:122
const BasicTNode * operator[](int i) const
Direct access to a neighbor in const context.
Definition: BasicTNode.cpp:90
bool hasFathers() const
Tell if this node has one or more father nodes.
Definition: BasicTNode.h:103
virtual ~BasicTNode()
Destructor.
Definition: BasicTNode.cpp:48