ARB
AP_buffer.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : AP_buffer.cxx //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // =============================================================== //
10 
11 #include "AP_buffer.hxx"
12 #include "ap_main.hxx"
13 
14 #include <iostream>
15 #include <fstream>
16 #include <algorithm>
17 
18 using namespace std;
19 
20 #if defined(PROVIDE_PRINT)
21 
22 inline string space(int count) {
23  return string(count, ' ');
24 }
25 
26 void NodeState::print(ostream& out, int indentLevel) const {
27  out << space(indentLevel)
28  << "NodeState=" << this
29  << " frameNr=" << frameNr << " mode=" << mode;
30  if (mode & STRUCTURE) {
31  out << " father=" << father << " lson=" << leftson << " rson=" << rightson;
32  out << " edges={";
33  for (int e = 0; e<3; ++e) {
34  out << " e[" << e << "]=" << edge[e] << "[" << edgeIndex[e] << "]";
35  }
36  out << " }";
37  }
38  if (mode & SEQUENCE) {
39  out << " sequence=" << sequence;
40  }
41  out << endl;
42 }
43 
44 void NodeStack::print(ostream& out, int indentLevel, Level frameNr) const {
45  size_t i = count_elements();
46  out << space(indentLevel) << "NodeStack=" << this << " size " << i << " frameNr=" << frameNr << endl;
47  for (NodeStack::const_iterator e = begin(); e != end(); ++e, --i) {
48  const AP_tree_nlen *node = *e;
49  out << space(indentLevel+1) << '[' << i << "] AP_tree_nlen*=" << node << " remembered_for_frame=" << node->last_remembered_frame() << endl;
50  node->get_states().print(out, indentLevel+2);
51  }
52 }
53 
54 void StateStack::print(ostream& out, int indentLevel) const {
55  size_t i = count_elements();
56  out << space(indentLevel) << "StateStack=" << this << " size " << i << endl;
57  for (StateStack::const_iterator e = begin(); e != end(); ++e) {
58  const NodeState& state = **e;
59  state.print(out, indentLevel+1);
60  }
61 }
62 
63 void FrameStack::print(ostream& out, int indentLevel) const {
64  size_t i = count_elements();
65  out << space(indentLevel) << "FrameStack=" << this << " size " << i << endl;
66 
67  Level frameNr = i;
68  for (FrameStack::const_iterator e = begin(); e != end(); ++e, --frameNr) {
69  const NodeStack& nodeStack = **e;
70  nodeStack.print(out, indentLevel+1, frameNr);
71  }
72 }
73 
74 
75 void AP_tree_nlen::print(std::ostream& out, int indentLevel, const char *label) const {
76  out << space(indentLevel)
77  << label << "=" << this
78  << " father=" << get_father()
79  << " rff=" << remembered_for_frame
80  << " mut=" << mutations
81 #if 0
82  << " dist=" << distance
83  << " touched=" << br.touched
84 #endif
85  << " seqcs=" << (hasSequence() ? get_seq()->checksum() : 0);
86 
87  for (int e = 0; e<3; ++e) {
88  out << " edge[" << e << "]=" << edge[e];
89  if (edge[e]) {
90  const AP_tree_edge& E = *edge[e];
91  if (E.isConnectedTo(this)) {
92  out << "->" << E.otherNode(this);
93  }
94  else {
95  out << "(not connected to 'this'!";
96  AP_tree_nlen *son = E.sonNode();
97  if (son) {
98  AP_tree_nlen *fath = E.otherNode(son);
99  out << " son=" << son << " father=" << fath;
100  }
101  else {
102  out << "no son node";
103  }
104 
105  out << ')';
106  }
107  }
108  }
109 
110  if (is_leaf()) {
111  out << " name=" << name << endl;
112  }
113  else {
114  out << endl;
115  get_leftson()->print(out, indentLevel+1, "left");
116  get_rightson()->print(out, indentLevel+1, "right");
117  }
118 }
119 
120 
121 void AP_main::print(ostream& out) {
122  out << "AP_main tree:" << endl;
123  get_root_node()->print(out, 1, "root");
124 
125  out << "AP_main frames:" << endl;
126  if (currFrame) {
127  out << " currFrame:" << endl;
128  currFrame->print(out, 2, frames.count_elements()+1);
129  }
130  else {
131  out << " no currFrame" << endl;
132  }
133  frames.print(out, 1);
134 }
135 
136 void AP_main::print2file(const char *file_in_ARBHOME) {
137  const char *full = GB_path_in_ARBHOME(file_in_ARBHOME);
138  std::ofstream out(full, std::ofstream::out);
139  print(out);
140  out.close();
141 }
142 
143 #endif
144 
145 using namespace std;
146 
147 template <typename SET>
148 void set_extract_common(SET& set1, SET& set2, SET& common) {
149  ap_assert(!set1.empty() && !set2.empty());
150 
151  set_intersection(
152  set1.begin(), set1.end(),
153  set2.begin(), set2.end(),
154  std::inserter<SET>(common, common.begin())
155  );
156 
157  for (typename SET::iterator e = common.begin(); e != common.end(); ++e) {
158  set1.erase(*e);
159  set2.erase(*e);
160  }
161 }
162 
164  if (!stack1.nodes.empty() && !stack2.nodes.empty()) {
165  set_extract_common(stack1.nodes, stack2.nodes, nodes);
166  }
167  if (!stack1.edges.empty() && !stack2.edges.empty()) {
168  set_extract_common(stack1.edges, stack2.edges, edges);
169  }
170 }
171 
173  for (NodeSet::iterator n = nodes.begin(); n != nodes.end(); ++n) {
174  AP_tree_nlen *todel = *n;
175 
176  // Nodes destroyed from here may link to other nodes, but all these links are outdated.
177  // They are just leftovers of calling revert() or accept() -> wipe them
178  todel->forget_relatives();
179  todel->forget_origin();
180 
181  delete todel;
182  }
183  forget_nodes();
184 }
185 
187  for (EdgeSet::iterator e = edges.begin(); e != edges.end(); ++e) {
188  AP_tree_edge *todel = *e;
189 
190  // Edges destroyed from here may link to nodes, but all these links are outdated.
191  // They are just leftovers of calling revert() or accept() -> wipe them
192  todel->node[0] = NULp;
193  todel->node[1] = NULp;
194 
195  delete todel;
196  }
197  forget_edges();
198 }
199 
200 void ResourceStack::forget_nodes() { nodes.clear(); }
201 void ResourceStack::forget_edges() { edges.clear(); }
202 
204  while (!nodes.empty()) target.put(getNode()); // @@@ optimize
205 }
207  while (!edges.empty()) target.put(getEdge()); // @@@ optimize
208 }
209 
211  // if previous==NULp, top StackFrameData is reverted
212  created.destroy_nodes();
213  destroyed.forget_nodes();
214 
215  created.destroy_edges();
216  destroyed.forget_edges();
217 }
218 
220  // if previous==NULp, top StackFrameData gets destroyed
221  ap_assert(correlated(!previous, !common));
222 
223  if (previous) {
224  if (common->has_nodes()) common->destroy_nodes();
225  created.move_nodes(previous->created);
226  destroyed.move_nodes(previous->destroyed);
227  }
228  else {
229  created.forget_nodes(); // they are finally accepted as part of the tree
230  destroyed.destroy_nodes();
231  }
232 
233  if (previous) {
234  if (common->has_edges()) common->destroy_edges();
235  created.move_edges(previous->created);
236  destroyed.move_edges(previous->destroyed);
237  }
238  else {
239  created.forget_edges(); // they are finally accepted as part of the tree
240  destroyed.destroy_edges();
241  }
242 }
243 
bool isConnectedTo(const AP_tree_nlen *n) const
void extract_common(ResourceStack &stack1, ResourceStack &stack2)
Definition: AP_buffer.cxx:163
void destroy_nodes()
Definition: AP_buffer.cxx:172
return string(buffer, length)
void space()
Definition: test_unit.h:403
void set_extract_common(SET &set1, SET &set2, SET &common)
Definition: AP_buffer.cxx:148
AP_tree_nlen * sonNode() const
STL namespace.
void forget_nodes()
Definition: AP_buffer.cxx:200
void destroy_edges()
Definition: AP_buffer.cxx:186
void forget_edges()
Definition: AP_buffer.cxx:201
void accept_resources(StackFrameData *previous, ResourceStack *common)
Definition: AP_buffer.cxx:219
BASE::const_iterator const_iterator
Definition: AP_buffer.hxx:44
POS_TREE1 * father
Definition: probe_tree.h:39
POS_TREE1 * get_father() const
Definition: probe_tree.h:49
GB_CSTR GB_path_in_ARBHOME(const char *relative_path)
Definition: adsocket.cxx:1121
void move_nodes(ResourceStack &target)
Definition: AP_buffer.cxx:203
bool has_nodes() const
Definition: AP_buffer.hxx:205
#define ap_assert(cond)
void revert_resources(StackFrameData *previous)
Definition: AP_buffer.cxx:210
void move_edges(ResourceStack &target)
Definition: AP_buffer.cxx:206
AP_tree_nlen * otherNode(const AP_tree_nlen *n) const
#define NULp
Definition: cxxforward.h:97
bool is_leaf() const
Definition: probe_tree.h:67
static long count_elements(GBDATA *gbd)
bool has_edges() const
Definition: AP_buffer.hxx:206
AP_tree_nlen * put(AP_tree_nlen *node)
Definition: AP_buffer.hxx:199
unsigned long Level
Definition: AP_buffer.hxx:126
const char * label
void print(const T &t)
Definition: test_unit.h:348