1#include <iostream>
2#include <iomanip>
3#include <map>
4
5#include <Core/Field.h>
6#include <Common/HashTable/HashMap.h>
7#include <Common/AutoArray.h>
8#include <IO/WriteHelpers.h>
9
10#include <Common/Stopwatch.h>
11
12
13int main(int argc, char ** argv)
14{
15 {
16 size_t n = 10;
17 using T = std::string;
18 DB::AutoArray<T> arr(n);
19
20 for (size_t i = 0; i < arr.size(); ++i)
21 arr[i] = "Hello, world! " + DB::toString(i);
22
23 for (size_t i = 0; i < arr.size(); ++i)
24 std::cerr << arr[i] << std::endl;
25 }
26
27 std::cerr << std::endl;
28
29 {
30 size_t n = 10;
31 using T = std::string;
32 using Arr = DB::AutoArray<T>;
33 Arr arr;
34
35 arr.resize(n);
36 for (size_t i = 0; i < arr.size(); ++i)
37 arr[i] = "Hello, world! " + DB::toString(i);
38
39 for (size_t i = 0; i < arr.size(); ++i)
40 std::cerr << arr[i] << std::endl;
41
42 std::cerr << std::endl;
43
44 Arr arr2 = std::move(arr);
45
46 std::cerr << arr.size() << ", " << arr2.size() << std::endl;
47
48 for (size_t i = 0; i < arr2.size(); ++i)
49 std::cerr << arr2[i] << std::endl;
50 }
51
52 std::cerr << std::endl;
53
54 {
55 size_t n = 10;
56 size_t keys = 10;
57 using T = std::string;
58 using Arr = DB::AutoArray<T>;
59 using Map = std::map<Arr, T>;
60 Map map;
61
62 for (size_t i = 0; i < keys; ++i)
63 {
64 Arr key(n);
65 for (size_t j = 0; j < n; ++j)
66 key[j] = DB::toString(rand());
67
68 map[std::move(key)] = "Hello, world! " + DB::toString(i);
69 }
70
71 for (Map::const_iterator it = map.begin(); it != map.end(); ++it)
72 {
73 std::cerr << "[";
74 for (size_t j = 0; j < n; ++j)
75 std::cerr << (j == 0 ? "" : ", ") << it->first[j];
76 std::cerr << "]";
77
78 std::cerr << ":\t" << it->second << std::endl;
79 }
80
81 std::cerr << std::endl;
82
83 Map map2 = std::move(map);
84
85 for (Map::const_iterator it = map2.begin(); it != map2.end(); ++it)
86 {
87 std::cerr << "[";
88 for (size_t j = 0; j < n; ++j)
89 std::cerr << (j == 0 ? "" : ", ") << it->first[j];
90 std::cerr << "]";
91
92 std::cerr << ":\t" << it->second << std::endl;
93 }
94 }
95
96 std::cerr << std::endl;
97
98 {
99 size_t n = 10;
100 size_t keys = 10;
101 using T = std::string;
102 using Arr = DB::AutoArray<T>;
103 using Vec = std::vector<Arr>;
104 Vec vec;
105
106 for (size_t i = 0; i < keys; ++i)
107 {
108 Arr key(n);
109 for (size_t j = 0; j < n; ++j)
110 key[j] = DB::toString(rand());
111
112 vec.push_back(std::move(key));
113 }
114
115 for (Vec::const_iterator it = vec.begin(); it != vec.end(); ++it)
116 {
117 std::cerr << "[";
118 for (size_t j = 0; j < n; ++j)
119 std::cerr << (j == 0 ? "" : ", ") << (*it)[j];
120 std::cerr << "]" << std::endl;
121 }
122
123 std::cerr << std::endl;
124
125 Vec vec2 = std::move(vec);
126
127 for (Vec::const_iterator it = vec2.begin(); it != vec2.end(); ++it)
128 {
129 std::cerr << "[";
130 for (size_t j = 0; j < n; ++j)
131 std::cerr << (j == 0 ? "" : ", ") << (*it)[j];
132 std::cerr << "]" << std::endl;
133 }
134 }
135
136 if (argc == 2 && !strcmp(argv[1], "1"))
137 {
138 size_t n = 5;
139 size_t map_size = 1000000;
140
141 using T = DB::Field;
142 T field = std::string("Hello, world");
143
144 using Arr = std::vector<T>;
145 using Map = HashMap<UInt64, Arr>;
146
147 Stopwatch watch;
148
149 Map map;
150 for (size_t i = 0; i < map_size; ++i)
151 {
152 Map::LookupResult it;
153 bool inserted;
154
155 map.emplace(rand(), it, inserted);
156 if (inserted)
157 {
158 new (&it->getMapped()) Arr(n);
159
160 for (size_t j = 0; j < n; ++j)
161 (it->getMapped())[j] = field;
162 }
163 }
164
165 std::cerr << std::fixed << std::setprecision(2)
166 << "Vector: Elapsed: " << watch.elapsedSeconds()
167 << " (" << map_size / watch.elapsedSeconds() << " rows/sec., "
168 << "sizeof(Map::value_type) = " << sizeof(Map::value_type)
169 << std::endl;
170 }
171
172 {
173 size_t n = 10000;
174 using Arr = DB::AutoArray<std::string>;
175 Arr arr1(n);
176 Arr arr2(n);
177
178 for (size_t i = 0; i < n; ++i)
179 {
180 arr1[i] = "Hello, world! " + DB::toString(i);
181 arr2[i] = "Goodbye, world! " + DB::toString(i);
182 }
183
184 arr2 = std::move(arr1);
185 arr1.resize(n);
186
187 std::cerr
188 << "arr1.size(): " << arr1.size() << ", arr2.size(): " << arr2.size() << std::endl
189 << "arr1.data(): " << arr1.data() << ", arr2.data(): " << arr2.data() << std::endl
190 << "arr1[0]: " << arr1[0] << ", arr2[0]: " << arr2[0] << std::endl;
191 }
192
193 return 0;
194}
195