Main Page | Namespace List | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Examples

iterator.h

00001 /* Copyright 2004
00002 Stanford University
00003 
00004 This file is part of the DSR PDB Library.
00005 
00006 The DSR PDB Library is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation; either version 2.1 of the License, or (at your
00009 option) any later version.
00010 
00011 The DSR PDB Library is distributed in the hope that it will be useful, but
00012 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014 License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with the DSR PDB Library; see the file COPYING.LIB.  If not, write to
00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00019 MA 02111-1307, USA. */
00020 
00021 #ifndef DSR_PDB_ITERATOR_H
00022 #define DSR_PDB_ITERATOR_H
00023 
00024 #include <dsrpdb/Point.h>
00025 #include <dsrpdb/Protein.h>
00026 
00027 namespace dsrpdb {
00029   struct Atom_coordinates {
00030     typedef Point result_type;
00031     typedef const std::pair<Residue::Atom_label, Atom>& argument_type;
00032     const result_type& operator()(argument_type a) const {
00033       return a.second.cartesian_coords();
00034     }
00035     
00036   };
00037 
00038   struct Atom_index {
00039     typedef Atom::Index result_type;
00040     typedef const std::pair<Residue::Atom_label, Atom>& argument_type;
00041     const result_type& operator()(argument_type a) const {
00042       static result_type rt;
00043       rt= a.second.index();
00044       return rt;
00045     }
00046     
00047   };
00048 
00049 
00051   template <class It, class Projector>
00052   class Projection_iterator {
00053     typedef Projection_iterator<It, Projector> This;
00054   public:
00055     typedef typename Projector::result_type value_type;
00056     typedef typename It::iterator_category iterator_category;
00057     typedef typename It::difference_type difference_type;
00058     typedef const typename Projector::result_type& reference;
00059     typedef const typename Projector::result_type* pointer;
00060 
00061     reference operator*() {
00062       //std::cout << ait_->second.index() << std::endl;
00063       return p_(*cur_);
00064     }
00065     pointer operator->() {
00066       //std::cout << ait_->second.index() << std::endl;
00067       return &p_(*cur_);
00068     }
00069     This operator++(int) {
00070       This t=*this;
00071       ++cur_;
00072       return t;
00073     }
00074     This operator++() {
00075       ++cur_;
00076       return *this;
00077     }
00078     
00079     bool operator==(const This& o) const {
00080       return cur_ == o.cur_;
00081     }
00082     bool operator!=(const This& o) const {
00083       return cur_!= o.cur_;
00084     }
00085 
00086     Projection_iterator(){}
00087     Projection_iterator(It c, Projector p): cur_(c), p_(p){}
00088     
00089   protected:
00090     It cur_;
00091     Projector p_;
00092   };
00093 
00094 
00095 
00096 
00097 
00099   template <class It, class Proj>
00100   Projection_iterator<It, Proj> projection_iterator(It a, Proj proj) {
00101     return Projection_iterator<It, Proj>(a, proj);
00102   }
00103 
00104   
00105 
00107   template <class It, class Filter>
00108   class Filter_iterator {
00109     typedef Filter_iterator<It, Filter> This;
00110   public:
00111     typedef typename It::value_type value_type;
00112     typedef typename It::iterator_category iterator_category;
00113     typedef typename It::difference_type difference_type;
00114     typedef typename It::reference reference;
00115     typedef typename It::pointer pointer;
00116 
00117     reference operator*() {
00118       return cur_.operator*();
00119     }
00120 
00121     pointer operator->() {
00122       return cur_.operator->();
00123     }
00124 
00125     This operator++(int) {
00126       This t=*this;
00127       ++cur_;
00128       fix();
00129       return t;
00130     }
00131     This operator++() {
00132       ++cur_;
00133       fix();
00134       return *this;
00135     }
00136     
00137     bool operator==(const This& o) const {
00138       return cur_ == o.cur_;
00139     }
00140     bool operator!=(const This& o) const {
00141       return cur_!= o.cur_;
00142     }
00143 
00144     Filter_iterator(){}
00145     Filter_iterator(It c, It e, Filter f): cur_(c), end_(e), f_(f){fix();}
00146     
00147   protected:
00148     void fix(){
00149       while (cur_ != end_ && !f_(*cur_)) ++cur_;
00150     }
00151     It cur_, end_;
00152     Filter f_;
00153   };
00154 
00156 
00159   template <class It, class F>
00160   Filter_iterator<It, F> filter_iterator(It b, It e, F f){
00161     return Filter_iterator<It, F>(b,e,f);
00162   }
00163 
00165   struct Yes {
00166     template <class A>
00167     bool operator()(A) {return true;}
00168   };
00169   
00171   struct Is_backbone {
00172     template <class A>
00173     bool operator()(const A &a) {
00174       return a.first == Residue::AL_C || a.first == Residue::AL_CA 
00175         || a.first == Residue::AL_N;
00176     }
00177   };
00178 
00180   struct Is_CA {
00181     template <class A>
00182     bool operator()(const A &a) {
00183       return a.first == Residue::AL_CA;
00184     }
00185   };
00186 
00188   typedef Projection_iterator<Filter_iterator<Protein::Const_atoms_iterator, Is_backbone>, Atom_coordinates>  Protein_backbone_coordinates_iterator;
00189   
00191 
00194   inline Protein_backbone_coordinates_iterator backbone_coordinates_begin(const Protein &p) {
00195     return projection_iterator(filter_iterator(p.atoms_begin(), p.atoms_end(), Is_backbone()), Atom_coordinates());
00196   }
00197 
00199   inline Protein_backbone_coordinates_iterator  backbone_coordinates_end(const Protein &p) {
00200     return projection_iterator(filter_iterator(p.atoms_end(), p.atoms_end(), Is_backbone()), Atom_coordinates());
00201   }
00202 
00203 
00205   typedef Projection_iterator<Filter_iterator<Protein::Const_atoms_iterator, Is_CA>, Atom_coordinates >  Ca_backbone_coordinates_iterator;
00206   
00208   inline Ca_backbone_coordinates_iterator ca_coordinates_begin(const Protein &p) {
00209     return projection_iterator(filter_iterator(p.atoms_begin(), p.atoms_end(), Is_CA()), Atom_coordinates());
00210   }
00211 
00213   inline Ca_backbone_coordinates_iterator  ca_coordinates_end(const Protein &p) {
00214     return projection_iterator(filter_iterator(p.atoms_end(), p.atoms_end(), Is_CA()), Atom_coordinates());
00215   }
00216 
00217 
00219   typedef Projection_iterator<Filter_iterator<Protein::Const_atoms_iterator, Yes>, Atom_coordinates>  All_coordinates_iterator;
00220   
00222   inline All_coordinates_iterator all_coordinates_begin(const Protein &p) {
00223     return projection_iterator(filter_iterator(p.atoms_begin(), p.atoms_end(), Yes()), Atom_coordinates());
00224   }
00225 
00227   inline All_coordinates_iterator  all_coordinates_end(const Protein &p) {
00228     return projection_iterator(filter_iterator(p.atoms_end(), p.atoms_end(), Yes()), Atom_coordinates());
00229   }
00230 
00231 
00233   typedef Projection_iterator<Filter_iterator<Protein::Const_atoms_iterator, Is_backbone>, Atom_index> Protein_backbone_indices_iterator;
00234 
00236   inline Protein_backbone_indices_iterator protein_backbone_indices_begin(const Protein &p) {
00237     return projection_iterator(filter_iterator(p.atoms_begin(), p.atoms_end(), Is_backbone()), Atom_index());
00238   }
00239 
00241   inline Protein_backbone_indices_iterator  protein_backbone_indices_end(const Protein &p) {
00242     return projection_iterator(filter_iterator(p.atoms_end(), p.atoms_end(), Is_backbone()), Atom_index());
00243   }
00244 
00245 }
00246 
00247 #endif