|
Boost Users : |
From: Rupert Bruce (rupert_at_[hidden])
Date: 2008-05-31 14:03:14
-----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<int, 2> 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<int, 2> myArray ( boost::extents[3][3] );
>
> int values = 0;
> for(boost::multi_array<int, 2>::index i = 0; i != 3; ++i)
> {
> for(boost::multi_array<int, 2>::index j = 0; j != 3; ++j)
> {
> myArray[i][j] = values++;
> }
> }
>
> // Verify values
> int verify1 = 0;
> for(boost::multi_array<int, 2>::index i = 0; i != 3; ++i)
> {
> for(boost::multi_array<int, 2>::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<int, 2>::index i = 3; i != 5; ++i)
> {
> for(boost::multi_array<int, 2>::index j = 3; j != 5; ++j)
> {
> myArray[i][j] = values++;
> }
> }
>
> //check the original data is still there
> int verify2 = 0;
> for(boost::multi_array<int, 2>::index i = 0; i != 3; ++i)
> {
> for(boost::multi_array<int, 2>::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<int, 2>::index i = 3; i != 5; ++i)
> {
> for(boost::multi_array<int, 2>::index j = 3; j != 5; ++j)
> {
> CHECK_EQUAL(myArray[i][j],verify1++);
> }
> }
>
>
> }
>
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
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<int, 2>
> and i want to change it in
> boost::multi_array<int, 5>
> 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-----
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net