selected_atom_iterator.h
Go to the documentation of this file.
00001 // Copyright (c) 2009-2010  INRIA Sophia-Antipolis (France).
00002 // All rights reserved.
00003 //
00004 //This file is part of ESBTL.
00005 //
00006 //ESBTL is free software: you can redistribute it and/or modify
00007 //it under the terms of the GNU General Public License as published by
00008 //the Free Software Foundation, either version 3 of the License, or
00009 //(at your option) any later version.
00010 //
00011 //ESBTL is distributed in the hope that it will be useful,
00012 //but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //GNU General Public License for more details.
00015 //
00016 //You should have received a copy of the GNU General Public License
00017 //along with ESBTL.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 //
00020 //Additional permission under GNU GPL version 3 section 7
00021 //
00022 //If you modify this Library, or any covered work, by linking or
00023 //combining it with CGAL (or a modified version of that library), the
00024 //licensors of this Library grant you additional permission to convey
00025 //the resulting work. Corresponding Source for a non-source form of
00026 //such a combination shall include the source code for the parts of CGAL
00027 //used as well as that of the covered work. 
00028 //
00029 //
00030 //
00031 // Author(s)     :  Sébastien Loriot
00032 
00033 
00034 
00035 #ifndef SELECTED_ATOM_ITERATOR_H
00036 #define SELECTED_ATOM_ITERATOR_H
00037 
00038 #include <ESBTL/iterators.h>
00039 
00040 //TODO this seems redundant with boost::filter_iterator
00041 // only advantage is that we can pass functor to iterator
00042 
00043 namespace ESBTL{
00044 
00045 
00046 //iterator over all atoms of a certain type of the model
00047 //is_const specify whether this is a const iterator or not
00048   
00059 template <class Model,class Subset_functor,bool is_const>
00060 class Selected_atom_iterator:
00061 public boost::iterator_facade<
00062     Selected_atom_iterator<Model,Subset_functor,is_const>,
00063     typename internal::do_add_const<typename Model::System::Atom,is_const>::type,
00064     boost::forward_traversal_tag>
00065 {
00066 private:
00067   typedef typename Model::System::Atom   Atom;
00068   typedef typename internal::Atoms_iterator_from_model<Model,is_const> Base_iterator;
00069 
00070 public:
00071   //functor may be initialized with info.
00076   Selected_atom_iterator(Base_iterator it,const Subset_functor& functor):atom_iterator(it),keep(functor)
00077   {if (!atom_iterator.is_end() && !keep(*atom_iterator)) increment();}
00078   //default constructor
00079   Selected_atom_iterator(Base_iterator it):atom_iterator(it)
00080   {if (!atom_iterator.is_end() && !keep(*atom_iterator)) increment();}
00081 
00082 private:
00083   friend class boost::iterator_core_access;
00084 
00085   void increment(){
00086     do{
00087       ++atom_iterator;
00088     }
00089     while(!atom_iterator.is_end() && !keep(*atom_iterator));
00090   }
00091     
00092   bool equal(Selected_atom_iterator const& other) const{
00093     return atom_iterator==other.atom_iterator;
00094   }
00095   
00096   typename internal::do_add_const_to_ref<Atom,is_const>::type dereference() const {
00097     return *atom_iterator;
00098   }
00099     
00100 //data members
00101   Base_iterator atom_iterator;
00102   Subset_functor keep;
00103 };  
00104   
00105 
00107 template <class Model,class Subset_functor,bool is_const>
00108 Selected_atom_iterator<Model,Subset_functor,is_const> 
00109 make_selected_atom_iterator(internal::Atoms_iterator_from_model<Model,is_const> iterator,const Subset_functor& functor){
00110   return Selected_atom_iterator<Model,Subset_functor,is_const> (iterator,functor);
00111 }
00112 
00114 template <class Subset_functor,class Model,bool is_const>
00115 Selected_atom_iterator<Model,Subset_functor,is_const> 
00116 make_selected_atom_iterator(internal::Atoms_iterator_from_model<Model,is_const> iterator){
00117   return Selected_atom_iterator<Model,Subset_functor,is_const> (iterator);
00118 }
00119   
00120 }//ESBTL
00121 
00122 #endif //SELECTED_ATOM_ITERATOR_H