From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2001-12-07 03:29:09


jhrwalter wrote:

> --- In boost_at_y..., Peter Schmitteckert (boost) <boost_at_s...> wrote:
>
>>Salut,
>>
>>On Thursday 06 December 2001 10:45, Toon Knapen wrote:
>>
>>
>>>>>For instance if I
>>>>>need a temporary matrix in a loop, but in every loop the total
>>>>>
> size
>
>>>>>of the matrix can be different, I can allocate a matrix with
>>>>>
> some
>
>>>>>maximal size and resize it in every loop to something smaller
>>>>>
> in every
>
>>>>>loop in constant amortised time ?
>>>>>
>>>>Currently not, but it should be possible to change this. May be
>>>>
> we
>
>>>>need to differentiate size() and capacity()?
>>>>
>>>Definitly. This would minimise memory allocation !
>>>
>>I also need this feature to avoid unneccessary allocation,
>>but here should be the possiblilty to finally release unneeded
>>
> memory.
>
>>What about the following idea:
>>-> resize( n1, n2) garanties "to hold size1 rows of size2
>>
> elements".
>
>>-> reserve(n3, n4) garanties that as long n3>=n1, n4 >=n2 no
>>
> allocations in
>
>>resize(n1,n2) are performed. if n3<n1 or n4 < n2 the matrix is
>>
> allowed to
>
>>shrink to the reserved size if requested in a subsequent resize(..)
>>
> in
>
>>order to minimize wasted memory.
>>But this would lead to the notion of a minimal_capacity().
>>
>
> Do we really need reserve()? IMO the allocation behaviour of resize()
> is a quality of implementation issue, especially since resize() is a
> method of the proxy concept. If we intend to make it's behaviour
> transparent, we'd have to introduce capacity() first.

I find this really necessary (there's also a good reason why they have
capacity and size in std::vector). Everytime you shrink the vector or
matrix (i.e. resize(x) with x < size() ), you are going to delete[] the
old data and new[] another array. This is very time consuming.
I would suggest to allign totally with what std::vector does : you can
resize without reallocation (if the capacity is large enough) and you
can easily shrink the capacity by copying to a smaller vector.

> OTOH capacity() won't be generic.

please elaborate.

> BTW, do we need to consider std::allocator?
In the long term : yes. But no allocater can be as smart as what you

can do with capacity() and resize(). So the allocator is not a solution

to the above problem.
An allocator would be nice e.g. to optimise allocation of many small
matrices.