vector_vector_resize.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright 2011 Mario Mulansky
  3. Copyright 2012 Karsten Ahnert
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or
  6. copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. /* reserved vector */
  9. #ifndef VECTOR_VECTOR_RESIZE_HPP
  10. #define VECTOR_VECTOR_RESIZE_HPP
  11. #include <vector>
  12. #include <boost/range.hpp>
  13. namespace boost { namespace numeric { namespace odeint {
  14. template<>
  15. struct is_resizeable< std::vector< std::vector< double > > >
  16. {
  17. typedef boost::true_type type;
  18. const static bool value = type::value;
  19. };
  20. template<>
  21. struct same_size_impl< std::vector< std::vector< double > > , std::vector< std::vector< double > > >
  22. {
  23. typedef std::vector< std::vector< double > > state_type;
  24. static bool same_size( const state_type &x1 ,
  25. const state_type &x2 )
  26. {
  27. bool same = ( boost::size( x1 ) == boost::size( x2 ) );
  28. if( !same )
  29. return false;
  30. typename state_type::const_iterator begin1 = boost::begin( x1 );
  31. typename state_type::const_iterator begin2 = boost::begin( x2 );
  32. while( begin1 != boost::end( x1 ) )
  33. same &= ( boost::size( *begin1++ ) == boost::size( *begin2++ ) );
  34. return same;
  35. }
  36. };
  37. template<>
  38. struct resize_impl< std::vector< std::vector< double > > , std::vector< std::vector< double > > >
  39. {
  40. typedef std::vector< std::vector< double > > state_type;
  41. static void resize( state_type &x1 , const state_type &x2 )
  42. {
  43. x1.resize( boost::size( x2 ) );
  44. typename state_type::iterator begin1 = boost::begin( x1 );
  45. typename state_type::const_iterator begin2 = boost::begin( x2 );
  46. while( begin1 != boost::end( x1 ) )
  47. (*begin1++).resize( boost::size( *begin2++ ) );
  48. }
  49. };
  50. template<>
  51. struct state_wrapper< std::vector< std::vector< double > > >
  52. {
  53. typedef std::vector< std::vector< double > > state_type;
  54. typedef state_wrapper< state_type > state_wrapper_type;
  55. typedef boost::true_type is_resizeable;
  56. state_type m_v;
  57. template< class State >
  58. bool same_size( const State &x )
  59. {
  60. bool same = ( boost::size( m_v ) == boost::size( x ) );
  61. if( !same )
  62. return false;
  63. typename state_type::iterator begin1 = boost::begin( m_v );
  64. typename State::const_iterator begin2 = boost::begin( x );
  65. while( begin1 != boost::end( m_v ) )
  66. same &= ( boost::size( *begin1++ ) == boost::size( *begin2++ ) );
  67. return same;
  68. }
  69. template< class State >
  70. bool resize( const State &x )
  71. {
  72. if( !same_size( x ) )
  73. {
  74. m_v.resize( boost::size( x ) );
  75. typename state_type::iterator begin1 = boost::begin( m_v );
  76. typename State::const_iterator begin2 = boost::begin( x );
  77. while( begin1 != boost::end( m_v ) )
  78. (*begin1++).resize( boost::size( *begin2++ ) );
  79. return true;
  80. } else
  81. return false;
  82. }
  83. };
  84. } } }
  85. #endif