Chi-Tech
chi_math_range.h
Go to the documentation of this file.
1#ifndef CHITECH_CHI_MATH_RANGE_H
2#define CHITECH_CHI_MATH_RANGE_H
3
4#include <type_traits>
5#include <vector>
6
7namespace chi_math
8{
9//###################################################################
10/**Returns a range of number according to the logic of the parameters.
11 *
12 * \param start First number in the sequence.
13 * \param end Termination criteria. If the delta is positive then
14 * the sequence will terminate if i>=end, otherwise if the
15 * delta is negative the sequence will terminate if i<=end
16 * \param delta Cannot be 0. Default 1. Can be negative.*/
17template<typename T, typename D = int>
18std::vector<T> Range(T start, T end, D delta=1)
19{
20 static_assert(std::is_signed<D>::value,
21 "chi_math::Range delta parameter must be signed");
22 const bool forward = (delta > 0);
23
24 std::vector<T> sequence = {};
25
26 if ( forward and start >= end) return sequence;
27 if (not forward and start <= end) return sequence;
28
29 T i = start;
30 bool terminate = false;
31 while (not terminate)
32 {
33 sequence.push_back(i);
34 i += delta;
35
36 if ( forward and i >= end) terminate = true;
37 if (not forward and i <= end) terminate = true;
38
39 if (not forward and i > start) terminate = true; //Wrap-around check
40 }
41
42 return sequence;
43}
44
45}//namespace chi_math
46
47#endif //CHITECH_CHI_MATH_RANGE_H
std::vector< T > Range(T start, T end, D delta=1)