~ubuntu-branches/ubuntu/saucy/libbpp-phyl/saucy-proposed

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/NodeTemplate.h

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
Import upstream version 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// File: NodeTemplate.h
 
3
// Created by: Vincent Ranwez
 
4
//             Julien Dutheil
 
5
// Created on: Thu Jun 28 18:11 2005
 
6
//
 
7
 
 
8
/*
 
9
Copyright or © or Copr. Julien Dutheil, (November 16, 2004)
 
10
 
 
11
Julien.Dutheil@univ-montp2.fr
 
12
 
 
13
This software is a computer program whose purpose is to provide classes
 
14
for phylogenetic data analysis.
 
15
 
 
16
This software is governed by the CeCILL  license under French law and
 
17
abiding by the rules of distribution of free software.  You can  use, 
 
18
modify and/ or redistribute the software under the terms of the CeCILL
 
19
license as circulated by CEA, CNRS and INRIA at the following URL
 
20
"http://www.cecill.info". 
 
21
 
 
22
As a counterpart to the access to the source code and  rights to copy,
 
23
modify and redistribute granted by the license, users are provided only
 
24
with a limited warranty  and the software's author,  the holder of the
 
25
economic rights,  and the successive licensors  have only  limited
 
26
liability. 
 
27
 
 
28
In this respect, the user's attention is drawn to the risks associated
 
29
with loading,  using,  modifying and/or developing or reproducing the
 
30
software by the user in light of its specific status of free software,
 
31
that may mean  that it is complicated to manipulate,  and  that  also
 
32
therefore means  that it is reserved for developers  and  experienced
 
33
professionals having in-depth computer knowledge. Users are therefore
 
34
encouraged to load and test the software's suitability as regards their
 
35
requirements in conditions enabling the security of their systems and/or 
 
36
data to be ensured and,  more generally, to use and operate it in the 
 
37
same conditions as regards security. 
 
38
 
 
39
The fact that you are presently reading this means that you have had
 
40
knowledge of the CeCILL license and that you accept its terms.
 
41
*/
 
42
 
 
43
#ifndef _NODETEMPLATE_H_
 
44
#define _NODETEMPLATE_H_
 
45
 
 
46
#include "Node.h"
 
47
 
 
48
namespace bpp
 
49
{
 
50
 
 
51
/**
 
52
 * @brief The NodeTemplate class.
 
53
 *
 
54
 * This class inherits from the Node class.
 
55
 * Its is a generic way to store any information to a node.
 
56
 * A NodeTemplate only add the setInfos and getInfos methods,
 
57
 * which set and retrieve a NodeInfo, whose type is given as a template
 
58
 * of the class.
 
59
 * This class is mainly for computation conveniency, one may define a NodeInfo
 
60
 * class with several results attached.
 
61
 * An example is provided in the PGMA class.
 
62
 * Another way is to use a map<Node *, NodeInfos>, with the limitation of the
 
63
 * map access.
 
64
 * One may also wish to use the property system of the Node class, but
 
65
 * properties are stored as in a map<string, Clonable *>, with the drawbacks
 
66
 * of the slow map access and the systematic use of dynamic_cast<NodeInfo *> to
 
67
 * convert from Clonable *.
 
68
 *
 
69
 * This class redefines all constructors and access methods (get*) with return
 
70
 * types as NodeTemplate and not Node (using covariant returns).
 
71
 *
 
72
 * @see Node, TreeTemplate
 
73
 */
 
74
template<class NodeInfos>
 
75
class NodeTemplate : public Node
 
76
{
 
77
        
 
78
        private:
 
79
 
 
80
                NodeInfos infos_;
 
81
 
 
82
        public:
 
83
                
 
84
                /**     
 
85
                 * @brief Build a new void NodeTemplate object.
 
86
                 */
 
87
                NodeTemplate() : Node(), infos_() {}
 
88
                        
 
89
                /**
 
90
                 * @brief Build a new NodeTemplate with specified id.
 
91
                 */
 
92
                NodeTemplate(int id) : Node(id), infos_() {}
 
93
 
 
94
                /**
 
95
                 * @brief Build a new NodeTemplate with specified name.
 
96
                 */
 
97
                NodeTemplate(const std::string& name) : Node(name), infos_() {}
 
98
 
 
99
                /**
 
100
                 * @brief Build a new NodeTemplate with specified id and name.
 
101
                 */
 
102
                NodeTemplate(int id, const std::string& name) : Node(id, name), infos_() {}
 
103
 
 
104
                /**
 
105
                 * @brief Copy constructor.
 
106
                 * 
 
107
                 * @param node The node to copy.
 
108
                 */
 
109
                NodeTemplate(const Node& node) : Node(node), infos_() {}
 
110
 
 
111
                /**
 
112
                 * @brief Copy constructor.
 
113
                 * 
 
114
                 * @param node The node to copy.
 
115
                 */
 
116
                NodeTemplate(const NodeTemplate<NodeInfos>& node):
 
117
      Node(node), infos_(node.infos_)
 
118
    {}
 
119
 
 
120
                /**
 
121
                 * @brief Assignation operator.
 
122
                 *
 
123
                 * @param node the node to copy.
 
124
                 * @return A reference toward this node.
 
125
                 */
 
126
                NodeTemplate<NodeInfos>& operator=(const NodeTemplate<NodeInfos>& node)
 
127
                {
 
128
      Node::operator=(node);
 
129
                        infos_ = node.infos_;
 
130
                        return *this;
 
131
                }
 
132
 
 
133
                virtual ~NodeTemplate() {}
 
134
 
 
135
    NodeTemplate<NodeInfos>* clone() const { return new NodeTemplate<NodeInfos>(*this); }
 
136
 
 
137
  public:
 
138
 
 
139
                const NodeTemplate<NodeInfos>* getFather() const { return dynamic_cast<const NodeTemplate<NodeInfos> *>(father_); }
 
140
 
 
141
                NodeTemplate<NodeInfos>* getFather() { return dynamic_cast<NodeTemplate<NodeInfos> *>(father_); }
 
142
                                
 
143
                NodeTemplate<NodeInfos>* removeFather() { NodeTemplate<NodeInfos> * f = dynamic_cast<NodeTemplate<NodeInfos> *>(father_); father_ = 0; return f; }
 
144
 
 
145
                const NodeTemplate<NodeInfos>* getSon(unsigned int i) const throw (IndexOutOfBoundsException) { return dynamic_cast<NodeTemplate<NodeInfos> *>(sons_[i]); }
 
146
                                
 
147
                NodeTemplate<NodeInfos>* getSon(unsigned int i) throw (IndexOutOfBoundsException) { return dynamic_cast<NodeTemplate<NodeInfos> *>(sons_[i]); }
 
148
                                
 
149
    std::vector<const NodeTemplate<NodeInfos>*> getNeighbors() const
 
150
                {
 
151
      std::vector<const Node*> neighbors = Node::getNeighbors();
 
152
      std::vector<const NodeTemplate<NodeInfos>*> neighbors2(neighbors.size());
 
153
                        for (unsigned int i = 0; i < neighbors.size(); i++)
 
154
                                neighbors2[i] = dynamic_cast<const NodeTemplate<NodeInfos>*>(neighbors[i]);
 
155
                        return neighbors2;
 
156
                }
 
157
                
 
158
    std::vector<NodeTemplate<NodeInfos>*> getNeighbors()
 
159
                {
 
160
      std::vector<Node*> neighbors = Node::getNeighbors();
 
161
      std::vector<NodeTemplate<NodeInfos>*> neighbors2(neighbors.size());
 
162
                        for (unsigned int i = 0; i < neighbors.size(); i++)
 
163
                                neighbors2[i] = dynamic_cast<NodeTemplate<NodeInfos>*>(neighbors[i]);
 
164
                        return neighbors2;
 
165
                }
 
166
                
 
167
                NodeTemplate<NodeInfos>* operator[](int i) { return dynamic_cast<NodeTemplate<NodeInfos>*>((i < 0) ? father_ : sons_[i]); }
 
168
                                
 
169
                const NodeTemplate<NodeInfos>* operator[](int i) const { return dynamic_cast<const NodeTemplate<NodeInfos> *>((i < 0) ? father_ : sons_[i]); }
 
170
 
 
171
 
 
172
                // Specific methods:
 
173
 
 
174
    /**
 
175
     * @return A reference toward the information object associated to this node.
 
176
     */
 
177
                virtual const NodeInfos& getInfos() const { return infos_; }
 
178
                
 
179
    /**
 
180
     * @return A reference toward the information object associated to this node.
 
181
     */
 
182
                virtual NodeInfos& getInfos() { return infos_; }
 
183
 
 
184
    /**
 
185
     * @brief Set the information to be associated to this node.
 
186
     * 
 
187
     * @param infos An information object.
 
188
     */
 
189
                virtual void setInfos(const NodeInfos& infos) { infos_ = infos; }
 
190
 
 
191
};
 
192
 
 
193
} //end of namespace bpp.
 
194
 
 
195
#endif  //_NODETEMPLATE_H_
 
196