Debian Codegraph @

contrib/epee/include/math_helper.h

raw symbols SCIP
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27
28
29
30 #pragma once
31
32
33 #include <list>
34 #include <numeric>
35 #include <random>
36 #include <boost/timer/timer.hpp>
37 #include <boost/uuid/uuid.hpp>
38 #include <boost/uuid/random_generator.hpp>
39
40 #include "syncobj.h"
41 #include "time_helper.h"
42
43 namespace epee
44 {
45 namespace math_helper
46 {
47
48 template<typename val, int default_base>
49 class average
50 {
51 public:
52
53 average()
54 {
55 m_base = default_base;
56 m_last_avg_val = 0;
57 }
58
59 bool set_base()
60 {
61 CRITICAL_REGION_LOCAL(m_lock);
62
63 m_base = default_base;
64 if(m_list.size() > m_base)
65 m_list.resize(m_base);
66
67 return true;
68 }
69
70 typedef val value_type;
71
72 void push(const value_type& vl)
73 {
74 CRITICAL_REGION_LOCAL(m_lock);
75
76 //#ifndef DEBUG_STUB
77 m_list.push_back(vl);
78 if(m_list.size() > m_base )
79 m_list.pop_front();
80 //#endif
81 }
82
83 double update(const value_type& vl)
84 {
85 CRITICAL_REGION_LOCAL(m_lock);
86 //#ifndef DEBUG_STUB
87 push(vl);
88 //#endif
89
90 return get_avg();
91 }
92
93 double get_avg()
94 {
95 CRITICAL_REGION_LOCAL(m_lock);
96
97 value_type vl = std::accumulate(m_list.begin(), m_list.end(), value_type(0));
98 if(m_list.size())
99 return m_last_avg_val = (double)(vl/m_list.size());
100
101 return m_last_avg_val = (double)vl;
102 }
103
104 value_type get_last_val()
105 {
106 CRITICAL_REGION_LOCAL(m_lock);
107 if(m_list.size())
108 return m_list.back();
109
110 return 0;
111 }
112
113 private:
114 unsigned int m_base;
115 double m_last_avg_val;
116 std::list<value_type> m_list;
117 critical_section m_lock;
118 };
119
120
121 #ifdef WINDOWS_PLATFORM
122
123 /************************************************************************/
124 /* */
125 /************************************************************************/
126 class timing_guard_base
127 {
128 public:
129 virtual ~timing_guard_base(){};
130 };
131
132 template<class T>
133 class timing_guard: public timing_guard_base
134 {
135 public:
136 timing_guard(T& avrg):m_avrg(avrg)
137 {
138 m_start_ticks = ::GetTickCount();
139 }
140
141 ~timing_guard()
142 {
143 m_avrg.push(::GetTickCount()-m_start_ticks);
144 }
145
146 private:
147 T& m_avrg;
148 DWORD m_start_ticks;
149 };
150
151 template<class t_timing>
152 timing_guard_base* create_timing_guard(t_timing& timing){return new timing_guard<t_timing>(timing);}
153
154 #define BEGIN_TIMING_ZONE(timing_var) { boost::shared_ptr<math_helper::timing_guard_base> local_timing_guard_ptr(math_helper::create_timing_guard(timing_var));
155 #define END_TIMING_ZONE() }
156 #endif
157
158 //#ifdef WINDOWS_PLATFORM_EX
159 template<uint64_t default_time_window>
160 class speed
161 {
162 public:
163
164 speed()
165 {
166 m_time_window = default_time_window;
167 m_last_speed_value = 0;
168 }
169 bool chick()
170 {
171 #ifndef DEBUG_STUB
172 uint64_t ticks = misc_utils::get_tick_count();
173 CRITICAL_REGION_BEGIN(m_lock);
174 m_chicks.push_back(ticks);
175 CRITICAL_REGION_END();
176 //flush(ticks);
177 #endif
178 return true;
179 }
180
181 bool chick(size_t count)
182 {
183 for(size_t s = 0; s != count; s++)
184 chick();
185
186 return true;
187 }
188
189
190 size_t get_speed()
191 {
192 flush(misc_utils::get_tick_count());
193 return m_last_speed_value = m_chicks.size();
194 }
195 private:
196
197 bool flush(uint64_t ticks)
198 {
199 CRITICAL_REGION_BEGIN(m_lock);
200 std::list<uint64_t>::iterator it = m_chicks.begin();
201 while(it != m_chicks.end())
202 {
203 if(*it + m_time_window < ticks)
204 m_chicks.erase(it++);
205 else
206 break;
207 }
208 CRITICAL_REGION_END();
209 return true;
210 }
211
212 std::list<uint64_t> m_chicks;
213 uint64_t m_time_window;
214 size_t m_last_speed_value;
215 critical_section m_lock;
216 };
217 //#endif
218
219 template<class tlist>
220 void randomize_list(tlist& t_list)
221 {
222 for(typename tlist::iterator it = t_list.begin();it!=t_list.end();it++)
223 {
224 size_t offset = rand()%t_list.size();
225 typename tlist::iterator it_2 = t_list.begin();
226 for(size_t local_offset = 0;local_offset!=offset;local_offset++)
227 it_2++;
228 if(it_2 == it)
229 continue;
230 std::swap(*it_2, *it);
231 }
232
233 }
234 template<typename get_interval, bool start_immediate = true>
235 class once_a_time
236 {
237 uint64_t get_time() const
238 {
239 #ifdef _WIN32
240 FILETIME fileTime;
241 GetSystemTimeAsFileTime(&fileTime);
242 unsigned __int64 present = 0;
243 present |= fileTime.dwHighDateTime;
244 present = present << 32;
245 present |= fileTime.dwLowDateTime;
246 present /= 10; // mic-sec
247 return present;
248 #else
249 struct timeval tv;
250 gettimeofday(&tv, NULL);
251 return tv.tv_sec * 1000000 + tv.tv_usec;
252 #endif
253 }
254
255 void set_next_interval()
256 {
257 m_interval = get_interval()();
258 }
259
260 public:
261 once_a_time()
262 {
263 m_last_worked_time = 0;
264 if(!start_immediate)
265 m_last_worked_time = get_time();
266 set_next_interval();
267 }
268
269 void trigger()
270 {
271 m_last_worked_time = 0;
272 }
273
274 template<class functor_t>
275 bool do_call(functor_t functr)
276 {
277 uint64_t current_time = get_time();
278
279 if(current_time - m_last_worked_time > m_interval)
280 {
281 bool res = functr();
282 m_last_worked_time = get_time();
283 set_next_interval();
284 return res;
285 }
286 return true;
287 }
288
289 private:
290 uint64_t m_last_worked_time;
291 uint64_t m_interval;
292 };
293
294 template<uint64_t N> struct get_constant_interval { public: uint64_t operator()() const { return N; } };
295
296 template<int default_interval, bool start_immediate = true>
297 class once_a_time_seconds: public once_a_time<get_constant_interval<default_interval * (uint64_t)1000000>, start_immediate> {};
298 template<int default_interval, bool start_immediate = true>
299 class once_a_time_milliseconds: public once_a_time<get_constant_interval<default_interval * (uint64_t)1000>, start_immediate> {};
300 template<typename get_interval, bool start_immediate = true>
301 class once_a_time_seconds_range: public once_a_time<get_interval, start_immediate> {};
302 }
303 }