sp_resize.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2006 Michael Stevens
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <iostream>
  8. #include <boost/numeric/ublas/vector_sparse.hpp>
  9. typedef double Real;
  10. template <class V>
  11. void printV(const V& v) {
  12. std::cout << "size: " << v.size() << " nnz_capacity: " << v.nnz_capacity() << " nnz: " << v.nnz() << std::endl;
  13. for (typename V::const_iterator i = v.begin(); i != v.end(); i++) {
  14. std::cout << i.index() << ":" << (*i) << " ";
  15. }
  16. std::cout << std::endl;
  17. }
  18. template <class V>
  19. void run_test()
  20. {
  21. V v(10);
  22. v[0] = 1;
  23. v[5] = 1;
  24. v[8] = 1;
  25. v[9] = 1;
  26. printV(v);
  27. v.resize(9); printV(v);
  28. v.resize(12); printV(v);
  29. v.resize(2); printV(v);
  30. v.resize(0); printV(v);
  31. v.resize(5); v[0] = 1; printV(v);
  32. v.resize(5,false); printV(v);
  33. }
  34. int main(int, char **) {
  35. std::cout << "---- MAPPED ----\n";
  36. run_test< boost::numeric::ublas::mapped_vector<Real> >();
  37. std::cout << "---- COMPRESSED ----\n";
  38. run_test< boost::numeric::ublas::compressed_vector<Real> >();
  39. std::cout << "---- COORDINATE ----\n";
  40. run_test< boost::numeric::ublas::coordinate_vector<Real> >();
  41. return 0;
  42. }