BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Iterator_project.h
Go to the documentation of this file.
00001 // Copyright (c) 2003  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_project.h $
00019 // $Id: Iterator_project.h 46206 2008-10-11 20:21:08Z spion $
00020 // 
00021 //
00022 // Author(s)     : Michael Hoffmann <hoffmann@inf.ethz.ch>
00023 //                 Lutz Kettner <kettner@mpi-sb.mpg.de>
00024 //                 Sylvain Pion
00025 
00026 #ifndef CGAL_ITERATOR_PROJECT_H
00027 #define CGAL_ITERATOR_PROJECT_H 1
00028 
00029 CGAL_BEGIN_NAMESPACE
00030 
00031 // Relies on iterator traits. Quite simplified compared to earlier version.
00032 
00033 // The pointer type and the reference type in the Iterator_project
00034 // are based on the value type from the projector, but the base iterator
00035 // determines whether they are const or mutable. The following template
00036 // class and its partial specialization helps creating the derived types.
00037 
00038 // If T === T1 return R1 else return R2
00039 template <class T, class T1, class R1, class R2>
00040 struct I_TYPE_MATCH_IF { typedef R2 Result; };  // else clause
00041 
00042 template <class T, class R1, class R2>
00043 struct I_TYPE_MATCH_IF<T,T,R1,R2> { typedef R1 Result; }; // then clause
00044 
00045 // keep 4 dummy template parameters around for backwards compatibility
00046 template < class I, class Fct,
00047            class D1 = int, class D2 = int, class D3 = int, class D4 = int >
00048 class Iterator_project {
00049 protected:
00050   I        nt;    // The internal iterator.
00051 public:
00052   typedef Iterator_project<I,Fct,D1,D2,D3,D4> Self;
00053   typedef I                                   Iterator; // base iterator
00054   typedef std::iterator_traits<I>             traits;
00055   typedef typename traits::difference_type    difference_type;
00056   typedef typename traits::iterator_category  iterator_category;
00057   typedef typename traits::value_type         base_value_type;
00058   typedef typename traits::pointer            base_pointer;
00059   typedef typename traits::reference          base_reference;
00060 
00061   typedef typename Fct::argument_type         argument_type;
00062   typedef typename Fct::result_type           value_type;
00063 
00064   // Use I_TYPE_MATCH_IF to find correct pointer and reference type.
00065 
00066   typedef I_TYPE_MATCH_IF< base_reference, const base_value_type &,
00067     const value_type &, value_type &> Match1;
00068   typedef typename Match1::Result             reference;
00069 
00070   typedef I_TYPE_MATCH_IF< base_pointer, const base_value_type *,
00071     const value_type *, value_type *> Match2;
00072   typedef typename Match2::Result             pointer;
00073 
00074   // CREATION
00075   // --------
00076 
00077   Iterator_project() {}
00078   Iterator_project( I j) : nt(j) {}
00079 
00080   // make two iterators assignable if the underlying iterators are
00081   template <class I2, class Q1, class Q2, class Q3, class Q4>
00082   Iterator_project( const Iterator_project<I2,Fct,Q1,Q2,Q3,Q4>& i2)
00083   : nt( i2.current_iterator()) {}
00084 
00085   template <class I2, class Q1, class Q2, class Q3, class Q4>
00086   Self& operator= ( const Iterator_project<I2,Fct,Q1,Q2,Q3,Q4>& i2) {
00087     nt = i2.current_iterator();
00088     return *this;
00089   }
00090 
00091   // OPERATIONS Forward Category
00092   // ---------------------------
00093 
00094   Iterator  current_iterator() const { return nt;}
00095   pointer   ptr() const {
00096     Fct fct;
00097     return &(fct(*nt));
00098   }
00099   bool      operator==( const Self& i) const { return ( nt == i.nt); }
00100   bool      operator!=( const Self& i) const { return !(*this == i); }
00101   reference operator* ()               const { return *ptr(); }
00102   pointer   operator->()               const { return ptr(); }
00103   Self&     operator++() {
00104     ++nt;
00105     return *this;
00106   }
00107   Self      operator++(int) {
00108     Self tmp = *this;
00109     ++*this;
00110     return tmp;
00111   }
00112 
00113   // OPERATIONS Bidirectional Category
00114   // ---------------------------------
00115 
00116   Self& operator--() {
00117     --nt;
00118     return *this;
00119   }
00120   Self  operator--(int) {
00121     Self tmp = *this;
00122     --*this;
00123     return tmp;
00124   }
00125 
00126   // OPERATIONS Random Access Category
00127   // ---------------------------------
00128 
00129   Self& operator+=( difference_type n) {
00130     nt += n;
00131     return *this;
00132   }
00133   Self  operator+( difference_type n) const {
00134     Self tmp = *this;
00135     return tmp += n;
00136   }
00137   Self& operator-=( difference_type n) {
00138     return operator+=( -n);
00139   }
00140   Self  operator-( difference_type n) const {
00141     Self tmp = *this;
00142     return tmp += -n;
00143   }
00144   difference_type  operator-( const Self& i) const { return nt - i.nt; }
00145   reference  operator[]( difference_type n) const {
00146     Self tmp = *this;
00147     tmp += n;
00148     return tmp.operator*();
00149   }
00150   bool operator< ( const Self& i) const { return ( nt < i.nt); }
00151   bool operator> ( const Self& i) const { return i < *this; }
00152   bool operator<=( const Self& i) const { return !(i < *this); }
00153   bool operator>=( const Self& i) const { return !(*this < i); }
00154 };
00155 
00156 template < class Dist, class Fct, class I,
00157            class D1, class D2, class D3, class D4>
00158 inline
00159 Iterator_project<I,Fct,D1,D2,D3,D4>
00160 operator+( Dist n, Iterator_project<I,Fct,D1,D2,D3,D4> i) {
00161   return i += n;
00162 }
00163 
00164 CGAL_END_NAMESPACE
00165 #endif // CGAL_ITERATOR_PROJECT_H //
00166 // EOF //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines