BWAPI
|
00001 // Copyright (c) 2007 GeometryFactory (France), Utrecht University (The Netherlands), 00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany), 00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg 00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria), 00005 // and Tel-Aviv University (Israel). All rights reserved. 00006 // 00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00008 // modify it under the terms of the GNU Lesser General Public License as 00009 // published by the Free Software Foundation; version 2.1 of the License. 00010 // See the file LICENSE.LGPL distributed with CGAL. 00011 // 00012 // Licensees holding a valid commercial license may use this file in 00013 // accordance with the commercial license agreement provided with the software. 00014 // 00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00017 // 00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/STL_Extension/include/CGAL/Iterator_transform.h $ 00019 // $Id: Iterator_transform.h 50707 2009-07-20 08:52:20Z spion $ 00020 // 00021 // 00022 // Author(s) : Michael Hoffmann <hoffmann@inf.ethz.ch> 00023 // Lutz Kettner <kettner@mpi-sb.mpg.de> 00024 // Sylvain Pion 00025 // Fernando Cacciola <fernando.cacciola@geometryfactory.com> 00026 00027 #ifndef CGAL_ITERATOR_TRANSFORM_H 00028 #define CGAL_ITERATOR_TRANSFORM_H 1 00029 00030 #include <CGAL/Iterator_project.h> 00031 00032 CGAL_BEGIN_NAMESPACE 00033 00034 template < class I, class Fct> 00035 class Iterator_transform { 00036 protected: 00037 I nt; // The internal iterator. 00038 public: 00039 typedef Iterator_transform<I,Fct> Self; 00040 typedef I Iterator; // base iterator 00041 typedef std::iterator_traits<I> traits; 00042 typedef typename traits::difference_type difference_type; 00043 typedef typename traits::iterator_category iterator_category; 00044 typedef typename traits::value_type base_value_type; 00045 typedef typename traits::pointer base_pointer; 00046 typedef typename traits::reference base_reference; 00047 00048 typedef typename Fct::argument_type argument_type; 00049 typedef typename Fct::result_type value_type; 00050 00051 00052 // This iterator returns rvalues by design (allowing the conversion function to return new objects) 00053 typedef value_type reference; 00054 00055 // Use I_TYPE_MATCH_IF to find correct pointer type. 00056 typedef I_TYPE_MATCH_IF< base_pointer, const base_value_type *, 00057 const value_type *, value_type *> Match2; 00058 typedef typename Match2::Result pointer; 00059 00060 // CREATION 00061 // -------- 00062 00063 Iterator_transform() {} 00064 Iterator_transform( I j) : nt(j) {} 00065 00066 // make two iterators assignable if the underlying iterators are 00067 template <class I2> 00068 Iterator_transform( const Iterator_transform<I2,Fct>& i2) 00069 : nt( i2.current_iterator()) {} 00070 00071 template <class I2> 00072 Self& operator= ( const Iterator_transform<I2,Fct>& i2) { 00073 nt = i2.current_iterator(); 00074 return *this; 00075 } 00076 00077 // OPERATIONS Forward Category 00078 // --------------------------- 00079 00080 Iterator current_iterator() const { return nt;} 00081 bool operator==( const Self& i) const { return ( nt == i.nt); } 00082 bool operator!=( const Self& i) const { return !(*this == i); } 00083 00084 struct Proxy 00085 { 00086 Proxy(const reference r) : ref(r) {} 00087 reference ref; 00088 pointer operator->() { return &ref; } 00089 }; 00090 00091 Proxy operator->() const 00092 { 00093 return Proxy(Fct()(*nt)); 00094 } 00095 00096 reference operator* () const 00097 { 00098 Fct fct; 00099 return fct(*nt); 00100 } 00101 00102 Self& operator++() { 00103 ++nt; 00104 return *this; 00105 } 00106 Self operator++(int) { 00107 Self tmp = *this; 00108 ++*this; 00109 return tmp; 00110 } 00111 00112 // OPERATIONS Bidirectional Category 00113 // --------------------------------- 00114 00115 Self& operator--() { 00116 --nt; 00117 return *this; 00118 } 00119 Self operator--(int) { 00120 Self tmp = *this; 00121 --*this; 00122 return tmp; 00123 } 00124 00125 // OPERATIONS Random Access Category 00126 // --------------------------------- 00127 00128 Self& operator+=( difference_type n) { 00129 nt += n; 00130 return *this; 00131 } 00132 Self operator+( difference_type n) const { 00133 Self tmp = *this; 00134 return tmp += n; 00135 } 00136 Self& operator-=( difference_type n) { 00137 return operator+=( -n); 00138 } 00139 Self operator-( difference_type n) const { 00140 Self tmp = *this; 00141 return tmp += -n; 00142 } 00143 difference_type operator-( const Self& i) const { return nt - i.nt; } 00144 reference operator[]( difference_type n) const { 00145 Self tmp = *this; 00146 tmp += n; 00147 return tmp.operator*(); 00148 } 00149 bool operator< ( const Self& i) const { return ( nt < i.nt); } 00150 bool operator> ( const Self& i) const { return i < *this; } 00151 bool operator<=( const Self& i) const { return !(i < *this); } 00152 bool operator>=( const Self& i) const { return !(*this < i); } 00153 }; 00154 00155 template < class Dist, class Fct, class I> 00156 inline 00157 Iterator_transform<I,Fct> 00158 operator+( Dist n, Iterator_transform<I,Fct> i) { 00159 return i += n; 00160 } 00161 00162 CGAL_END_NAMESPACE 00163 #endif // CGAL_Iterator_transform_H // 00164 // EOF //