[Boost.MultiArray]: size question

Dear all, we use here the multi_array as a 2 dimensional matrix. Often we want to resize the row or column dependent on some GUI event. Code could be: void Bla::ResizeRows(size_t nRows) { m_matrix.resize(boost::extents[nRows][m_matrix[0].size()]); } However this uses the 0-index of row count (i.e. 'm_matrix[0].size()') to determine the size of the columns. Will this not crash if you have 0 rows? And if so is there a better alternative? wkr.

On 12/17/12 09:50, gast128 wrote:
Dear all,
we use here the multi_array as a 2 dimensional matrix. Often we want to resize the row or column dependent on some GUI event. Code could be:
void Bla::ResizeRows(size_t nRows) { m_matrix.resize(boost::extents[nRows][m_matrix[0].size()]); }
However this uses the 0-index of row count (i.e. 'm_matrix[0].size()') to determine the size of the columns. Will this not crash if you have 0 rows? And if so is there a better alternative?
wkr. The test driver:
http://svn.boost.org/svn/boost/sandbox/variadic_templates/sandbox/array_dyn/... produces output: ********reshape demo*********** dir_op=0 shape={ 0, 3} my_data.size()=0 ai= } . shape={ 1, 3} ai= { { 0, 0, 0} } . shape={ 5, 3} ai= { { 0, 0, 0} , { 0, 0, 0} , { 0, 0, 0} , { 0, 0, 0} , { 0, 0, 0} } . Compilation finished at Mon Dec 17 11:11:25 Which suggests it will work when 1st axis (the row axis) has 0 as it's size. Unfortunately, this is not Boost.MultiArray and it's probably a lot slower since the sizes of the dimensions are all runtime values. Also, the printout when the shape includes a 0, as in: shape={ 0, 3} does not have balanced curly brackets: ai= } I'm not sure how to handle that yet.

On 12/17/12 11:24, Larry Evans wrote:
On 12/17/12 09:50, gast128 wrote:
Dear all,
we use here the multi_array as a 2 dimensional matrix. Often we want to resize the row or column dependent on some GUI event. Code could be:
void Bla::ResizeRows(size_t nRows) { m_matrix.resize(boost::extents[nRows][m_matrix[0].size()]); }
However this uses the 0-index of row count (i.e. 'm_matrix[0].size()') to determine the size of the columns. Will this not crash if you have 0 rows? And if so is there a better alternative?
wkr. The test driver:
http://svn.boost.org/svn/boost/sandbox/variadic_templates/sandbox/array_dyn/...
produces output:
********reshape demo*********** dir_op=0 shape={ 0, 3} my_data.size()=0 ai= } . shape={ 1, 3} ai= { { 0, 0, 0} } . shape={ 5, 3} ai= { { 0, 0, 0} , { 0, 0, 0} , { 0, 0, 0} , { 0, 0, 0} , { 0, 0, 0} } .
Compilation finished at Mon Dec 17 11:11:25
Which suggests it will work when 1st axis (the row axis) has 0 as it's size. Unfortunately, this is not Boost.MultiArray and it's probably a lot slower since the sizes of the dimensions are all runtime values. Also, the printout when the shape includes a 0, as in:
shape={ 0, 3}
does not have balanced curly brackets:
ai= }
I'm not sure how to handle that yet.
Addition of print_array<.>::visit_empty and corresponding change to array_host seems to solve the empty axis print problem: ********reshape demo*********** dir_op=0 shape={ 3, 0} my_data.size()=0 ai= { { } , { } , { } } . shape={ 3, 1} ai= { { 0} , { 1} , { 2} } . shape={ 3, 5} ai= { { 0, 3, 6, 9, 12} , { 1, 4, 7, 10, 13} , { 2, 5, 8, 11, 14} } . shape={ 0, 5} ai= { } . Compilation finished at Mon Dec 17 12:55:44 HTH. -regards, Larry

This gives at least an assert: BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]); Isn't there a better solution?

On 12/17/12 14:14, gast128 wrote:
This gives at least an assert:
BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
Isn't there a better solution?
Are you referring to multi_array for the array_dyn alternative mentioned in my OP? Or this this reply not intended for me? Could you provide the complete program that has this statement in it? I've grepped files here: http://svn.boost.org/svn/boost/sandbox/variadic_templates/sandbox/array_dyn for extents, and found none; hence, I'm confused :( -regards, Larry

Larry Evans
On 12/17/12 14:14, gast128 wrote:
This gives at least an assert:
BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
Isn't there a better solution?
Are you referring to multi_array for the array_dyn alternative mentioned in my OP? Or this this reply not intended for me?
Could you provide the complete program that has this statement in it?
I've grepped files here:
http://svn.boost.org/svn/boost/sandbox/variadic_templates/sandbox/array_dyn
for extents, and found none; hence, I'm confused :(
I am talking about multi_array, see title. Example code:
void f()
{
typedef boost::multi_array

On 18 December 2012 08:24, gast128
I am talking about multi_array, see title. Example code:
void f() { typedef boost::multi_array
Matrix; Matrix m1(boost::extents[2][3]); _ASSERT(m1.size() == 2); _ASSERT(m1[0].size() == 3);
m1.resize(boost::extents[0][3]); _ASSERT(m1.size() == 0); //_ASSERT(m1[0].size() == 3); //<--assert }
Hi,
The shape function has the list of multi_array dimensions.
This should do it:
#include

On 12/17/12 15:24, gast128 wrote:
Larry Evans
writes: On 12/17/12 14:14, gast128 wrote:
This gives at least an assert:
BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
Isn't there a better solution?
Are you referring to multi_array for the array_dyn alternative mentioned in my OP? Or this this reply not intended for me?
Could you provide the complete program that has this statement in it?
I've grepped files here:
http://svn.boost.org/svn/boost/sandbox/variadic_templates/sandbox/array_dyn
for extents, and found none; hence, I'm confused :(
I am talking about multi_array, see title. Example code:
You did mention alternatives in your OP: is there a better alternative? and array_dyn.hpp is an alternative which, as shown by the example runs, doesn't have a problem with 0 sized dimensions. Of course maybe you meant a better alternative which still used Boost.MultiArray; however, that was not clear from your OP; hence, I supplied the array_dyn alternative. -regards, Larry

Yes with the shape function it works. Thx. For Larry: with 'alternative' I meant 'alternative ways within the Boost.MultiArray library' (e.g. with the 'shape() function). Still thx for your effort you put in answering my question.
participants (3)
-
gast128
-
Larry Evans
-
Vitaly Budovski