bpp-core  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends
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. CNRS, (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 
45  if (father_) {
46  father_->removeSon(this);
47  }
48  for (size_t i = 0 ; i < sons_.size() ; i++) {
49  sons_[i]->removeFather();
50  }
51 }
52 
54  sons_(node.sons_),
55  father_(node.father_)
56 {};
57 
59  sons_ = node.sons_;
60  father_ = node.father_;
61  return * this;
62 }
63 
64 // Neighbors
65 
66 const BasicTNode* BasicTNode::getNeighbor(int pos) const {
67  if (pos < 0 || pos > static_cast<int>(sons_.size())) {
68  throw IndexOutOfBoundsException("BasicTNode::getNeighbor() pos is out of bounds", pos, 0, static_cast<int>(sons_.size()));
69  }
70  if (pos == 0)
71  return father_;
72  else
73  return sons_[static_cast<size_t>(pos - 1)];
74 }
75 
77  if (pos < 0 || pos > static_cast<int>(sons_.size())) {
78  throw IndexOutOfBoundsException("BasicTNode::getNeighbor() pos is out of bounds", pos, 0, static_cast<int>(sons_.size()));
79  }
80  if (pos == 0)
81  return father_;
82  else
83  return sons_[static_cast<size_t>(pos - 1)];
84 }
85 
86 const BasicTNode* BasicTNode::operator[](int i) const {
87  if (i < 0) {
88  return father_;
89  } else {
90  return sons_[static_cast<size_t>(i)];
91  }
92 }
93 
95  if (i < 0) {
96  return father_;
97  } else {
98  return sons_[static_cast<size_t>(i)];
99  }
100 }
101 
102 // Fathers
103 
104 const BasicTNode* BasicTNode::getFather(int pos) const {
105  if (pos != 0) {
106  throw IndexOutOfBoundsException("BasicTNode::getFather() pos must be 0 for TNode", pos, 0, 0);
107  }
108  return getFather();
109 }
110 
112  if (pos != 0) {
113  throw IndexOutOfBoundsException("BasicTNode::getFather() pos must be 0 for TNode", pos, 0, 0);
114  }
115  return getFather();
116 }
117 
119  return father_;
120 }
121 
123  return father_;
124 }
125 
126 bool BasicTNode::isFather(const BasicTNode* node) const {
127  if (father_ == node)
128  return true;
129  return false;
130 }
131 
133  if (!node)
134  throw NullPointerException("BasicTNode::addFather() Empty node given as input");
135  if (father_)
136  throw Exception("BasicTNode::addFather() This node already has a father.");
137  if (!isFather(node))
138  father_ = node;
139  if (!node->isSon(this))
140  node->addSon(this);
141 }
142 
144  if (hasFathers()) {
145  BasicTNode* father = father_;
146  father_ = 0;
147  father->removeSon(this);
148  return father;
149  }
150  return 0;
151 }
152 
153 // Sons
154 
155 const BasicTNode* BasicTNode::getSon(int pos) const {
156  if (pos < 0 || pos > static_cast<int>(sons_.size()) - 1) {
157  throw IndexOutOfBoundsException("BasicTNode::getSon() pos out of range", pos, 0, sons_.size() - 1);
158  }
159  return sons_[static_cast<size_t>(pos)];
160 }
161 
163  if (pos < 0 || pos > static_cast<int>(sons_.size()) - 1) {
164  throw IndexOutOfBoundsException("BasicTNode::getSon() pos out of range", pos, 0, sons_.size() - 1);
165  }
166  return sons_[static_cast<size_t>(pos)];
167 }
168 
169 bool BasicTNode::isSon(const BasicTNode* node) const {
170  for (size_t i = 0 ; i < sons_.size() ; i++) {
171  if (sons_[i] == node)
172  return true;
173  }
174  return false;
175 }
176 
178  if (!node)
179  throw NullPointerException("BasicTNode::addSon() Empty node given as input.");
180  if (!isSon(node))
181  sons_.push_back(node);
182  if (!node->isFather(this))
183  node->addFather(this);
184 }
185 
187  if (!node)
188  throw NullPointerException("BasicTNode::removeSon() Empty node given as input.");
189  for (size_t i = 0 ; i < sons_.size() ; i++) {
190  if (sons_[i] == node) {
191  sons_.erase(sons_.begin() + i);
192  node->removeFather();
193  }
194  }
195 }
196 
198  if (pos < 0 || pos > static_cast<int>(sons_.size() - 1))
199  throw IndexOutOfBoundsException("BasicTNode::removeSon() pos out of bound", pos, 0, sons_.size() - 1);
200  BasicTNode* node = sons_[static_cast<size_t>(pos)];
201  sons_.erase(sons_.begin() + pos);
202  node->removeFather();
203  return node;
204 }