ivl 679
ivl/details/array/impl/specialization/fixed_val_repeat_class.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 
00030 template <class T,
00031                  T V,
00032                  class DERIVED_INFO
00033              >
00034 class array<T, data::fixed_val_repeat<T, V, DERIVED_INFO> >
00035         :
00036         public array_common_base<
00037                 array<T, data::fixed_val_repeat<T, V, DERIVED_INFO> > >
00038 {
00039 
00040 private:
00041         typedef array_common_base<array<T,
00042                 data::fixed_val_repeat<T, V, DERIVED_INFO> > > common_base_class;
00043 
00044         size_t len;
00045 
00046 public:
00047         typedef array this_type;
00048 
00049         typedef this_type this_array_type;
00050 
00051         typedef this_type array_type;
00052 
00053         typedef T elem_type;
00054 
00055         typedef typename common_base_class::base_class base_class;
00056 
00058         typedef size_t size_type;
00059 
00061         typedef ptrdiff_t diff_type;
00062 
00063         using base_class::derived;
00064 
00065 
00066         void resize(size_t len)
00067         {
00068                 this->len = len;
00069         }
00070         void reset(size_t len)
00071         {
00072                 this->len = len;
00073         }
00074 
00075         class const_iterator
00076         {
00077                 size_t i;
00078         public:
00079 
00080                 // iterator_traits
00081                 typedef std::random_access_iterator_tag iterator_category;
00082                 typedef T value_type;
00083                 typedef ptrdiff_t difference_type;
00084                 typedef const value_type* pointer;
00085                 typedef value_type reference;
00086 
00087                 const_iterator() { }
00088                 const_iterator(const const_iterator& it) : i(it.i) { }
00089                 const_iterator(size_t i) : i(i) { }
00090 
00091                 const_iterator& operator++() { ++i; return *this; }
00092                 const_iterator& operator--() { --i; return *this; }
00093 
00094                 const_iterator operator++(int) { return const_iterator(i++); }
00095                 const_iterator operator--(int) { return const_iterator(i++); }
00096 
00097                 // random access
00098                 const_iterator operator +(const difference_type i) const
00099                         { return const_iterator(this->i + i); }
00100                 const_iterator operator -(const difference_type i) const
00101                         { return const_iterator(this->i - i); }
00102                 const_iterator& operator +=(const difference_type i)
00103                         { this->i += i; return *this; }
00104                 const_iterator& operator -=(const difference_type i)
00105                         { this->i -= i; return *this; }
00106 
00107                 const T operator *() const { return V; }
00108 
00109                 const T* operator ->() const { return NULL; }
00110 
00111                 const T operator [] (size_t j) const { return V; }
00112 
00113                 const_iterator& operator=(const const_iterator& it)
00114                         { i = it->i; return *this; }
00115 
00116                 bool operator==(const const_iterator& it) const { return i == it->i; }
00117                 bool operator!=(const const_iterator& it) const { return i != it->i; }
00118 
00119         };
00120 
00121         typedef typename const_iterator::reference const_reference;
00122         typedef const_iterator best_iterator;
00123         typedef const_reference best_reference;
00124 
00125         typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00126 
00127         typedef ptrdiff_t iter_border_walker;
00128 
00129         const_iterator begin() const { return const_iterator(0); }
00130         const_iterator end() const { return const_iterator(len); }
00131         const_reverse_iterator rbegin() const { return const_iterator(len); }
00132         const_reverse_iterator rend() const { return const_iterator(0); }
00133 
00136         //TODO: explain this better.
00138         size_t length() const { return len; }
00140         size_type size() const { return length(); }
00142         size_t numel() const { return length(); }
00145         iter_border_walker first_to_last() { return this->length() - 1; }
00146         iter_border_walker begin_to_end() { return this->length(); }
00147 
00153 
00154         const T& operator[](size_t offset) const {
00155                 CHECK(offset >= 0 && offset < length(), erange);
00156                 return V;
00157         }
00162 
00163         array() : len(0) { }
00164 
00166         explicit array(size_t count) : len(count) { }
00167 
00169         explicit array(int count) : len(count) { }
00170 
00172         explicit array(long int count) : len(count) { }
00173 
00176         array(size_t count, const T& s) : len(count) { }
00177 
00180         array(size_t count, const T *ptr) : len(count) { }
00181 
00183         template <class J>
00184         array(const internal::reference_face<J>& s) : len(1) { }
00185 
00187         array(const this_type& a) : len(a.len) { }
00188 
00191         template <class S>
00192         array(const array<T, S>& a, size_t n) : len(n) { }
00193 
00196         template <class S>
00197         array(const array<T, S>& a) : len(a.length()) { }
00198 
00201         template <class J, class S>
00202         array(size_t count, const array<J, S>& a) : len(count) { }
00203         //TODO: ensure (in this but more importantly in other classes
00204         // that the same rule is applied to all (count, array) - (array, count) pairs.
00205         // i have in mind some rule that, in one case, the minimum (or maximum)
00206         // of array length, n is used. Ensure that this doesnt hold.
00207 
00210 
00211         ~array() { }
00212 
00213 
00217 
00218         this_type& operator=(const this_type& o) { len = o.len; return *this; }
00222 };
00223 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations