bpp-core  2.1.0
BasicTNode.cpp
Go to the documentation of this file.
00001 // 
00002 // File:    BasicTNode.cpp
00003 // Author:  Sylvain Gaillard
00004 // Created: 14/01/2011 14:59:07
00005 // 
00006 
00007 /*
00008 Copyright or © or Copr. CNRS, (January 12, 2011)
00009 
00010 This software is a computer program whose purpose is to provide utilitary
00011 classes. This file belongs to the Bio++ Project.
00012 
00013 This software is governed by the CeCILL  license under French law and
00014 abiding by the rules of distribution of free software.  You can  use, 
00015 modify and/ or redistribute the software under the terms of the CeCILL
00016 license as circulated by CEA, CNRS and INRIA at the following URL
00017 "http://www.cecill.info". 
00018 
00019 As a counterpart to the access to the source code and  rights to copy,
00020 modify and redistribute granted by the license, users are provided only
00021 with a limited warranty  and the software's author,  the holder of the
00022 economic rights,  and the successive licensors  have only  limited
00023 liability. 
00024 
00025 In this respect, the user's attention is drawn to the risks associated
00026 with loading,  using,  modifying and/or developing or reproducing the
00027 software by the user in light of its specific status of free software,
00028 that may mean  that it is complicated to manipulate,  and  that  also
00029 therefore means  that it is reserved for developers  and  experienced
00030 professionals having in-depth computer knowledge. Users are therefore
00031 encouraged to load and test the software's suitability as regards their
00032 requirements in conditions enabling the security of their systems and/or 
00033 data to be ensured and,  more generally, to use and operate it in the 
00034 same conditions as regards security. 
00035 
00036 The fact that you are presently reading this means that you have had
00037 knowledge of the CeCILL license and that you accept its terms.
00038 */
00039 
00040 #include "BasicTNode.h"
00041 
00042 using namespace bpp;
00043 
00044 BasicTNode::~BasicTNode() {
00045   if (father_) {
00046     father_->removeSon(this);
00047   }
00048   for (size_t i = 0 ; i < sons_.size() ; i++) {
00049     sons_[i]->removeFather();
00050   }
00051 }
00052 
00053 BasicTNode::BasicTNode(const BasicTNode& node):
00054   sons_(node.sons_),
00055   father_(node.father_)
00056 {};
00057 
00058 BasicTNode& BasicTNode::operator=(const BasicTNode& node) {
00059   sons_ = node.sons_;
00060   father_ = node.father_;
00061   return * this;
00062 }
00063 
00064 // Neighbors
00065 
00066 const BasicTNode* BasicTNode::getNeighbor(int pos) const {
00067   if (pos < 0 || pos > static_cast<int>(sons_.size())) {
00068     throw IndexOutOfBoundsException("BasicTNode::getNeighbor() pos is out of bounds", pos, 0, static_cast<int>(sons_.size()));
00069   }
00070   if (pos == 0)
00071     return father_;
00072   else
00073     return sons_[static_cast<size_t>(pos - 1)];
00074 }
00075 
00076 BasicTNode* BasicTNode::getNeighbor(int pos) {
00077   if (pos < 0 || pos > static_cast<int>(sons_.size())) {
00078     throw IndexOutOfBoundsException("BasicTNode::getNeighbor() pos is out of bounds", pos, 0, static_cast<int>(sons_.size()));
00079   }
00080   if (pos == 0)
00081     return father_;
00082   else
00083     return sons_[static_cast<size_t>(pos - 1)];
00084 }
00085 
00086 const BasicTNode* BasicTNode::operator[](int i) const {
00087   if (i < 0) {
00088     return father_;
00089   } else {
00090     return sons_[static_cast<size_t>(i)];
00091   }
00092 }
00093 
00094 BasicTNode* BasicTNode::operator[](int i) {
00095   if (i < 0) {
00096     return father_;
00097   } else {
00098     return sons_[static_cast<size_t>(i)];
00099   }
00100 }
00101 
00102 // Fathers
00103 
00104 const BasicTNode* BasicTNode::getFather(int pos) const {
00105   if (pos != 0) {
00106     throw IndexOutOfBoundsException("BasicTNode::getFather() pos must be 0 for TNode", pos, 0, 0);
00107   }
00108   return getFather();
00109 }
00110 
00111 BasicTNode* BasicTNode::getFather(int pos) {
00112   if (pos != 0) {
00113     throw IndexOutOfBoundsException("BasicTNode::getFather() pos must be 0 for TNode", pos, 0, 0);
00114   }
00115   return getFather();
00116 }
00117 
00118 const BasicTNode* BasicTNode::getFather() const {
00119   return father_;
00120 }
00121 
00122 BasicTNode* BasicTNode::getFather() {
00123   return father_;
00124 }
00125 
00126 bool BasicTNode::isFather(const BasicTNode* node) const {
00127   if (father_ == node)
00128     return true;
00129   return false;
00130 }
00131 
00132 void BasicTNode::addFather(BasicTNode* node) {
00133   if (!node)
00134     throw NullPointerException("BasicTNode::addFather() Empty node given as input");
00135   if (father_)
00136     throw Exception("BasicTNode::addFather() This node already has a father.");
00137   if (!isFather(node))
00138     father_ = node;
00139   if (!node->isSon(this))
00140     node->addSon(this);
00141 }
00142 
00143 BasicTNode* BasicTNode::removeFather() {
00144   if (hasFathers()) {
00145     BasicTNode* father = father_;
00146     father_ = 0;
00147     father->removeSon(this);
00148     return father;
00149   }
00150   return 0;
00151 }
00152 
00153 // Sons
00154 
00155 const BasicTNode* BasicTNode::getSon(int pos) const {
00156   if (pos < 0 || pos > static_cast<int>(sons_.size()) - 1) {
00157     throw IndexOutOfBoundsException("BasicTNode::getSon() pos out of range", pos, 0, sons_.size() - 1);
00158   }
00159   return sons_[static_cast<size_t>(pos)];
00160 }
00161 
00162 BasicTNode* BasicTNode::getSon(int pos) {
00163   if (pos < 0 || pos > static_cast<int>(sons_.size()) - 1) {
00164     throw IndexOutOfBoundsException("BasicTNode::getSon() pos out of range", pos, 0, sons_.size() - 1);
00165   }
00166   return sons_[static_cast<size_t>(pos)];
00167 }
00168 
00169 bool BasicTNode::isSon(const BasicTNode* node) const {
00170   for (size_t i = 0 ; i < sons_.size() ; i++) {
00171     if (sons_[i] == node)
00172       return true;
00173   }
00174   return false;
00175 }
00176 
00177 void BasicTNode::addSon(BasicTNode* node) {
00178   if (!node)
00179     throw NullPointerException("BasicTNode::addSon() Empty node given as input.");
00180   if (!isSon(node))
00181     sons_.push_back(node);
00182   if (!node->isFather(this))
00183     node->addFather(this);
00184 }
00185 
00186 void BasicTNode::removeSon(BasicTNode* node) {
00187   if (!node)
00188     throw NullPointerException("BasicTNode::removeSon() Empty node given as input.");
00189   for (size_t i = 0 ; i < sons_.size() ; i++) {
00190     if (sons_[i] == node) {
00191       sons_.erase(sons_.begin() + i);
00192       node->removeFather();
00193     }
00194   }
00195 }
00196 
00197 BasicTNode* BasicTNode::removeSon(int pos) {
00198   if (pos < 0 || pos > static_cast<int>(sons_.size() - 1))
00199     throw IndexOutOfBoundsException("BasicTNode::removeSon() pos out of bound", pos, 0, sons_.size() - 1);
00200   BasicTNode* node = sons_[static_cast<size_t>(pos)];
00201   sons_.erase(sons_.begin() + pos);
00202   node->removeFather();
00203   return node;
00204 }
 All Classes Namespaces Files Functions Variables Typedefs Friends