CASM  1.1.0
A Clusters Approach to Statistical Mechanics
algorithm.hh
Go to the documentation of this file.
1 #ifndef CONTAINER_ALGO_HH
2 #define CONTAINER_ALGO_HH
3 
4 #include <vector>
5 
6 namespace CASM {
7 
8 template <typename T>
9 std::vector<T> sequence(T first, T last) {
10  std::vector<T> seq;
11  if (0 < last - first) seq.reserve(last - first);
12  while (first <= last) {
13  seq.push_back(first);
14  first += 1;
15  }
16  return seq;
17 }
18 
19 template <typename T>
20 std::vector<T> sequence(T first, T inc, T last) {
21  std::vector<T> seq;
22  // Decreasing case
23  if (first + inc < first) {
24  while (last <= first) {
25  seq.push_back(first);
26  first += inc;
27  }
28  }
29  // Increasing case
30  else if (first < first + inc) {
31  while (first <= last) {
32  seq.push_back(first);
33  first += inc;
34  }
35  } else { // increment=0 case
36  if (first == last) {
37  seq.push_back(first);
38  }
39  }
40  return seq;
41 }
42 } // namespace CASM
43 
44 #endif
Main CASM namespace.
Definition: APICommand.hh:8
std::vector< T > sequence(T first, T last)
Definition: algorithm.hh:9