1 | #include <boost/container/vector.hpp>
|
---|
2 | #include <boost/algorithm/string.hpp>
|
---|
3 | #include <boost/progress.hpp>
|
---|
4 | #include <boost/range.hpp>
|
---|
5 | #include <boost/swap.hpp>
|
---|
6 | #include <iostream>
|
---|
7 | #include <cstddef>
|
---|
8 | #include <cstdlib>
|
---|
9 | #include <string>
|
---|
10 |
|
---|
11 | using namespace std;
|
---|
12 | using namespace boost;
|
---|
13 | using namespace algorithm;
|
---|
14 |
|
---|
15 | bool log_alloc = false;
|
---|
16 |
|
---|
17 | #ifdef LOG_ALLOCATIONS
|
---|
18 | void *operator new(size_t x)
|
---|
19 | {
|
---|
20 | if(log_alloc) cout << "new:\t" << x << endl;
|
---|
21 | return malloc(x);
|
---|
22 | }
|
---|
23 | void operator delete(void *p)
|
---|
24 | {
|
---|
25 | free(p);
|
---|
26 | }
|
---|
27 |
|
---|
28 | void *operator new[](size_t x)
|
---|
29 | {
|
---|
30 | if(log_alloc) cout << "new[]:\t" << x << endl;
|
---|
31 | return malloc(x);
|
---|
32 | }
|
---|
33 | void operator delete[](void *p)
|
---|
34 | {
|
---|
35 | free(p);
|
---|
36 | }
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | typedef iterator_range<string::const_iterator> string_view;
|
---|
40 | typedef container::vector<string_view> Vector;
|
---|
41 |
|
---|
42 | class Assigner
|
---|
43 | {
|
---|
44 | static Vector *c;
|
---|
45 | public:
|
---|
46 | typedef Vector::const_iterator const_iterator;
|
---|
47 | typedef Vector::const_iterator iterator;
|
---|
48 |
|
---|
49 | explicit Assigner(Vector &r)
|
---|
50 | {
|
---|
51 | c = &r;
|
---|
52 | }
|
---|
53 |
|
---|
54 | template<typename Iterator>
|
---|
55 | explicit Assigner(Iterator first, Iterator last)
|
---|
56 | {
|
---|
57 | c->assign(first, last);
|
---|
58 | }
|
---|
59 |
|
---|
60 | const_iterator begin() const
|
---|
61 | {
|
---|
62 | return c->begin();
|
---|
63 | }
|
---|
64 | const_iterator end() const
|
---|
65 | {
|
---|
66 | return c->end();
|
---|
67 | }
|
---|
68 |
|
---|
69 | void swap(Assigner &) {}
|
---|
70 | };
|
---|
71 | Vector *Assigner::c = 0;
|
---|
72 |
|
---|
73 |
|
---|
74 | template<typename Container>
|
---|
75 | void test(Container &c, const string &s)
|
---|
76 | {
|
---|
77 | size_t r = 0;
|
---|
78 | {
|
---|
79 | progress_timer t;
|
---|
80 | cout << "start" << endl;
|
---|
81 | log_alloc = true;
|
---|
82 | for(unsigned i = 0; i < 1000000; ++i)
|
---|
83 | {
|
---|
84 | split(c, s, [](char c){ return c == ' '; });
|
---|
85 | for(auto &&x : c)
|
---|
86 | r += distance(x);
|
---|
87 | }
|
---|
88 | log_alloc = false;
|
---|
89 | cout << "end" << endl;
|
---|
90 | }
|
---|
91 | cout << r << endl;
|
---|
92 | }
|
---|
93 |
|
---|
94 | int main()
|
---|
95 | {
|
---|
96 | string s;
|
---|
97 | for(unsigned i = 0; i != 64; ++i)
|
---|
98 | s += "1 ";
|
---|
99 |
|
---|
100 | Vector v;
|
---|
101 | Assigner a(v);
|
---|
102 | v.reserve(s.size());
|
---|
103 |
|
---|
104 | test(a, s);
|
---|
105 | test(v, s);
|
---|
106 | }
|
---|