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);
This commit is contained in:
Mark Olesen
2017-10-25 08:45:05 +02:00
parent 88f6b8888c
commit 5c1ec7ecb8
9 changed files with 82 additions and 73 deletions

View File

@ -117,14 +117,14 @@ public:
//- Construct null //- Construct null
inline DynamicList(); inline DynamicList();
//- Construct given size. //- Construct an empty list with given reserve size.
explicit inline DynamicList(const label nElem); explicit inline DynamicList(const label nElem);
//- Construct with given size and value for all elements. //- Construct with given size and value for all elements.
inline DynamicList(const label nElem, const T& val); inline DynamicList(const label nElem, const T& val);
//- Construct with given size initializing all elements to zero //- 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. //- Copy construct.
inline DynamicList(const DynamicList<T, SizeMin>& lst); inline DynamicList(const DynamicList<T, SizeMin>& lst);
@ -174,10 +174,14 @@ public:
// Member Functions // 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 // Edit
@ -311,7 +315,7 @@ public:
//- Return non-const access to an element, resizing list if //- Return non-const access to an element, resizing list if
// necessary // necessary
inline T& operator()(const label elemI); inline T& operator()(const label i);
//- Assignment of all addressed entries to the given value //- Assignment of all addressed entries to the given value
inline void operator=(const T& val); inline void operator=(const T& val);

View File

@ -63,17 +63,11 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList()
template<class T, int SizeMin> template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList inline Foam::DynamicList<T, SizeMin>::DynamicList(const label nElem)
(
const label nElem
)
: :
List<T>(nElem), capacity_(0)
capacity_(nElem)
{ {
// We could also enforce sizing granularity reserve(nElem);
List<T>::size(0);
} }
@ -85,7 +79,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
) )
: :
List<T>(nElem, val), List<T>(nElem, val),
capacity_(nElem) capacity_(List<T>::size())
{} {}
@ -97,7 +91,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
) )
: :
List<T>(nElem, Zero), List<T>(nElem, Zero),
capacity_(nElem) capacity_(List<T>::size())
{} {}
@ -108,7 +102,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
) )
: :
List<T>(lst), List<T>(lst),
capacity_(lst.size()) capacity_(List<T>::size())
{} {}
@ -120,7 +114,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
) )
: :
List<T>(lst), List<T>(lst),
capacity_(lst.size()) capacity_(List<T>::size())
{} {}
@ -131,7 +125,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
) )
: :
List<T>(lst), List<T>(lst),
capacity_(lst.size()) capacity_(List<T>::size())
{} {}
@ -157,7 +151,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
) )
: :
List<T>(begIter, endIter), List<T>(begIter, endIter),
capacity_(this->size()) capacity_(List<T>::size())
{} {}
@ -168,7 +162,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
) )
: :
List<T>(lst), List<T>(lst),
capacity_(lst.size()) capacity_(List<T>::size())
{} {}
@ -179,7 +173,7 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
) )
: :
List<T>(lst), List<T>(lst),
capacity_(lst.size()) capacity_(List<T>::size())
{} {}
@ -234,8 +228,14 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, int SizeMin> template<class T, int SizeMin>
inline Foam::label Foam::DynamicList<T, SizeMin>::capacity() inline Foam::label Foam::DynamicList<T, SizeMin>::min_size() const
const {
return SizeMin;
}
template<class T, int SizeMin>
inline Foam::label Foam::DynamicList<T, SizeMin>::capacity() const
{ {
return capacity_; return capacity_;
} }
@ -730,15 +730,15 @@ inline Foam::label Foam::DynamicList<T, SizeMin>::subset
template<class T, int SizeMin> template<class T, int SizeMin>
inline T& Foam::DynamicList<T, SizeMin>::operator() 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);
} }

View File

@ -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: public:
// Static Member Functions // Static Member Functions
@ -211,10 +204,6 @@ public:
// Member Functions // Member Functions
//- Return the number of elements in the UList
inline label size() const;
// Edit // Edit
//- Alias for setSize(const label) //- Alias for setSize(const label)

View File

@ -151,29 +151,17 @@ inline void Foam::List<T>::resize(const label newSize, const T& val)
template<class T> template<class T>
inline T& Foam::List<T>::newElmt(const label i) 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); 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> template<class T>
inline Foam::Xfer<Foam::List<T>> Foam::List<T>::xfer() 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> template<class T>
inline void Foam::List<T>::append(const T& val) 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(); label nextFree = this->size();
setSize(nextFree + lst.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(); label nextFree = this->size();
setSize(nextFree + lst.size()); setSize(nextFree + lst.size());
forAll(lst, elemI) forAll(lst, i)
{ {
this->operator[](nextFree++) = lst[elemI]; this->operator[](nextFree++) = lst[i];
} }
} }

View File

@ -102,6 +102,10 @@ protected:
// Protected Member Functions // 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 //- Write the UList with its compound type
void writeEntry(Ostream& os) const; void writeEntry(Ostream& os) const;
@ -465,7 +469,7 @@ public:
friend Ostream& operator<< <T> friend Ostream& operator<< <T>
( (
Ostream& os, Ostream& os,
const UList<T>& L const UList<T>& lst
); );
//- Read List contents from Istream. //- Read List contents from Istream.

View File

@ -327,6 +327,14 @@ Foam::UList<T>::crend() const
return &v_[-1]; return &v_[-1];
} }
template<class T>
inline void Foam::UList<T>::size(const label n)
{
size_ = n;
}
template<class T> template<class T>
inline Foam::label Foam::UList<T>::size() const inline Foam::label Foam::UList<T>::size() const
{ {

View File

@ -164,9 +164,9 @@ Foam::Ostream& Foam::UList<T>::writeList
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
template<class T> 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);
} }

View File

@ -199,6 +199,10 @@ public:
//- Clear the list and delete storage. //- Clear the list and delete storage.
inline void clearStorage(); 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. //- Shrink the allocated space to the number of elements used.
// Returns a reference to the DynamicField. // Returns a reference to the DynamicField.
inline DynamicField<T, SizeMin>& shrink(); inline DynamicField<T, SizeMin>& shrink();
@ -222,7 +226,7 @@ public:
//- Return non-const access to an element, resizing list if //- Return non-const access to an element, resizing list if
// necessary // necessary
inline T& operator()(const label elemI); inline T& operator()(const label i);
//- Assignment of all addressed entries to the given value //- Assignment of all addressed entries to the given value
inline void operator=(const T& val); inline void operator=(const T& val);

View File

@ -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> template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>& inline Foam::DynamicField<T, SizeMin>&
Foam::DynamicField<T, SizeMin>::shrink() Foam::DynamicField<T, SizeMin>::shrink()
@ -317,10 +329,10 @@ Foam::DynamicField<T, SizeMin>::shrink()
label nextFree = Field<T>::size(); label nextFree = Field<T>::size();
if (capacity_ > nextFree) if (capacity_ > nextFree)
{ {
// use the full list when resizing // Use the full list when resizing
Field<T>::size(capacity_); Field<T>::size(capacity_);
// the new size // The new size
capacity_ = nextFree; capacity_ = nextFree;
Field<T>::setSize(capacity_); Field<T>::setSize(capacity_);
Field<T>::size(nextFree); Field<T>::size(nextFree);
@ -368,9 +380,9 @@ Foam::DynamicField<T, SizeMin>::append
label nextFree = List<T>::size(); label nextFree = List<T>::size();
setSize(nextFree + lst.size()); setSize(nextFree + lst.size());
forAll(lst, elemI) forAll(lst, i)
{ {
this->operator[](nextFree++) = lst[elemI]; this->operator[](nextFree++) = lst[i];
} }
return *this; return *this;
} }
@ -401,15 +413,15 @@ inline T Foam::DynamicField<T, SizeMin>::remove()
template<class T, int SizeMin> template<class T, int SizeMin>
inline T& Foam::DynamicField<T, SizeMin>::operator() 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);
} }