ivl 679
ivl/details/array_2d/impl/iterator/wrap_2d_iterator.hpp
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_2D_DETAILS_ITERATOR_WRAP_2D_ITERATOR_HPP
00025 #define IVL_ARRAY_2D_DETAILS_ITERATOR_WRAP_2D_ITERATOR_HPP
00026 
00027 namespace ivl {
00028 
00029 namespace data {
00030 
00031 namespace data_details {
00032 
00033 template <class A>
00034 struct wrap_2d_elem
00035 {
00036         typedef ivl::array<typename A::elem_type,
00037                 data::ref_iterator<
00038                 typename types::best_iterator_nd<A>::type,
00039                 typename A::const_iterator_nd> > type;
00040 };
00041 
00042 } /* namespace data_details */
00043 
00044 
00045 template <class A, int OUT_D = 1, int IN_D = 0>
00046 class wrap_2d_iterator
00047 {
00048         typedef typename types::best_iterator_nd<A>::type iter_nd;
00049 
00050         typedef typename data_details::wrap_2d_elem<A>::type elem_type;
00051 
00052         // may be not necessary
00053         typedef typename types::best_iterator_nd<A>::is_writeable is_writeable;
00054         typedef typename types::t_not<is_writeable>::type is_const;
00055 
00056 /*
00057         friend class data_details::rnd_iter_operator_expander<
00058                 wrap_2d_iterator<A>, typename data_details::wrap_2d_elem<A>::type,
00059                 false, typename data_details::wrap_2d_elem<A>::type>;
00060 */
00061         A* a;
00062         iter_nd it;
00063 
00064 public:
00065         typedef wrap_2d_iterator this_type;
00066 
00067         // iterator_traits
00068     typedef std::random_access_iterator_tag iterator_category;
00069     typedef elem_type value_type;
00070         typedef ptrdiff_t difference_type;
00071         typedef elem_type* pointer;
00072         typedef elem_type reference;
00073 
00074         // constructors
00075         wrap_2d_iterator() { }
00076         wrap_2d_iterator(A& a) : a(&a), it(a._begin(OUT_D)) { }
00077         wrap_2d_iterator(const wrap_2d_iterator& o) : a(o.a), it(o.it) { }
00078 
00079         // members
00080 
00081         elem_type operator *() const
00082                 { return elem_type(a->_begin(IN_D, it), a->size(IN_D)); }
00083 
00084         elem_type operator [](size_t j) const
00085                 { return elem_type(a->_begin(IN_D, it + j), a->size(IN_D)); }
00086 
00087         // increment-decrement
00088         this_type& operator++() { ++it; return *this; }
00089         this_type& operator--() { --it; return *this; }
00090 
00091         this_type operator++(int) { this_type tmp(*this); it++; return tmp; }
00092         this_type operator--(int) { this_type tmp(*this); it--; return tmp; }
00093 
00094         // random access
00095         this_type operator +(const size_t j) const
00096                 { this_type tmp(*this); tmp.it += j; return tmp; }
00097 
00098         this_type operator -(const size_t j) const
00099                 { this_type tmp(*this); tmp.it -= j; return tmp; }
00100 
00101         this_type& operator +=(const size_t j) { it += j; return *this; }
00102         this_type& operator -=(const size_t j) { it -= j; return *this; }
00103 
00104         // difference
00105         difference_type operator -(const this_type& a) const {
00106                 return difference_type(it - a.it);
00107         }
00108 
00109         //copy same type iterator
00110         this_type& operator=(const this_type& o)
00111                 { a = o->a; it = o->it; return *this; }
00112 
00113         bool operator==(const this_type& o) const
00114                 { return (it == o.it); }
00115 
00116         bool operator!=(const this_type& o) const
00117                 { return (it != o.it); }
00118 };
00119 
00120 
00121 #if 0
00122 //note: some parts of this below might remain useful for the nd impl
00123 template <class A>
00124 class wrap_2d_iterator
00125         : public data_details::rnd_iter_operator_expander<
00126                 wrap_2d_iterator<A>, typename A::elem_type,
00127                 types::t_not<types::best_iterator_nd<A>::is_writeable>::value,
00128                 typename types::best_iterator_nd<A>::type::reference>
00129 {
00130         typedef typename types::best_iterator_nd<A>::type iter_nd;
00131 
00132         typedef typename types::best_iterator_nd<A>::is_writeable is_writeable;
00133 
00134         typedef typename types::t_not<is_writeable>::type is_const;
00135 
00136         typedef typename iter_nd::reference ref;
00137 
00138         friend class data_details::iter_operator_expander
00139                 <wrap_2d_iterator<A>, typename A::elem_type, is_const, ref>;
00140 
00141         typedef typename types::apply_const<T, is_const>::type best_value_type;
00142 
00143         typedef ref best_ref_type;
00144 
00145         // this struct is used to disable specific specialization members
00146         struct not_a_type { operator size_t() { return 0; } };
00147 
00148         typedef typename types::t_if<types::t_expr<CONST>, not_a_type,
00149                 types::code_word<> >::type invalid_if_const;
00150 
00151         A* o;
00152         iter_nd i;
00153 #endif
00154 
00155 
00156 } /* namespace data */
00157 
00158 } /* namespace ivl */
00159 
00160 #endif // IVL_ARRAY_2D_DETAILS_ITERATOR_WRAP_2D_ITERATOR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations