1 //===- Sequence.h - Utility for producing sequences of values ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// This routine provides some synthesis utilities to produce sequences of
10 /// values. The names are intentionally kept very short as they tend to occur
11 /// in common and widely used contexts.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_SEQUENCE_H
16 #define LLVM_ADT_SEQUENCE_H
18 #include "llvm/ADT/iterator.h"
19 #include "llvm/ADT/iterator_range.h"
28 template <typename ValueT
>
29 class value_sequence_iterator
30 : public iterator_facade_base
<value_sequence_iterator
<ValueT
>,
31 std::random_access_iterator_tag
,
33 using BaseT
= typename
value_sequence_iterator::iterator_facade_base
;
38 using difference_type
= typename
BaseT::difference_type
;
39 using reference
= typename
BaseT::reference
;
41 value_sequence_iterator() = default;
42 value_sequence_iterator(const value_sequence_iterator
&) = default;
43 value_sequence_iterator(value_sequence_iterator
&&Arg
)
44 : Value(std::move(Arg
.Value
)) {}
46 template <typename U
, typename Enabler
= decltype(ValueT(std::declval
<U
>()))>
47 value_sequence_iterator(U
&&Value
) : Value(std::forward
<U
>(Value
)) {}
49 value_sequence_iterator
&operator+=(difference_type N
) {
53 value_sequence_iterator
&operator-=(difference_type N
) {
57 using BaseT::operator-;
58 difference_type
operator-(const value_sequence_iterator
&RHS
) const {
59 return Value
- RHS
.Value
;
62 bool operator==(const value_sequence_iterator
&RHS
) const {
63 return Value
== RHS
.Value
;
65 bool operator<(const value_sequence_iterator
&RHS
) const {
66 return Value
< RHS
.Value
;
69 reference
operator*() const { return Value
; }
72 } // end namespace detail
74 template <typename ValueT
>
75 iterator_range
<detail::value_sequence_iterator
<ValueT
>> seq(ValueT Begin
,
77 return make_range(detail::value_sequence_iterator
<ValueT
>(Begin
),
78 detail::value_sequence_iterator
<ValueT
>(End
));
81 } // end namespace llvm
83 #endif // LLVM_ADT_SEQUENCE_H