matrix_assign.hpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. //
  2. // Copyright (c) 2000-2002
  3. // Joerg Walter, Mathias Koch
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // The authors gratefully acknowledge the support of
  10. // GeNeSys mbH & Co. KG in producing this work.
  11. //
  12. #ifndef _BOOST_UBLAS_MATRIX_ASSIGN_
  13. #define _BOOST_UBLAS_MATRIX_ASSIGN_
  14. #include <boost/numeric/ublas/traits.hpp>
  15. // Required for make_conformant storage
  16. #include <vector>
  17. // Iterators based on ideas of Jeremy Siek
  18. namespace boost { namespace numeric { namespace ublas {
  19. namespace detail {
  20. // Weak equality check - useful to compare equality two arbitary matrix expression results.
  21. // Since the actual expressions are unknown, we check for and arbitary error bound
  22. // on the relative error.
  23. // For a linear expression the infinity norm makes sense as we do not know how the elements will be
  24. // combined in the expression. False positive results are inevitable for arbirary expressions!
  25. template<class E1, class E2, class S>
  26. BOOST_UBLAS_INLINE
  27. bool equals (const matrix_expression<E1> &e1, const matrix_expression<E2> &e2, S epsilon, S min_norm) {
  28. return norm_inf (e1 - e2) <= epsilon *
  29. std::max<S> (std::max<S> (norm_inf (e1), norm_inf (e2)), min_norm);
  30. }
  31. template<class E1, class E2>
  32. BOOST_UBLAS_INLINE
  33. bool expression_type_check (const matrix_expression<E1> &e1, const matrix_expression<E2> &e2) {
  34. typedef typename type_traits<typename promote_traits<typename E1::value_type,
  35. typename E2::value_type>::promote_type>::real_type real_type;
  36. return equals (e1, e2, BOOST_UBLAS_TYPE_CHECK_EPSILON, BOOST_UBLAS_TYPE_CHECK_MIN);
  37. }
  38. template<class M, class E, class R>
  39. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  40. void make_conformant (M &m, const matrix_expression<E> &e, row_major_tag, R) {
  41. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  42. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  43. typedef R conformant_restrict_type;
  44. typedef typename M::size_type size_type;
  45. typedef typename M::difference_type difference_type;
  46. typedef typename M::value_type value_type;
  47. // FIXME unbounded_array with push_back maybe better
  48. std::vector<std::pair<size_type, size_type> > index;
  49. typename M::iterator1 it1 (m.begin1 ());
  50. typename M::iterator1 it1_end (m.end1 ());
  51. typename E::const_iterator1 it1e (e ().begin1 ());
  52. typename E::const_iterator1 it1e_end (e ().end1 ());
  53. while (it1 != it1_end && it1e != it1e_end) {
  54. difference_type compare = it1.index1 () - it1e.index1 ();
  55. if (compare == 0) {
  56. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  57. typename M::iterator2 it2 (it1.begin ());
  58. typename M::iterator2 it2_end (it1.end ());
  59. typename E::const_iterator2 it2e (it1e.begin ());
  60. typename E::const_iterator2 it2e_end (it1e.end ());
  61. #else
  62. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  63. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  64. typename E::const_iterator2 it2e (begin (it1e, iterator1_tag ()));
  65. typename E::const_iterator2 it2e_end (end (it1e, iterator1_tag ()));
  66. #endif
  67. if (it2 != it2_end && it2e != it2e_end) {
  68. size_type it2_index = it2.index2 (), it2e_index = it2e.index2 ();
  69. for (;;) {
  70. difference_type compare2 = it2_index - it2e_index;
  71. if (compare2 == 0) {
  72. ++ it2, ++ it2e;
  73. if (it2 != it2_end && it2e != it2e_end) {
  74. it2_index = it2.index2 ();
  75. it2e_index = it2e.index2 ();
  76. } else
  77. break;
  78. } else if (compare2 < 0) {
  79. increment (it2, it2_end, - compare2);
  80. if (it2 != it2_end)
  81. it2_index = it2.index2 ();
  82. else
  83. break;
  84. } else if (compare2 > 0) {
  85. if (conformant_restrict_type::other (it2e.index1 (), it2e.index2 ()))
  86. if (static_cast<value_type>(*it2e) != value_type/*zero*/())
  87. index.push_back (std::pair<size_type, size_type> (it2e.index1 (), it2e.index2 ()));
  88. ++ it2e;
  89. if (it2e != it2e_end)
  90. it2e_index = it2e.index2 ();
  91. else
  92. break;
  93. }
  94. }
  95. }
  96. while (it2e != it2e_end) {
  97. if (conformant_restrict_type::other (it2e.index1 (), it2e.index2 ()))
  98. if (static_cast<value_type>(*it2e) != value_type/*zero*/())
  99. index.push_back (std::pair<size_type, size_type> (it2e.index1 (), it2e.index2 ()));
  100. ++ it2e;
  101. }
  102. ++ it1, ++ it1e;
  103. } else if (compare < 0) {
  104. increment (it1, it1_end, - compare);
  105. } else if (compare > 0) {
  106. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  107. typename E::const_iterator2 it2e (it1e.begin ());
  108. typename E::const_iterator2 it2e_end (it1e.end ());
  109. #else
  110. typename E::const_iterator2 it2e (begin (it1e, iterator1_tag ()));
  111. typename E::const_iterator2 it2e_end (end (it1e, iterator1_tag ()));
  112. #endif
  113. while (it2e != it2e_end) {
  114. if (conformant_restrict_type::other (it2e.index1 (), it2e.index2 ()))
  115. if (static_cast<value_type>(*it2e) != value_type/*zero*/())
  116. index.push_back (std::pair<size_type, size_type> (it2e.index1 (), it2e.index2 ()));
  117. ++ it2e;
  118. }
  119. ++ it1e;
  120. }
  121. }
  122. while (it1e != it1e_end) {
  123. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  124. typename E::const_iterator2 it2e (it1e.begin ());
  125. typename E::const_iterator2 it2e_end (it1e.end ());
  126. #else
  127. typename E::const_iterator2 it2e (begin (it1e, iterator1_tag ()));
  128. typename E::const_iterator2 it2e_end (end (it1e, iterator1_tag ()));
  129. #endif
  130. while (it2e != it2e_end) {
  131. if (conformant_restrict_type::other (it2e.index1 (), it2e.index2 ()))
  132. if (static_cast<value_type>(*it2e) != value_type/*zero*/())
  133. index.push_back (std::pair<size_type, size_type> (it2e.index1 (), it2e.index2 ()));
  134. ++ it2e;
  135. }
  136. ++ it1e;
  137. }
  138. // ISSUE proxies require insert_element
  139. for (size_type k = 0; k < index.size (); ++ k)
  140. m (index [k].first, index [k].second) = value_type/*zero*/();
  141. }
  142. template<class M, class E, class R>
  143. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  144. void make_conformant (M &m, const matrix_expression<E> &e, column_major_tag, R) {
  145. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  146. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  147. typedef R conformant_restrict_type;
  148. typedef typename M::size_type size_type;
  149. typedef typename M::difference_type difference_type;
  150. typedef typename M::value_type value_type;
  151. std::vector<std::pair<size_type, size_type> > index;
  152. typename M::iterator2 it2 (m.begin2 ());
  153. typename M::iterator2 it2_end (m.end2 ());
  154. typename E::const_iterator2 it2e (e ().begin2 ());
  155. typename E::const_iterator2 it2e_end (e ().end2 ());
  156. while (it2 != it2_end && it2e != it2e_end) {
  157. difference_type compare = it2.index2 () - it2e.index2 ();
  158. if (compare == 0) {
  159. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  160. typename M::iterator1 it1 (it2.begin ());
  161. typename M::iterator1 it1_end (it2.end ());
  162. typename E::const_iterator1 it1e (it2e.begin ());
  163. typename E::const_iterator1 it1e_end (it2e.end ());
  164. #else
  165. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  166. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  167. typename E::const_iterator1 it1e (begin (it2e, iterator2_tag ()));
  168. typename E::const_iterator1 it1e_end (end (it2e, iterator2_tag ()));
  169. #endif
  170. if (it1 != it1_end && it1e != it1e_end) {
  171. size_type it1_index = it1.index1 (), it1e_index = it1e.index1 ();
  172. for (;;) {
  173. difference_type compare2 = it1_index - it1e_index;
  174. if (compare2 == 0) {
  175. ++ it1, ++ it1e;
  176. if (it1 != it1_end && it1e != it1e_end) {
  177. it1_index = it1.index1 ();
  178. it1e_index = it1e.index1 ();
  179. } else
  180. break;
  181. } else if (compare2 < 0) {
  182. increment (it1, it1_end, - compare2);
  183. if (it1 != it1_end)
  184. it1_index = it1.index1 ();
  185. else
  186. break;
  187. } else if (compare2 > 0) {
  188. if (conformant_restrict_type::other (it1e.index1 (), it1e.index2 ()))
  189. if (static_cast<value_type>(*it1e) != value_type/*zero*/())
  190. index.push_back (std::pair<size_type, size_type> (it1e.index1 (), it1e.index2 ()));
  191. ++ it1e;
  192. if (it1e != it1e_end)
  193. it1e_index = it1e.index1 ();
  194. else
  195. break;
  196. }
  197. }
  198. }
  199. while (it1e != it1e_end) {
  200. if (conformant_restrict_type::other (it1e.index1 (), it1e.index2 ()))
  201. if (static_cast<value_type>(*it1e) != value_type/*zero*/())
  202. index.push_back (std::pair<size_type, size_type> (it1e.index1 (), it1e.index2 ()));
  203. ++ it1e;
  204. }
  205. ++ it2, ++ it2e;
  206. } else if (compare < 0) {
  207. increment (it2, it2_end, - compare);
  208. } else if (compare > 0) {
  209. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  210. typename E::const_iterator1 it1e (it2e.begin ());
  211. typename E::const_iterator1 it1e_end (it2e.end ());
  212. #else
  213. typename E::const_iterator1 it1e (begin (it2e, iterator2_tag ()));
  214. typename E::const_iterator1 it1e_end (end (it2e, iterator2_tag ()));
  215. #endif
  216. while (it1e != it1e_end) {
  217. if (conformant_restrict_type::other (it1e.index1 (), it1e.index2 ()))
  218. if (static_cast<value_type>(*it1e) != value_type/*zero*/())
  219. index.push_back (std::pair<size_type, size_type> (it1e.index1 (), it1e.index2 ()));
  220. ++ it1e;
  221. }
  222. ++ it2e;
  223. }
  224. }
  225. while (it2e != it2e_end) {
  226. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  227. typename E::const_iterator1 it1e (it2e.begin ());
  228. typename E::const_iterator1 it1e_end (it2e.end ());
  229. #else
  230. typename E::const_iterator1 it1e (begin (it2e, iterator2_tag ()));
  231. typename E::const_iterator1 it1e_end (end (it2e, iterator2_tag ()));
  232. #endif
  233. while (it1e != it1e_end) {
  234. if (conformant_restrict_type::other (it1e.index1 (), it1e.index2 ()))
  235. if (static_cast<value_type>(*it1e) != value_type/*zero*/())
  236. index.push_back (std::pair<size_type, size_type> (it1e.index1 (), it1e.index2 ()));
  237. ++ it1e;
  238. }
  239. ++ it2e;
  240. }
  241. // ISSUE proxies require insert_element
  242. for (size_type k = 0; k < index.size (); ++ k)
  243. m (index [k].first, index [k].second) = value_type/*zero*/();
  244. }
  245. }//namespace detail
  246. // Explicitly iterating row major
  247. template<template <class T1, class T2> class F, class M, class T>
  248. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  249. void iterating_matrix_assign_scalar (M &m, const T &t, row_major_tag) {
  250. typedef F<typename M::iterator2::reference, T> functor_type;
  251. typedef typename M::difference_type difference_type;
  252. difference_type size1 (m.size1 ());
  253. difference_type size2 (m.size2 ());
  254. typename M::iterator1 it1 (m.begin1 ());
  255. BOOST_UBLAS_CHECK (size2 == 0 || m.end1 () - it1 == size1, bad_size ());
  256. while (-- size1 >= 0) {
  257. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  258. typename M::iterator2 it2 (it1.begin ());
  259. #else
  260. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  261. #endif
  262. BOOST_UBLAS_CHECK (it1.end () - it2 == size2, bad_size ());
  263. difference_type temp_size2 (size2);
  264. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  265. while (-- temp_size2 >= 0)
  266. functor_type::apply (*it2, t), ++ it2;
  267. #else
  268. DD (temp_size2, 4, r, (functor_type::apply (*it2, t), ++ it2));
  269. #endif
  270. ++ it1;
  271. }
  272. }
  273. // Explicitly iterating column major
  274. template<template <class T1, class T2> class F, class M, class T>
  275. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  276. void iterating_matrix_assign_scalar (M &m, const T &t, column_major_tag) {
  277. typedef F<typename M::iterator1::reference, T> functor_type;
  278. typedef typename M::difference_type difference_type;
  279. difference_type size2 (m.size2 ());
  280. difference_type size1 (m.size1 ());
  281. typename M::iterator2 it2 (m.begin2 ());
  282. BOOST_UBLAS_CHECK (size1 == 0 || m.end2 () - it2 == size2, bad_size ());
  283. while (-- size2 >= 0) {
  284. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  285. typename M::iterator1 it1 (it2.begin ());
  286. #else
  287. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  288. #endif
  289. BOOST_UBLAS_CHECK (it2.end () - it1 == size1, bad_size ());
  290. difference_type temp_size1 (size1);
  291. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  292. while (-- temp_size1 >= 0)
  293. functor_type::apply (*it1, t), ++ it1;
  294. #else
  295. DD (temp_size1, 4, r, (functor_type::apply (*it1, t), ++ it1));
  296. #endif
  297. ++ it2;
  298. }
  299. }
  300. // Explicitly indexing row major
  301. template<template <class T1, class T2> class F, class M, class T>
  302. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  303. void indexing_matrix_assign_scalar (M &m, const T &t, row_major_tag) {
  304. typedef F<typename M::reference, T> functor_type;
  305. typedef typename M::size_type size_type;
  306. size_type size1 (m.size1 ());
  307. size_type size2 (m.size2 ());
  308. for (size_type i = 0; i < size1; ++ i) {
  309. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  310. for (size_type j = 0; j < size2; ++ j)
  311. functor_type::apply (m (i, j), t);
  312. #else
  313. size_type j (0);
  314. DD (size2, 4, r, (functor_type::apply (m (i, j), t), ++ j));
  315. #endif
  316. }
  317. }
  318. // Explicitly indexing column major
  319. template<template <class T1, class T2> class F, class M, class T>
  320. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  321. void indexing_matrix_assign_scalar (M &m, const T &t, column_major_tag) {
  322. typedef F<typename M::reference, T> functor_type;
  323. typedef typename M::size_type size_type;
  324. size_type size2 (m.size2 ());
  325. size_type size1 (m.size1 ());
  326. for (size_type j = 0; j < size2; ++ j) {
  327. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  328. for (size_type i = 0; i < size1; ++ i)
  329. functor_type::apply (m (i, j), t);
  330. #else
  331. size_type i (0);
  332. DD (size1, 4, r, (functor_type::apply (m (i, j), t), ++ i));
  333. #endif
  334. }
  335. }
  336. // Dense (proxy) case
  337. template<template <class T1, class T2> class F, class M, class T, class C>
  338. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  339. void matrix_assign_scalar (M &m, const T &t, dense_proxy_tag, C) {
  340. typedef C orientation_category;
  341. #ifdef BOOST_UBLAS_USE_INDEXING
  342. indexing_matrix_assign_scalar<F> (m, t, orientation_category ());
  343. #elif BOOST_UBLAS_USE_ITERATING
  344. iterating_matrix_assign_scalar<F> (m, t, orientation_category ());
  345. #else
  346. typedef typename M::size_type size_type;
  347. size_type size1 (m.size1 ());
  348. size_type size2 (m.size2 ());
  349. if (size1 >= BOOST_UBLAS_ITERATOR_THRESHOLD &&
  350. size2 >= BOOST_UBLAS_ITERATOR_THRESHOLD)
  351. iterating_matrix_assign_scalar<F> (m, t, orientation_category ());
  352. else
  353. indexing_matrix_assign_scalar<F> (m, t, orientation_category ());
  354. #endif
  355. }
  356. // Packed (proxy) row major case
  357. template<template <class T1, class T2> class F, class M, class T>
  358. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  359. void matrix_assign_scalar (M &m, const T &t, packed_proxy_tag, row_major_tag) {
  360. typedef F<typename M::iterator2::reference, T> functor_type;
  361. typedef typename M::difference_type difference_type;
  362. typename M::iterator1 it1 (m.begin1 ());
  363. difference_type size1 (m.end1 () - it1);
  364. while (-- size1 >= 0) {
  365. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  366. typename M::iterator2 it2 (it1.begin ());
  367. difference_type size2 (it1.end () - it2);
  368. #else
  369. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  370. difference_type size2 (end (it1, iterator1_tag ()) - it2);
  371. #endif
  372. while (-- size2 >= 0)
  373. functor_type::apply (*it2, t), ++ it2;
  374. ++ it1;
  375. }
  376. }
  377. // Packed (proxy) column major case
  378. template<template <class T1, class T2> class F, class M, class T>
  379. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  380. void matrix_assign_scalar (M &m, const T &t, packed_proxy_tag, column_major_tag) {
  381. typedef F<typename M::iterator1::reference, T> functor_type;
  382. typedef typename M::difference_type difference_type;
  383. typename M::iterator2 it2 (m.begin2 ());
  384. difference_type size2 (m.end2 () - it2);
  385. while (-- size2 >= 0) {
  386. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  387. typename M::iterator1 it1 (it2.begin ());
  388. difference_type size1 (it2.end () - it1);
  389. #else
  390. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  391. difference_type size1 (end (it2, iterator2_tag ()) - it1);
  392. #endif
  393. while (-- size1 >= 0)
  394. functor_type::apply (*it1, t), ++ it1;
  395. ++ it2;
  396. }
  397. }
  398. // Sparse (proxy) row major case
  399. template<template <class T1, class T2> class F, class M, class T>
  400. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  401. void matrix_assign_scalar (M &m, const T &t, sparse_proxy_tag, row_major_tag) {
  402. typedef F<typename M::iterator2::reference, T> functor_type;
  403. typename M::iterator1 it1 (m.begin1 ());
  404. typename M::iterator1 it1_end (m.end1 ());
  405. while (it1 != it1_end) {
  406. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  407. typename M::iterator2 it2 (it1.begin ());
  408. typename M::iterator2 it2_end (it1.end ());
  409. #else
  410. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  411. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  412. #endif
  413. while (it2 != it2_end)
  414. functor_type::apply (*it2, t), ++ it2;
  415. ++ it1;
  416. }
  417. }
  418. // Sparse (proxy) column major case
  419. template<template <class T1, class T2> class F, class M, class T>
  420. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  421. void matrix_assign_scalar (M &m, const T &t, sparse_proxy_tag, column_major_tag) {
  422. typedef F<typename M::iterator1::reference, T> functor_type;
  423. typename M::iterator2 it2 (m.begin2 ());
  424. typename M::iterator2 it2_end (m.end2 ());
  425. while (it2 != it2_end) {
  426. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  427. typename M::iterator1 it1 (it2.begin ());
  428. typename M::iterator1 it1_end (it2.end ());
  429. #else
  430. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  431. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  432. #endif
  433. while (it1 != it1_end)
  434. functor_type::apply (*it1, t), ++ it1;
  435. ++ it2;
  436. }
  437. }
  438. // Dispatcher
  439. template<template <class T1, class T2> class F, class M, class T>
  440. BOOST_UBLAS_INLINE
  441. void matrix_assign_scalar (M &m, const T &t) {
  442. typedef typename M::storage_category storage_category;
  443. typedef typename M::orientation_category orientation_category;
  444. matrix_assign_scalar<F> (m, t, storage_category (), orientation_category ());
  445. }
  446. template<class SC, bool COMPUTED, class RI1, class RI2>
  447. struct matrix_assign_traits {
  448. typedef SC storage_category;
  449. };
  450. template<bool COMPUTED>
  451. struct matrix_assign_traits<dense_tag, COMPUTED, packed_random_access_iterator_tag, packed_random_access_iterator_tag> {
  452. typedef packed_tag storage_category;
  453. };
  454. template<>
  455. struct matrix_assign_traits<dense_tag, false, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  456. typedef sparse_tag storage_category;
  457. };
  458. template<>
  459. struct matrix_assign_traits<dense_tag, true, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  460. typedef sparse_proxy_tag storage_category;
  461. };
  462. template<bool COMPUTED>
  463. struct matrix_assign_traits<dense_proxy_tag, COMPUTED, packed_random_access_iterator_tag, packed_random_access_iterator_tag> {
  464. typedef packed_proxy_tag storage_category;
  465. };
  466. template<bool COMPUTED>
  467. struct matrix_assign_traits<dense_proxy_tag, COMPUTED, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  468. typedef sparse_proxy_tag storage_category;
  469. };
  470. template<>
  471. struct matrix_assign_traits<packed_tag, false, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  472. typedef sparse_tag storage_category;
  473. };
  474. template<>
  475. struct matrix_assign_traits<packed_tag, true, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  476. typedef sparse_proxy_tag storage_category;
  477. };
  478. template<bool COMPUTED>
  479. struct matrix_assign_traits<packed_proxy_tag, COMPUTED, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  480. typedef sparse_proxy_tag storage_category;
  481. };
  482. template<>
  483. struct matrix_assign_traits<sparse_tag, true, dense_random_access_iterator_tag, dense_random_access_iterator_tag> {
  484. typedef sparse_proxy_tag storage_category;
  485. };
  486. template<>
  487. struct matrix_assign_traits<sparse_tag, true, packed_random_access_iterator_tag, packed_random_access_iterator_tag> {
  488. typedef sparse_proxy_tag storage_category;
  489. };
  490. template<>
  491. struct matrix_assign_traits<sparse_tag, true, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  492. typedef sparse_proxy_tag storage_category;
  493. };
  494. // Explicitly iterating row major
  495. template<template <class T1, class T2> class F, class M, class E>
  496. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  497. void iterating_matrix_assign (M &m, const matrix_expression<E> &e, row_major_tag) {
  498. typedef F<typename M::iterator2::reference, typename E::value_type> functor_type;
  499. typedef typename M::difference_type difference_type;
  500. difference_type size1 (BOOST_UBLAS_SAME (m.size1 (), e ().size1 ()));
  501. difference_type size2 (BOOST_UBLAS_SAME (m.size2 (), e ().size2 ()));
  502. typename M::iterator1 it1 (m.begin1 ());
  503. BOOST_UBLAS_CHECK (size2 == 0 || m.end1 () - it1 == size1, bad_size ());
  504. typename E::const_iterator1 it1e (e ().begin1 ());
  505. BOOST_UBLAS_CHECK (size2 == 0 || e ().end1 () - it1e == size1, bad_size ());
  506. while (-- size1 >= 0) {
  507. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  508. typename M::iterator2 it2 (it1.begin ());
  509. typename E::const_iterator2 it2e (it1e.begin ());
  510. #else
  511. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  512. typename E::const_iterator2 it2e (begin (it1e, iterator1_tag ()));
  513. #endif
  514. BOOST_UBLAS_CHECK (it1.end () - it2 == size2, bad_size ());
  515. BOOST_UBLAS_CHECK (it1e.end () - it2e == size2, bad_size ());
  516. difference_type temp_size2 (size2);
  517. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  518. while (-- temp_size2 >= 0)
  519. functor_type::apply (*it2, *it2e), ++ it2, ++ it2e;
  520. #else
  521. DD (temp_size2, 2, r, (functor_type::apply (*it2, *it2e), ++ it2, ++ it2e));
  522. #endif
  523. ++ it1, ++ it1e;
  524. }
  525. }
  526. // Explicitly iterating column major
  527. template<template <class T1, class T2> class F, class M, class E>
  528. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  529. void iterating_matrix_assign (M &m, const matrix_expression<E> &e, column_major_tag) {
  530. typedef F<typename M::iterator1::reference, typename E::value_type> functor_type;
  531. typedef typename M::difference_type difference_type;
  532. difference_type size2 (BOOST_UBLAS_SAME (m.size2 (), e ().size2 ()));
  533. difference_type size1 (BOOST_UBLAS_SAME (m.size1 (), e ().size1 ()));
  534. typename M::iterator2 it2 (m.begin2 ());
  535. BOOST_UBLAS_CHECK (size1 == 0 || m.end2 () - it2 == size2, bad_size ());
  536. typename E::const_iterator2 it2e (e ().begin2 ());
  537. BOOST_UBLAS_CHECK (size1 == 0 || e ().end2 () - it2e == size2, bad_size ());
  538. while (-- size2 >= 0) {
  539. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  540. typename M::iterator1 it1 (it2.begin ());
  541. typename E::const_iterator1 it1e (it2e.begin ());
  542. #else
  543. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  544. typename E::const_iterator1 it1e (begin (it2e, iterator2_tag ()));
  545. #endif
  546. BOOST_UBLAS_CHECK (it2.end () - it1 == size1, bad_size ());
  547. BOOST_UBLAS_CHECK (it2e.end () - it1e == size1, bad_size ());
  548. difference_type temp_size1 (size1);
  549. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  550. while (-- temp_size1 >= 0)
  551. functor_type::apply (*it1, *it1e), ++ it1, ++ it1e;
  552. #else
  553. DD (temp_size1, 2, r, (functor_type::apply (*it1, *it1e), ++ it1, ++ it1e));
  554. #endif
  555. ++ it2, ++ it2e;
  556. }
  557. }
  558. // Explicitly indexing row major
  559. template<template <class T1, class T2> class F, class M, class E>
  560. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  561. void indexing_matrix_assign (M &m, const matrix_expression<E> &e, row_major_tag) {
  562. typedef F<typename M::reference, typename E::value_type> functor_type;
  563. typedef typename M::size_type size_type;
  564. size_type size1 (BOOST_UBLAS_SAME (m.size1 (), e ().size1 ()));
  565. size_type size2 (BOOST_UBLAS_SAME (m.size2 (), e ().size2 ()));
  566. for (size_type i = 0; i < size1; ++ i) {
  567. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  568. for (size_type j = 0; j < size2; ++ j)
  569. functor_type::apply (m (i, j), e () (i, j));
  570. #else
  571. size_type j (0);
  572. DD (size2, 2, r, (functor_type::apply (m (i, j), e () (i, j)), ++ j));
  573. #endif
  574. }
  575. }
  576. // Explicitly indexing column major
  577. template<template <class T1, class T2> class F, class M, class E>
  578. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  579. void indexing_matrix_assign (M &m, const matrix_expression<E> &e, column_major_tag) {
  580. typedef F<typename M::reference, typename E::value_type> functor_type;
  581. typedef typename M::size_type size_type;
  582. size_type size2 (BOOST_UBLAS_SAME (m.size2 (), e ().size2 ()));
  583. size_type size1 (BOOST_UBLAS_SAME (m.size1 (), e ().size1 ()));
  584. for (size_type j = 0; j < size2; ++ j) {
  585. #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
  586. for (size_type i = 0; i < size1; ++ i)
  587. functor_type::apply (m (i, j), e () (i, j));
  588. #else
  589. size_type i (0);
  590. DD (size1, 2, r, (functor_type::apply (m (i, j), e () (i, j)), ++ i));
  591. #endif
  592. }
  593. }
  594. // Dense (proxy) case
  595. template<template <class T1, class T2> class F, class R, class M, class E, class C>
  596. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  597. void matrix_assign (M &m, const matrix_expression<E> &e, dense_proxy_tag, C) {
  598. // R unnecessary, make_conformant not required
  599. typedef C orientation_category;
  600. #ifdef BOOST_UBLAS_USE_INDEXING
  601. indexing_matrix_assign<F> (m, e, orientation_category ());
  602. #elif BOOST_UBLAS_USE_ITERATING
  603. iterating_matrix_assign<F> (m, e, orientation_category ());
  604. #else
  605. typedef typename M::difference_type difference_type;
  606. size_type size1 (BOOST_UBLAS_SAME (m.size1 (), e ().size1 ()));
  607. size_type size2 (BOOST_UBLAS_SAME (m.size2 (), e ().size2 ()));
  608. if (size1 >= BOOST_UBLAS_ITERATOR_THRESHOLD &&
  609. size2 >= BOOST_UBLAS_ITERATOR_THRESHOLD)
  610. iterating_matrix_assign<F> (m, e, orientation_category ());
  611. else
  612. indexing_matrix_assign<F> (m, e, orientation_category ());
  613. #endif
  614. }
  615. // Packed (proxy) row major case
  616. template<template <class T1, class T2> class F, class R, class M, class E>
  617. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  618. void matrix_assign (M &m, const matrix_expression<E> &e, packed_proxy_tag, row_major_tag) {
  619. typedef typename matrix_traits<E>::value_type expr_value_type;
  620. typedef F<typename M::iterator2::reference, expr_value_type> functor_type;
  621. // R unnecessary, make_conformant not required
  622. typedef typename M::difference_type difference_type;
  623. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  624. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  625. #if BOOST_UBLAS_TYPE_CHECK
  626. typedef typename M::value_type value_type;
  627. matrix<value_type, row_major> cm (m.size1 (), m.size2 ());
  628. indexing_matrix_assign<scalar_assign> (cm, m, row_major_tag ());
  629. indexing_matrix_assign<F> (cm, e, row_major_tag ());
  630. #endif
  631. typename M::iterator1 it1 (m.begin1 ());
  632. typename M::iterator1 it1_end (m.end1 ());
  633. typename E::const_iterator1 it1e (e ().begin1 ());
  634. typename E::const_iterator1 it1e_end (e ().end1 ());
  635. difference_type it1_size (it1_end - it1);
  636. difference_type it1e_size (it1e_end - it1e);
  637. difference_type diff1 (0);
  638. if (it1_size > 0 && it1e_size > 0)
  639. diff1 = it1.index1 () - it1e.index1 ();
  640. if (diff1 != 0) {
  641. difference_type size1 = (std::min) (diff1, it1e_size);
  642. if (size1 > 0) {
  643. it1e += size1;
  644. it1e_size -= size1;
  645. diff1 -= size1;
  646. }
  647. size1 = (std::min) (- diff1, it1_size);
  648. if (size1 > 0) {
  649. it1_size -= size1;
  650. //Disabled warning C4127 because the conditional expression is constant
  651. #ifdef _MSC_VER
  652. #pragma warning(push)
  653. #pragma warning(disable: 4127)
  654. #endif
  655. if (!functor_type::computed) {
  656. #ifdef _MSC_VER
  657. #pragma warning(pop)
  658. #endif
  659. while (-- size1 >= 0) { // zeroing
  660. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  661. typename M::iterator2 it2 (it1.begin ());
  662. typename M::iterator2 it2_end (it1.end ());
  663. #else
  664. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  665. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  666. #endif
  667. difference_type size2 (it2_end - it2);
  668. while (-- size2 >= 0)
  669. functor_type::apply (*it2, expr_value_type/*zero*/()), ++ it2;
  670. ++ it1;
  671. }
  672. } else {
  673. it1 += size1;
  674. }
  675. diff1 += size1;
  676. }
  677. }
  678. difference_type size1 ((std::min) (it1_size, it1e_size));
  679. it1_size -= size1;
  680. it1e_size -= size1;
  681. while (-- size1 >= 0) {
  682. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  683. typename M::iterator2 it2 (it1.begin ());
  684. typename M::iterator2 it2_end (it1.end ());
  685. typename E::const_iterator2 it2e (it1e.begin ());
  686. typename E::const_iterator2 it2e_end (it1e.end ());
  687. #else
  688. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  689. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  690. typename E::const_iterator2 it2e (begin (it1e, iterator1_tag ()));
  691. typename E::const_iterator2 it2e_end (end (it1e, iterator1_tag ()));
  692. #endif
  693. difference_type it2_size (it2_end - it2);
  694. difference_type it2e_size (it2e_end - it2e);
  695. difference_type diff2 (0);
  696. if (it2_size > 0 && it2e_size > 0) {
  697. diff2 = it2.index2 () - it2e.index2 ();
  698. difference_type size2 = (std::min) (diff2, it2e_size);
  699. if (size2 > 0) {
  700. it2e += size2;
  701. it2e_size -= size2;
  702. diff2 -= size2;
  703. }
  704. size2 = (std::min) (- diff2, it2_size);
  705. if (size2 > 0) {
  706. it2_size -= size2;
  707. //Disabled warning C4127 because the conditional expression is constant
  708. #ifdef _MSC_VER
  709. #pragma warning(push)
  710. #pragma warning(disable: 4127)
  711. #endif
  712. if (!functor_type::computed) {
  713. #ifdef _MSC_VER
  714. #pragma warning(pop)
  715. #endif
  716. while (-- size2 >= 0) // zeroing
  717. functor_type::apply (*it2, expr_value_type/*zero*/()), ++ it2;
  718. } else {
  719. it2 += size2;
  720. }
  721. diff2 += size2;
  722. }
  723. }
  724. difference_type size2 ((std::min) (it2_size, it2e_size));
  725. it2_size -= size2;
  726. it2e_size -= size2;
  727. while (-- size2 >= 0)
  728. functor_type::apply (*it2, *it2e), ++ it2, ++ it2e;
  729. size2 = it2_size;
  730. //Disabled warning C4127 because the conditional expression is constant
  731. #ifdef _MSC_VER
  732. #pragma warning(push)
  733. #pragma warning(disable: 4127)
  734. #endif
  735. if (!functor_type::computed) {
  736. #ifdef _MSC_VER
  737. #pragma warning(pop)
  738. #endif
  739. while (-- size2 >= 0) // zeroing
  740. functor_type::apply (*it2, expr_value_type/*zero*/()), ++ it2;
  741. } else {
  742. it2 += size2;
  743. }
  744. ++ it1, ++ it1e;
  745. }
  746. size1 = it1_size;
  747. //Disabled warning C4127 because the conditional expression is constant
  748. #ifdef _MSC_VER
  749. #pragma warning(push)
  750. #pragma warning(disable: 4127)
  751. #endif
  752. if (!functor_type::computed) {
  753. #ifdef _MSC_VER
  754. #pragma warning(pop)
  755. #endif
  756. while (-- size1 >= 0) { // zeroing
  757. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  758. typename M::iterator2 it2 (it1.begin ());
  759. typename M::iterator2 it2_end (it1.end ());
  760. #else
  761. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  762. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  763. #endif
  764. difference_type size2 (it2_end - it2);
  765. while (-- size2 >= 0)
  766. functor_type::apply (*it2, expr_value_type/*zero*/()), ++ it2;
  767. ++ it1;
  768. }
  769. } else {
  770. it1 += size1;
  771. }
  772. #if BOOST_UBLAS_TYPE_CHECK
  773. if (! disable_type_check<bool>::value)
  774. BOOST_UBLAS_CHECK (detail::expression_type_check (m, cm), external_logic ());
  775. #endif
  776. }
  777. // Packed (proxy) column major case
  778. template<template <class T1, class T2> class F, class R, class M, class E>
  779. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  780. void matrix_assign (M &m, const matrix_expression<E> &e, packed_proxy_tag, column_major_tag) {
  781. typedef typename matrix_traits<E>::value_type expr_value_type;
  782. typedef F<typename M::iterator1::reference, expr_value_type> functor_type;
  783. // R unnecessary, make_conformant not required
  784. typedef typename M::difference_type difference_type;
  785. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  786. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  787. #if BOOST_UBLAS_TYPE_CHECK
  788. typedef typename M::value_type value_type;
  789. matrix<value_type, column_major> cm (m.size1 (), m.size2 ());
  790. indexing_matrix_assign<scalar_assign> (cm, m, column_major_tag ());
  791. indexing_matrix_assign<F> (cm, e, column_major_tag ());
  792. #endif
  793. typename M::iterator2 it2 (m.begin2 ());
  794. typename M::iterator2 it2_end (m.end2 ());
  795. typename E::const_iterator2 it2e (e ().begin2 ());
  796. typename E::const_iterator2 it2e_end (e ().end2 ());
  797. difference_type it2_size (it2_end - it2);
  798. difference_type it2e_size (it2e_end - it2e);
  799. difference_type diff2 (0);
  800. if (it2_size > 0 && it2e_size > 0)
  801. diff2 = it2.index2 () - it2e.index2 ();
  802. if (diff2 != 0) {
  803. difference_type size2 = (std::min) (diff2, it2e_size);
  804. if (size2 > 0) {
  805. it2e += size2;
  806. it2e_size -= size2;
  807. diff2 -= size2;
  808. }
  809. size2 = (std::min) (- diff2, it2_size);
  810. if (size2 > 0) {
  811. it2_size -= size2;
  812. //Disabled warning C4127 because the conditional expression is constant
  813. #ifdef _MSC_VER
  814. #pragma warning(push)
  815. #pragma warning(disable: 4127)
  816. #endif
  817. if (!functor_type::computed) {
  818. #ifdef _MSC_VER
  819. #pragma warning(pop)
  820. #endif
  821. while (-- size2 >= 0) { // zeroing
  822. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  823. typename M::iterator1 it1 (it2.begin ());
  824. typename M::iterator1 it1_end (it2.end ());
  825. #else
  826. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  827. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  828. #endif
  829. difference_type size1 (it1_end - it1);
  830. while (-- size1 >= 0)
  831. functor_type::apply (*it1, expr_value_type/*zero*/()), ++ it1;
  832. ++ it2;
  833. }
  834. } else {
  835. it2 += size2;
  836. }
  837. diff2 += size2;
  838. }
  839. }
  840. difference_type size2 ((std::min) (it2_size, it2e_size));
  841. it2_size -= size2;
  842. it2e_size -= size2;
  843. while (-- size2 >= 0) {
  844. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  845. typename M::iterator1 it1 (it2.begin ());
  846. typename M::iterator1 it1_end (it2.end ());
  847. typename E::const_iterator1 it1e (it2e.begin ());
  848. typename E::const_iterator1 it1e_end (it2e.end ());
  849. #else
  850. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  851. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  852. typename E::const_iterator1 it1e (begin (it2e, iterator2_tag ()));
  853. typename E::const_iterator1 it1e_end (end (it2e, iterator2_tag ()));
  854. #endif
  855. difference_type it1_size (it1_end - it1);
  856. difference_type it1e_size (it1e_end - it1e);
  857. difference_type diff1 (0);
  858. if (it1_size > 0 && it1e_size > 0) {
  859. diff1 = it1.index1 () - it1e.index1 ();
  860. difference_type size1 = (std::min) (diff1, it1e_size);
  861. if (size1 > 0) {
  862. it1e += size1;
  863. it1e_size -= size1;
  864. diff1 -= size1;
  865. }
  866. size1 = (std::min) (- diff1, it1_size);
  867. if (size1 > 0) {
  868. it1_size -= size1;
  869. //Disabled warning C4127 because the conditional expression is constant
  870. #ifdef _MSC_VER
  871. #pragma warning(push)
  872. #pragma warning(disable: 4127)
  873. #endif
  874. if (!functor_type::computed) {
  875. #ifdef _MSC_VER
  876. #pragma warning(pop)
  877. #endif
  878. while (-- size1 >= 0) // zeroing
  879. functor_type::apply (*it1, expr_value_type/*zero*/()), ++ it1;
  880. } else {
  881. it1 += size1;
  882. }
  883. diff1 += size1;
  884. }
  885. }
  886. difference_type size1 ((std::min) (it1_size, it1e_size));
  887. it1_size -= size1;
  888. it1e_size -= size1;
  889. while (-- size1 >= 0)
  890. functor_type::apply (*it1, *it1e), ++ it1, ++ it1e;
  891. size1 = it1_size;
  892. //Disabled warning C4127 because the conditional expression is constant
  893. #ifdef _MSC_VER
  894. #pragma warning(push)
  895. #pragma warning(disable: 4127)
  896. #endif
  897. if (!functor_type::computed) {
  898. #ifdef _MSC_VER
  899. #pragma warning(pop)
  900. #endif
  901. while (-- size1 >= 0) // zeroing
  902. functor_type::apply (*it1, expr_value_type/*zero*/()), ++ it1;
  903. } else {
  904. it1 += size1;
  905. }
  906. ++ it2, ++ it2e;
  907. }
  908. size2 = it2_size;
  909. //Disabled warning C4127 because the conditional expression is constant
  910. #ifdef _MSC_VER
  911. #pragma warning(push)
  912. #pragma warning(disable: 4127)
  913. #endif
  914. if (!functor_type::computed) {
  915. #ifdef _MSC_VER
  916. #pragma warning(pop)
  917. #endif
  918. while (-- size2 >= 0) { // zeroing
  919. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  920. typename M::iterator1 it1 (it2.begin ());
  921. typename M::iterator1 it1_end (it2.end ());
  922. #else
  923. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  924. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  925. #endif
  926. difference_type size1 (it1_end - it1);
  927. while (-- size1 >= 0)
  928. functor_type::apply (*it1, expr_value_type/*zero*/()), ++ it1;
  929. ++ it2;
  930. }
  931. } else {
  932. it2 += size2;
  933. }
  934. #if BOOST_UBLAS_TYPE_CHECK
  935. if (! disable_type_check<bool>::value)
  936. BOOST_UBLAS_CHECK (detail::expression_type_check (m, cm), external_logic ());
  937. #endif
  938. }
  939. // Sparse row major case
  940. template<template <class T1, class T2> class F, class R, class M, class E>
  941. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  942. void matrix_assign (M &m, const matrix_expression<E> &e, sparse_tag, row_major_tag) {
  943. typedef F<typename M::iterator2::reference, typename E::value_type> functor_type;
  944. // R unnecessary, make_conformant not required
  945. //Disabled warning C4127 because the conditional expression is constant
  946. #ifdef _MSC_VER
  947. #pragma warning(push)
  948. #pragma warning(disable: 4127)
  949. #endif
  950. BOOST_STATIC_ASSERT ((!functor_type::computed));
  951. #ifdef _MSC_VER
  952. #pragma warning(pop)
  953. #endif
  954. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  955. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  956. typedef typename M::value_type value_type;
  957. // Sparse type has no numeric constraints to check
  958. m.clear ();
  959. typename E::const_iterator1 it1e (e ().begin1 ());
  960. typename E::const_iterator1 it1e_end (e ().end1 ());
  961. while (it1e != it1e_end) {
  962. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  963. typename E::const_iterator2 it2e (it1e.begin ());
  964. typename E::const_iterator2 it2e_end (it1e.end ());
  965. #else
  966. typename E::const_iterator2 it2e (begin (it1e, iterator1_tag ()));
  967. typename E::const_iterator2 it2e_end (end (it1e, iterator1_tag ()));
  968. #endif
  969. while (it2e != it2e_end) {
  970. value_type t (*it2e);
  971. if (t != value_type/*zero*/())
  972. m.insert_element (it2e.index1 (), it2e.index2 (), t);
  973. ++ it2e;
  974. }
  975. ++ it1e;
  976. }
  977. }
  978. // Sparse column major case
  979. template<template <class T1, class T2> class F, class R, class M, class E>
  980. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  981. void matrix_assign (M &m, const matrix_expression<E> &e, sparse_tag, column_major_tag) {
  982. typedef F<typename M::iterator1::reference, typename E::value_type> functor_type;
  983. // R unnecessary, make_conformant not required
  984. //Disabled warning C4127 because the conditional expression is constant
  985. #ifdef _MSC_VER
  986. #pragma warning(push)
  987. #pragma warning(disable: 4127)
  988. #endif
  989. BOOST_STATIC_ASSERT ((!functor_type::computed));
  990. #ifdef _MSC_VER
  991. #pragma warning(pop)
  992. #endif
  993. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  994. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  995. typedef typename M::value_type value_type;
  996. // Sparse type has no numeric constraints to check
  997. m.clear ();
  998. typename E::const_iterator2 it2e (e ().begin2 ());
  999. typename E::const_iterator2 it2e_end (e ().end2 ());
  1000. while (it2e != it2e_end) {
  1001. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1002. typename E::const_iterator1 it1e (it2e.begin ());
  1003. typename E::const_iterator1 it1e_end (it2e.end ());
  1004. #else
  1005. typename E::const_iterator1 it1e (begin (it2e, iterator2_tag ()));
  1006. typename E::const_iterator1 it1e_end (end (it2e, iterator2_tag ()));
  1007. #endif
  1008. while (it1e != it1e_end) {
  1009. value_type t (*it1e);
  1010. if (t != value_type/*zero*/())
  1011. m.insert_element (it1e.index1 (), it1e.index2 (), t);
  1012. ++ it1e;
  1013. }
  1014. ++ it2e;
  1015. }
  1016. }
  1017. // Sparse proxy or functional row major case
  1018. template<template <class T1, class T2> class F, class R, class M, class E>
  1019. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1020. void matrix_assign (M &m, const matrix_expression<E> &e, sparse_proxy_tag, row_major_tag) {
  1021. typedef typename matrix_traits<E>::value_type expr_value_type;
  1022. typedef F<typename M::iterator2::reference, expr_value_type> functor_type;
  1023. typedef R conformant_restrict_type;
  1024. typedef typename M::size_type size_type;
  1025. typedef typename M::difference_type difference_type;
  1026. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  1027. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  1028. #if BOOST_UBLAS_TYPE_CHECK
  1029. typedef typename M::value_type value_type;
  1030. matrix<value_type, row_major> cm (m.size1 (), m.size2 ());
  1031. indexing_matrix_assign<scalar_assign> (cm, m, row_major_tag ());
  1032. indexing_matrix_assign<F> (cm, e, row_major_tag ());
  1033. #endif
  1034. detail::make_conformant (m, e, row_major_tag (), conformant_restrict_type ());
  1035. typename M::iterator1 it1 (m.begin1 ());
  1036. typename M::iterator1 it1_end (m.end1 ());
  1037. typename E::const_iterator1 it1e (e ().begin1 ());
  1038. typename E::const_iterator1 it1e_end (e ().end1 ());
  1039. while (it1 != it1_end && it1e != it1e_end) {
  1040. difference_type compare = it1.index1 () - it1e.index1 ();
  1041. if (compare == 0) {
  1042. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1043. typename M::iterator2 it2 (it1.begin ());
  1044. typename M::iterator2 it2_end (it1.end ());
  1045. typename E::const_iterator2 it2e (it1e.begin ());
  1046. typename E::const_iterator2 it2e_end (it1e.end ());
  1047. #else
  1048. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  1049. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  1050. typename E::const_iterator2 it2e (begin (it1e, iterator1_tag ()));
  1051. typename E::const_iterator2 it2e_end (end (it1e, iterator1_tag ()));
  1052. #endif
  1053. if (it2 != it2_end && it2e != it2e_end) {
  1054. size_type it2_index = it2.index2 (), it2e_index = it2e.index2 ();
  1055. for (;;) {
  1056. difference_type compare2 = it2_index - it2e_index;
  1057. if (compare2 == 0) {
  1058. functor_type::apply (*it2, *it2e);
  1059. ++ it2, ++ it2e;
  1060. if (it2 != it2_end && it2e != it2e_end) {
  1061. it2_index = it2.index2 ();
  1062. it2e_index = it2e.index2 ();
  1063. } else
  1064. break;
  1065. } else if (compare2 < 0) {
  1066. //Disabled warning C4127 because the conditional expression is constant
  1067. #ifdef _MSC_VER
  1068. #pragma warning(push)
  1069. #pragma warning(disable: 4127)
  1070. #endif
  1071. if (!functor_type::computed) {
  1072. #ifdef _MSC_VER
  1073. #pragma warning(pop)
  1074. #endif
  1075. functor_type::apply (*it2, expr_value_type/*zero*/());
  1076. ++ it2;
  1077. } else
  1078. increment (it2, it2_end, - compare2);
  1079. if (it2 != it2_end)
  1080. it2_index = it2.index2 ();
  1081. else
  1082. break;
  1083. } else if (compare2 > 0) {
  1084. increment (it2e, it2e_end, compare2);
  1085. if (it2e != it2e_end)
  1086. it2e_index = it2e.index2 ();
  1087. else
  1088. break;
  1089. }
  1090. }
  1091. }
  1092. //Disabled warning C4127 because the conditional expression is constant
  1093. #ifdef _MSC_VER
  1094. #pragma warning(push)
  1095. #pragma warning(disable: 4127)
  1096. #endif
  1097. if (!functor_type::computed) {
  1098. #ifdef _MSC_VER
  1099. #pragma warning(pop)
  1100. #endif
  1101. while (it2 != it2_end) { // zeroing
  1102. functor_type::apply (*it2, expr_value_type/*zero*/());
  1103. ++ it2;
  1104. }
  1105. } else {
  1106. it2 = it2_end;
  1107. }
  1108. ++ it1, ++ it1e;
  1109. } else if (compare < 0) {
  1110. //Disabled warning C4127 because the conditional expression is constant
  1111. #ifdef _MSC_VER
  1112. #pragma warning(push)
  1113. #pragma warning(disable: 4127)
  1114. #endif
  1115. if (!functor_type::computed) {
  1116. #ifdef _MSC_VER
  1117. #pragma warning(pop)
  1118. #endif
  1119. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1120. typename M::iterator2 it2 (it1.begin ());
  1121. typename M::iterator2 it2_end (it1.end ());
  1122. #else
  1123. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  1124. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  1125. #endif
  1126. while (it2 != it2_end) { // zeroing
  1127. functor_type::apply (*it2, expr_value_type/*zero*/());
  1128. ++ it2;
  1129. }
  1130. ++ it1;
  1131. } else {
  1132. increment (it1, it1_end, - compare);
  1133. }
  1134. } else if (compare > 0) {
  1135. increment (it1e, it1e_end, compare);
  1136. }
  1137. }
  1138. //Disabled warning C4127 because the conditional expression is constant
  1139. #ifdef _MSC_VER
  1140. #pragma warning(push)
  1141. #pragma warning(disable: 4127)
  1142. #endif
  1143. if (!functor_type::computed) {
  1144. #ifdef _MSC_VER
  1145. #pragma warning(pop)
  1146. #endif
  1147. while (it1 != it1_end) {
  1148. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1149. typename M::iterator2 it2 (it1.begin ());
  1150. typename M::iterator2 it2_end (it1.end ());
  1151. #else
  1152. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  1153. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  1154. #endif
  1155. while (it2 != it2_end) { // zeroing
  1156. functor_type::apply (*it2, expr_value_type/*zero*/());
  1157. ++ it2;
  1158. }
  1159. ++ it1;
  1160. }
  1161. } else {
  1162. it1 = it1_end;
  1163. }
  1164. #if BOOST_UBLAS_TYPE_CHECK
  1165. if (! disable_type_check<bool>::value)
  1166. BOOST_UBLAS_CHECK (detail::expression_type_check (m, cm), external_logic ());
  1167. #endif
  1168. }
  1169. // Sparse proxy or functional column major case
  1170. template<template <class T1, class T2> class F, class R, class M, class E>
  1171. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1172. void matrix_assign (M &m, const matrix_expression<E> &e, sparse_proxy_tag, column_major_tag) {
  1173. typedef typename matrix_traits<E>::value_type expr_value_type;
  1174. typedef F<typename M::iterator1::reference, expr_value_type> functor_type;
  1175. typedef R conformant_restrict_type;
  1176. typedef typename M::size_type size_type;
  1177. typedef typename M::difference_type difference_type;
  1178. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  1179. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  1180. #if BOOST_UBLAS_TYPE_CHECK
  1181. typedef typename M::value_type value_type;
  1182. matrix<value_type, column_major> cm (m.size1 (), m.size2 ());
  1183. indexing_matrix_assign<scalar_assign> (cm, m, column_major_tag ());
  1184. indexing_matrix_assign<F> (cm, e, column_major_tag ());
  1185. #endif
  1186. detail::make_conformant (m, e, column_major_tag (), conformant_restrict_type ());
  1187. typename M::iterator2 it2 (m.begin2 ());
  1188. typename M::iterator2 it2_end (m.end2 ());
  1189. typename E::const_iterator2 it2e (e ().begin2 ());
  1190. typename E::const_iterator2 it2e_end (e ().end2 ());
  1191. while (it2 != it2_end && it2e != it2e_end) {
  1192. difference_type compare = it2.index2 () - it2e.index2 ();
  1193. if (compare == 0) {
  1194. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1195. typename M::iterator1 it1 (it2.begin ());
  1196. typename M::iterator1 it1_end (it2.end ());
  1197. typename E::const_iterator1 it1e (it2e.begin ());
  1198. typename E::const_iterator1 it1e_end (it2e.end ());
  1199. #else
  1200. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  1201. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  1202. typename E::const_iterator1 it1e (begin (it2e, iterator2_tag ()));
  1203. typename E::const_iterator1 it1e_end (end (it2e, iterator2_tag ()));
  1204. #endif
  1205. if (it1 != it1_end && it1e != it1e_end) {
  1206. size_type it1_index = it1.index1 (), it1e_index = it1e.index1 ();
  1207. for (;;) {
  1208. difference_type compare2 = it1_index - it1e_index;
  1209. if (compare2 == 0) {
  1210. functor_type::apply (*it1, *it1e);
  1211. ++ it1, ++ it1e;
  1212. if (it1 != it1_end && it1e != it1e_end) {
  1213. it1_index = it1.index1 ();
  1214. it1e_index = it1e.index1 ();
  1215. } else
  1216. break;
  1217. } else if (compare2 < 0) {
  1218. //Disabled warning C4127 because the conditional expression is constant
  1219. #ifdef _MSC_VER
  1220. #pragma warning(push)
  1221. #pragma warning(disable: 4127)
  1222. #endif
  1223. if (!functor_type::computed) {
  1224. #ifdef _MSC_VER
  1225. #pragma warning(pop)
  1226. #endif
  1227. functor_type::apply (*it1, expr_value_type/*zero*/()); // zeroing
  1228. ++ it1;
  1229. } else
  1230. increment (it1, it1_end, - compare2);
  1231. if (it1 != it1_end)
  1232. it1_index = it1.index1 ();
  1233. else
  1234. break;
  1235. } else if (compare2 > 0) {
  1236. increment (it1e, it1e_end, compare2);
  1237. if (it1e != it1e_end)
  1238. it1e_index = it1e.index1 ();
  1239. else
  1240. break;
  1241. }
  1242. }
  1243. }
  1244. //Disabled warning C4127 because the conditional expression is constant
  1245. #ifdef _MSC_VER
  1246. #pragma warning(push)
  1247. #pragma warning(disable: 4127)
  1248. #endif
  1249. if (!functor_type::computed) {
  1250. #ifdef _MSC_VER
  1251. #pragma warning(pop)
  1252. #endif
  1253. while (it1 != it1_end) { // zeroing
  1254. functor_type::apply (*it1, expr_value_type/*zero*/());
  1255. ++ it1;
  1256. }
  1257. } else {
  1258. it1 = it1_end;
  1259. }
  1260. ++ it2, ++ it2e;
  1261. } else if (compare < 0) {
  1262. //Disabled warning C4127 because the conditional expression is constant
  1263. #ifdef _MSC_VER
  1264. #pragma warning(push)
  1265. #pragma warning(disable: 4127)
  1266. #endif
  1267. if (!functor_type::computed) {
  1268. #ifdef _MSC_VER
  1269. #pragma warning(pop)
  1270. #endif
  1271. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1272. typename M::iterator1 it1 (it2.begin ());
  1273. typename M::iterator1 it1_end (it2.end ());
  1274. #else
  1275. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  1276. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  1277. #endif
  1278. while (it1 != it1_end) { // zeroing
  1279. functor_type::apply (*it1, expr_value_type/*zero*/());
  1280. ++ it1;
  1281. }
  1282. ++ it2;
  1283. } else {
  1284. increment (it2, it2_end, - compare);
  1285. }
  1286. } else if (compare > 0) {
  1287. increment (it2e, it2e_end, compare);
  1288. }
  1289. }
  1290. //Disabled warning C4127 because the conditional expression is constant
  1291. #ifdef _MSC_VER
  1292. #pragma warning(push)
  1293. #pragma warning(disable: 4127)
  1294. #endif
  1295. if (!functor_type::computed) {
  1296. #ifdef _MSC_VER
  1297. #pragma warning(pop)
  1298. #endif
  1299. while (it2 != it2_end) {
  1300. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1301. typename M::iterator1 it1 (it2.begin ());
  1302. typename M::iterator1 it1_end (it2.end ());
  1303. #else
  1304. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  1305. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  1306. #endif
  1307. while (it1 != it1_end) { // zeroing
  1308. functor_type::apply (*it1, expr_value_type/*zero*/());
  1309. ++ it1;
  1310. }
  1311. ++ it2;
  1312. }
  1313. } else {
  1314. it2 = it2_end;
  1315. }
  1316. #if BOOST_UBLAS_TYPE_CHECK
  1317. if (! disable_type_check<bool>::value)
  1318. BOOST_UBLAS_CHECK (detail::expression_type_check (m, cm), external_logic ());
  1319. #endif
  1320. }
  1321. // Dispatcher
  1322. template<template <class T1, class T2> class F, class M, class E>
  1323. BOOST_UBLAS_INLINE
  1324. void matrix_assign (M &m, const matrix_expression<E> &e) {
  1325. typedef typename matrix_assign_traits<typename M::storage_category,
  1326. F<typename M::reference, typename E::value_type>::computed,
  1327. typename E::const_iterator1::iterator_category,
  1328. typename E::const_iterator2::iterator_category>::storage_category storage_category;
  1329. // give preference to matrix M's orientation if known
  1330. typedef typename boost::mpl::if_<boost::is_same<typename M::orientation_category, unknown_orientation_tag>,
  1331. typename E::orientation_category ,
  1332. typename M::orientation_category >::type orientation_category;
  1333. typedef basic_full<typename M::size_type> unrestricted;
  1334. matrix_assign<F, unrestricted> (m, e, storage_category (), orientation_category ());
  1335. }
  1336. template<template <class T1, class T2> class F, class R, class M, class E>
  1337. BOOST_UBLAS_INLINE
  1338. void matrix_assign (M &m, const matrix_expression<E> &e) {
  1339. typedef R conformant_restrict_type;
  1340. typedef typename matrix_assign_traits<typename M::storage_category,
  1341. F<typename M::reference, typename E::value_type>::computed,
  1342. typename E::const_iterator1::iterator_category,
  1343. typename E::const_iterator2::iterator_category>::storage_category storage_category;
  1344. // give preference to matrix M's orientation if known
  1345. typedef typename boost::mpl::if_<boost::is_same<typename M::orientation_category, unknown_orientation_tag>,
  1346. typename E::orientation_category ,
  1347. typename M::orientation_category >::type orientation_category;
  1348. matrix_assign<F, conformant_restrict_type> (m, e, storage_category (), orientation_category ());
  1349. }
  1350. template<class SC, class RI1, class RI2>
  1351. struct matrix_swap_traits {
  1352. typedef SC storage_category;
  1353. };
  1354. template<>
  1355. struct matrix_swap_traits<dense_proxy_tag, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  1356. typedef sparse_proxy_tag storage_category;
  1357. };
  1358. template<>
  1359. struct matrix_swap_traits<packed_proxy_tag, sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag> {
  1360. typedef sparse_proxy_tag storage_category;
  1361. };
  1362. // Dense (proxy) row major case
  1363. template<template <class T1, class T2> class F, class R, class M, class E>
  1364. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1365. void matrix_swap (M &m, matrix_expression<E> &e, dense_proxy_tag, row_major_tag) {
  1366. typedef F<typename M::iterator2::reference, typename E::reference> functor_type;
  1367. // R unnecessary, make_conformant not required
  1368. //typedef typename M::size_type size_type; // gcc is complaining that this is not used, although this is not right
  1369. typedef typename M::difference_type difference_type;
  1370. typename M::iterator1 it1 (m.begin1 ());
  1371. typename E::iterator1 it1e (e ().begin1 ());
  1372. difference_type size1 (BOOST_UBLAS_SAME (m.size1 (), typename M::size_type (e ().end1 () - it1e)));
  1373. while (-- size1 >= 0) {
  1374. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1375. typename M::iterator2 it2 (it1.begin ());
  1376. typename E::iterator2 it2e (it1e.begin ());
  1377. difference_type size2 (BOOST_UBLAS_SAME (m.size2 (), typename M::size_type (it1e.end () - it2e)));
  1378. #else
  1379. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  1380. typename E::iterator2 it2e (begin (it1e, iterator1_tag ()));
  1381. difference_type size2 (BOOST_UBLAS_SAME (m.size2 (), typename M::size_type (end (it1e, iterator1_tag ()) - it2e)));
  1382. #endif
  1383. while (-- size2 >= 0)
  1384. functor_type::apply (*it2, *it2e), ++ it2, ++ it2e;
  1385. ++ it1, ++ it1e;
  1386. }
  1387. }
  1388. // Dense (proxy) column major case
  1389. template<template <class T1, class T2> class F, class R, class M, class E>
  1390. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1391. void matrix_swap (M &m, matrix_expression<E> &e, dense_proxy_tag, column_major_tag) {
  1392. typedef F<typename M::iterator1::reference, typename E::reference> functor_type;
  1393. // R unnecessary, make_conformant not required
  1394. // typedef typename M::size_type size_type; // gcc is complaining that this is not used, although this is not right
  1395. typedef typename M::difference_type difference_type;
  1396. typename M::iterator2 it2 (m.begin2 ());
  1397. typename E::iterator2 it2e (e ().begin2 ());
  1398. difference_type size2 (BOOST_UBLAS_SAME (m.size2 (), typename M::size_type (e ().end2 () - it2e)));
  1399. while (-- size2 >= 0) {
  1400. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1401. typename M::iterator1 it1 (it2.begin ());
  1402. typename E::iterator1 it1e (it2e.begin ());
  1403. difference_type size1 (BOOST_UBLAS_SAME (m.size1 (), typename M::size_type (it2e.end () - it1e)));
  1404. #else
  1405. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  1406. typename E::iterator1 it1e (begin (it2e, iterator2_tag ()));
  1407. difference_type size1 (BOOST_UBLAS_SAME (m.size1 (), typename M::size_type (end (it2e, iterator2_tag ()) - it1e)));
  1408. #endif
  1409. while (-- size1 >= 0)
  1410. functor_type::apply (*it1, *it1e), ++ it1, ++ it1e;
  1411. ++ it2, ++ it2e;
  1412. }
  1413. }
  1414. // Packed (proxy) row major case
  1415. template<template <class T1, class T2> class F, class R, class M, class E>
  1416. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1417. void matrix_swap (M &m, matrix_expression<E> &e, packed_proxy_tag, row_major_tag) {
  1418. typedef F<typename M::iterator2::reference, typename E::reference> functor_type;
  1419. // R unnecessary, make_conformant not required
  1420. typedef typename M::difference_type difference_type;
  1421. typename M::iterator1 it1 (m.begin1 ());
  1422. typename E::iterator1 it1e (e ().begin1 ());
  1423. difference_type size1 (BOOST_UBLAS_SAME (m.end1 () - it1, e ().end1 () - it1e));
  1424. while (-- size1 >= 0) {
  1425. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1426. typename M::iterator2 it2 (it1.begin ());
  1427. typename E::iterator2 it2e (it1e.begin ());
  1428. difference_type size2 (BOOST_UBLAS_SAME (it1.end () - it2, it1e.end () - it2e));
  1429. #else
  1430. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  1431. typename E::iterator2 it2e (begin (it1e, iterator1_tag ()));
  1432. difference_type size2 (BOOST_UBLAS_SAME (end (it1, iterator1_tag ()) - it2, end (it1e, iterator1_tag ()) - it2e));
  1433. #endif
  1434. while (-- size2 >= 0)
  1435. functor_type::apply (*it2, *it2e), ++ it2, ++ it2e;
  1436. ++ it1, ++ it1e;
  1437. }
  1438. }
  1439. // Packed (proxy) column major case
  1440. template<template <class T1, class T2> class F, class R, class M, class E>
  1441. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1442. void matrix_swap (M &m, matrix_expression<E> &e, packed_proxy_tag, column_major_tag) {
  1443. typedef F<typename M::iterator1::reference, typename E::reference> functor_type;
  1444. // R unnecessary, make_conformant not required
  1445. typedef typename M::difference_type difference_type;
  1446. typename M::iterator2 it2 (m.begin2 ());
  1447. typename E::iterator2 it2e (e ().begin2 ());
  1448. difference_type size2 (BOOST_UBLAS_SAME (m.end2 () - it2, e ().end2 () - it2e));
  1449. while (-- size2 >= 0) {
  1450. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1451. typename M::iterator1 it1 (it2.begin ());
  1452. typename E::iterator1 it1e (it2e.begin ());
  1453. difference_type size1 (BOOST_UBLAS_SAME (it2.end () - it1, it2e.end () - it1e));
  1454. #else
  1455. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  1456. typename E::iterator1 it1e (begin (it2e, iterator2_tag ()));
  1457. difference_type size1 (BOOST_UBLAS_SAME (end (it2, iterator2_tag ()) - it1, end (it2e, iterator2_tag ()) - it1e));
  1458. #endif
  1459. while (-- size1 >= 0)
  1460. functor_type::apply (*it1, *it1e), ++ it1, ++ it1e;
  1461. ++ it2, ++ it2e;
  1462. }
  1463. }
  1464. // Sparse (proxy) row major case
  1465. template<template <class T1, class T2> class F, class R, class M, class E>
  1466. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1467. void matrix_swap (M &m, matrix_expression<E> &e, sparse_proxy_tag, row_major_tag) {
  1468. typedef F<typename M::iterator2::reference, typename E::reference> functor_type;
  1469. typedef R conformant_restrict_type;
  1470. typedef typename M::size_type size_type;
  1471. typedef typename M::difference_type difference_type;
  1472. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  1473. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  1474. detail::make_conformant (m, e, row_major_tag (), conformant_restrict_type ());
  1475. // FIXME should be a seperate restriction for E
  1476. detail::make_conformant (e (), m, row_major_tag (), conformant_restrict_type ());
  1477. typename M::iterator1 it1 (m.begin1 ());
  1478. typename M::iterator1 it1_end (m.end1 ());
  1479. typename E::iterator1 it1e (e ().begin1 ());
  1480. typename E::iterator1 it1e_end (e ().end1 ());
  1481. while (it1 != it1_end && it1e != it1e_end) {
  1482. difference_type compare = it1.index1 () - it1e.index1 ();
  1483. if (compare == 0) {
  1484. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1485. typename M::iterator2 it2 (it1.begin ());
  1486. typename M::iterator2 it2_end (it1.end ());
  1487. typename E::iterator2 it2e (it1e.begin ());
  1488. typename E::iterator2 it2e_end (it1e.end ());
  1489. #else
  1490. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  1491. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  1492. typename E::iterator2 it2e (begin (it1e, iterator1_tag ()));
  1493. typename E::iterator2 it2e_end (end (it1e, iterator1_tag ()));
  1494. #endif
  1495. if (it2 != it2_end && it2e != it2e_end) {
  1496. size_type it2_index = it2.index2 (), it2e_index = it2e.index2 ();
  1497. for (;;) {
  1498. difference_type compare2 = it2_index - it2e_index;
  1499. if (compare2 == 0) {
  1500. functor_type::apply (*it2, *it2e);
  1501. ++ it2, ++ it2e;
  1502. if (it2 != it2_end && it2e != it2e_end) {
  1503. it2_index = it2.index2 ();
  1504. it2e_index = it2e.index2 ();
  1505. } else
  1506. break;
  1507. } else if (compare2 < 0) {
  1508. increment (it2, it2_end, - compare2);
  1509. if (it2 != it2_end)
  1510. it2_index = it2.index2 ();
  1511. else
  1512. break;
  1513. } else if (compare2 > 0) {
  1514. increment (it2e, it2e_end, compare2);
  1515. if (it2e != it2e_end)
  1516. it2e_index = it2e.index2 ();
  1517. else
  1518. break;
  1519. }
  1520. }
  1521. }
  1522. #if BOOST_UBLAS_TYPE_CHECK
  1523. increment (it2e, it2e_end);
  1524. increment (it2, it2_end);
  1525. #endif
  1526. ++ it1, ++ it1e;
  1527. } else if (compare < 0) {
  1528. #if BOOST_UBLAS_TYPE_CHECK
  1529. while (it1.index1 () < it1e.index1 ()) {
  1530. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1531. typename M::iterator2 it2 (it1.begin ());
  1532. typename M::iterator2 it2_end (it1.end ());
  1533. #else
  1534. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  1535. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  1536. #endif
  1537. increment (it2, it2_end);
  1538. ++ it1;
  1539. }
  1540. #else
  1541. increment (it1, it1_end, - compare);
  1542. #endif
  1543. } else if (compare > 0) {
  1544. #if BOOST_UBLAS_TYPE_CHECK
  1545. while (it1e.index1 () < it1.index1 ()) {
  1546. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1547. typename E::iterator2 it2e (it1e.begin ());
  1548. typename E::iterator2 it2e_end (it1e.end ());
  1549. #else
  1550. typename E::iterator2 it2e (begin (it1e, iterator1_tag ()));
  1551. typename E::iterator2 it2e_end (end (it1e, iterator1_tag ()));
  1552. #endif
  1553. increment (it2e, it2e_end);
  1554. ++ it1e;
  1555. }
  1556. #else
  1557. increment (it1e, it1e_end, compare);
  1558. #endif
  1559. }
  1560. }
  1561. #if BOOST_UBLAS_TYPE_CHECK
  1562. while (it1e != it1e_end) {
  1563. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1564. typename E::iterator2 it2e (it1e.begin ());
  1565. typename E::iterator2 it2e_end (it1e.end ());
  1566. #else
  1567. typename E::iterator2 it2e (begin (it1e, iterator1_tag ()));
  1568. typename E::iterator2 it2e_end (end (it1e, iterator1_tag ()));
  1569. #endif
  1570. increment (it2e, it2e_end);
  1571. ++ it1e;
  1572. }
  1573. while (it1 != it1_end) {
  1574. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1575. typename M::iterator2 it2 (it1.begin ());
  1576. typename M::iterator2 it2_end (it1.end ());
  1577. #else
  1578. typename M::iterator2 it2 (begin (it1, iterator1_tag ()));
  1579. typename M::iterator2 it2_end (end (it1, iterator1_tag ()));
  1580. #endif
  1581. increment (it2, it2_end);
  1582. ++ it1;
  1583. }
  1584. #endif
  1585. }
  1586. // Sparse (proxy) column major case
  1587. template<template <class T1, class T2> class F, class R, class M, class E>
  1588. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1589. void matrix_swap (M &m, matrix_expression<E> &e, sparse_proxy_tag, column_major_tag) {
  1590. typedef F<typename M::iterator1::reference, typename E::reference> functor_type;
  1591. typedef R conformant_restrict_type;
  1592. typedef typename M::size_type size_type;
  1593. typedef typename M::difference_type difference_type;
  1594. BOOST_UBLAS_CHECK (m.size1 () == e ().size1 (), bad_size ());
  1595. BOOST_UBLAS_CHECK (m.size2 () == e ().size2 (), bad_size ());
  1596. detail::make_conformant (m, e, column_major_tag (), conformant_restrict_type ());
  1597. // FIXME should be a seperate restriction for E
  1598. detail::make_conformant (e (), m, column_major_tag (), conformant_restrict_type ());
  1599. typename M::iterator2 it2 (m.begin2 ());
  1600. typename M::iterator2 it2_end (m.end2 ());
  1601. typename E::iterator2 it2e (e ().begin2 ());
  1602. typename E::iterator2 it2e_end (e ().end2 ());
  1603. while (it2 != it2_end && it2e != it2e_end) {
  1604. difference_type compare = it2.index2 () - it2e.index2 ();
  1605. if (compare == 0) {
  1606. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1607. typename M::iterator1 it1 (it2.begin ());
  1608. typename M::iterator1 it1_end (it2.end ());
  1609. typename E::iterator1 it1e (it2e.begin ());
  1610. typename E::iterator1 it1e_end (it2e.end ());
  1611. #else
  1612. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  1613. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  1614. typename E::iterator1 it1e (begin (it2e, iterator2_tag ()));
  1615. typename E::iterator1 it1e_end (end (it2e, iterator2_tag ()));
  1616. #endif
  1617. if (it1 != it1_end && it1e != it1e_end) {
  1618. size_type it1_index = it1.index1 (), it1e_index = it1e.index1 ();
  1619. for (;;) {
  1620. difference_type compare2 = it1_index - it1e_index;
  1621. if (compare2 == 0) {
  1622. functor_type::apply (*it1, *it1e);
  1623. ++ it1, ++ it1e;
  1624. if (it1 != it1_end && it1e != it1e_end) {
  1625. it1_index = it1.index1 ();
  1626. it1e_index = it1e.index1 ();
  1627. } else
  1628. break;
  1629. } else if (compare2 < 0) {
  1630. increment (it1, it1_end, - compare2);
  1631. if (it1 != it1_end)
  1632. it1_index = it1.index1 ();
  1633. else
  1634. break;
  1635. } else if (compare2 > 0) {
  1636. increment (it1e, it1e_end, compare2);
  1637. if (it1e != it1e_end)
  1638. it1e_index = it1e.index1 ();
  1639. else
  1640. break;
  1641. }
  1642. }
  1643. }
  1644. #if BOOST_UBLAS_TYPE_CHECK
  1645. increment (it1e, it1e_end);
  1646. increment (it1, it1_end);
  1647. #endif
  1648. ++ it2, ++ it2e;
  1649. } else if (compare < 0) {
  1650. #if BOOST_UBLAS_TYPE_CHECK
  1651. while (it2.index2 () < it2e.index2 ()) {
  1652. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1653. typename M::iterator1 it1 (it2.begin ());
  1654. typename M::iterator1 it1_end (it2.end ());
  1655. #else
  1656. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  1657. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  1658. #endif
  1659. increment (it1, it1_end);
  1660. ++ it2;
  1661. }
  1662. #else
  1663. increment (it2, it2_end, - compare);
  1664. #endif
  1665. } else if (compare > 0) {
  1666. #if BOOST_UBLAS_TYPE_CHECK
  1667. while (it2e.index2 () < it2.index2 ()) {
  1668. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1669. typename E::iterator1 it1e (it2e.begin ());
  1670. typename E::iterator1 it1e_end (it2e.end ());
  1671. #else
  1672. typename E::iterator1 it1e (begin (it2e, iterator2_tag ()));
  1673. typename E::iterator1 it1e_end (end (it2e, iterator2_tag ()));
  1674. #endif
  1675. increment (it1e, it1e_end);
  1676. ++ it2e;
  1677. }
  1678. #else
  1679. increment (it2e, it2e_end, compare);
  1680. #endif
  1681. }
  1682. }
  1683. #if BOOST_UBLAS_TYPE_CHECK
  1684. while (it2e != it2e_end) {
  1685. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1686. typename E::iterator1 it1e (it2e.begin ());
  1687. typename E::iterator1 it1e_end (it2e.end ());
  1688. #else
  1689. typename E::iterator1 it1e (begin (it2e, iterator2_tag ()));
  1690. typename E::iterator1 it1e_end (end (it2e, iterator2_tag ()));
  1691. #endif
  1692. increment (it1e, it1e_end);
  1693. ++ it2e;
  1694. }
  1695. while (it2 != it2_end) {
  1696. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1697. typename M::iterator1 it1 (it2.begin ());
  1698. typename M::iterator1 it1_end (it2.end ());
  1699. #else
  1700. typename M::iterator1 it1 (begin (it2, iterator2_tag ()));
  1701. typename M::iterator1 it1_end (end (it2, iterator2_tag ()));
  1702. #endif
  1703. increment (it1, it1_end);
  1704. ++ it2;
  1705. }
  1706. #endif
  1707. }
  1708. // Dispatcher
  1709. template<template <class T1, class T2> class F, class M, class E>
  1710. BOOST_UBLAS_INLINE
  1711. void matrix_swap (M &m, matrix_expression<E> &e) {
  1712. typedef typename matrix_swap_traits<typename M::storage_category,
  1713. typename E::const_iterator1::iterator_category,
  1714. typename E::const_iterator2::iterator_category>::storage_category storage_category;
  1715. // give preference to matrix M's orientation if known
  1716. typedef typename boost::mpl::if_<boost::is_same<typename M::orientation_category, unknown_orientation_tag>,
  1717. typename E::orientation_category ,
  1718. typename M::orientation_category >::type orientation_category;
  1719. typedef basic_full<typename M::size_type> unrestricted;
  1720. matrix_swap<F, unrestricted> (m, e, storage_category (), orientation_category ());
  1721. }
  1722. template<template <class T1, class T2> class F, class R, class M, class E>
  1723. BOOST_UBLAS_INLINE
  1724. void matrix_swap (M &m, matrix_expression<E> &e) {
  1725. typedef R conformant_restrict_type;
  1726. typedef typename matrix_swap_traits<typename M::storage_category,
  1727. typename E::const_iterator1::iterator_category,
  1728. typename E::const_iterator2::iterator_category>::storage_category storage_category;
  1729. // give preference to matrix M's orientation if known
  1730. typedef typename boost::mpl::if_<boost::is_same<typename M::orientation_category, unknown_orientation_tag>,
  1731. typename E::orientation_category ,
  1732. typename M::orientation_category >::type orientation_category;
  1733. matrix_swap<F, conformant_restrict_type> (m, e, storage_category (), orientation_category ());
  1734. }
  1735. }}}
  1736. #endif