Library: fix a suptle misconception in the design of IterAdapter
again surprising how such fundamental bugs can hide for years... Here the reason is that IterAdapter leaves the representation of "NIL" to its instantiation / users; some users (here in for example the ScopedCollection) can choose to allow for different representations of "NIL", but the comparison provided by IterAdapter just compares the embedded pos by face value.
This commit is contained in:
parent
908d1a8faa
commit
0b621e71c5
4 changed files with 52 additions and 4 deletions
|
|
@ -240,7 +240,9 @@ namespace lib {
|
|||
{
|
||||
if (check())
|
||||
iterNext (source_,pos_); // extension point: free function iterNext(...)
|
||||
}
|
||||
check();
|
||||
} // checkPoint() might mark end condition
|
||||
// for comparison with IterAdapter{}
|
||||
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -470,8 +470,8 @@ namespace lib {
|
|||
|
||||
|
||||
|
||||
typedef IterAdapter< I *, const ScopedCollection *> iterator;
|
||||
typedef IterAdapter<const I *, const ScopedCollection *> const_iterator;
|
||||
using iterator = IterAdapter< I *, const ScopedCollection *>;
|
||||
using const_iterator = IterAdapter<const I *, const ScopedCollection *>;
|
||||
|
||||
|
||||
iterator begin() { return iterator (this, _access_begin()); }
|
||||
|
|
|
|||
|
|
@ -78,7 +78,6 @@ namespace test{
|
|||
iterator end() { return iterator(); }
|
||||
const_iterator begin() const { return const_iterator(data_.begin(),data_.end()); }
|
||||
const_iterator end() const { return const_iterator(); }
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -130,6 +129,8 @@ namespace test{
|
|||
iterator end () { return iterator(); }
|
||||
const_iterator end () const { return const_iterator(); }
|
||||
|
||||
size_t size() const { return numberz_.size(); }
|
||||
|
||||
|
||||
|
||||
protected: /* ==== API for the IterAdapter ==== */
|
||||
|
|
@ -351,6 +352,36 @@ namespace test{
|
|||
|
||||
// *iter = i+1; ///////////TODO this should be const, but it isn't
|
||||
}
|
||||
|
||||
|
||||
//---- verify support for C++11 element iteration
|
||||
i = 0;
|
||||
for (auto& elm : elms) // NOTE: TestContainer exposes pointers
|
||||
{
|
||||
++elm; // can indeed modify contents
|
||||
--elm;
|
||||
CHECK (*elm == i);
|
||||
++i;
|
||||
}
|
||||
CHECK (size_t(i) == elms.size());
|
||||
|
||||
i = 0;
|
||||
for (auto const& elm : elms)
|
||||
{
|
||||
CHECK (*elm == i);
|
||||
// ++elm; // can not modify contents
|
||||
++i;
|
||||
}
|
||||
CHECK (size_t(i) == elms.size());
|
||||
|
||||
i = 0;
|
||||
for (auto const& elm : const_elms)
|
||||
{
|
||||
CHECK (*elm == i);
|
||||
// ++elm; // can not modify contents
|
||||
++i;
|
||||
}
|
||||
CHECK (size_t(i) == elms.size());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -182,6 +182,21 @@ namespace test{
|
|||
}
|
||||
|
||||
|
||||
// Test c++11 foreach iteration
|
||||
check = 0;
|
||||
for (auto& entry : coll)
|
||||
{
|
||||
CHECK (check == entry.getVal());
|
||||
++check;
|
||||
}
|
||||
check = 0;
|
||||
for (auto const& entry : const_coll)
|
||||
{
|
||||
CHECK (check == entry.getVal());
|
||||
++check;
|
||||
}
|
||||
|
||||
|
||||
// Verify correct behaviour of iteration end
|
||||
CHECK (! (coll.end()));
|
||||
CHECK (isnil (coll.end()));
|
||||
|
|
|
|||
Loading…
Reference in a new issue