Add make_range() & make_const_range() for creating iterator ranges.

This commit is contained in:
Lei Zhang 2016-08-12 10:04:23 -04:00
parent 4b3247feba
commit 95dc16d1ec
2 changed files with 22 additions and 11 deletions

View File

@ -105,6 +105,24 @@ class IteratorRange {
IteratorType end_; IteratorType end_;
}; };
// Returns a (begin, end) iterator pair for the given container.
template <typename ValueType,
class IteratorType = UptrVectorIterator<ValueType>>
inline IteratorRange<IteratorType> make_range(
std::vector<std::unique_ptr<ValueType>>& container) {
return {IteratorType(&container, container.begin()),
IteratorType(&container, container.end())};
}
// Returns a const (begin, end) iterator pair for the given container.
template <typename ValueType,
class IteratorType = UptrVectorIterator<ValueType, true>>
inline IteratorRange<IteratorType> make_const_range(
const std::vector<std::unique_ptr<ValueType>>& container) {
return {IteratorType(&container, container.cbegin()),
IteratorType(&container, container.cend())};
}
template <typename VT, bool IC> template <typename VT, bool IC>
inline UptrVectorIterator<VT, IC>& UptrVectorIterator<VT, IC>::operator++() { inline UptrVectorIterator<VT, IC>& UptrVectorIterator<VT, IC>::operator++() {
++iterator_; ++iterator_;

View File

@ -198,26 +198,19 @@ inline Module::inst_iterator Module::debug_end() {
} }
inline IteratorRange<Module::inst_iterator> Module::debugs() { inline IteratorRange<Module::inst_iterator> Module::debugs() {
return IteratorRange<inst_iterator>(inst_iterator(&debugs_, debugs_.begin()), return make_range(debugs_);
inst_iterator(&debugs_, debugs_.end()));
} }
inline IteratorRange<Module::const_inst_iterator> Module::debugs() const { inline IteratorRange<Module::const_inst_iterator> Module::debugs() const {
return IteratorRange<const_inst_iterator>( return make_const_range(debugs_);
const_inst_iterator(&debugs_, debugs_.cbegin()),
const_inst_iterator(&debugs_, debugs_.cend()));
} }
inline IteratorRange<Module::inst_iterator> Module::annotations() { inline IteratorRange<Module::inst_iterator> Module::annotations() {
return IteratorRange<inst_iterator>( return make_range(annotations_);
inst_iterator(&annotations_, annotations_.begin()),
inst_iterator(&annotations_, annotations_.end()));
} }
inline IteratorRange<Module::const_inst_iterator> Module::annotations() const { inline IteratorRange<Module::const_inst_iterator> Module::annotations() const {
return IteratorRange<const_inst_iterator>( return make_const_range(annotations_);
const_inst_iterator(&annotations_, annotations_.cbegin()),
const_inst_iterator(&annotations_, annotations_.cend()));
} }
inline Module::const_iterator Module::cbegin() const { inline Module::const_iterator Module::cbegin() const {