ViennaCL - The Vienna Computing Library  1.5.1
adapter.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TOOLS_ADAPTER_HPP_
2 #define VIENNACL_TOOLS_ADAPTER_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include <string>
26 #include <fstream>
27 #include <sstream>
28 #include <assert.h>
29 #include "viennacl/forwards.h"
30 
31 #include <vector>
32 #include <map>
33 
34 namespace viennacl
35 {
36  namespace tools
37  {
38 
47  template <typename SCALARTYPE, typename SizeType, bool is_iterator1, bool is_forward>
49  {
51 
52  public:
56 
57  const_sparse_matrix_adapted_iterator(std::vector<std::map<SizeType, SCALARTYPE> > const & mat, int i, int j)
58  : mat_(mat), i_(i), j_(j)
59  {
60  if (i < 0) //reverse iterator end
61  {
62  //iter2 = mat_[0].rend(); //reverse iterator end
63  }
64  else //i_ is valid
65  {
66  if (j < 0)
67  {
68  //iter2 = mat_[i].rend();
69  }
70  else //j_ is valid
71  {
72  if (i_ < mat_.size() && mat_[i].size() > 0 )
73  {
74  //TODO: Start at entry j, not at the beginning
75  if (static_cast<int>(mat_[i].rbegin()->first) < j)
76  iter2 = mat_[i].end();
77  else
78  iter2 = mat_[i].begin();
79  }
80  else if (i_ < mat_.size() && mat_[i].size() == 0)
81  iter2 = mat_[i].end();
82  else //i is out of range -> end iterator requested
83  iter2 = mat_.back().end(); //forward iterator end
84  }
85  }
86  }
87 
88  SCALARTYPE operator*(void) const
89  {
90  if (is_iterator1)
91  {
92  typedef typename std::map<SizeType, SCALARTYPE>::const_iterator col_iterator;
93 
94  col_iterator colit = mat_[i_].find(static_cast<unsigned int>(j_));
95 
96  if (colit != mat_[i_].end())
97  return colit->second;
98  return 0.0;
99  }
100  else
101  return iter2->second;
102  }
103 
105  {
106  if (is_iterator1)
107  {
108  if (is_forward)
109  ++i_;
110  else
111  --i_;
112  }
113  else
114  ++iter2;
115  return *this;
116  }
117  self_type operator++(int) { self_type tmp = *this; ++(*this); return tmp; }
118 
119  self_type operator+=(SizeType offset)
120  {
121  if (is_iterator1)
122  {
123  if (is_forward)
124  i_ += offset;
125  else
126  i_ -= offset;
127  }
128  else
129  {
130  for (SizeType k=0; k<offset; ++k)
131  ++iter2; //Note: User must ensure that this is always valid...
132  }
133  return *this;
134  }
135 
136  bool operator==(self_type const & other) const
137  {
138  return is_iterator1 ? (i_ == other.i_) : (iter2 == other.iter2);
139  }
140 
141  bool operator!=(self_type const & other) const { return !(*this == other); }
142 
143  size_type index1() const { return i_; }
145  {
146  if (is_iterator1)
147  return 0;
148  else
149  return iter2->first;
150  }
151 
153  {
155  }
157  {
158  int end_ = static_cast<int>(mat_[i_].size());
159  if (end_ > 0)
160  end_ = mat_[i_].rbegin()->first;
161  return const_sparse_matrix_adapted_iterator<SCALARTYPE, SizeType, !is_iterator1, true>(mat_, static_cast<int>(i_), end_ + 1);
162  }
163 
164  private:
165  std::vector<std::map<SizeType, SCALARTYPE> > const & mat_;
166  typename std::map<SizeType, SCALARTYPE>::const_iterator iter2;
167  size_type i_;
168  size_type j_;
169  };
170 
175  template <typename SCALARTYPE, typename SizeType = unsigned int>
177  {
178  public:
181 
183  typedef SCALARTYPE value_type;
185 
186  const_sparse_matrix_adapter(std::vector<std::map<SizeType, SCALARTYPE> > const & mat)
187  : mat_(mat), size1_(mat_.size()), size2_(mat_.size()) {}
188 
189  const_sparse_matrix_adapter(std::vector<std::map<SizeType, SCALARTYPE> > const & mat, size_type num_rows, size_type num_cols)
190  : mat_(mat), size1_(num_rows), size2_(num_cols) {}
191 
192  size_type size1() const { return size1_; }
193  size_type size2() const { return size2_; }
194 
195  const_iterator1 begin1() const { return const_iterator1(mat_, 0, 0); }
196  const_iterator1 end1() const { return const_iterator1(mat_, static_cast<int>(size1()), static_cast<int>(size2())); }
197 
198  const_reverse_iterator1 rbegin1() const { return const_reverse_iterator1(mat_, static_cast<int>(size1() - 1), 0); }
199  const_reverse_iterator1 rend1() const { return const_reverse_iterator1(mat_, -1, static_cast<int>(size2())); }
200 
201  const_iterator2 begin2() const { return const_iterator2(mat_, 0, 0); }
202  const_iterator2 end2() const { return const_iterator2(mat_, size1(), size2()); }
203 
204  SCALARTYPE operator()(SizeType i, SizeType j) const
205  {
206  typedef typename std::map<SizeType, SCALARTYPE>::const_iterator col_iterator;
207 
208  col_iterator colit = mat_[i].find(j);
209 
210  if (colit != mat_[i].end())
211  return colit->second;
212  return 0.0;
213  }
214 
215  private:
216  std::vector<std::map<SizeType, SCALARTYPE> > const & mat_;
217  size_type size1_;
218  size_type size2_;
219  };
220 
221 
229  template <typename SCALARTYPE, typename SizeType, bool is_iterator1>
231  {
233 
234  public:
238 
239  sparse_matrix_adapted_iterator(std::vector<std::map<SizeType, SCALARTYPE> > & mat, int i, int j)
240  : mat_(mat), i_(i), j_(j)
241  {
242  if (i < 0) //reverse iterator end
243  {
244  //iter2 = mat_[0].rend(); //reverse iterator end
245  }
246  else //_i is valid
247  {
248  if (j < 0)
249  {
250  //iter2 = mat[i]_.rend();
251  }
252  else //_j is valid
253  {
254  if (i_ < mat_.size() && mat_[i].size() > 0 )
255  {
256  //TODO: Start at entry j, not at the beginning
257  if (static_cast<int>(mat_[i].rbegin()->first) < j)
258  iter2 = mat_[i].end();
259  else
260  iter2 = mat_[i].begin();
261  }
262  else if (i_ < mat_.size() && mat_[i].size() == 0)
263  iter2 = mat_[i].end();
264  else //i is out of range -> end iterator requested
265  iter2 = mat_.back().end(); //forward iterator end
266  }
267  }
268  }
269 
270  SCALARTYPE & operator*(void)
271  {
272  if (is_iterator1)
273  {
274  return mat_[i_][static_cast<SizeType>(j_)];
275  }
276  else
277  return iter2->second;
278  }
279 
281  {
282  if (is_iterator1)
283  ++i_;
284  else
285  ++iter2;
286  return *this;
287  }
288  self_type operator++(int) { self_type tmp = *this; ++(*this); return tmp; }
289 
291  {
292  if (is_iterator1)
293  i_ += offset;
294  else
295  {
296  for (size_type k=0; k<offset; ++k)
297  ++iter2; //Note: User must ensure that this is always valid...
298  }
299  return *this;
300  }
301 
302  bool operator==(self_type const & other) const
303  {
304  if (is_iterator1)
305  return (i_ == other.i_);
306  return (iter2 == other.iter2);
307  }
308  bool operator!=(self_type const & other) const { return !(*this == other); }
309 
310  size_type index1() const { return i_; }
312  {
313  if (is_iterator1)
314  return 0;
315  else
316  return iter2->first;
317  }
318 
320  {
321  return sparse_matrix_adapted_iterator<SCALARTYPE, SizeType, !is_iterator1>(mat_, static_cast<int>(i_), 0);
322  }
324  {
325  int end_ = static_cast<int>(mat_[i_].size());
326  if (end_ > 0)
327  end_ = mat_[i_].rbegin()->first;
328  return sparse_matrix_adapted_iterator<SCALARTYPE, SizeType, !is_iterator1>(mat_, static_cast<int>(i_), end_ + 1);
329  }
330 
331  private:
332  std::vector<std::map<SizeType, SCALARTYPE> > & mat_;
333  typename std::map<SizeType, SCALARTYPE>::iterator iter2;
334  size_type i_;
335  size_type j_;
336  };
337 
338 
339 
344  template <typename SCALARTYPE, typename SizeType = unsigned int>
345  class sparse_matrix_adapter : public const_sparse_matrix_adapter<SCALARTYPE, SizeType>
346  {
348  public:
353  typedef SizeType size_type;
354 
355  sparse_matrix_adapter(std::vector<std::map<SizeType, SCALARTYPE> > & mat)
356  : BaseType(mat), mat_(mat), size1_(mat_.size()), size2_(mat_.size()) {}
357 
358  sparse_matrix_adapter(std::vector<std::map<SizeType, SCALARTYPE> > & mat,
359  vcl_size_t num_rows,
360  vcl_size_t num_cols)
361  : BaseType(mat, num_rows, num_cols), mat_(mat), size1_(static_cast<size_type>(num_rows)), size2_(static_cast<size_type>(num_cols)) {}
362 
363  iterator1 begin1() { return iterator1(mat_, 0, 0); }
364  iterator1 end1() { return iterator1(mat_, static_cast<int>(mat_.size()), static_cast<int>(mat_.back().size())); }
365 
366  const_iterator1 begin1() const { return const_iterator1(mat_, 0, 0); }
367  const_iterator1 end1() const { return const_iterator1(mat_, size1(), size2()); }
368 
369  iterator2 begin2() { return iterator2(mat_, 0, 0); }
370  iterator2 end2() { return iterator2(mat_, mat_.size(), mat_.back().size()); }
371 
372  const_iterator2 begin2() const { return const_iterator2(mat_, 0, 0); }
373  const_iterator2 end2() const { return const_iterator2(mat_, size1(), size2()); }
374 
375  SCALARTYPE & operator()(vcl_size_t i, vcl_size_t j) { return mat_[i][static_cast<size_type>(j)]; }
376 
377  void resize(vcl_size_t i, vcl_size_t j, bool preserve = true)
378  {
379  if (i>0)
380  mat_.resize(i);
381  if (!preserve)
382  clear();
383 
384  size1_ = static_cast<size_type>(i);
385  size2_ = static_cast<size_type>(j);
386  }
387 
388  void clear()
389  {
390  for (size_type i=0; i<mat_.size(); ++i)
391  mat_[i].clear();
392  }
393 
394  size_type size1() { return size1_; }
395  size_type size1() const { return size1_; } //Note: Due to name hiding it is not sufficient to have it in the base class
396 
397  //assume a square matrix
398  size_type size2() { return size2_; }
399  size_type size2() const { return size2_; } //Note: Due to name hiding it is not sufficient to have it in the base class
400 
401  private:
402  std::vector<std::map<SizeType, SCALARTYPE> > & mat_;
403  size_type size1_;
404  size_type size2_;
405  };
406 
407 
408  }
409 }
410 #endif
vcl_size_t size_type
Definition: adapter.hpp:184
size_type index1() const
Definition: adapter.hpp:143
void resize(vcl_size_t i, vcl_size_t j, bool preserve=true)
Definition: adapter.hpp:377
sparse_matrix_adapter(std::vector< std::map< SizeType, SCALARTYPE > > &mat)
Definition: adapter.hpp:355
std::size_t vcl_size_t
Definition: forwards.h:58
const_iterator1 end1() const
Definition: adapter.hpp:367
SCALARTYPE operator*(void) const
Definition: adapter.hpp:88
const_iterator2 begin2() const
Definition: adapter.hpp:201
size_type size1()
Definition: adapter.hpp:394
size_type size1() const
Definition: adapter.hpp:192
self_type iterator2
Definition: adapter.hpp:236
const_sparse_matrix_adapted_iterator< SCALARTYPE, SizeType, false, true > const_iterator2
Definition: adapter.hpp:180
size_type size2() const
Definition: adapter.hpp:399
This file provides the forward declarations for the main types used within ViennaCL.
const_iterator1 end1() const
Definition: adapter.hpp:196
sparse_matrix_adapted_iterator< SCALARTYPE, SizeType,!is_iterator1 > begin() const
Definition: adapter.hpp:319
const_sparse_matrix_adapted_iterator< SCALARTYPE, SizeType,!is_iterator1, true > end() const
Definition: adapter.hpp:156
self_type operator+=(SizeType offset)
Definition: adapter.hpp:119
iterator1 begin1()
Definition: adapter.hpp:363
const_sparse_matrix_adapted_iterator< SCALARTYPE, SizeType, true, true > const_iterator1
Definition: adapter.hpp:351
const_sparse_matrix_adapter(std::vector< std::map< SizeType, SCALARTYPE > > const &mat, size_type num_rows, size_type num_cols)
Definition: adapter.hpp:189
sparse_matrix_adapted_iterator< SCALARTYPE, SizeType,!is_iterator1 > end() const
Definition: adapter.hpp:323
self_type operator+=(size_type offset)
Definition: adapter.hpp:290
self_type & operator++(void)
Definition: adapter.hpp:104
bool operator==(self_type const &other) const
Definition: adapter.hpp:136
const_iterator1 begin1() const
Definition: adapter.hpp:366
size_type size2()
Definition: adapter.hpp:398
size_type size2() const
Definition: adapter.hpp:193
self_type iterator1
Definition: adapter.hpp:235
iterator1 end1()
Definition: adapter.hpp:364
A const iterator for sparse matrices of type std::vector<std::map<SizeType, SCALARTYPE> > ...
Definition: adapter.hpp:48
sparse_matrix_adapter(std::vector< std::map< SizeType, SCALARTYPE > > &mat, vcl_size_t num_rows, vcl_size_t num_cols)
Definition: adapter.hpp:358
const_iterator2 begin2() const
Definition: adapter.hpp:372
size_type size1() const
Definition: adapter.hpp:395
const_reverse_iterator1 rend1() const
Definition: adapter.hpp:199
SCALARTYPE operator()(SizeType i, SizeType j) const
Definition: adapter.hpp:204
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:144
self_type iterator2
Definition: adapter.hpp:54
sparse_matrix_adapted_iterator< SCALARTYPE, SizeType, true > iterator1
Definition: adapter.hpp:349
SCALARTYPE value_type
Definition: adapter.hpp:183
SizeType size_type
Definition: adapter.hpp:353
const_sparse_matrix_adapted_iterator< SCALARTYPE, SizeType, false, true > const_iterator2
Definition: adapter.hpp:352
size_type index2() const
Definition: adapter.hpp:144
const_sparse_matrix_adapted_iterator< SCALARTYPE, SizeType,!is_iterator1, true > begin() const
Definition: adapter.hpp:152
Adapts a constant sparse matrix type made up from std::vector<std::map<SizeType, SCALARTYPE> > to bas...
Definition: adapter.hpp:176
A non-const iterator for sparse matrices of type std::vector<std::map<SizeType, SCALARTYPE> > ...
Definition: adapter.hpp:230
SCALARTYPE & operator*(void)
Definition: adapter.hpp:270
bool operator==(self_type const &other) const
Definition: adapter.hpp:302
const_sparse_matrix_adapted_iterator(std::vector< std::map< SizeType, SCALARTYPE > > const &mat, int i, int j)
Definition: adapter.hpp:57
const_reverse_iterator1 rbegin1() const
Definition: adapter.hpp:198
sparse_matrix_adapted_iterator< SCALARTYPE, SizeType, false > iterator2
Definition: adapter.hpp:350
SCALARTYPE & operator()(vcl_size_t i, vcl_size_t j)
Definition: adapter.hpp:375
size_type index2() const
Definition: adapter.hpp:311
size_type index1() const
Definition: adapter.hpp:310
void clear()
Definition: adapter.hpp:388
bool operator!=(self_type const &other) const
Definition: adapter.hpp:141
self_type operator++(int)
Definition: adapter.hpp:117
self_type iterator1
Definition: adapter.hpp:53
const_sparse_matrix_adapter(std::vector< std::map< SizeType, SCALARTYPE > > const &mat)
Definition: adapter.hpp:186
iterator2 begin2()
Definition: adapter.hpp:369
const_iterator1 begin1() const
Definition: adapter.hpp:195
Adapts a non-const sparse matrix type made up from std::vector<std::map<SizeType, SCALARTYPE> > to ba...
Definition: adapter.hpp:345
vcl_size_t size_type
Definition: adapter.hpp:55
const_sparse_matrix_adapted_iterator< SCALARTYPE, SizeType, true, false > const_reverse_iterator1
Definition: adapter.hpp:182
const_iterator2 end2() const
Definition: adapter.hpp:373
vcl_size_t size_type
Definition: adapter.hpp:237
const_iterator2 end2() const
Definition: adapter.hpp:202
const_sparse_matrix_adapted_iterator< SCALARTYPE, SizeType, true, true > const_iterator1
Definition: adapter.hpp:179
self_type operator++(int)
Definition: adapter.hpp:288
iterator2 end2()
Definition: adapter.hpp:370
sparse_matrix_adapted_iterator(std::vector< std::map< SizeType, SCALARTYPE > > &mat, int i, int j)
Definition: adapter.hpp:239
bool operator!=(self_type const &other) const
Definition: adapter.hpp:308
self_type & operator++(void)
Definition: adapter.hpp:280