Commit 5c1ec7ec authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: relocate protected List::size(label) to UList (issue #595)

- makes it accessible for containers that manage their own storage
  and derive directly from UList.

- DynamicList::min_size() method to access the corresponding
  SizeMin template parameter.

- ensure consistency in the reserve size for the constructor

      DynamicList<..> lst(N);

  now has identical sizing as

      DynamicList<..> lst();
      reserve(N);
parent 88f6b888
......@@ -117,14 +117,14 @@ public:
//- Construct null
inline DynamicList();
//- Construct given size.
//- Construct an empty list with given reserve size.
explicit inline DynamicList(const label nElem);
//- Construct with given size and value for all elements.
inline DynamicList(const label nElem, const T& val);
//- Construct with given size initializing all elements to zero
inline DynamicList(const label s, const zero);
inline DynamicList(const label nElem, const zero);
//- Copy construct.
inline DynamicList(const DynamicList<T, SizeMin>& lst);
......@@ -174,10 +174,14 @@ public:
// Member Functions
// Access
// Access
//- Normal lower capacity limit - the SizeMin template parameter
inline label min_size() const;
//- Size of the underlying storage.
inline label capacity() const;
//- Size of the underlying storage.
inline label capacity() const;
// Edit
......@@ -311,7 +315,7 @@ public:
//- Return non-const access to an element, resizing list if
// necessary
inline T& operator()(const label elemI);
inline T& operator()(const label i);
//- Assignment of all addressed entries to the given value
inline void operator=(const T& val);
......
......@@ -63,17 +63,11 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList()
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList
(
const label nElem
)
inline Foam::DynamicList<T, SizeMin>::DynamicList(const label nElem)
:
List<T>(nElem),
capacity_(nElem)
capacity_(0)
{
// We could also enforce sizing granularity
List<T>::size(0);
reserve(nElem);
}
......@@ -85,7 +79,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
)
:
List<T>(nElem, val),
capacity_(nElem)
capacity_(List<T>::size())
{}
......@@ -97,7 +91,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
)
:
List<T>(nElem, Zero),
capacity_(nElem)
capacity_(List<T>::size())
{}
......@@ -108,7 +102,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
)
:
List<T>(lst),
capacity_(lst.size())
capacity_(List<T>::size())
{}
......@@ -120,7 +114,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
)
:
List<T>(lst),
capacity_(lst.size())
capacity_(List<T>::size())
{}
......@@ -131,7 +125,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
)
:
List<T>(lst),
capacity_(lst.size())
capacity_(List<T>::size())
{}
......@@ -157,7 +151,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
)
:
List<T>(begIter, endIter),
capacity_(this->size())
capacity_(List<T>::size())
{}
......@@ -168,7 +162,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
)
:
List<T>(lst),
capacity_(lst.size())
capacity_(List<T>::size())
{}
......@@ -179,7 +173,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
)
:
List<T>(lst),
capacity_(lst.size())
capacity_(List<T>::size())
{}
......@@ -234,8 +228,14 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, int SizeMin>
inline Foam::label Foam::DynamicList<T, SizeMin>::capacity()
const
inline Foam::label Foam::DynamicList<T, SizeMin>::min_size() const
{
return SizeMin;
}
template<class T, int SizeMin>
inline Foam::label Foam::DynamicList<T, SizeMin>::capacity() const
{
return capacity_;
}
......@@ -730,15 +730,15 @@ inline Foam::label Foam::DynamicList<T, SizeMin>::subset
template<class T, int SizeMin>
inline T& Foam::DynamicList<T, SizeMin>::operator()
(
const label elemI
const label i
)
{
if (elemI >= List<T>::size())
if (i >= List<T>::size())
{
setSize(elemI + 1);
setSize(i + 1);
}
return this->operator[](elemI);
return this->operator[](i);
}
......
......@@ -111,13 +111,6 @@ class List
);
protected:
//- Override size to be inconsistent with allocated storage.
// Use with care
inline void size(const label n);
public:
// Static Member Functions
......@@ -211,10 +204,6 @@ public:
// Member Functions
//- Return the number of elements in the UList
inline label size() const;
// Edit
//- Alias for setSize(const label)
......
......@@ -151,29 +151,17 @@ inline void Foam::List<T>::resize(const label newSize, const T& val)
template<class T>
inline T& Foam::List<T>::newElmt(const label i)
{
if (i >= this->size())
const label n = this->size();
if (i >= n)
{
setSize(2*this->size());
setSize(2*n);
}
return UList<T>::operator[](i);
}
template<class T>
inline void Foam::List<T>::size(const label n)
{
UList<T>::size_ = n;
}
template<class T>
inline Foam::label Foam::List<T>::size() const
{
return UList<T>::size_;
}
template<class T>
inline Foam::Xfer<Foam::List<T>> Foam::List<T>::xfer()
{
......@@ -184,7 +172,7 @@ inline Foam::Xfer<Foam::List<T>> Foam::List<T>::xfer()
template<class T>
inline void Foam::List<T>::append(const T& val)
{
setSize(size()+1, val);
setSize(this->size()+1, val);
}
......@@ -200,9 +188,9 @@ inline void Foam::List<T>::append(const UList<T>& lst)
label nextFree = this->size();
setSize(nextFree + lst.size());
forAll(lst, elemI)
forAll(lst, i)
{
this->operator[](nextFree++) = lst[elemI];
this->operator[](nextFree++) = lst[i];
}
}
......@@ -213,9 +201,9 @@ inline void Foam::List<T>::append(const UIndirectList<T>& lst)
label nextFree = this->size();
setSize(nextFree + lst.size());
forAll(lst, elemI)
forAll(lst, i)
{
this->operator[](nextFree++) = lst[elemI];
this->operator[](nextFree++) = lst[i];
}
}
......
......@@ -102,6 +102,10 @@ protected:
// Protected Member Functions
//- Override size to be inconsistent with allocated storage.
// Use with care
inline void size(const label n);
//- Write the UList with its compound type
void writeEntry(Ostream& os) const;
......@@ -465,7 +469,7 @@ public:
friend Ostream& operator<< <T>
(
Ostream& os,
const UList<T>& L
const UList<T>& lst
);
//- Read List contents from Istream.
......
......@@ -327,6 +327,14 @@ Foam::UList<T>::crend() const
return &v_[-1];
}
template<class T>
inline void Foam::UList<T>::size(const label n)
{
size_ = n;
}
template<class T>
inline Foam::label Foam::UList<T>::size() const
{
......
......@@ -164,9 +164,9 @@ Foam::Ostream& Foam::UList<T>::writeList
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
template<class T>
Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
Foam::Ostream& Foam::operator<<(Ostream& os, const UList<T>& lst)
{
return L.writeList(os, 10);
return lst.writeList(os, 10);
}
......
......@@ -199,6 +199,10 @@ public:
//- Clear the list and delete storage.
inline void clearStorage();
//- Expand the addressable size to fit the allocated capacity.
// Returns the previous addressable size.
inline label expandStorage();
//- Shrink the allocated space to the number of elements used.
// Returns a reference to the DynamicField.
inline DynamicField<T, SizeMin>& shrink();
......@@ -222,7 +226,7 @@ public:
//- Return non-const access to an element, resizing list if
// necessary
inline T& operator()(const label elemI);
inline T& operator()(const label i);
//- Assignment of all addressed entries to the given value
inline void operator=(const T& val);
......
......@@ -310,6 +310,18 @@ inline void Foam::DynamicField<T, SizeMin>::clearStorage()
}
template<class T, int SizeMin>
inline Foam::label Foam::DynamicField<T, SizeMin>::expandStorage()
{
const label nextFree = Field<T>::size();
// Allow addressing into the entire list
Field<T>::size(capacity_);
return nextFree;
}
template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>&
Foam::DynamicField<T, SizeMin>::shrink()
......@@ -317,10 +329,10 @@ Foam::DynamicField<T, SizeMin>::shrink()
label nextFree = Field<T>::size();
if (capacity_ > nextFree)
{
// use the full list when resizing
// Use the full list when resizing
Field<T>::size(capacity_);
// the new size
// The new size
capacity_ = nextFree;
Field<T>::setSize(capacity_);
Field<T>::size(nextFree);
......@@ -368,9 +380,9 @@ Foam::DynamicField<T, SizeMin>::append
label nextFree = List<T>::size();
setSize(nextFree + lst.size());
forAll(lst, elemI)
forAll(lst, i)
{
this->operator[](nextFree++) = lst[elemI];
this->operator[](nextFree++) = lst[i];
}
return *this;
}
......@@ -401,15 +413,15 @@ inline T Foam::DynamicField<T, SizeMin>::remove()
template<class T, int SizeMin>
inline T& Foam::DynamicField<T, SizeMin>::operator()
(
const label elemI
const label i
)
{
if (elemI >= Field<T>::size())
if (i >= Field<T>::size())
{
setSize(elemI + 1);
setSize(i + 1);
}
return this->operator[](elemI);
return this->operator[](i);
}
......
Markdown is supported
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