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