[multi_array] how to modify the dimension of an array?
Hello everybody,
i'm looking for the best way to modify the dimensions of an array. For
example, i got this one:
boost::multi_array
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 r0d wrote:
Hello everybody,
i'm looking for the best way to modify the dimensions of an array. For example, i got this one:
boost::multi_array
array ( boost::extents[3][3] ); and i want to change it in an array of 5 dimensions.
Now, what i'm doing is to create a new array of 5 dimensions and copying the first one into it, then delete the first one. But i'm sure that a more efficient way exists. No?
use resize() - see below
TEST(multi_array)
{
boost::multi_array
everweb wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
r0d wrote:
Hello everybody,
i'm looking for the best way to modify the dimensions of an array. For example, i got this one:
boost::multi_array
array ( boost::extents[3][3] ); and i want to change it in an array of 5 dimensions.
Now, what i'm doing is to create a new array of 5 dimensions and copying the first one into it, then delete the first one. But i'm sure that a more efficient way exists. No?
use resize() - see below
TEST(multi_array) { boost::multi_array
myArray ( boost::extents[3][3] ); int values = 0; for(boost::multi_array
::index i = 0; i != 3; ++i) { for(boost::multi_array ::index j = 0; j != 3; ++j) { myArray[i][j] = values++; } } // Verify values int verify1 = 0; for(boost::multi_array
::index i = 0; i != 3; ++i) { for(boost::multi_array ::index j = 0; j != 3; ++j) { CHECK_EQUAL(myArray[i][j],verify1++); } } myArray.resize(boost::extents[5][5]); //continue setting 'values' in the newly added array space for(boost::multi_array
::index i = 3; i != 5; ++i) { for(boost::multi_array ::index j = 3; j != 5; ++j) { myArray[i][j] = values++; } } //check the original data is still there int verify2 = 0; for(boost::multi_array
::index i = 0; i != 3; ++i) { for(boost::multi_array ::index j = 0; j != 3; ++j) { CHECK_EQUAL(myArray[i][j],verify2++); } } //check that the new array space has correct values for(boost::multi_array
::index i = 3; i != 5; ++i) { for(boost::multi_array ::index j = 3; j != 5; ++j) { CHECK_EQUAL(myArray[i][j],verify1++); } } }
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iD8DBQFIQGnW/X5Z5iyVCbERAmCmAJ4qgpbHzcUYqGor8kDUabQfDHwCLQCfZ7EH dTGc5PybZhGR0zgyUxx92q0= =VwiS -----END PGP SIGNATURE----- _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thank you everweb,
but that's not what i want to do. Surely i expained badly my problem (my
english is too bad).
I want to change the number of dimensions of the array. For example, i got
an array:
boost::multi_array
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 r0d wrote:
everweb wrote: r0d wrote:
Hello everybody,
i'm looking for the best way to modify the dimensions of an array. For example, i got this one:
boost::multi_array
array ( boost::extents[3][3] ); and i want to change it in an array of 5 dimensions.
Now, what i'm doing is to create a new array of 5 dimensions and copying the first one into it, then delete the first one. But i'm sure that a more efficient way exists. No? use resize() - see below
TEST(multi_array) { boost::multi_array
myArray ( boost::extents[3][3] ); int values = 0; for(boost::multi_array
::index i = 0; i != 3; ++i) { for(boost::multi_array ::index j = 0; j != 3; ++j) { myArray[i][j] = values++; } } // Verify values int verify1 = 0; for(boost::multi_array
::index i = 0; i != 3; ++i) { for(boost::multi_array ::index j = 0; j != 3; ++j) { CHECK_EQUAL(myArray[i][j],verify1++); } } myArray.resize(boost::extents[5][5]); //continue setting 'values' in the newly added array space for(boost::multi_array
::index i = 3; i != 5; ++i) { for(boost::multi_array ::index j = 3; j != 5; ++j) { myArray[i][j] = values++; } } //check the original data is still there int verify2 = 0; for(boost::multi_array
::index i = 0; i != 3; ++i) { for(boost::multi_array ::index j = 0; j != 3; ++j) { CHECK_EQUAL(myArray[i][j],verify2++); } } //check that the new array space has correct values for(boost::multi_array
::index i = 3; i != 5; ++i) { for(boost::multi_array ::index j = 3; j != 5; ++j) { CHECK_EQUAL(myArray[i][j],verify1++); } } }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thank you everweb,
but that's not what i want to do. Surely i expained badly my problem (my english is too bad).
I want to change the number of dimensions of the array. For example, i got an array: boost::multi_array
and i want to change it in boost::multi_array
Is it possible to di it easyly?
Ah, when you said "modify the dimensions of an array", I interpreted that as "modify the size of the dimensions", not "modify the number of dimensions" ! So you want to go from myArray[3][3] to myArray[3][3][3][3][3], not from [3][3] to [5][5] ? In my mind, that is such a dramatic change that I can't see a way to "easily" implement it - which of the 5 dimensions should be copied from the original 2 - always the first 2 ? What if you meant the hypothetical redimension() function to go from [3][3] to [n][3][n][3][n] instead of [3][3][n][n][n] ? I'd say your copy, delete is the best way forward. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org iD8DBQFIQZLi/X5Z5iyVCbERAvutAJ99AopSUh64ncW5E/z2rXd1vHGAKACfbqWt UbRNyk3c9NG42uUc+RjL2aI= =aYl1 -----END PGP SIGNATURE-----
participants (2)
-
r0d
-
Rupert Bruce