tyru (owner)

Revisions

  • 5bff75 tyru Sat Nov 07 19:09:43 -0800 2009
  • 40e3c3 tyru Sat Nov 07 03:14:01 -0800 2009
gist: 228658 Download_button fork
public
Public Clone URL: git://gist.github.com/228658.git
Embed All Files: show embed
iota.cpp #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <memory>
#include <cassert>
 
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
 
 
 
namespace iota {
    namespace {
        template <typename T>
        struct const_value_type {
            typedef const typename T::value_type
                type;
        };
    }
 
    template <typename Container>
    Container
    iota(typename const_value_type<Container>::type& max,
         typename const_value_type<Container>::type& begin,
         typename const_value_type<Container>::type& step)
    {
        assert(max >= begin);
 
        Container c;
        for (typename Container::value_type i = begin; i <= max; i += step) {
            c.push_back(i);
        }
        return c;
    }
 
    template <typename Container>
    inline Container
    iota(typename const_value_type<Container>::type& max,
         typename const_value_type<Container>::type& step)
    {
        return iota<Container>(
            max,
            static_cast<typename const_value_type<Container>::type&>(1),
            step
        );
    }
}
 
 
int main(int argc, char *argv[]) {
    using namespace std;
 
    switch (argc) {
    case 3:
        {
            const vector<int>& v = iota::iota<vector<int> >(atoi(argv[1]), atoi(argv[2]));
            copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
            break;
        }
 
    case 4:
        {
            const vector<int>& v = iota::iota<vector<int> >(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
            copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
            break;
        }
 
    default:
        cerr << "Usage: './iota max step' or './iota max begin step'" << endl;
        return -1;
    }
 
    return 0;
}