• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Sequence.h - Utility for producing sequences of values ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This routine provides some synthesis utilities to produce sequences of
11 /// values. The names are intentionally kept very short as they tend to occur
12 /// in common and widely used contexts.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_ADT_SEQ_H
17 #define LLVM_ADT_SEQ_H
18 
19 #include "llvm/ADT/iterator.h"
20 #include "llvm/ADT/iterator_range.h"
21 
22 namespace llvm {
23 
24 namespace detail {
25 template <typename ValueT>
26 class value_sequence_iterator
27     : public iterator_facade_base<value_sequence_iterator<ValueT>,
28                                   std::random_access_iterator_tag,
29                                   const ValueT> {
30   typedef typename value_sequence_iterator::iterator_facade_base BaseT;
31 
32   ValueT Value;
33 
34 public:
35   typedef typename BaseT::difference_type difference_type;
36   typedef typename BaseT::reference reference;
37 
38   value_sequence_iterator() = default;
39   value_sequence_iterator(const value_sequence_iterator &) = default;
value_sequence_iterator(value_sequence_iterator && Arg)40   value_sequence_iterator(value_sequence_iterator &&Arg)
41       : Value(std::move(Arg.Value)) {}
42 
43   template <typename U, typename Enabler = decltype(ValueT(std::declval<U>()))>
value_sequence_iterator(U && Value)44   value_sequence_iterator(U &&Value) : Value(std::forward<U>(Value)) {}
45 
46   value_sequence_iterator &operator+=(difference_type N) {
47     Value += N;
48     return *this;
49   }
50   value_sequence_iterator &operator-=(difference_type N) {
51     Value -= N;
52     return *this;
53   }
54   using BaseT::operator-;
55   difference_type operator-(const value_sequence_iterator &RHS) const {
56     return Value - RHS.Value;
57   }
58 
59   bool operator==(const value_sequence_iterator &RHS) const {
60     return Value == RHS.Value;
61   }
62   bool operator<(const value_sequence_iterator &RHS) const {
63     return Value < RHS.Value;
64   }
65 
66   reference operator*() const { return Value; }
67 };
68 } // End detail namespace.
69 
70 template <typename ValueT>
seq(ValueT Begin,ValueT End)71 iterator_range<detail::value_sequence_iterator<ValueT>> seq(ValueT Begin,
72                                                             ValueT End) {
73   return make_range(detail::value_sequence_iterator<ValueT>(Begin),
74                     detail::value_sequence_iterator<ValueT>(End));
75 }
76 
77 }
78 
79 #endif
80