-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeContainer.cpp
More file actions
185 lines (153 loc) · 4.87 KB
/
nodeContainer.cpp
File metadata and controls
185 lines (153 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* hybrid-coal is used to compute gene tree probabilities given species network under coalescent process.
*
* Copyright (C) 2010 -- 2015 Sha (Joe) Zhu
*
* This file is part of hybrid-coal
*
* hybrid-coal is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nodeContainer.hpp"
#include <map>
NodeContainer::NodeContainer() {
this->last_node_ = NULL;
this->first_node_ = NULL;
this->size_ = 0;
}
// Used for copy Trees
NodeContainer::NodeContainer( const NodeContainer &nc) {
this->size_ = 0;
this->last_node_ = NULL;
this->first_node_ = NULL;
std::map<Node const*, Node*> node_mapping;
node_mapping[NULL] = NULL;
for ( auto it = nc.iterator(); it.good(); ++it ) {
Node *node = new Node(**it);
node_mapping[*it] = node;
node->child.clear(); // clear child nodes
this->add(node);
}
for ( auto it = iterator(); it.good(); ++it ) {
if ( (*it)->parent1() != NULL ) {
(*it)->set_parent1 ( node_mapping[(*it)->parent1()] );
(*it)->parent1()->child.push_back( (*it) );
}
if ( (*it)->parent2() != NULL ) {
(*it)->set_parent2 ( node_mapping[(*it)->parent2()] );
(*it)->parent2()->child.push_back( (*it) );
}
}
//unsorted_node_ = node_mapping[nc.unsorted_node_];
}
Node* NodeContainer::at(size_t nr) const {
Node* current = first();
for (size_t i = 0; i < nr; ++i) {
assert(current != NULL);
current = current->next();
}
if ( current == NULL ) throw std::out_of_range("NodeContainer out of range");
return current;
}
// Adds 'node' to the container
void NodeContainer::add( Node* node ) {
++size_;
if ( this->first() == NULL ) {
this->set_first( node );
this->set_last( node );
return;
}
assert( this->first() != NULL );
// Currently not used!
// Adding to the front
//node->set_next( this->first() );
//node->set_previous( NULL );
//this->first()->set_previous(node);
//this->set_first( node );
//return;
//Adding to the End, similar to push_back
node->set_previous(this->back());
node->set_next(NULL);
this->back()->set_next(node);
this->set_last(node);
return;
}
void NodeContainer::remove( Node *node ) {
--size_;
if ( node->is_first() && node->is_last() ) {
this->set_first(NULL);
this->set_last(NULL);
}
else if ( node->is_first() ) {
this->set_first(node->next());
node->next()->set_previous(NULL);
}
else if ( node->is_last() ) {
this->set_last(node->previous());
node->previous()->set_next(NULL);
}
else {
node->previous()->set_next(node->next());
node->next()->set_previous(node->previous());
}
delete node;
//assert( this->sorted() );
}
//void NodeContainer::move(Node *node, const double new_height) {
//assert( node != NULL );
//// Stupid edge case first: We may have only one node.
//if ( node->is_first() && node->is_last() ) {
//node->set_height(new_height);
//return;
//}
//// Remove from old place
//remove(node, false);
//// Add at new place
//Node* current = NULL;
//if ( node->height() < new_height ) {
//if ( node->is_first() ) current = NULL;
//else current = node->previous();
//} else {
//current = first();
//}
//node->set_height(new_height);
//this->add(node, current);
//assert( this->sorted() );
//}
// Removes all nodes;
// The loop deletes the node from the previous iteration because we still need
// the current node for calling ++it.
void NodeContainer::clear() {
Node* tmp = NULL;
for ( NodeIterator it = this->iterator(); it.good(); ++it ) {
if (tmp != NULL) delete tmp;
tmp = *it;
}
if ( tmp != NULL ) delete tmp;
this->set_first( NULL );
this->set_last( NULL );
this->size_ = 0;
}
void NodeContainer::add_before(Node* add, Node* before){
add->set_next(before);
add->set_previous(before->previous());
if ( add->previous() != NULL ) add->previous()->set_next(add);
before->set_previous(add);
if ( add->is_last() ) this->set_last(add);
}
void swap(NodeContainer& a, NodeContainer& b) {
using std::swap;
swap(a.first_node_, b.first_node_);
swap(a.last_node_, b.last_node_);
swap(a.size_, b.size_);
}