Commit 0e7b1351 authored by Mark Olesen's avatar Mark Olesen
Browse files

STYLE: adjust const access for linked-list iterators 'operator*'

- provides const/non-const access to the underlying list, but the
  iterator access itself is const.

- provide linked-list iterator 'found()' method for symmetry with
  hash-table iterators. Use nullptr for more clarity.
parent f73b5b62
......@@ -56,7 +56,6 @@ int main(int argc, char *argv[])
Info<< "element:" << *iter << endl;
}
Info<< nl << "And again using the same STL iterator: " << nl << endl;
forAllIters(myList, iter)
......
......@@ -254,14 +254,14 @@ public:
// Member operators
T& operator*()
T& operator*() const
{
return
static_cast<link&>
(LListBase_iterator::operator*()).obj_;
}
T& operator()()
T& operator()() const
{
return operator*();
}
......@@ -312,14 +312,14 @@ public:
// Member operators
const T& operator*()
const T& operator*() const
{
return
static_cast<const link&>
(LListBase_const_iterator::operator*()).obj_;
}
const T& operator()()
const T& operator()() const
{
return operator*();
}
......
......@@ -196,12 +196,12 @@ public:
// Member operators
T& operator*()
T& operator*() const
{
return *(LList<LListBase, T*>::iterator::operator*());
}
T& operator()()
T& operator()() const
{
return operator*();
}
......@@ -235,12 +235,12 @@ public:
// Member operators
const T& operator*()
const T& operator*() const
{
return *(LList<LListBase, T*>::const_iterator::operator*());
}
const T& operator()()
const T& operator()() const
{
return operator*();
}
......
......@@ -192,12 +192,12 @@ public:
// Member operators
T& operator*()
T& operator*() const
{
return static_cast<T&>(LListBase_iterator::operator*());
}
T& operator()()
T& operator()() const
{
return operator*();
}
......@@ -247,14 +247,14 @@ public:
// Member operators
const T& operator*()
const T& operator*() const
{
return
static_cast<const T&>
(LListBase_const_iterator::operator*());
}
const T& operator()()
const T& operator()() const
{
return operator*();
}
......@@ -309,14 +309,14 @@ public:
// Member operators
const T& operator*()
const T& operator*() const
{
return
static_cast<const T&>
(LListBase::const_reverse_iterator::operator*());
}
const T& operator()()
const T& operator()() const
{
return operator*();
}
......
......@@ -188,7 +188,7 @@ Foam::DLListBase::link* Foam::DLListBase::removeHead()
if (!first_)
{
last_ = 0;
last_ = nullptr;
}
f->deregister();
......@@ -204,8 +204,8 @@ Foam::DLListBase::link* Foam::DLListBase::remove(DLListBase::link* l)
if (l == first_ && first_ == last_)
{
first_ = 0;
last_ = 0;
first_ = nullptr;
last_ = nullptr;
}
else if (l == first_)
{
......
......@@ -25,9 +25,10 @@ Class
Foam::DLListBase
Description
Base doubly-linked list.
Base for doubly-linked lists.
SourceFiles
DLListBaseI.H
DLListBase.C
\*---------------------------------------------------------------------------*/
......@@ -84,10 +85,10 @@ private:
// Private Member Functions
//- Disallow default bitwise copy construct
DLListBase(const DLListBase&);
DLListBase(const DLListBase&) = delete;
//- Disallow default bitwise assignment
void operator=(const DLListBase&);
void operator=(const DLListBase&) = delete;
public:
......@@ -184,18 +185,14 @@ public:
friend class DLListBase;
friend class const_iterator;
// Private data
//- Reference to the list this is an iterator for
DLListBase& curList_;
//- Reference to the list this is an iterator for
DLListBase& curList_;
//- Current element
link* curElmt_;
//- Current element
link* curElmt_;
//- Copy of the link
link curLink_;
// Private Member Functions
//- Copy of the link
link curLink_;
//- Construct for a given SLListBase with nullptr element and link.
// Only used to create endIter
......@@ -206,17 +203,18 @@ public:
//- Construct for a given DLListBase and link
inline iterator(DLListBase&, link*);
// Member operators
//- Currently pointing at a valid entry
inline bool found() const;
inline void operator=(const iterator&);
inline void operator=(const iterator& iter);
inline bool operator==(const iterator&) const;
inline bool operator!=(const iterator&) const;
inline bool operator==(const iterator& iter) const;
inline bool operator!=(const iterator& iter) const;
inline link& operator*();
inline link& operator*() const;
inline iterator& operator++();
inline iterator operator++(int);
inline iterator& operator++();
inline iterator operator++(int);
};
inline iterator begin();
......@@ -228,13 +226,11 @@ public:
//- An STL-conforming const_iterator
class const_iterator
{
// Private data
//- Reference to the list this is an iterator for
const DLListBase& curList_;
//- Reference to the list this is an iterator for
const DLListBase& curList_;
//- Current element
const link* curElmt_;
//- Current element
const link* curElmt_;
public:
......@@ -242,19 +238,20 @@ public:
inline const_iterator(const DLListBase&, const link*);
//- Construct from a non-const iterator
inline const_iterator(const iterator&);
inline const_iterator(const DLListBase::iterator& iter);
// Member operators
//- Currently pointing at a valid entry
inline bool found() const;
inline void operator=(const const_iterator&);
inline void operator=(const const_iterator& iter);
inline bool operator==(const const_iterator&) const;
inline bool operator!=(const const_iterator&) const;
inline bool operator==(const const_iterator& iter) const;
inline bool operator!=(const const_iterator& iter) const;
inline const link& operator*();
inline const link& operator*() const;
inline const_iterator& operator++();
inline const_iterator operator++(int);
inline const_iterator& operator++();
inline const_iterator operator++(int);
};
inline const_iterator cbegin() const;
......@@ -269,30 +266,29 @@ public:
//- An STL-conforming const_reverse_iterator
class const_reverse_iterator
{
// Private data
//- Reference to the list this is an reverse_iterator for
const DLListBase& curList_;
//- Reference to the list this is an reverse_iterator for
const DLListBase& curList_;
//- Current element
const link* curElmt_;
//- Current element
const link* curElmt_;
public:
//- Construct for a given DLListBase and link
inline const_reverse_iterator(const DLListBase&, const link*);
inline const_reverse_iterator(const DLListBase& lst, const link*);
// Member operators
//- Currently pointing at a valid entry
inline bool found() const;
inline void operator=(const const_reverse_iterator&);
inline void operator=(const const_reverse_iterator& iter);
inline bool operator==(const const_reverse_iterator&) const;
inline bool operator!=(const const_reverse_iterator&) const;
inline bool operator==(const const_reverse_iterator& iter) const;
inline bool operator!=(const const_reverse_iterator& iter) const;
inline const link& operator*();
inline const link& operator*() const;
inline const_reverse_iterator& operator++();
inline const_reverse_iterator operator++(int);
inline const_reverse_iterator& operator++();
inline const_reverse_iterator operator++(int);
};
inline const_reverse_iterator crbegin() const;
......
......@@ -29,15 +29,15 @@ License
inline Foam::DLListBase::link::link()
:
prev_(0),
next_(0)
prev_(nullptr),
next_(nullptr)
{}
inline Foam::DLListBase::DLListBase()
:
first_(0),
last_(0),
first_(nullptr),
last_(nullptr),
nElmts_(0)
{}
......@@ -63,14 +63,14 @@ inline Foam::DLListBase::~DLListBase()
inline bool Foam::DLListBase::link::registered() const
{
return prev_ != 0 && next_ != 0;
return prev_ != nullptr && next_ != nullptr;
}
inline void Foam::DLListBase::link::deregister()
{
prev_ = 0;
next_ = 0;
prev_ = nullptr;
next_ = nullptr;
}
......@@ -140,8 +140,8 @@ Foam::DLListBase::last() const
inline void Foam::DLListBase::clear()
{
first_ = 0;
last_ = 0;
first_ = nullptr;
last_ = nullptr;
nElmts_ = 0;
}
......@@ -195,6 +195,12 @@ inline Foam::DLListBase::iterator::iterator(DLListBase& s)
{}
inline bool Foam::DLListBase::iterator::found() const
{
return (curElmt_ != nullptr);
}
inline void Foam::DLListBase::iterator::operator=(const iterator& iter)
{
curElmt_ = iter.curElmt_;
......@@ -215,7 +221,7 @@ inline bool Foam::DLListBase::iterator::operator!=(const iterator& iter) const
inline Foam::DLListBase::link&
Foam::DLListBase::iterator::operator*()
Foam::DLListBase::iterator::operator*() const
{
return *curElmt_;
}
......@@ -226,9 +232,9 @@ Foam::DLListBase::iterator::operator++()
{
// Check if the curElmt_ is the last element (if it points to itself)
// or if the list is empty because the last element may have been removed
if (curLink_.next_ == curElmt_ || curList_.last_ == 0)
if (curLink_.next_ == curElmt_ || curList_.last_ == nullptr)
{
curElmt_ = 0;
curElmt_ = nullptr;
}
else
{
......@@ -282,13 +288,22 @@ inline Foam::DLListBase::const_iterator::const_iterator
{}
inline Foam::DLListBase::const_iterator::const_iterator(const iterator& iter)
inline Foam::DLListBase::const_iterator::const_iterator
(
const DLListBase::iterator& iter
)
:
curList_(iter.curList_),
curElmt_(iter.curElmt_)
{}
inline bool Foam::DLListBase::const_iterator::found() const
{
return (curElmt_ != nullptr);
}
inline void Foam::DLListBase::const_iterator::operator=
(
const const_iterator& iter
......@@ -317,7 +332,7 @@ inline bool Foam::DLListBase::const_iterator::operator!=
inline const Foam::DLListBase::link&
Foam::DLListBase::const_iterator::operator*()
Foam::DLListBase::const_iterator::operator*() const
{
return *curElmt_;
}
......@@ -328,7 +343,7 @@ Foam::DLListBase::const_iterator::operator++()
{
if (curElmt_ == curList_.last_)
{
curElmt_ = 0;
curElmt_ = nullptr;
}
else
{
......@@ -387,15 +402,21 @@ Foam::DLListBase::end() const
inline Foam::DLListBase::const_reverse_iterator::const_reverse_iterator
(
const DLListBase& s,
const DLListBase& lst,
const link* elmt
)
:
curList_(s),
curList_(lst),
curElmt_(elmt)
{}
inline bool Foam::DLListBase::const_reverse_iterator::found() const
{
return (curElmt_ != nullptr);
}
inline void Foam::DLListBase::const_reverse_iterator::operator=
(
const const_reverse_iterator& iter
......@@ -424,7 +445,7 @@ inline bool Foam::DLListBase::const_reverse_iterator::operator!=
inline const Foam::DLListBase::link&
Foam::DLListBase::const_reverse_iterator::operator*()
Foam::DLListBase::const_reverse_iterator::operator*() const
{
return *curElmt_;
}
......@@ -435,7 +456,7 @@ Foam::DLListBase::const_reverse_iterator::operator++()
{
if (curElmt_ == curList_.first_)
{
curElmt_ = 0;
curElmt_ = nullptr;
}
else
{
......@@ -460,7 +481,7 @@ Foam::DLListBase::crbegin() const
{
if (size())
{
return const_reverse_iterator(*this, last());
return const_reverse_iterator(*this, this->last());
}
else
{
......
......@@ -79,7 +79,7 @@ Foam::SLListBase::link* Foam::SLListBase::removeHead()
{
nElmts_--;
if (last_ == 0)
if (last_ == nullptr)
{
FatalErrorInFunction
<< "remove from empty list"
......@@ -90,7 +90,7 @@ Foam::SLListBase::link* Foam::SLListBase::removeHead()
if (f == last_)
{
last_ = 0;
last_ = nullptr;
}
else
{
......@@ -132,7 +132,7 @@ Foam::SLListBase::link* Foam::SLListBase::remove(SLListBase::link* it)
prev = p;
}
return 0;
return nullptr;
}
......
......@@ -25,9 +25,10 @@ Class
Foam::SLListBase
Description
Base singly-linked list.
Base for singly-linked lists.
SourceFiles
SLListBaseI.H
SLListBase.C
\*---------------------------------------------------------------------------*/
......@@ -81,10 +82,10 @@ private:
// Private Member Functions
//- Disallow default bitwise copy construct
SLListBase(const SLListBase&);
SLListBase(const SLListBase&) = delete;
//- Disallow default bitwise assignment
void operator=(const SLListBase&);
void operator=(const SLListBase&) = delete;
public:
......@@ -166,18 +167,14 @@ public:
friend class SLListBase;
friend class const_iterator;
// Private data
//- Reference to the list this is an iterator for
SLListBase& curList_;
//- Reference to the list this is an iterator for
SLListBase& curList_;
//- Current element
link* curElmt_;
//- Current element
link* curElmt_;
//- Copy of the link
link curLink_;
// Private Member Functions
//- Copy of the link
link curLink_;
//- Construct for a given SLListBase with nullptr element and link.
// Only used to create endIter
......@@ -188,17 +185,18 @@ public:
//- Construct for a given SLListBase and link
inline iterator(SLListBase&, link*);
// Member operators
//- Currently pointing at a valid entry
inline bool found() const;
inline void operator=(const iterator&);
inline void operator=(const iterator& iter);
inline bool operator==(const iterator&) const;
inline bool operator!=(const iterator&) const;
inline bool operator==(const iterator& iter) const;
inline bool operator!=(const iterator& iter) const;
inline link& operator*();
inline link& operator*() const;
inline iterator& operator++();
inline iterator operator++(int);
inline iterator& operator++();
inline iterator operator++(int);
};
inline iterator begin();
......@@ -210,13 +208,11 @@ public:
//- An STL-conforming const_iterator
class const_iterator
{
// Private data
//- Reference to the list this is an iterator for
const SLListBase& curList_;
//- Reference to the list this is an iterator for
const SLListBase& curList_;
//- Current element
const link* curElmt_;
//- Current element
const link* curElmt_;
public:
......@@ -224,20 +220,20 @@ public:
inline const_iterator(const SLListBase&, const link*);
//- Construct from a non-const iterator
inline const_iterator(const iterator&);
inline const_iterator(const SLListBase::iterator& iter);