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 00030 template <class T, 00031 class DERIVED_INFO 00032 > 00033 class array<T, data::ref_val_repeat<DERIVED_INFO> > 00034 : 00035 public array_common_base<array<T, 00036 data::ref_val_repeat<DERIVED_INFO> > > 00037 { 00038 00039 private: 00040 typedef array_common_base<array<T, 00041 data::ref_val_repeat<DERIVED_INFO> > > common_base_class; 00042 00043 size_t len; 00044 const T* val; 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 this_type::derived_type derived_type; 00056 00057 typedef typename common_base_class::base_class base_class; 00058 00060 typedef size_t size_type; 00061 00063 typedef ptrdiff_t diff_type; 00064 00065 using base_class::derived; 00066 00067 void setval(const T& val) 00068 { 00069 this->val = &val; 00070 } 00071 00072 void resize(size_t len) 00073 { 00074 this->len = len; 00075 } 00076 00077 class const_iterator 00078 { 00079 size_t i; 00080 const T* val; 00081 public: 00082 00083 // iterator_traits 00084 typedef std::random_access_iterator_tag iterator_category; 00085 typedef T value_type; 00086 typedef ptrdiff_t difference_type; 00087 typedef const value_type* pointer; 00088 typedef value_type reference; 00089 00090 const_iterator() { } 00091 const_iterator(const const_iterator& it) : i(it.i), val(it.val) { } 00092 const_iterator(size_t i, const T* val) : i(i), val(val) { } 00093 00094 const_iterator& operator++() { ++i; return *this; } 00095 const_iterator& operator--() { --i; return *this; } 00096 00097 const_iterator operator++(int) { return const_iterator(i++); } 00098 const_iterator operator--(int) { return const_iterator(i++); } 00099 00100 // random access 00101 const_iterator operator +(const difference_type i) const 00102 { return const_iterator(this->i + i, val); } 00103 const_iterator operator -(const difference_type i) const 00104 { return const_iterator(this->i - i, val); } 00105 const_iterator& operator +=(const difference_type i) 00106 { this->i += i; return *this; } 00107 const_iterator& operator -=(const difference_type i) 00108 { this->i -= i; return *this; } 00109 00110 const T operator *() const { return *val; } 00111 00112 const T* operator ->() const { return val; } 00113 00114 const T operator [] (size_t j) const { return *val; } 00115 00116 const_iterator& operator=(const const_iterator& it) 00117 { i = it.i; val = it.val; return *this; } 00118 00119 bool operator==(const const_iterator& it) const { return i == it.i; } 00120 bool operator!=(const const_iterator& it) const { return i != it.i; } 00121 00122 }; 00123 00124 typedef typename const_iterator::reference const_reference; 00125 typedef const_reference best_reference; 00126 typedef const_iterator best_iterator; 00127 00128 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00129 00130 typedef ptrdiff_t iter_border_walker; 00131 00132 const_iterator begin() const { return const_iterator(0, val); } 00133 const_iterator end() const { return const_iterator(len, val); } 00134 const_reverse_iterator rbegin() const { return const_iterator(len, val); } 00135 const_reverse_iterator rend() const { return const_iterator(0, val); } 00136 00139 00140 size_t length() const { return len; } 00142 size_type size() const { return length(); } 00144 size_t numel() const { return length(); } 00147 iter_border_walker first_to_last() { return this->length() - 1; } 00148 iter_border_walker begin_to_end() { return this->length(); } 00149 00155 00156 const T& operator[](size_t offset) const { 00157 CHECK(offset >= 0 && offset < length(), erange); 00158 return *val; 00159 } 00164 00165 array() : len(0) { } 00166 00168 explicit array(size_t count) : len(count) { } 00169 00171 explicit array(int count) : len(count) { } 00172 00174 explicit array(long int count) : len(count) { } 00175 00177 array(size_t count, const T& s) : len(count), val(&s) { } 00178 00181 array(size_t count, const T *ptr) : len(count), val(ptr) { } 00182 00184 array(const this_type& a) : len(a.len), val(a.val) { } 00185 00188 template <class J, class S> 00189 array(const array<J, S>& a, size_t n) : len(n) { } 00190 00193 template <class J, class S> 00194 array(const array<J, S>& a) : len(a.length()) { } 00195 00198 template <class J, class S> 00199 array(size_t count, const array<J, S>& a) : len(count) { } 00200 //TODO: ensure (in this but more importantly in other classes 00201 // that the same rule is applied to all (count, array) - (array, count) pairs. 00202 // i have in mind some rule that, in one case, the minimum (or maximum) 00203 // of array length, n is used. Ensure that this doesnt hold. 00204 00207 00208 ~array() { } 00209 00210 void init(size_t count, const T& s) { len = count; val = &s; } 00211 00212 void init(const this_type& a) { len = a.len; val = a.val; } 00213 00217 00218 this_type& operator=(const this_type& o) 00219 { len = o.len; val = o.val; return *this; } 00223 }; 00224