PackedList iterator bugfix

- compare iteratorBase == iteratorBase by value, not position
  thus this works
      list[a] == list[b] ...

- compare iterator == iteratorBase and const_iterator == iteratorBase
  by position, not value. The inheritance rules means that this works:
      iter == list.end() ...
  this will compare positions:
      iter == list[5];
  Of course, this will still compare values:
      *iter == list[5];
This commit is contained in:
Mark Olesen
2009-02-27 13:43:43 +01:00
parent 576d9388f0
commit dbc9b7427a
3 changed files with 99 additions and 5 deletions

View File

@ -73,6 +73,50 @@ int main(int argc, char *argv[])
list1[1] = list1[8] = list1[10] = list1[14] = 2; list1[1] = list1[8] = list1[10] = list1[14] = 2;
list1.print(Info); list1.print(Info);
Info<< "\ntest operator== between references\n";
if (list1[1] == list1[8])
{
Info<< "[1] == [8] (expected)\n";
}
else
{
Info<< "[1] != [8] (unexpected)\n";
}
if (list1[0] != list1[1])
{
Info<< "[0] != [1] (expected)\n";
}
else
{
Info<< "[0] == [1] (unexpected)\n";
}
Info<< "\ntest operator== with iterator\n";
{
PackedList<3>::iterator iter = list1[1];
if (iter != list1[8])
{
Info<< "iter != [8] (expected)\n";
}
else
{
Info<< "iter == [8] (unexpected)\n";
}
if (*iter != list1[8])
{
Info<< "*iter != [8] (unexpected)\n";
}
else
{
Info<< "*iter == [8] (expected)\n";
}
}
{ {
const PackedList<3>& constLst = list1; const PackedList<3>& constLst = list1;
Info<< "\ntest operator[] const with out-of-range index\n"; Info<< "\ntest operator[] const with out-of-range index\n";

View File

@ -330,7 +330,7 @@ public:
// Member Operators // Member Operators
//- Compare positions //- Compare values (not positions)
inline bool operator==(const iteratorBase&) const; inline bool operator==(const iteratorBase&) const;
inline bool operator!=(const iteratorBase&) const; inline bool operator!=(const iteratorBase&) const;
@ -380,6 +380,10 @@ public:
// Member Operators // Member Operators
//- Compare positions (not values)
inline bool operator==(const iteratorBase&) const;
inline bool operator!=(const iteratorBase&) const;
//- Assign from iteratorBase, eg iter = packedlist[i] //- Assign from iteratorBase, eg iter = packedlist[i]
// An out-of-range iterator is assigned end() // An out-of-range iterator is assigned end()
inline iterator& operator=(const iteratorBase&); inline iterator& operator=(const iteratorBase&);
@ -425,17 +429,21 @@ public:
//- Construct from iterator base, eg iter(packedlist[i]) //- Construct from iterator base, eg iter(packedlist[i])
// but also "const_iterator iter = packedlist[i];" // but also "const_iterator iter = packedlist[i];"
// An out-of-range iterator is assigned end() // An out-of-range iterator is assigned cend()
inline const_iterator(const iteratorBase&); inline const_iterator(const iteratorBase&);
//- Construct from base list and position index //- Construct from base list and position index
inline const_iterator(const PackedList*, const label); inline const_iterator(const PackedList*, const label);
//- Construct from non-const iterator //- Construct from iterator
inline const_iterator(const iterator&); inline const_iterator(const iterator&);
// Member operators // Member operators
//- Compare positions (not values)
inline bool operator==(const iteratorBase&) const;
inline bool operator!=(const iteratorBase&) const;
//- Assign from iteratorBase or derived //- Assign from iteratorBase or derived
// eg, iter = packedlist[i] or even iter = list.begin() // eg, iter = packedlist[i] or even iter = list.begin()
inline const_iterator& operator=(const iteratorBase&); inline const_iterator& operator=(const iteratorBase&);

View File

@ -176,7 +176,7 @@ inline bool Foam::PackedList<nBits>::iteratorBase::operator==
const iteratorBase& iter const iteratorBase& iter
) const ) const
{ {
return this->index_ == iter.index_; return this->get() == iter.get();
} }
@ -186,7 +186,7 @@ inline bool Foam::PackedList<nBits>::iteratorBase::operator!=
const iteratorBase& iter const iteratorBase& iter
) const ) const
{ {
return this->index_ != iter.index_; return this->get() != iter.get();
} }
@ -311,6 +311,48 @@ inline Foam::PackedList<nBits>::const_iterator::const_iterator
{} {}
template<unsigned nBits>
inline bool Foam::PackedList<nBits>::iterator::operator==
(
const iteratorBase& iter
) const
{
return this->index_ == iter.index_;
}
template<unsigned nBits>
inline bool Foam::PackedList<nBits>::iterator::operator!=
(
const iteratorBase& iter
) const
{
return this->index_ != iter.index_;
}
template<unsigned nBits>
inline bool Foam::PackedList<nBits>::const_iterator::operator==
(
const iteratorBase& iter
) const
{
return this->index_ == iter.index_;
}
template<unsigned nBits>
inline bool Foam::PackedList<nBits>::const_iterator::operator!=
(
const iteratorBase& iter
) const
{
return this->index_ != iter.index_;
}
template<unsigned nBits> template<unsigned nBits>
inline typename Foam::PackedList<nBits>::iterator& inline typename Foam::PackedList<nBits>::iterator&
Foam::PackedList<nBits>::iterator::operator=(const iteratorBase& iter) Foam::PackedList<nBits>::iterator::operator=(const iteratorBase& iter)