[Mlir-commits] [mlir] 93813e5 - [mlir] Add a utility iterator range that repeats a given value `n` times.

River Riddle llvmlistbot at llvm.org
Fri Feb 21 15:17:41 PST 2020


Author: River Riddle
Date: 2020-02-21T15:15:32-08:00
New Revision: 93813e5feb18ece7becd1eece24d0138c955fd53

URL: https://github.com/llvm/llvm-project/commit/93813e5feb18ece7becd1eece24d0138c955fd53
DIFF: https://github.com/llvm/llvm-project/commit/93813e5feb18ece7becd1eece24d0138c955fd53.diff

LOG: [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

Added: 
    

Modified: 
    mlir/include/mlir/Support/STLExtras.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Support/STLExtras.h b/mlir/include/mlir/Support/STLExtras.h
index 527234921d95..729e4583df46 100644
--- a/mlir/include/mlir/Support/STLExtras.h
+++ b/mlir/include/mlir/Support/STLExtras.h
@@ -339,6 +339,26 @@ template <typename ContainerTy> auto make_second_range(ContainerTy &&c) {
       });
 }
 
+/// A range class that repeats a specific value for a set number of times.
+template <typename T>
+class RepeatRange
+    : public detail::indexed_accessor_range_base<RepeatRange<T>, T, const T> {
+public:
+  using detail::indexed_accessor_range_base<
+      RepeatRange<T>, T, const T>::indexed_accessor_range_base;
+
+  /// Given that we are repeating a specific value, we can simply return that
+  /// value when offsetting the base or dereferencing the iterator.
+  static T offset_base(const T &val, ptr
diff _t) { return val; }
+  static const T &dereference_iterator(const T &val, ptr
diff _t) { return val; }
+};
+
+/// Make a range that repeats the given value 'n' times.
+template <typename ValueTy>
+RepeatRange<ValueTy> make_repeated_range(const ValueTy &value, size_t n) {
+  return RepeatRange<ValueTy>(value, n);
+}
+
 /// Returns true of the given range only contains a single element.
 template <typename ContainerTy> bool has_single_element(ContainerTy &&c) {
   auto it = std::begin(c), e = std::end(c);


        


More information about the Mlir-commits mailing list