Antonio Xanadu Guest
|
Posted: Fri Apr 23, 2004 11:11 pm Post subject: policy for dynamic array's capacity |
|
|
Hi,
IMHO, it would better if dynamic arrays e.g. std::vector
supported user-defined "capacity policy".
Examples:
struct DoubleGrow
{
// ever growing storage
size_t computeCapacity(size_t currSize, size_t newSize, size_t
currCapacity)
{ return (newSize > currCapacity) ? 2*newSize : currCapacity; }
};
struct MinGrow
{
size_t computeCapacity(size_t currSize, size_t newSize, size_t
currCapacity)
{ return (newSize > currCapacity) ? newSize : currCapacity; }
};
struct Simple
{
size_t computeCapacity(size_t currSize, size_t newSize, size_t
currCapacity)
{ return newSize; }
};
struct MoreInteresting // this one can shrink
{
size_t computeCapacity(size_t currSize, size_t newSize, size_t
currCapacity)
{
if (newSize > currCapacity || 2*newSize < currCapacity)
return newSize;
return currCapacity;
}
};
struct Stateful //has state, remembers previous usage, predicts capacity
{
size_t computeCapacity(size_t currSize, size_t newSize, size_te
currCapacity)
{
size_t result = ....; // some clever computations
return result;
}
SomeType m_stats;
};
..... // many more
typedef libxx::array
Here, VEC's storage can only grow (I don't have such a guarantee with
standard
arrays)
Of course, there may exist better design for the interface of such a policy
but the fact is that quite often it is very useful to have more control on
dynamic array's capacity.
Your considerations?
AX
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|