ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TSeq.h
Go to the documentation of this file.
1 #ifndef ROOT_TSeq
2 #define ROOT_TSeq
3 
4 #include <iterator>
5 #include <type_traits>
6 
7 /**
8 \class ROOT::TSeq
9 \brief A pseudo container class which is a generator of indices.
10 
11 \tparam T Type of the numerical sequence.
12 \ingroup Containers
13 A pseudo container class which is a generator of indices. The model is the `xrange`
14 built-in function of Python.
15 Possible usages:
16 Loop on a sequence of integers
17 ~~~{.cpp}
18  for (auto i : TSeqI(10)) {
19  cout << "Element " << i << endl;
20  }
21 ~~~
22 Loop on a sequence of integers in steps
23 ~~~{.cpp}
24  for (auto i : TSeqI(-5, 29, 6)) {
25  cout << "Element " << i << endl;
26  }
27 ~~~
28 Loop backwards on a sequence of integers
29 ~~~{.cpp}
30  for (auto i : TSeqI(50, 30, -3)) {
31  cout << "Element " << i << endl;
32  }
33 ~~~
34 Use an stl algorithm, for_each
35 ~~~{.cpp}
36  TSeqUL ulSeq(2,30,3);
37  std::for_each(std::begin(ulSeq),std::end(ulSeq),[](ULong_t i){cout << "For each: " << i <<endl;});
38 ~~~
39 Random access:
40 ~~~{.cpp}
41  cout << "Random access: 3rd element is " << ulSeq[2] << endl;
42 ~~~
43 A function to create sequences inferring the type:
44 ~~~.{cpp}
45  for (auto i : MakeSeq(1000000000000UL, 1000000000003UL)) {
46  cout << "Element " << i << endl;
47  }
48 ~~~
49 
50 **/
51 
52 namespace ROOT {
53 
54  template<class T>
55  class TSeq {
56  private:
58  static_assert(std::is_integral<T>::value, "Only integral types are supported.");
59  }
60  const T fBegin;
61  const T fEnd;
62  const T fStep;
63  public:
64  using value_type = T;
66 
67  TSeq(T end): fBegin(), fEnd(end), fStep(1) {
69  }
70  TSeq(T begin, T end, T step = 1):
71  fBegin(begin), fEnd(end), fStep(step) {
73  }
74 
75  class iterator: public std::iterator<std::random_access_iterator_tag, T, difference_type> {
76  private:
79  public:
80  iterator(T start, T step): fCounter(start), fStep(step) {}
81  T operator*() const {
82  return fCounter;
83  }
85  fCounter += fStep;
86  return *this;
87  };
89  iterator tmp(*this);
90  operator++();
91  return tmp;
92  }
93  bool operator==(const iterator &other) {
94  return fCounter == other.fCounter;
95  }
96  bool operator!=(const iterator &other) {
97  return fCounter != other.fCounter;
98  }
99  T operator+(int v) {
100  return fCounter + v;
101  }
102  T operator-(int v) {
103  return fCounter - v;
104  }
106  fCounter -= fStep;
107  return *this;
108  }
110  iterator tmp(*this);
111  operator--();
112  return tmp;
113  }
114  };
115 
116  iterator begin() const {
117  return iterator(fBegin, fStep);
118  }
119  iterator end() const {
120  auto isStepMultiple = (fEnd - fBegin) % fStep == 0;
121  auto theEnd = isStepMultiple ? fEnd : fStep * ((T)((fEnd - fBegin) / fStep) + 1) + fBegin;
122  return iterator(theEnd, fStep);
123  }
124 
125  T const &front() const {
126  return fBegin;
127  }
128 
129  T operator[](T s) const {
130  return s * fStep + fBegin;
131  }
132 
133  std::size_t size() const {
134  return end() - begin();
135  }
136 
137  T step() const {
138  return fStep;
139  }
140 
141  bool empty() const {
142  return fEnd == fBegin;
143  }
144 
145  };
146 
147  using TSeqI = TSeq<int>;
149  using TSeqL = TSeq<long>;
151 
152  template<class T>
154  {
155  return TSeq<T>(end);
156  }
157 
158  template<class T>
159  TSeq<T> MakeSeq(T begin, T end, T step = 1)
160  {
161  return TSeq<T>(begin, end, step);
162  }
163 
164 }
165 
166 #include <sstream>
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// Print a TSeq at the prompt:
170 
171 namespace cling {
172  template<class T>
173  std::string printValue(ROOT::TSeq<T> *val)
174  {
175  std::ostringstream ret;
176  ret << "Sequence of values. Begin: " << *val->begin()
177  << " - End: " << *val->end()
178  << " - Step: " << val->step();
179  return ret.str();
180  }
181 }
182 
183 #endif
T operator*() const
Definition: TSeq.h:81
iterator operator++(int)
Definition: TSeq.h:88
iterator begin() const
Definition: TSeq.h:116
const T fStep
Definition: TSeq.h:62
typename std::make_signed< T >::type difference_type
Definition: TSeq.h:65
bool operator==(const iterator &other)
Definition: TSeq.h:93
T const & front() const
Definition: TSeq.h:125
size_t
Definition: TBuffer.cxx:28
T step() const
Definition: TSeq.h:137
TTree * T
iterator & operator--()
Definition: TSeq.h:105
T operator-(int v)
Definition: TSeq.h:102
T operator+(int v)
Definition: TSeq.h:99
bool operator!=(const iterator &other)
Definition: TSeq.h:96
iterator operator--(int)
Definition: TSeq.h:109
iterator(T start, T step)
Definition: TSeq.h:80
std::string printValue(const TDatime &val)
Print a TDatime at the prompt.
Definition: TDatime.cxx:447
bool empty() const
Definition: TSeq.h:141
T operator[](T s) const
Definition: TSeq.h:129
SVector< double, 2 > v
Definition: Dict.h:5
TSeq< T > MakeSeq(T end)
Definition: TSeq.h:153
void checkIntegralType()
Definition: TSeq.h:57
const T fEnd
Definition: TSeq.h:61
A pseudo container class which is a generator of indices.
Definition: TSeq.h:55
int type
Definition: TGX11.cxx:120
T value_type
Definition: TSeq.h:64
std::size_t size() const
Definition: TSeq.h:133
TSeq(T end)
Definition: TSeq.h:67
TSeq(T begin, T end, T step=1)
Definition: TSeq.h:70
iterator end() const
Definition: TSeq.h:119
const T fBegin
Definition: TSeq.h:60
float value
Definition: math.cpp:443
iterator & operator++()
Definition: TSeq.h:84