Commit 7b769b50 authored by Mark Olesen's avatar Mark Olesen
Browse files

added C++0x-style cbegin(), cend() iterator methods

- added STL-compatible resize() method.
  Should this be the primary entry point?

- made [DS]LListBase end iterators private
parent 19fc7954
......@@ -384,6 +384,12 @@ public:
};
//- const_iterator set to the beginning of the HashTable
inline const_iterator cbegin() const;
//- const_iterator set to beyond the end of the HashTable
inline const const_iterator& cend() const;
//- const_iterator set to the beginning of the HashTable
inline const_iterator begin() const;
......
......@@ -452,7 +452,7 @@ const Key& Foam::HashTable<T, Key, Hash>::const_iterator::key()
template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::begin() const
Foam::HashTable<T, Key, Hash>::cbegin() const
{
label i = 0;
......@@ -477,6 +477,22 @@ Foam::HashTable<T, Key, Hash>::begin() const
}
template<class T, class Key, class Hash>
inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
Foam::HashTable<T, Key, Hash>::cend() const
{
return HashTable<T, Key, Hash>::endConstIter_;
}
template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::begin() const
{
return this->cbegin();
}
template<class T, class Key, class Hash>
inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
Foam::HashTable<T, Key, Hash>::end() const
......
......@@ -317,19 +317,24 @@ public:
};
//- iterator set to the begining of the StaticHashTable
//- iterator set to the beginning of the StaticHashTable
inline iterator begin();
//- iterator set to beyond the end of the StaticHashTable
inline const iterator& end();
//- const_iterator set to the begining of the StaticHashTable
//- const_iterator set to the beginning of the StaticHashTable
inline const_iterator cbegin() const;
//- const_iterator set to beyond the end of the StaticHashTable
inline const const_iterator& cend() const;
//- const_iterator set to the beginning of the StaticHashTable
inline const_iterator begin() const;
//- const_iterator set to beyond the end of the StaticHashTable
inline const const_iterator& end() const;
// IOstream Operator
friend Istream& operator>> <T, Key, Hash>
......
......@@ -355,7 +355,7 @@ Foam::StaticHashTable<T, Key, Hash>::end()
template<class T, class Key, class Hash>
inline typename Foam::StaticHashTable<T, Key, Hash>::const_iterator
Foam::StaticHashTable<T, Key, Hash>::begin() const
Foam::StaticHashTable<T, Key, Hash>::cbegin() const
{
// Find first non-empty entry
forAll(keys_, hashIdx)
......@@ -377,6 +377,22 @@ Foam::StaticHashTable<T, Key, Hash>::begin() const
}
template<class T, class Key, class Hash>
inline const typename Foam::StaticHashTable<T, Key, Hash>::const_iterator&
Foam::StaticHashTable<T, Key, Hash>::cend() const
{
return StaticHashTable<T, Key, Hash>::endConstIter_;
}
template<class T, class Key, class Hash>
inline typename Foam::StaticHashTable<T, Key, Hash>::const_iterator
Foam::StaticHashTable<T, Key, Hash>::begin() const
{
return this->cbegin();
}
template<class T, class Key, class Hash>
inline const typename Foam::StaticHashTable<T, Key, Hash>::const_iterator&
Foam::StaticHashTable<T, Key, Hash>::end() const
......
......@@ -30,14 +30,14 @@ License
#include "IOstreams.H"
#include "long.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
Foam::DLListBase::iterator Foam::DLListBase::endIter
Foam::DLListBase::iterator Foam::DLListBase::endIter_
(
const_cast<DLListBase&>(static_cast<const DLListBase&>(DLListBase()))
);
Foam::DLListBase::const_iterator Foam::DLListBase::endConstIter
Foam::DLListBase::const_iterator Foam::DLListBase::endConstIter_
(
static_cast<const DLListBase&>(DLListBase()),
reinterpret_cast<const link*>(0)
......
......@@ -176,7 +176,7 @@ public:
// STL iterator
//- An STL iterator
//- An STL-conforming iterator
class iterator
{
friend class DLListBase;
......@@ -193,16 +193,17 @@ public:
//- Copy of the link
link curLink_;
// Private Member Functions
//- Construct for a given SLListBase with NULL element and link.
// Only used to create endIter
inline iterator(DLListBase&);
public:
//- Construct for a given DLListBase and link
inline iterator(DLListBase&, link*);
//- Construct for a given DLListBase
// setting element and link to NULL.
// Only used to create endIter
inline iterator(DLListBase&);
// Member operators
inline void operator=(const iterator&);
......@@ -217,16 +218,12 @@ public:
};
inline iterator begin();
//- iterator returned by end()
static iterator endIter;
inline const iterator& end();
// STL const_iterator
//- An STL const_iterator
//- An STL-conforming const_iterator
class const_iterator
{
// Private data
......@@ -258,12 +255,20 @@ public:
inline const_iterator operator++(int);
};
inline const_iterator cbegin() const;
inline const const_iterator& cend() const;
inline const_iterator begin() const;
inline const const_iterator& end() const;
private:
//- iterator returned by end()
static iterator endIter_;
//- const_iterator returned by end()
static const_iterator endConstIter;
static const_iterator endConstIter_;
inline const const_iterator& end() const;
};
......
......@@ -259,14 +259,14 @@ Foam::DLListBase::begin()
}
else
{
return endIter;
return endIter_;
}
}
inline const Foam::DLListBase::iterator& Foam::DLListBase::end()
{
return endIter;
return endIter_;
}
......@@ -350,7 +350,7 @@ Foam::DLListBase::const_iterator::operator++(int)
inline Foam::DLListBase::const_iterator
Foam::DLListBase::begin() const
Foam::DLListBase::cbegin() const
{
if (size())
{
......@@ -358,15 +358,29 @@ Foam::DLListBase::begin() const
}
else
{
return endConstIter;
return endConstIter_;
}
}
inline const Foam::DLListBase::const_iterator&
Foam::DLListBase::cend() const
{
return endConstIter_;
}
inline Foam::DLListBase::const_iterator
Foam::DLListBase::begin() const
{
return this->cbegin();
}
inline const Foam::DLListBase::const_iterator&
Foam::DLListBase::end() const
{
return endConstIter;
return endConstIter_;
}
......
......@@ -29,12 +29,12 @@ License
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::SLListBase::iterator Foam::SLListBase::endIter
Foam::SLListBase::iterator Foam::SLListBase::endIter_
(
const_cast<SLListBase&>(static_cast<const SLListBase&>(SLListBase()))
);
Foam::SLListBase::const_iterator Foam::SLListBase::endConstIter
Foam::SLListBase::const_iterator Foam::SLListBase::endConstIter_
(
static_cast<const SLListBase&>(SLListBase()),
reinterpret_cast<const link*>(0)
......
......@@ -178,16 +178,16 @@ public:
//- Copy of the link
link curLink_;
public:
// Private Member Functions
//- Construct for a given SLListBase and link
inline iterator(SLListBase&, link*);
//- Construct for a given SLListBase
// setting element and link to NULL.
//- Construct for a given SLListBase with NULL element and link.
// Only used to create endIter
inline iterator(SLListBase&);
public:
//- Construct for a given SLListBase and link
inline iterator(SLListBase&, link*);
// Member operators
......@@ -203,10 +203,6 @@ public:
};
inline iterator begin();
//- iterator returned by end()
static iterator endIter;
inline const iterator& end();
......@@ -245,12 +241,20 @@ public:
inline const_iterator operator++(int);
};
inline const_iterator cbegin() const;
inline const const_iterator& cend() const;
inline const_iterator begin() const;
inline const const_iterator& end() const;
private:
//- iterator returned by end()
static iterator endIter_;
//- const_iterator returned by end()
static const_iterator endConstIter;
static const_iterator endConstIter_;
inline const const_iterator& end() const;
};
......
......@@ -231,7 +231,7 @@ Foam::SLListBase::begin()
}
else
{
return endIter;
return endIter_;
}
}
......@@ -239,7 +239,7 @@ Foam::SLListBase::begin()
inline const Foam::SLListBase::iterator&
Foam::SLListBase::end()
{
return endIter;
return endIter_;
}
......@@ -323,7 +323,7 @@ Foam::SLListBase::const_iterator::operator++(int)
inline Foam::SLListBase::const_iterator
Foam::SLListBase::begin() const
Foam::SLListBase::cbegin() const
{
if (size())
{
......@@ -331,15 +331,29 @@ Foam::SLListBase::begin() const
}
else
{
return endConstIter;
return endConstIter_;
}
}
inline const Foam::SLListBase::const_iterator&
Foam::SLListBase::cend() const
{
return endConstIter_;
}
inline Foam::SLListBase::const_iterator
Foam::SLListBase::begin() const
{
return this->cbegin();
}
inline const Foam::SLListBase::const_iterator&
Foam::SLListBase::end() const
{
return endConstIter;
return endConstIter_;
}
......
......@@ -142,12 +142,17 @@ void Foam::CompactListList<T>::setSize(const label nRows)
template<class T>
void Foam::CompactListList<T>::setSize(const label nRows, const label nData)
void Foam::CompactListList<T>::setSize
(
const label nRows,
const label nData
)
{
offsets_.setSize(nRows);
m_.setSize(nData);
}
template<class T>
void Foam::CompactListList<T>::setSize
(
......@@ -160,19 +165,6 @@ void Foam::CompactListList<T>::setSize
m_.setSize(nData, t);
}
template<class T>
Foam::labelList Foam::CompactListList<T>::sizes() const
{
labelList rowSizes(offsets_.size());
label prevOffset = 0;
forAll(offsets_, i)
{
rowSizes[i] = offsets_[i]-prevOffset;
prevOffset = offsets_[i];
}
return rowSizes;
}
template<class T>
void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes)
......@@ -189,6 +181,22 @@ void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes)
m_.setSize(sumSize);
}
template<class T>
Foam::labelList Foam::CompactListList<T>::sizes() const
{
labelList rowSizes(offsets_.size());
label prevOffset = 0;
forAll(offsets_, i)
{
rowSizes[i] = offsets_[i]-prevOffset;
prevOffset = offsets_[i];
}
return rowSizes;
}
template<class T>
void Foam::CompactListList<T>::clear()
{
......
......@@ -156,15 +156,28 @@ public:
//- Reset sizes of CompactListList and value for new elements.
void setSize(const label nRows, const label nData, const T&);
//- Return sizes (to be used e.g. for construction)
labelList sizes() const;
//- Reset size of CompactListList.
void setSize(const UList<label>& rowSizes);
//- Reset size of CompactListList.
// This form only allows contraction of the CompactListList.
inline void resize(const label nRows);
//- Reset size of CompactListList.
inline void resize(const label nRows, const label nData);
//- Reset sizes of CompactListList and value for new elements.
inline void resize(const label nRows, const label nData, const T&);
//- Reset size of CompactListList.
inline void resize(const UList<label>& rowSizes);
//- Clear the CompactListList, i.e. set sizes to zero.
void clear();
//- Return sizes (to be used e.g. for construction)
labelList sizes() const;
//- Transfer the contents of the argument CompactListList
// into this CompactListList and annull the argument list.
void transfer(CompactListList<T>&);
......
......@@ -176,6 +176,43 @@ inline Foam::Xfer<Foam::CompactListList<T> > Foam::CompactListList<T>::xfer()
}
template<class T>
inline void Foam::CompactListList<T>::resize(const label nRows)
{
this->setSize(nRows);
}
template<class T>
inline void Foam::CompactListList<T>::resize
(
const label nRows,
const label nData
)
{
this->setSize(nRows, nData);
}
template<class T>
inline void Foam::CompactListList<T>::resize
(
const label nRows,
const label nData,
const T& t
)
{
this->setSize(nRows, nData, t);
}
template<class T>
inline void Foam::CompactListList<T>::resize(const UList<label>& rowSizes)
{
this->setSize(rowSizes);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
......
......@@ -142,6 +142,15 @@ public:
//- Alter the addressed list size and fill new space with a constant.
inline void setSize(const label, const T&);
//- Alter the addressed list size.
// New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for
// setting values (as per List usage).
inline void resize(const label);
//- Alter the addressed list size and fill new space with a constant.
inline void resize(const label, const T&);
//- Reserve allocation space for at least this size.
// Never shrinks the allocated size, use setCapacity() for that.
inline void reserve(const label);
......
......@@ -207,6 +207,27 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::resize
(
const label nElem
)
{
this->setSize(nElem);
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::resize
(
const label nElem,
const T& t
)
{
this->setSize(nElem, t);
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clear()
{
......
......@@ -153,6 +153,10 @@ public:
// Edit
//- Dummy resize function
// needed to make FixedList consistent with List
inline void resize(const label);
//- Dummy setSize function
// needed to make FixedList consistent with List
inline void setSize(const label);
......@@ -227,12 +231,16 @@ public:
//- Random access iterator for traversing FixedList.
typedef const T* const_iterator;
//- Return a const_iterator to begin traversing the
// constant FixedList.
//- Return const_iterator to begin traversing the constant FixedList.
inline const_iterator cbegin() const;
//- Return const_iterator to end traversing the constant FixedList.
inline const_iterator cend() const;
//- Return const_iterator to begin traversing the constant FixedList.
inline const_iterator begin() const;