-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymorphic_vector.cpp
77 lines (55 loc) · 1.56 KB
/
polymorphic_vector.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
76
77
#include <cassert>
#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
struct Base {
};
struct A: public Base {
A(int a): name("struct A"), a(a) {}
std::string name;
int a;
void print() const { std::cout << this << name << a << std::endl; }
};
struct B: public Base {
B(double b): name("struct B"), b(b) {}
std::string name;
double b;
void print() const { std::cout << this << name << b << std::endl; }
};
template <typename T = void, typename... Ts>
struct CompositeVector {
CompositeVector(std::vector<T>& t, std::vector<Ts>&... ts): container(t), composite(ts...) {}
std::vector<T> container;
CompositeVector<Ts...> composite;
void visit(const std::function<void(const T&)>& f, const std::function<void(const Ts&)>... fs) const {
std::for_each(container.cbegin(), container.cend(), f);
composite.visit(fs...);
}
};
template <>
struct CompositeVector<void> {
void visit() const {}
};
int main() {
std::vector<A> vecA;
vecA.reserve(20);
std::vector<B> vecB;
vecB.reserve(20);
std::vector<A*> vecA2;
std::vector<B*> vecB2;
for (int i = 0; i < 20; ++i) {
for (int j = 32001; j > 0; --j) vecA2.push_back(new A(j));
vecA.push_back(*vecA2[32001*i + i]);
}
for (int i = 0; i < 20; ++i) {
for (int j = 32001; j > 0; --j) vecB2.push_back(new B(0.33*j));
vecB.push_back(*vecB2[32001*i + i]);
}
const auto vec = CompositeVector<A, B>(vecA, vecB);
const auto al = [](const A& a)->void{ a.print(); };
const auto bl = [](const B& b)->void{ b.print(); };
for (auto i = 50000; i > 0; --i)
vec.visit(al, bl);
return 0;
}