Opened 13 years ago

Closed 13 years ago

#321 closed enhancement (fixed)

Comparison of Iterators

Reported by: Peter Owned by: Jari Häkkinen
Priority: major Milestone: yat 0.4
Component: utility Version:
Keywords: Cc:

Description

Comparisons such as operator== in Iterator are dangerous.

The reason is that is that == returns false if container_ is not the same. When working with lookups such as DataLookup1D this could give quite surprising results. For example I have seen code like this

some_function_taking_a_range(DataLookup(data,i).begin(), DataLookup(data,i).end());

and it will not work as expected.

The simple solution is to ignore container_ in Iterator for all comparisons. This is probably better but not optimal since two DataLookup1D::begin() will always be equal. But when is it useful to compare Iterators from different containers.

A better solution would be to work on underlying pointer. If the pointer does exist, i.e., it is not a proxy we could compare the pointers instead. When working with proxies, we could set typedef pointer to void and specialize comparisons on that.

Change History (2)

comment:1 Changed 13 years ago by Peter

Milestone: yat 1.0yat 0.4.1

comment:2 Changed 13 years ago by Peter

Milestone: yat 0.4.1yat 0.4
Resolution: fixed
Status: newclosed

The example in description is no longer an issue. DataLookup1D::iterator is now a StrideIterator?<Container2DIterator>. Comparison of a StrideIterator? calls the underlying iterator, in this case the Container2DIterator. The comparison of Container2DIterator uses the index variable to compare. If the pointers to containers are not pointing to the same container (typically a MatrixLookup? or sister) a yat_assert is triggered.

Using the underlying double& as suggested in description, would be confusing. Because the Lookups are general and therefore the order in physical memory is irrelevant for the Lookup.

Note: See TracTickets for help on using tickets.