This is an archive of the discontinued LLVM Phabricator instance.

Make iterators of BitVector easier to use by inheriting iterator_facade_base
AcceptedPublic

Authored by jdh8 on Jul 27 2020, 11:58 AM.

Details

Summary

This change is needed to apply generic algorithmic functions (like in
STLExtras.h) to the result of SmallBitVector::set_bits

Diff Detail

Unit TestsFailed

Event Timeline

jdh8 created this revision.Jul 27 2020, 11:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2020, 11:58 AM
dblaikie accepted this revision.Jul 27 2020, 1:56 PM
dblaikie added a subscriber: dblaikie.
dblaikie added inline comments.
llvm/include/llvm/ADT/BitVector.h
36

Looks like this isn't a forward iterator - it's an input iterator.

A forward iterator requires:

"The type std::iterator_traits<It>::reference must be exactly
T& if It satisfies LegacyOutputIterator (It is mutable)
const T& otherwise (It is constant),"

And that the return type of "op*" is referenec.

Since the return type of op* is unsigned, this could be an input iterator.

Or, I guess, the return type could be changed, since there is ongoing storage for the value (so long as the iterator isn't incremented - but that gets complicated with reverse iterators... ).

This revision is now accepted and ready to land.Jul 27 2020, 1:56 PM

I had an attempt here: https://reviews.llvm.org/D33419, I think similar concerns came up in that review.

jdh8 planned changes to this revision.Jul 27 2020, 11:24 PM

Indeed it is not a forward iterator because it is bound to no object. I am changing it to an input iterator.

jdh8 updated this revision to Diff 281132.Jul 28 2020, 12:49 AM

BitVector's iterator should be an input iterator instead of a forward iterator
because it is bound to no unsigned object

This revision is now accepted and ready to land.Jul 28 2020, 12:49 AM
dblaikie accepted this revision.Jul 28 2020, 10:17 AM

I had an attempt here: https://reviews.llvm.org/D33419, I think similar concerns came up in that review.

Indeed, sorry that fell through & I didn't understand enough about iterators to instead suggest falling back to an input iterator instead of figuring out how to fit the semantics of the forward iterator. Perhaps after this is committed you could resurrect your original patch to remove the op!= and maybe add the extra checks you were proposing to the facade?

BitVector's iterator should be an input iterator instead of a forward iterator
because it is bound to no unsigned object

Great, thanks!

Indeed, sorry that fell through & I didn't understand enough about iterators to instead suggest falling back to an input iterator instead of figuring out how to fit the semantics of the forward iterator.

No worries, I was quite confused as well there. Thanks for figuring this stuff out!