Commit 428a6d22a3cd3488b5fce4af42eb502c3fdc751e

Authored by Jordi Inglada
1 parent edd4ad52
Exists in master

ENH: python-like range

Showing 1 changed file with 13 additions and 0 deletions   Show diff stats
include/cbutils.h
... ... @@ -22,6 +22,7 @@
22 22 #include <sstream>
23 23 #include <vector>
24 24 #include <random>
  25 +#include <cassert>
25 26 namespace cbutils
26 27 {
27 28 // ----------------- String manipulation ---------------------------------------
... ... @@ -85,6 +86,18 @@ Seq tail(const Seq&amp; s)
85 86 std::copy(head, std::end(s), std::back_inserter(result));
86 87 return result;
87 88 }
  89 +template <typename T>
  90 +std::vector<T> range(T minvalue, T maxvalue, T step=1)
  91 +{
  92 + assert(step>0);
  93 + auto nb_elements = (maxvalue-minvalue)/step+1;
  94 + std::vector<T> result(nb_elements);
  95 + result[0]=minvalue;
  96 + for(size_t i=1; i<nb_elements; ++i)
  97 + result[i] = result[i-1]+step;
  98 +
  99 + return result;
  100 +}
88 101 }// end ns seq
89 102 // ----------------- Random numbers --------------------------------------------
90 103 namespace rand
... ...