1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * Copyright (c) 2020 Kohei Yoshida
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  ************************************************************************/
28 
29 #include <mdds/multi_type_vector.hpp>
30 #include <mdds/multi_type_vector_trait.hpp>
31 
32 #include <stdio.h>
33 #include <string>
34 #include <sys/time.h>
35 
36 namespace {
37 
38 class stack_printer
39 {
40 public:
stack_printer(const char * msg)41     explicit stack_printer(const char* msg) :
42         msMsg(msg)
43     {
44         fprintf(stdout, "%s: --begin\n", msMsg.c_str());
45         mfStartTime = getTime();
46     }
47 
~stack_printer()48     ~stack_printer()
49     {
50         double fEndTime = getTime();
51         fprintf(stdout, "%s: --end (duration: %g sec)\n", msMsg.c_str(), (fEndTime-mfStartTime));
52     }
53 
printTime(int line) const54     void printTime(int line) const
55     {
56         double fEndTime = getTime();
57         fprintf(stdout, "%s: --(%d) (duration: %g sec)\n", msMsg.c_str(), line, (fEndTime-mfStartTime));
58     }
59 
60 private:
getTime() const61     double getTime() const
62     {
63         timeval tv;
64         gettimeofday(&tv, NULL);
65         return tv.tv_sec + tv.tv_usec / 1000000.0;
66     }
67 
68     ::std::string msMsg;
69     double mfStartTime;
70 };
71 
72 }
73 
74 
run_no_position_hint()75 void run_no_position_hint()
76 {
77     stack_printer __stack_printer__("::run_no_position_hint");
78 
79     using mtv_type = mdds::multi_type_vector<mdds::mtv::element_block_func>;
80 
81     size_t size = 50000;
82 
83     // Initialize the container with one empty block of size 50000.
84     mtv_type db(size);
85 
86     // Set non-empty value at every other logical position from top down.
87     for (size_t i = 0; i < size; ++i)
88     {
89         if (i % 2)
90             db.set<double>(i, 1.0);
91     }
92 }
93 
run_with_position_hint()94 void run_with_position_hint()
95 {
96     stack_printer __stack_printer__("::run_with_position_hint");
97 
98     using mtv_type = mdds::multi_type_vector<mdds::mtv::element_block_func>;
99 
100     size_t size = 50000;
101 
102     // Initialize the container with one empty block of size 50000.
103     mtv_type db(size);
104     mtv_type::iterator pos = db.begin();
105 
106     // Set non-empty value at every other logical position from top down.
107     for (size_t i = 0; i < size; ++i)
108     {
109         if (i % 2)
110             // Pass the position hint as the first argument, and receive a new
111             // one returned from the method for the next call.
112             pos = db.set<double>(pos, i, 1.0);
113     }
114 }
115 
main()116 int main()
117 {
118     run_no_position_hint();
119     run_with_position_hint();
120 
121     return EXIT_SUCCESS;
122 }
123 
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
125