36 #ifndef GMM_VECTOR_TO_MATRIX_H__
37 #define GMM_VECTOR_TO_MATRIX_H__
47 template <
typename PT>
struct gen_row_vector {
48 typedef gen_row_vector<PT> this_type;
49 typedef typename std::iterator_traits<PT>::value_type V;
51 typedef typename std::iterator_traits<PT>::reference ref_V;
52 typedef typename linalg_traits<this_type>::reference reference;
54 simple_vector_ref<PT> vec;
59 size_type ncols(
void)
const {
return vect_size(vec); }
61 gen_row_vector(ref_V v) : vec(v) {}
63 gen_row_vector(
const gen_row_vector<CPT> &cr) : vec(cr.vec) {}
66 template <
typename PT>
67 struct gen_row_vector_iterator {
68 typedef gen_row_vector<PT> this_type;
69 typedef typename modifiable_pointer<PT>::pointer MPT;
70 typedef typename std::iterator_traits<PT>::value_type V;
71 typedef simple_vector_ref<PT> value_type;
72 typedef const simple_vector_ref<PT> *pointer;
73 typedef const simple_vector_ref<PT> &reference;
74 typedef ptrdiff_t difference_type;
76 typedef std::random_access_iterator_tag iterator_category;
77 typedef gen_row_vector_iterator<PT> iterator;
79 simple_vector_ref<PT> vec;
82 iterator &operator ++() { isend =
true;
return *
this; }
83 iterator &operator --() { isend =
false;
return *
this; }
84 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
85 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
86 iterator &operator +=(difference_type i)
87 {
if (i) isend =
false;
return *
this; }
88 iterator &operator -=(difference_type i)
89 {
if (i) isend =
true;
return *
this; }
91 { iterator itt = *
this;
return (itt += i); }
93 { iterator itt = *
this;
return (itt -= i); }
94 difference_type
operator -(
const iterator &i)
const {
95 return (isend ==
true) ? ((i.isend ==
true) ? 0 : 1)
96 : ((i.isend ==
true) ? -1 : 0);
99 const simple_vector_ref<PT>& operator *()
const {
return vec; }
100 const simple_vector_ref<PT>& operator [](
int i) {
return vec; }
102 bool operator ==(
const iterator &i)
const {
return (isend == i.isend); }
103 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
104 bool operator < (
const iterator &i)
const {
return (*
this - i < 0); }
106 gen_row_vector_iterator(
void) {}
107 gen_row_vector_iterator(
const gen_row_vector_iterator<MPT> &itm)
108 : vec(itm.vec), isend(itm.isend) {}
109 gen_row_vector_iterator(
const gen_row_vector<PT> &m,
bool iis_end)
110 : vec(m.vec), isend(iis_end) { }
114 template <
typename PT>
115 struct linalg_traits<gen_row_vector<PT> > {
116 typedef gen_row_vector<PT> this_type;
117 typedef typename std::iterator_traits<PT>::value_type V;
118 typedef typename which_reference<PT>::is_reference is_reference;
119 typedef abstract_matrix linalg_type;
120 typedef typename linalg_traits<V>::origin_type origin_type;
121 typedef typename select_ref<
const origin_type *, origin_type *,
122 PT>::ref_type porigin_type;
123 typedef typename linalg_traits<V>::value_type value_type;
124 typedef typename select_ref<value_type,
125 typename linalg_traits<V>::reference, PT>::ref_type reference;
126 typedef abstract_null_type sub_col_type;
127 typedef abstract_null_type col_iterator;
128 typedef abstract_null_type const_sub_col_type;
129 typedef abstract_null_type const_col_iterator;
130 typedef simple_vector_ref<const V *> const_sub_row_type;
131 typedef typename select_ref<abstract_null_type,
132 simple_vector_ref<V *>, PT>::ref_type sub_row_type;
133 typedef gen_row_vector_iterator<typename const_pointer<PT>::pointer>
135 typedef typename select_ref<abstract_null_type,
136 gen_row_vector_iterator<PT>, PT>::ref_type row_iterator;
137 typedef typename linalg_traits<V>::storage_type storage_type;
138 typedef row_major sub_orientation;
139 typedef typename linalg_traits<V>::index_sorted index_sorted;
140 static size_type nrows(
const this_type &) {
return 1; }
141 static size_type ncols(
const this_type &m) {
return m.ncols(); }
142 static const_sub_row_type row(
const const_row_iterator &it) {
return *it; }
143 static sub_row_type row(
const row_iterator &it) {
return *it; }
144 static const_row_iterator row_begin(
const this_type &m)
145 {
return const_row_iterator(m,
false); }
146 static row_iterator row_begin(this_type &m)
147 {
return row_iterator(m,
false); }
148 static const_row_iterator row_end(
const this_type &m)
149 {
return const_row_iterator(m,
true); }
150 static row_iterator row_end(this_type &m)
151 {
return row_iterator(m,
true); }
152 static origin_type* origin(this_type &m) {
return m.vec.origin; }
153 static const origin_type* origin(
const this_type &m)
154 {
return m.vec.origin; }
155 static void do_clear(this_type &m)
156 {
clear(row(mat_row_begin(m))); }
157 static value_type access(
const const_row_iterator &itrow,
size_type i)
158 {
return itrow.vec[i]; }
159 static reference access(
const row_iterator &itrow,
size_type i)
160 {
return itrow.vec[i]; }
163 template <
typename PT>
164 std::ostream &operator <<(std::ostream &o,
const gen_row_vector<PT>& m)
165 { gmm::write(o,m);
return o; }
171 template <
typename PT>
struct gen_col_vector {
172 typedef gen_col_vector<PT> this_type;
173 typedef typename std::iterator_traits<PT>::value_type V;
175 typedef typename std::iterator_traits<PT>::reference ref_V;
176 typedef typename linalg_traits<this_type>::reference reference;
178 simple_vector_ref<PT> vec;
182 size_type ncols(
void)
const {
return 1; }
183 size_type nrows(
void)
const {
return vect_size(vec); }
185 gen_col_vector(ref_V v) : vec(v) {}
187 gen_col_vector(
const gen_col_vector<CPT> &cr) : vec(cr.vec) {}
190 template <
typename PT>
191 struct gen_col_vector_iterator {
192 typedef gen_col_vector<PT> this_type;
193 typedef typename modifiable_pointer<PT>::pointer MPT;
194 typedef typename std::iterator_traits<PT>::value_type V;
195 typedef simple_vector_ref<PT> value_type;
196 typedef const simple_vector_ref<PT> *pointer;
197 typedef const simple_vector_ref<PT> &reference;
198 typedef ptrdiff_t difference_type;
200 typedef std::random_access_iterator_tag iterator_category;
201 typedef gen_col_vector_iterator<PT> iterator;
203 simple_vector_ref<PT> vec;
206 iterator &operator ++() { isend =
true;
return *
this; }
207 iterator &operator --() { isend =
false;
return *
this; }
208 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
209 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
210 iterator &operator +=(difference_type i)
211 {
if (i) isend =
false;
return *
this; }
212 iterator &operator -=(difference_type i)
213 {
if (i) isend =
true;
return *
this; }
215 { iterator itt = *
this;
return (itt += i); }
217 { iterator itt = *
this;
return (itt -= i); }
218 difference_type
operator -(
const iterator &i)
const {
219 return (isend ==
true) ? ((i.isend ==
true) ? 0 : 1)
220 : ((i.isend ==
true) ? -1 : 0);
223 const simple_vector_ref<PT>& operator *()
const {
return vec; }
224 const simple_vector_ref<PT>& operator [](
int i) {
return vec; }
226 bool operator ==(
const iterator &i)
const {
return (isend == i.isend); }
227 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
228 bool operator < (
const iterator &i)
const {
return (*
this - i < 0); }
230 gen_col_vector_iterator(
void) {}
231 gen_col_vector_iterator(
const gen_col_vector_iterator<MPT> &itm)
232 : vec(itm.vec), isend(itm.isend) {}
233 gen_col_vector_iterator(
const gen_col_vector<PT> &m,
bool iis_end)
234 : vec(m.vec), isend(iis_end) { }
238 template <
typename PT>
239 struct linalg_traits<gen_col_vector<PT> > {
240 typedef gen_col_vector<PT> this_type;
241 typedef typename std::iterator_traits<PT>::value_type V;
242 typedef typename which_reference<PT>::is_reference is_reference;
243 typedef abstract_matrix linalg_type;
244 typedef typename linalg_traits<V>::origin_type origin_type;
245 typedef typename select_ref<
const origin_type *, origin_type *,
246 PT>::ref_type porigin_type;
247 typedef typename linalg_traits<V>::value_type value_type;
248 typedef typename select_ref<value_type,
249 typename linalg_traits<V>::reference, PT>::ref_type reference;
250 typedef abstract_null_type sub_row_type;
251 typedef abstract_null_type row_iterator;
252 typedef abstract_null_type const_sub_row_type;
253 typedef abstract_null_type const_row_iterator;
254 typedef simple_vector_ref<const V *> const_sub_col_type;
255 typedef typename select_ref<abstract_null_type,
256 simple_vector_ref<V *>, PT>::ref_type sub_col_type;
257 typedef gen_col_vector_iterator<typename const_pointer<PT>::pointer>
259 typedef typename select_ref<abstract_null_type,
260 gen_col_vector_iterator<PT>, PT>::ref_type col_iterator;
261 typedef typename linalg_traits<V>::storage_type storage_type;
262 typedef col_major sub_orientation;
263 typedef typename linalg_traits<V>::index_sorted index_sorted;
264 static size_type ncols(
const this_type &) {
return 1; }
265 static size_type nrows(
const this_type &m) {
return m.nrows(); }
266 static const_sub_col_type col(
const const_col_iterator &it) {
return *it; }
267 static sub_col_type col(
const col_iterator &it) {
return *it; }
268 static const_col_iterator col_begin(
const this_type &m)
269 {
return const_col_iterator(m,
false); }
270 static col_iterator col_begin(this_type &m)
271 {
return col_iterator(m,
false); }
272 static const_col_iterator col_end(
const this_type &m)
273 {
return const_col_iterator(m,
true); }
274 static col_iterator col_end(this_type &m)
275 {
return col_iterator(m,
true); }
276 static origin_type* origin(this_type &m) {
return m.vec.origin; }
277 static const origin_type* origin(
const this_type &m)
278 {
return m.vec.origin; }
279 static void do_clear(this_type &m)
280 {
clear(col(mat_col_begin(m))); }
281 static value_type access(
const const_col_iterator &itcol,
size_type i)
282 {
return itcol.vec[i]; }
283 static reference access(
const col_iterator &itcol,
size_type i)
284 {
return itcol.vec[i]; }
287 template <
typename PT>
288 std::ostream &operator <<(std::ostream &o,
const gen_col_vector<PT>& m)
289 { gmm::write(o,m);
return o; }
296 template <
class V>
inline
297 typename select_return< gen_row_vector<const V *>, gen_row_vector<V *>,
298 const V *>::return_type
299 row_vector(
const V& v) {
300 return typename select_return< gen_row_vector<const V *>,
301 gen_row_vector<V *>,
const V *>::return_type(linalg_cast(v));
304 template <
class V>
inline
305 typename select_return< gen_row_vector<const V *>, gen_row_vector<V *>,
308 return typename select_return< gen_row_vector<const V *>,
309 gen_row_vector<V *>, V *>::return_type(linalg_cast(v));
312 template <
class V>
inline gen_row_vector<const V *>
313 const_row_vector(V& v)
314 {
return gen_row_vector<const V *>(v); }
317 template <
class V>
inline
318 typename select_return< gen_col_vector<const V *>, gen_col_vector<V *>,
319 const V *>::return_type
320 col_vector(
const V& v) {
321 return typename select_return< gen_col_vector<const V *>,
322 gen_col_vector<V *>,
const V *>::return_type(linalg_cast(v));
325 template <
class V>
inline
326 typename select_return< gen_col_vector<const V *>, gen_col_vector<V *>,
329 return typename select_return< gen_col_vector<const V *>,
330 gen_col_vector<V *>, V *>::return_type(linalg_cast(v));
333 template <
class V>
inline gen_col_vector<const V *>
334 const_col_vector(V& v)
335 {
return gen_col_vector<const V *>(v); }
340 #endif // GMM_VECTOR_TO_MATRIX_H__