| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : SEC_iter.hxx // |
|---|
| 4 | // Purpose : secondary structure iterators // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in August 2007 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // =============================================================== // |
|---|
| 11 | |
|---|
| 12 | #ifndef SEC_ITER_HXX |
|---|
| 13 | #define SEC_ITER_HXX |
|---|
| 14 | |
|---|
| 15 | // iterates over all parts of the structure (segments/strands) |
|---|
| 16 | class SEC_base_part_iterator { |
|---|
| 17 | SEC_base_part *start; |
|---|
| 18 | SEC_base_part *curr; |
|---|
| 19 | |
|---|
| 20 | public: |
|---|
| 21 | SEC_base_part_iterator(SEC_root *root) |
|---|
| 22 | : start(root->get_root_loop()->get_fixpoint_strand()), curr(start) |
|---|
| 23 | {} |
|---|
| 24 | |
|---|
| 25 | SEC_base_part& operator*() { return *curr; } |
|---|
| 26 | SEC_base_part* operator->() { return curr; } |
|---|
| 27 | |
|---|
| 28 | SEC_base_part_iterator& operator++() { |
|---|
| 29 | if (curr) { |
|---|
| 30 | curr = curr->next(); |
|---|
| 31 | if (curr == start) curr = 0; |
|---|
| 32 | } |
|---|
| 33 | return *this; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | operator bool() const { return curr != 0; } |
|---|
| 37 | |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | // iterates over all bases of the structure (loops/helices) |
|---|
| 41 | class SEC_base_iterator { |
|---|
| 42 | SEC_base_part_iterator part; // always points to an outgoing strand |
|---|
| 43 | bool do_loop; // true -> curr is origin loop, false -> curr is helix |
|---|
| 44 | |
|---|
| 45 | SEC_base *curr() { |
|---|
| 46 | SEC_helix_strand& strand = static_cast<SEC_helix_strand&>(*part); |
|---|
| 47 | return do_loop |
|---|
| 48 | ? static_cast<SEC_base*>(strand.get_origin_loop()) |
|---|
| 49 | : static_cast<SEC_base*>(strand.get_helix()); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public: |
|---|
| 53 | SEC_base_iterator(SEC_root *root) : part(root), do_loop(true) {} |
|---|
| 54 | |
|---|
| 55 | SEC_base& operator*() { return *curr(); } |
|---|
| 56 | SEC_base* operator->() { return curr(); } |
|---|
| 57 | |
|---|
| 58 | SEC_base_iterator& operator++() { |
|---|
| 59 | if (part) { |
|---|
| 60 | if (do_loop) do_loop = false; |
|---|
| 61 | else { |
|---|
| 62 | // skip over all strands pointing to root |
|---|
| 63 | int steps = 0; |
|---|
| 64 | do { |
|---|
| 65 | ++++part; |
|---|
| 66 | ++steps; |
|---|
| 67 | } |
|---|
| 68 | while (part && static_cast<SEC_helix_strand&>(*part).pointsToRoot()); |
|---|
| 69 | do_loop = (steps == 1); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | return *this; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | operator bool() const { return part; } |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | // iterates over all strands in one loop (starting with fixpoint strand) |
|---|
| 80 | class SEC_strand_iterator { |
|---|
| 81 | SEC_helix_strand *start; |
|---|
| 82 | SEC_helix_strand *curr; |
|---|
| 83 | public: |
|---|
| 84 | SEC_strand_iterator(SEC_loop *loop) : start(loop->get_fixpoint_strand()), curr(start) {} |
|---|
| 85 | |
|---|
| 86 | SEC_helix_strand& operator*() { return *curr; } |
|---|
| 87 | SEC_helix_strand* operator->() { return curr; } |
|---|
| 88 | |
|---|
| 89 | SEC_strand_iterator& operator++() { |
|---|
| 90 | sec_assert(curr); |
|---|
| 91 | SEC_segment *seg = curr->get_next_segment(); |
|---|
| 92 | |
|---|
| 93 | if (seg) { |
|---|
| 94 | curr = seg->get_next_strand(); |
|---|
| 95 | if (curr == start) curr = 0; |
|---|
| 96 | } |
|---|
| 97 | else { |
|---|
| 98 | curr = 0; |
|---|
| 99 | } |
|---|
| 100 | return *this; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | operator bool() const { return curr != 0; } |
|---|
| 104 | }; |
|---|
| 105 | |
|---|
| 106 | // iterates over all segments in one loop (starting with segment behind fixpoint strand) |
|---|
| 107 | class SEC_segment_iterator : private SEC_strand_iterator { |
|---|
| 108 | const SEC_strand_iterator& strand_iter() const { return static_cast<const SEC_strand_iterator&>(*this); } |
|---|
| 109 | SEC_strand_iterator& strand_iter() { return static_cast<SEC_strand_iterator&>(*this); } |
|---|
| 110 | public: |
|---|
| 111 | SEC_segment_iterator(SEC_loop *loop) : SEC_strand_iterator(loop) {} |
|---|
| 112 | |
|---|
| 113 | SEC_segment& operator*() { return *strand_iter()->get_next_segment(); } |
|---|
| 114 | SEC_segment* operator->() { return strand_iter()->get_next_segment(); } |
|---|
| 115 | |
|---|
| 116 | SEC_segment_iterator& operator++() { |
|---|
| 117 | SEC_strand_iterator& si = strand_iter(); |
|---|
| 118 | ++si; |
|---|
| 119 | return *this; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | operator bool() const { return strand_iter(); } |
|---|
| 123 | |
|---|
| 124 | SEC_helix_strand *get_previous_strand() { return &*strand_iter(); } |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | // const versions |
|---|
| 129 | |
|---|
| 130 | class SEC_strand_const_iterator : private SEC_strand_iterator { |
|---|
| 131 | const SEC_strand_iterator& strand_iter() const { return static_cast<const SEC_strand_iterator&>(*this); } |
|---|
| 132 | SEC_strand_iterator& strand_iter() { return static_cast<SEC_strand_iterator&>(*this); } |
|---|
| 133 | public: |
|---|
| 134 | SEC_strand_const_iterator(const SEC_loop *loop) : SEC_strand_iterator(const_cast<SEC_loop*>(loop)) {} |
|---|
| 135 | const SEC_helix_strand& operator*() { return *strand_iter(); } |
|---|
| 136 | const SEC_helix_strand* operator->() { return strand_iter().operator->(); } |
|---|
| 137 | SEC_strand_const_iterator& operator++() { return static_cast<SEC_strand_const_iterator&>(++strand_iter()); } |
|---|
| 138 | operator bool() const { return bool(strand_iter()); } |
|---|
| 139 | }; |
|---|
| 140 | |
|---|
| 141 | #else |
|---|
| 142 | #error SEC_iter.hxx included twice |
|---|
| 143 | #endif // SEC_ITER_HXX |
|---|