Commit 129e16f9 authored by Mark Olesen's avatar Mark Olesen
Browse files

DynamicList improvements/disambiguities

* DynamicList::allocSize(label)
  - Adjust the allocated size. The addressed list can be truncated but not
    extended, use setSize() for that.

* DynamicList::reserve(label)
  - Reserve allocation for *at least* this number of elements.
    Never shrinks the allocated size, nor touches the addressed list size.

* DynamicList::setSize(label)
  - proposed behaviour:
    Adjust the addressed list size, allocating extra space if required.
  - The current behaviour is ambiguous about what addressable size will
    actually get set and using it to extend the addressable size (as
    per List) automatically shrinks the allocated space to this size!
parent e72a6234
......@@ -43,9 +43,14 @@ int main(int argc, char *argv[])
ldl[0](3) = 3;
ldl[0](1) = 1;
ldl[0].setSize(5); // increase allocated size
ldl[1].setSize(10); // increase allocated size
ldl[1](2) = 2;
ldl[0].allocSize(5); // increase allocated size
ldl[1].allocSize(10); // increase allocated size
ldl[0].reserve(15); // should increase allocated size
ldl[1].reserve(5); // should not decrease allocated size
ldl[1](3) = 2; // allocates space and sets value
// this works without a segfault, but doesn't change the list size
ldl[0][4] = 4;
ldl[1] = 3;
......@@ -78,7 +83,7 @@ int main(int argc, char *argv[])
{
dlA.append(i);
}
dlA.setSize(10);
dlA.allocSize(10);
Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
<< " " << dlA.size() << "/" << dlA.allocSize() << endl;
......@@ -95,7 +100,6 @@ int main(int argc, char *argv[])
Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
<< " " << dlB.size() << "/" << dlB.allocSize() << endl;
return 0;
}
......
......@@ -118,18 +118,30 @@ public:
//- Size of the underlying storage.
inline label allocSize() const;
// Edit
//- Alter the list size.
// When the new size is greater than the addressed list size, the
//- Alter the size of the underlying storage.
// The addressed size will be truncated if needed to fit, but will
// otherwise remain untouched.
inline void allocSize(const label);
// CURRENT BEHAVIOUR
//- When the new size is greater than the addressed list size, the
// allocated list sizes is adjusted and the
// addressed size does not change.
// Otherwise the addressed list size is just reduced and the
// allocated size does not change.
//
// PROPOSED BEHAVIOUR
//- Alter the addressed list size.
// New space will be allocated if required.
inline void setSize(const label);
//- Clear the list, i.e. set the size to zero.
//- Reserve allocation space for at least this size.
// Never shrinks the allocated size, use allocSize() for that.
inline void reserve(const label);
//- Clear the addressed list, i.e. set the size to zero.
// Allocated size does not change
inline void clear();
......
......@@ -70,12 +70,61 @@ const
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::allocSize
(
const label s
)
{
label nextFree = List<T>::size();
allocSize_ = s;
// truncate addressed size too?
if (nextFree > allocSize_)
{
nextFree = allocSize_;
}
// adjust allocated size, and addressed size if necessary
List<T>::setSize(allocSize_);
List<T>::size(nextFree);
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::reserve
(
const label s
)
{
if (s > allocSize_)
{
allocSize_ = max
(
s,
label(SizeMult*allocSize_/SizeDiv + SizeInc)
);
// adjust allocated size, leave addressed size untouched
label nextFree = List<T>::size();
List<T>::setSize(allocSize_);
List<T>::size(nextFree);
}
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
(
const label s
)
{
#if 1
// CURRENT BEHAVIOUR:
// slightly ambiguous about what size the list will actually get
// cannot increase the size of the addressed list (for compatibility
// with List), without automatically adjusting the allocated space!
label nextFree = List<T>::size();
if (s <= nextFree)
{
......@@ -89,6 +138,22 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
List<T>::setSize(allocSize_);
}
List<T>::size(nextFree);
#else
// allocate more space?
if (s > allocSize_)
{
allocSize_ = max
(
s,
label(SizeMult*allocSize_/SizeDiv + SizeInc)
);
List<T>::setSize(allocSize_);
}
// adjust addressed size
List<T>::size(s);
#endif
}
......@@ -153,21 +218,10 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append(const T& e)
// Work on copy free index since gets overwritten by setSize
label nextFree = List<T>::size();
nextFree++;
reserve(nextFree+1);
List<T>::size(nextFree+1);
if (nextFree > allocSize_)
{
allocSize_ = max
(
nextFree,
label(SizeMult*allocSize_/SizeDiv + SizeInc)
);
List<T>::setSize(allocSize_);
}
List<T>::size(nextFree);
this->operator[](nextFree - 1) = e;
this->operator[](nextFree) = e;
}
......@@ -201,19 +255,9 @@ inline T& Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator()
)
{
label nextFree = List<T>::size();
nextFree = max(nextFree, i + 1);
if (nextFree > allocSize_)
{
allocSize_ = max
(
nextFree,
label(SizeMult*allocSize_/SizeDiv + SizeInc)
);
List<T>::setSize(allocSize_);
}
reserve(nextFree);
List<T>::size(nextFree);
return this->operator[](i);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment