ivl 679
|
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>. 00002 A C++ template library extending syntax towards mathematical notation. 00003 00004 Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr> 00005 Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr> 00006 00007 ivl is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License 00009 version 3 as published by the Free Software Foundation. 00010 00011 Alternatively, you can redistribute it and/or modify it under the terms 00012 of the GNU General Public License version 2 as published by the Free 00013 Software Foundation. 00014 00015 ivl is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00018 See the GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 and a copy of the GNU Lesser General Public License along 00022 with ivl. If not, see <http://www.gnu.org/licenses/>. */ 00023 00024 #ifndef IVL_ARRAY_ND_DETAILS_IMPL_ITERATOR_SWITCH_ND_HPP 00025 #define IVL_ARRAY_ND_DETAILS_IMPL_ITERATOR_SWITCH_ND_HPP 00026 00027 namespace ivl { 00028 00029 namespace array_nd_details { 00030 /* 00031 template <class T, class IS_AR> 00032 struct switch_nd_op {}; 00033 00034 template <class T, class IS_AR> 00035 struct switch_nd_op<T, types::t_true> 00036 { 00037 template <class A, class IT> 00038 static inline T get(A& a, const IT& it, int d) { return T(a->size(d), it); } 00039 }; 00040 00041 template <class T, class IS_AR> 00042 struct switch_nd_op<T, types::t_false> 00043 { 00044 template <class A, class IT> 00045 static inline T get(A& a, const IT& it, int d) { return *it; } 00046 }; 00047 */ 00048 } /* namespace array_nd_details */ 00049 00050 template<class T, class IT, class A, int D> 00051 class switch_nd 00052 { 00053 A* a; 00054 IT it; 00055 public: 00056 typedef std::random_access_iterator_tag iterator_category; 00057 typedef T value_type; 00058 typedef typename IT::difference_type difference_type; 00059 typedef internal::pointer_face<T> pointer; 00060 typedef internal::reference_face<T, switch_nd> reference; 00061 00062 typedef switch_nd this_type; 00063 00064 // constructors 00065 switch_nd() { } 00066 switch_nd(const switch_nd& o) : it(o.it), a(o.a) { } 00067 //template<int D2> 00068 //switch_nd(const switch_nd<T, IT, A, D2>& o) : it(o.it), a(o.a) { } 00069 00070 switch_nd(const IT& it, A& a) : it(it), a(a) { } 00071 00072 reference operator*() const 00073 { return reference(T(a->size(D), a->_begin(D, it)), *this); } 00074 00075 reference operator[](size_t j) const 00076 { return it[j]; } 00077 00078 pointer operator->() const 00079 { return pointer(*this, T(a->size(D), a->_begin(D, it))); } 00080 00081 00082 // increment-decrement 00083 this_type& operator++() { ++it; return *this; } 00084 this_type& operator--() { --it; return *this; } 00085 00086 this_type operator++(int) 00087 { this_type tmp(*this); ++it; return tmp; } 00088 00089 this_type operator--(int) 00090 { this_type tmp(*this); --it; return tmp; } 00091 00092 // random access 00093 this_type operator +(const typename IT::difference_type j) const 00094 { this_type tmp(it + j, a); return tmp; } 00095 00096 this_type operator -(const typename IT::difference_type j) const 00097 { this_type tmp(it - j, a); return tmp; } 00098 00099 this_type& operator +=(const typename IT::difference_type j) 00100 { it += j; return *this; } 00101 00102 this_type& operator -=(const typename IT::difference_type j) 00103 { it -= j; return *this; } 00104 00105 // difference 00106 typename IT::difference_type operator -(const this_type& a) const 00107 { return it - a.it; } 00108 00109 // comparing 00110 bool operator==(const this_type& o) const { return it == o.it; } 00111 bool operator!=(const this_type& o) const { return it != o.it; } 00112 bool operator<(const this_type& o) const { return it < o.it; } 00113 bool operator<=(const this_type& o) const 00114 { return ((*this < it) || (*this == it)); } 00115 bool operator>(const this_type& o) const 00116 { return ((!(*this < it)) && (!(*this == it))); } 00117 bool operator>=(const this_type& o) const { return !(*this < it); } 00118 }; 00119 00120 } /* namespace ivl */ 00121 00122 #endif // IVL_ARRAY_ND_DETAILS_IMPL_ITERATOR_SWITCH_ND_HPP 00123 00124