2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
swap & deep_swap
|
2013-07-17 19:24:07 +02:00
|
|
|
==================================
|
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
There are two possible interpretation of "swapping two arrays" :
|
|
|
|
either use 3 moves like std::swap, i.e. swapping the pointer to the data in memory,
|
|
|
|
or making a deep swap, i.e. really swapping all the elements in memeory.
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
.. _arr_swap:
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
swap
|
2013-07-17 19:24:07 +02:00
|
|
|
--------
|
|
|
|
|
|
|
|
* swap just exchange the (shared) pointer to the data in memory,
|
|
|
|
it does not affect the data them self.
|
|
|
|
This distinction of importance for views in particular.
|
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
* For the regular type, std::swap and swap are equivalent.
|
|
|
|
For the views, std::swap is explicitely deleted, since it is incorrect
|
|
|
|
due to the lack of move constructor for a view.
|
|
|
|
Use `swap` instead.
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
* **Example** :
|
|
|
|
|
|
|
|
.. compileblock::
|
|
|
|
|
|
|
|
#include <triqs/arrays.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
using triqs::arrays::vector; using triqs::arrays::range;
|
|
|
|
int main () {
|
|
|
|
triqs::arrays::vector<double> V(3), W(4);
|
|
|
|
V() = 3; W()=4; // initialize
|
|
|
|
auto VV = V(range (0,2));
|
|
|
|
auto VW = W(range (0,2));
|
|
|
|
|
|
|
|
std::cout << "V = "<< V << " W = " << W<< " V view "<< VV<< " W view "<< VW<< std::endl;
|
|
|
|
swap(V,W);
|
|
|
|
std::cout << "V = "<< V << " W = " << W<< " V view "<< VV<< " W view "<< VW<< std::endl;
|
|
|
|
swap(VV,VW);
|
|
|
|
std::cout << "V = "<< V << " W = " << W<< " V view "<< VV<< " W view "<< VW<< std::endl;
|
|
|
|
}
|
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
.. _arr_deep_swap:
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
deep_swap
|
|
|
|
--------------
|
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Currently implemented only for triqs::vector and triqs::vector_view.
|
|
|
|
|
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
* deep_swap swaps the data in memory.
|
|
|
|
|
|
|
|
* **Example** (compare with swap) :
|
|
|
|
|
|
|
|
.. compileblock::
|
|
|
|
|
|
|
|
#include <triqs/arrays.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
using triqs::arrays::vector; using triqs::arrays::range;
|
|
|
|
int main () {
|
|
|
|
triqs::arrays::vector<double> V(3), W(3);
|
|
|
|
V() = 3; W()=5; // initialize
|
|
|
|
auto VV = V(range (0,2));
|
|
|
|
auto VW = W(range (0,2));
|
|
|
|
|
|
|
|
std::cout << "V = "<< V << " W = " << W<< " V view "<< VV<< " W view "<< VW<< std::endl;
|
|
|
|
deep_swap(V,W);
|
|
|
|
std::cout << "V = "<< V << " W = " << W<< " V view "<< VV<< " W view "<< VW<< std::endl;
|
|
|
|
deep_swap(VV,VW);
|
|
|
|
std::cout << "V = "<< V << " W = " << W<< " V view "<< VV<< " W view "<< VW<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|