-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.cpp
More file actions
111 lines (92 loc) · 2.04 KB
/
Copy pathSequence.cpp
File metadata and controls
111 lines (92 loc) · 2.04 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <vector>
template <class T>
class Sequence
{
private:
std::vector<T> data;
public:
Sequence(const T &a0, std::size_t k, std::function<T(const T&)> next)
{
data.push_back(a0);
for (std::size_t i = 0; i < k - 1; i++)
{
data.push_back(next(data[i]));
}
}
T& operator [] (std::size_t index)
{
if(index >= data.size())
{
throw std::invalid_argument("Invalid index");
}
return this->data[index];
}
const T& operator [] (std::size_t index) const
{
if(index >= data.size())
{
throw std::invalid_argument("Inavlid index");
}
return this->data[index];
}
class SequenceIterator
{
private:
friend class Sequence;
T* pointer;
public:
SequenceIterator(T* pointer)
{
this->pointer = pointer;
}
T& operator * ()
{
return *pointer;
}
const T& operator * () const
{
return *pointer;
}
SequenceIterator& operator ++ ()
{
++this->pointer;
return *this;
}
SequenceIterator operator ++ (int)
{
SequenceIterator old = *this;
++(*this);
return old;
}
bool operator == (const SequenceIterator& other) const
{
return this->pointer == other.pointer;
}
bool operator != (const SequenceIterator& other) const
{
return this->pointer != other.pointer;
}
};
SequenceIterator begin()
{
return SequenceIterator(&this->data[0]);
}
SequenceIterator end()
{
return SequenceIterator(&this->data[0] + this->data.size());
}
};
int p2(int x)
{
return x + 2;
}
int main()
{
Sequence<int> even10(0, 10, p2);
for (int x : even10)
{
std::cout << x << " ";
}
std::cout << std::endl;
}