Skip to content

Commit 93813e5

Browse files
committed
[mlir] Add a utility iterator range that repeats a given value n times.
This range is useful when an desired API expects a range or when comparing two different ranges for equality, but the underlying data is a splat. This range removes the need to explicitly construct a vector in those cases. Differential Revision: https://reviews.llvm.org/D74683
1 parent ca4ea51 commit 93813e5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

mlir/include/mlir/Support/STLExtras.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,26 @@ template <typename ContainerTy> auto make_second_range(ContainerTy &&c) {
339339
});
340340
}
341341

342+
/// A range class that repeats a specific value for a set number of times.
343+
template <typename T>
344+
class RepeatRange
345+
: public detail::indexed_accessor_range_base<RepeatRange<T>, T, const T> {
346+
public:
347+
using detail::indexed_accessor_range_base<
348+
RepeatRange<T>, T, const T>::indexed_accessor_range_base;
349+
350+
/// Given that we are repeating a specific value, we can simply return that
351+
/// value when offsetting the base or dereferencing the iterator.
352+
static T offset_base(const T &val, ptrdiff_t) { return val; }
353+
static const T &dereference_iterator(const T &val, ptrdiff_t) { return val; }
354+
};
355+
356+
/// Make a range that repeats the given value 'n' times.
357+
template <typename ValueTy>
358+
RepeatRange<ValueTy> make_repeated_range(const ValueTy &value, size_t n) {
359+
return RepeatRange<ValueTy>(value, n);
360+
}
361+
342362
/// Returns true of the given range only contains a single element.
343363
template <typename ContainerTy> bool has_single_element(ContainerTy &&c) {
344364
auto it = std::begin(c), e = std::end(c);

0 commit comments

Comments
 (0)