Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / clustalw / src / tree / UPGMA / Node.cpp
1 #ifdef HAVE_CONFIG_H
2     #include "config.h"
3 #endif
4 #include "Node.h"
5 #include "../../general/VectorUtility.h"
6 #include "../../general/debuglogObject.h"
7 #include "../../general/clustalw.h"
8 #include <iostream>
9 #include <sstream>
10 namespace clustalw
11
12 {
13
14 Node::Node(int _seqNum, double *dists, int numDist)
15     : next(0),
16       left(0),
17       right(0),
18       size(1),
19       seqNum(_seqNum),
20       height(0.0),
21       ptrToDistMatRow(dists),
22       minDist(numeric_limits<double>::max()),
23       indexToMinDist(-1),
24       numDists(numDist),
25       order(0)
26 {
27     allElements.resize(1);
28     allElements[0] = seqNum;
29                     
30     if (ptrToDistMatRow)
31     {
32         findMinDist();
33     }
34 }
35
36
37 void Node::merge(Node **rightNode, double _height)
38 {
39     left = new Node(*this);
40     right = *rightNode;
41
42     left->ptrToDistMatRow = 0;
43     size = left->size + right->size;
44     seqNum = -1;
45     height = _height;
46     left->height = height;
47     right->height = height;
48     
49     vectorutils::mergeVectors(&allElements, &(right->allElements));
50     right->allElements.clear();      
51     
52     if (next == right)
53     {
54         next = right->next;
55     }
56     else
57     {    
58         *rightNode = right->next;
59     }
60 }
61
62
63 void Node::findMinDist()
64 {    
65     double *distIterator = ptrToDistMatRow;
66     double *minDistSoFar = distIterator++;
67     
68     // We search from the end of our area of the array       
69     for(int i = numDists; --i; distIterator++) // When --i gets to zero it will stop
70     {    
71         if ((*distIterator >= 0) && (*distIterator < *minDistSoFar))
72         {
73             minDistSoFar = distIterator;
74         }
75     }
76     
77     minDist = *minDistSoFar;
78     indexToMinDist = minDistSoFar - ptrToDistMatRow;
79 }
80  
81 void Node::printElements()
82 {
83     for(int i = 0; i < (int)allElements.size(); i++)
84     {
85         cout << " " << allElements[i];
86     }
87     cout << "\n";               
88 }
89
90 string Node::elementsToString()
91 {
92     ostringstream elems;
93     for(int i = 0; i < (int)allElements.size(); i++)
94     {
95         elems << " " << allElements[i];
96     }
97     return elems.str();
98 }
99
100 void Node::makeEmpty()
101 {
102     makeEmpty(this);
103 }
104
105 void Node::makeEmpty(Node* t)
106 {
107     if(t != 0)
108     {
109         makeEmpty(t->left);
110         makeEmpty(t->right);
111         delete t;
112     }
113     t = 0;
114 }
115                                            
116 }