| 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 | #pragma once |
| 30 | |
| 31 | #include <boost/utility/value_init.hpp> |
| 32 | #include <boost/shared_ptr.hpp> |
| 33 | #include <vector> |
| 34 | namespace epee |
| 35 | { |
| 36 | #define AUTO_VAL_INIT(v) boost::value_initialized<decltype(v)>() |
| 37 | |
| 38 | namespace misc_utils |
| 39 | { |
| 40 | bool sleep_no_w(long ms); |
| 41 | |
| 42 | template <typename T> |
| 43 | T get_mid(const T &a, const T &b) |
| 44 | { |
| 45 | //returns the average of two numbers; overflow safe and works with at least all integral and floating point types |
| 46 | //(a+b)/2 = (a/2) + (b/2) + ((a - 2*(a/2)) + (b - 2*(b/2)))/2 |
| 47 | return (a/2) + (b/2) + ((a - 2*(a/2)) + (b - 2*(b/2)))/2; |
| 48 | } |
| 49 | |
| 50 | template<class type_vec_type> |
| 51 | type_vec_type median(std::vector<type_vec_type> &v) |
| 52 | { |
| 53 | if(v.empty()) |
| 54 | return boost::value_initialized<type_vec_type>(); |
| 55 | if(v.size() == 1) |
| 56 | return v[0]; |
| 57 | |
| 58 | size_t n = (v.size()) / 2; |
| 59 | std::sort(v.begin(), v.end()); |
| 60 | //nth_element(v.begin(), v.begin()+n-1, v.end()); |
| 61 | if(v.size()%2) |
| 62 | {//1, 3, 5... |
| 63 | return v[n]; |
| 64 | }else |
| 65 | {//2, 4, 6... |
| 66 | return get_mid<type_vec_type>(v[n-1],v[n]); |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | |
| 71 | /************************************************************************/ |
| 72 | /* */ |
| 73 | /************************************************************************/ |
| 74 | |
| 75 | struct call_befor_die_base |
| 76 | { |
| 77 | virtual ~call_befor_die_base(){} |
| 78 | }; |
| 79 | |
| 80 | typedef boost::shared_ptr<call_befor_die_base> auto_scope_leave_caller; |
| 81 | |
| 82 | |
| 83 | template<class t_scope_leave_handler> |
| 84 | struct call_befor_die: public call_befor_die_base |
| 85 | { |
| 86 | t_scope_leave_handler m_func; |
| 87 | call_befor_die(t_scope_leave_handler f):m_func(f) |
| 88 | {} |
| 89 | ~call_befor_die() |
| 90 | { |
| 91 | try { m_func(); } |
| 92 | catch (...) { /* ignore */ } |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | template<class t_scope_leave_handler> |
| 97 | auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f) |
| 98 | { |
| 99 | auto_scope_leave_caller slc(new call_befor_die<t_scope_leave_handler>(f)); |
| 100 | return slc; |
| 101 | } |
| 102 | |
| 103 | template<typename T> struct struct_init: T |
| 104 | { |
| 105 | struct_init(): T{} {} |
| 106 | }; |
| 107 | |
| 108 | } |
| 109 | } |