Boost logo

Boost :

From: boost (boost_at_[hidden])
Date: 2001-12-12 05:41:17


Salut,

On Wednesday 12 December 2001 11:20, you wrote:

> >
> > Then I don't understand, why capacity1()/capacity2() is non-generic,
> > since I'm not allowed to make use of capacity() in a sense, that I
> > reshape the matrix, so capacity() is useless and we have
> > capacity1()/capacity2() only.
>
> Let's take examples:
>
> Case 1: the storage for the first matrix type is an 1-D array, i.e. m
> (i, j) = a [i * size + j]. Then client code could perform something
> like the following operation with capacity():
>
> if (new_size1 * new_size2 <= m.capacity ())
> m.resize (new_size1, new_size2);
> else
> std::cout << "oops: reallocation necessary" << std::endl;
>
> Case 2: the storage for the next matrix type is a 2-D array, i.e. m
> (i, j) = a [i] [j]. Then client code could perform the following
> operation with capacity1()/capacity2():
>
> if (new_size1 <= m.capacity1 () && new_size2 <= m.capacity2 ()) {
> m.resize (new_size1, new_size2);
> else
> std::cout << "oops: reallocation necessary" << std::endl;
>

But, since I'm not allowed to reshape the Matrix, case 1 ist an
insufficient test. Sure, it assures that one doesn't get a core dump,
but what happens if new_size1() is larger than capacity1(), still
preserving that
new_size1 * new_size2 < capacity() = capacity1() * capacity2() ?
The code is still correct in the sense that it won't access
not allocated regions, but the matrix is reshaped, i.e. it's not
a sub matrix anymore.

So, case 1 should look like

if (new_size1 <= m.capacity1 () && new_size2 <= m.capacity2 ())
    m.resize (new_size1, new_size2);
else if (new_size1 * new_size2 <= m.capacity ())
{
    std::cout << "Warning: Matrix is reshaped" << std::endl;
    m.resize (new_size1, new_size2);
} else
    std::cout << "oops: reallocation necessary" << std::endl;

Best wishes,
Peter


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk