Support Class Library
A set of tools providing classes and utility
range.h
Go to the documentation of this file.
1 #pragma once
3 #include <scl/macros.h>
5 #include <limits>
8 #include <scl/concepts/require.h>
10 
11 namespace scl{
12  namespace stream{
13  namespace creators{
14  namespace details{
19  template <class T>
22 
23  public:
27 
28  protected:
42  T from, to, step, cur;
43 
44  public:
51  RangeCreator(T from, T to, T step) : from{from}, to{to}, step{step}, cur{from} {
52  }
53 
54  bool hasNext() const override{
55  return this->step > 0 ? this->cur < this->to : this->cur >= this->to;
56  }
57 
58  payload_type next() override{
59  if(!this->hasNext())
60  return payload_type::withoutValue();
61 
62  T value = this->cur;
63  this->cur += this->step;
64  return payload_type::withValue(value);
65  }
66  };
67  }
68 
77  template <class T = int, class = META::enable_if_t<META::is_arithmetic<T>()>>
78  Stream<T> range(T from, T to, T step = 1){
79  namespace make = scl::tools::make;
80  return Stream<T>{
81  make::ptr<details::RangeCreator<T>>(from, to, step)
82  };
83  }
84 
92  template <class T = int, class = META::enable_if_t<META::is_arithmetic<T>()>>
93  Stream<T> rangeTo(T to, T step = 1){
94  return range<T>(0, to, step);
95  }
96 
104  template <class T = int, class = META::enable_if_t<META::is_arithmetic<T>()>>
106  return range<T>(from, 0, step);
107  }
108 
116  template <class T = double, class = META::enable_if_t<
117  META::is_arithmetic<T>()
118  && std::numeric_limits<T>::has_infinity
119  >>
121  return range<T>(from, std::numeric_limits<T>::infinity(), step);
122  }
123  }
124  }
125 }
payload_type next() override
Retrieve the next iterator payload.
Definition: range.h:58
Stream< T > range(T from, T to, T step=1)
Create an scl::stream::Stream for a specified range.
Definition: range.h:78
StreamIteratorPayload< T > payload_type
Type alias for the payload used.
typename payload_type::value_type value_type
Type alias for the data type manipulated.
RangeCreator(T from, T to, T step)
Construct from initial, last and increment.
Definition: range.h:51
Global namespace of the SCL.
Definition: alias.hpp:3
bool hasNext() const override
Determines whether or not this iterator can produce another value payload.
Definition: range.h:54
T step
the individual increment
Definition: range.h:42
Stream< T > infiniteRange(T from=0, T step=1)
Generate a range from the given value to +inf.
Definition: range.h:120
Creator for a numeric range.
Definition: range.h:20
Group of factory functions.
Definition: any.h:8
Stream< T > rangeFrom(T from, T step=-1)
Generate a range from the given value to 0.
Definition: range.h:105
Stream< T > rangeTo(T to, T step=1)
Range from 0 to the specified value.
Definition: range.h:93
A class for iterators that start a stream chain.
Copyable concept, a type is copyable if it is both copy constructible and copy assignable.
Definition: Copyable.h:16
typename iterator_type::payload_type payload_type
Definition: range.h:26
typename iterator_type::value_type value_type
Definition: range.h:25
#define static_require(cpt)
Definition: macros.h:6
Class representing a stream of data.
Definition: Stream.h:15
typename std::enable_if< B, T >::type enable_if_t
An handy type alias for the result of std::enable_if.
Definition: enable_if.h:12