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::val_repeat<DERIVED_INFO> > 00034 : 00035 public array_common_base<array<T, 00036 data::val_repeat<DERIVED_INFO> > > 00037 { 00038 00039 private: 00040 typedef array_common_base<array<T, 00041 data::val_repeat<DERIVED_INFO> > > common_base_class; 00042 00043 size_t len; 00044 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(T val) 00068 { 00069 this->val = val; 00070 } 00071 00072 void resize(size_t len) 00073 { 00074 this->len = len; 00075 } 00076 00077 00078 class const_iterator 00079 { 00080 size_t i; 00081 T val; 00082 public: 00083 00084 // iterator_traits 00085 typedef std::random_access_iterator_tag iterator_category; 00086 typedef T value_type; 00087 typedef ptrdiff_t difference_type; 00088 typedef const value_type* pointer; 00089 typedef value_type reference; 00090 00091 const_iterator() { } 00092 const_iterator(const const_iterator& it) : i(it.i), val(it.val) { } 00093 const_iterator(size_t i, T val) : i(i), val(val) { } 00094 00095 const_iterator& operator++() { ++i; return *this; } 00096 const_iterator& operator--() { --i; return *this; } 00097 00098 const_iterator operator++(int) { return const_iterator(i++); } 00099 const_iterator operator--(int) { return const_iterator(i++); } 00100 00101 // random access 00102 const_iterator operator +(const difference_type i) const 00103 { return const_iterator(this->i + i, val); } 00104 const_iterator operator -(const difference_type i) const 00105 { return const_iterator(this->i - i, val); } 00106 const_iterator& operator +=(const difference_type i) 00107 { this->i += i; return *this; } 00108 const_iterator& operator -=(const difference_type i) 00109 { this->i -= i; return *this; } 00110 00111 ptrdiff_t operator-(const const_iterator& o) const 00112 { return this->i - o.i; } 00113 00114 const T operator *() const { return val; } 00115 00116 const T* operator ->() const { return &val; } 00117 00118 const T operator [] (size_t j) const { return val; } 00119 00120 const_iterator& operator=(const const_iterator& it) 00121 { i = it.i; val = it.val; return *this; } 00122 00123 bool operator==(const const_iterator& it) const { return i == it.i; } 00124 bool operator!=(const const_iterator& it) const { return i != it.i; } 00125 bool operator<(const const_iterator& it) const { return (i < it.i); } 00126 bool operator<=(const const_iterator& it) const { return (i <= it.i); } 00127 bool operator>(const const_iterator& it) const { return (i > it.i); } 00128 bool operator>=(const const_iterator& it) const { return (i >= it.i); } 00129 00130 00131 }; 00132 00133 typedef typename const_iterator::reference const_reference; 00134 typedef const_reference best_reference; 00135 typedef const_iterator best_iterator; 00136 00137 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00138 00139 typedef ptrdiff_t iter_border_walker; 00140 00141 const_iterator begin() const { return const_iterator(0, val); } 00142 const_iterator end() const { return const_iterator(len, val); } 00143 const_reverse_iterator rbegin() const { return const_iterator(len, val); } 00144 const_reverse_iterator rend() const { return const_iterator(0, val); } 00145 00148 00149 size_t length() const { return len; } 00151 size_type size() const { return length(); } 00153 size_t numel() const { return length(); } 00156 iter_border_walker first_to_last() { return this->length() - 1; } 00157 iter_border_walker begin_to_end() { return this->length(); } 00158 00164 00165 T operator[](size_t offset) const { 00166 CHECK(offset >= 0 && offset < length(), erange); 00167 return val; 00168 } 00173 00174 array() : len(0) { } 00175 00177 explicit array(size_t count) : len(count) { } 00178 00180 explicit array(int count) : len(count) { } 00181 00183 explicit array(long int count) : len(count) { } 00184 00186 array(size_t count, T s) : len(count), val(s) { } 00187 00190 array(size_t count, const T *ptr) : len(count), val(*ptr) { } 00191 00193 array(const this_type& a) : len(a.len), val(a.val) { } 00194 00197 template <class S> 00198 array(const array<T, S>& a, size_t n) : len(n) { } 00199 00202 template <class S> 00203 array(const array<T, S>& a) : len(a.length()) { } 00204 00207 template <class J, class S> 00208 array(size_t count, const array<J, S>& a) : len(count) 00209 { this->init_size_with(a); } 00210 00211 template <class J> 00212 void init_size_with_element(const J& s) { val = s; }; 00213 00214 00215 void init(size_t count, T s) { len = count; val = s; } 00216 00217 void init(const this_type& a) { len = a.len; val = a.val; } 00218 00221 00222 ~array() { } 00223 00224 00228 00229 this_type& operator=(const this_type& o) 00230 { len = o.len; val = o.val; return *this; } 00234 }; 00235