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_;
};
// 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>
inline UptrVectorIterator<VT, IC>& UptrVectorIterator<VT, IC>::operator++() {
++iterator_;

View File

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