Logo ROOT   6.08/07
Reference Guide
TSeq.hxx
Go to the documentation of this file.
1 /* @(#)root/core/cont:$Id$ */
2 // Author: Danilo Piparo November 2015
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TSeq
13 #define ROOT_TSeq
14 
15 #include <iterator>
16 #include <type_traits>
17 
18 /**
19 \class ROOT::TSeq
20 \brief A pseudo container class which is a generator of indices.
21 
22 \tparam T Type of the numerical sequence.
23 \ingroup Containers
24 A pseudo container class which is a generator of indices. The model is the `xrange`
25 built-in function of Python.
26 Possible usages:
27 Loop on a sequence of integers
28 ~~~{.cpp}
29  for (auto i : TSeqI(10)) {
30  cout << "Element " << i << endl;
31  }
32 ~~~
33 Loop on a sequence of integers in steps
34 ~~~{.cpp}
35  for (auto i : TSeqI(-5, 29, 6)) {
36  cout << "Element " << i << endl;
37  }
38 ~~~
39 Loop backwards on a sequence of integers
40 ~~~{.cpp}
41  for (auto i : TSeqI(50, 30, -3)) {
42  cout << "Element " << i << endl;
43  }
44 ~~~
45 Use an stl algorithm, for_each
46 ~~~{.cpp}
47  TSeqUL ulSeq(2,30,3);
48  std::for_each(std::begin(ulSeq),std::end(ulSeq),[](ULong_t i){cout << "For each: " << i <<endl;});
49 ~~~
50 Random access:
51 ~~~{.cpp}
52  cout << "Random access: 3rd element is " << ulSeq[2] << endl;
53 ~~~
54 A function to create sequences inferring the type:
55 ~~~{.cpp}
56  for (auto i : MakeSeq(1000000000000UL, 1000000000003UL)) {
57  cout << "Element " << i << endl;
58  }
59 ~~~
60 
61 **/
62 
63 namespace ROOT {
64 
65  template<class T>
66  class TSeq {
67  private:
69  static_assert(std::is_integral<T>::value, "Only integral types are supported.");
70  }
71  const T fBegin;
72  const T fEnd;
73  const T fStep;
74  public:
75  using value_type = T;
77 
78  TSeq(T theEnd): fBegin(), fEnd(theEnd), fStep(1) {
80  }
81  TSeq(T theBegin, T theEnd, T theStep = 1):
82  fBegin(theBegin), fEnd(theEnd), fStep(theStep) {
84  }
85 
86  class iterator: public std::iterator<std::random_access_iterator_tag, T, difference_type> {
87  private:
90  public:
91  iterator(T start, T step): fCounter(start), fStep(step) {}
92  T operator*() const {
93  return fCounter;
94  }
96  fCounter += fStep;
97  return *this;
98  };
100  iterator tmp(*this);
101  operator++();
102  return tmp;
103  }
104  bool operator==(const iterator &other) const {
105  return fCounter == other.fCounter;
106  }
107  bool operator!=(const iterator &other) const {
108  return fCounter != other.fCounter;
109  }
110  T operator+(int v) const {
111  return fCounter + v;
112  }
113  T operator-(int v) const {
114  return fCounter - v;
115  }
116  T operator+(const iterator &other) const {
117  return fCounter + other.fCounter;
118  }
119  T operator-(const iterator &other) const {
120  return fCounter - other.fCounter;
121  }
123  fCounter -= fStep;
124  return *this;
125  }
127  iterator tmp(*this);
128  operator--();
129  return tmp;
130  }
131  };
132 
133  iterator begin() const {
134  return iterator(fBegin, fStep);
135  }
136  iterator end() const {
137  auto isStepMultiple = (fEnd - fBegin) % fStep == 0;
138  auto theEnd = isStepMultiple ? fEnd : fStep * ((T)((fEnd - fBegin) / fStep) + 1) + fBegin;
139  return iterator(theEnd, fStep);
140  }
141 
142  T const &front() const {
143  return fBegin;
144  }
145 
146  T operator[](T s) const {
147  return s * fStep + fBegin;
148  }
149 
150  std::size_t size() const {
151  return end() - begin();
152  }
153 
154  T step() const {
155  return fStep;
156  }
157 
158  bool empty() const {
159  return fEnd == fBegin;
160  }
161 
162  };
163 
164  using TSeqI = TSeq<int>;
166  using TSeqL = TSeq<long>;
168 
169  template<class T>
171  {
172  return TSeq<T>(end);
173  }
174 
175  template<class T>
177  {
178  return TSeq<T>(begin, end, step);
179  }
180 
181 }
182 
183 #include <sstream>
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Print a TSeq at the prompt:
187 
188 namespace cling {
189  template<class T>
190  std::string printValue(ROOT::TSeq<T> *val)
191  {
192  std::ostringstream ret;
193  ret << "A sequence of values: " << *val->begin()
194  << " <= i < " << *val->end();
195  auto step = val->step();
196  if (1 != step)
197  ret << " in steps of " << step;
198  return ret.str();
199  }
200 }
201 
202 #endif
iterator operator++(int)
Definition: TSeq.hxx:99
bool empty() const
Definition: TSeq.hxx:158
T const & front() const
Definition: TSeq.hxx:142
T operator[](T s) const
Definition: TSeq.hxx:146
const T fStep
Definition: TSeq.hxx:73
typename std::make_signed< T >::type difference_type
Definition: TSeq.hxx:76
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
T operator*() const
Definition: TSeq.hxx:92
double T(double x)
Definition: ChebyshevPol.h:34
T operator+(int v) const
Definition: TSeq.hxx:110
std::size_t size() const
Definition: TSeq.hxx:150
std::string printValue(ROOT::TSeq< T > *val)
Definition: TSeq.hxx:190
T operator-(const iterator &other) const
Definition: TSeq.hxx:119
T operator+(const iterator &other) const
Definition: TSeq.hxx:116
iterator & operator--()
Definition: TSeq.hxx:122
T operator-(int v) const
Definition: TSeq.hxx:113
TSeq(T theBegin, T theEnd, T theStep=1)
Definition: TSeq.hxx:81
iterator operator--(int)
Definition: TSeq.hxx:126
iterator(T start, T step)
Definition: TSeq.hxx:91
SVector< double, 2 > v
Definition: Dict.h:5
TSeq< T > MakeSeq(T end)
Definition: TSeq.hxx:170
void checkIntegralType()
Definition: TSeq.hxx:68
bool operator!=(const iterator &other) const
Definition: TSeq.hxx:107
const T fEnd
Definition: TSeq.hxx:72
TSeq(T theEnd)
Definition: TSeq.hxx:78
T step() const
Definition: TSeq.hxx:154
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
int type
Definition: TGX11.cxx:120
Print a TSeq at the prompt:
Definition: TDatime.h:114
T value_type
Definition: TSeq.hxx:75
iterator end() const
Definition: TSeq.hxx:136
bool operator==(const iterator &other) const
Definition: TSeq.hxx:104
const T fBegin
Definition: TSeq.hxx:71
iterator begin() const
Definition: TSeq.hxx:133
iterator & operator++()
Definition: TSeq.hxx:95