Release of synfigstudio 0.61.09 version. *****
[synfig.git] / ETL / tags / stable / ETL / _misc.h
1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Misc
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 ** Copyright (c) 2007 Chris Moore
8 **
9 ** This package is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU General Public License as
11 ** published by the Free Software Foundation; either version 2 of
12 ** the License, or (at your option) any later version.
13 **
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ** General Public License for more details.
18 **
19 ** === N O T E S ===========================================================
20 **
21 ** ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __ETL__MISC_H_
26 #define __ETL__MISC_H_
27
28 /* === H E A D E R S ======================================================= */
29 #include <cmath>
30
31 #include <math.h>
32
33 /* === M A C R O S ========================================================= */
34
35 /* === T Y P E D E F S ===================================================== */
36
37 /* === C L A S S E S & S T R U C T S ======================================= */
38
39 _ETL_BEGIN_NAMESPACE
40
41 template<typename I, typename T> inline I
42 binary_find(I begin, I end, const T& value)
43 {
44 #if 1
45         I iter(begin+(end-begin)/2);
46
47         while(end-begin>1 && !(*iter==value))
48         {
49                 ((*iter<value)?begin:end) = iter;
50
51                 iter = begin+(end-begin)/2;
52         }
53         return iter;
54 #else
55         size_t len_(end-begin);
56         size_t half_(len_/2);
57
58         I iter(begin);
59         iter+=half_;
60
61         while(len_>1 && !(*iter==value))
62         {
63                 ((*iter<value)?begin:end) = iter;
64
65                 len_=half_;
66                 half_/=2;
67
68                 iter=begin;
69                 iter+=half_;
70         }
71         return iter;
72 #endif
73 }
74
75 inline int round_to_int(const float x) {
76         /*!     \todo Isn't there some x86 FPU instruction for quickly
77         **      converting a float to a rounded integer? It's worth
78         **      looking into at some point... */
79         // return static_cast<int>(x+0.5f);                     // <-- (a) fast, but rounds -1.333 to 0!
80         // return static_cast<int>(rintf(x));           // <-- (b) slow, but correct
81     if (x>=0) return static_cast<int>(x + 0.5); // <-- slower than (a), but correct, and faster than (b)
82     else      return static_cast<int>(x - 0.5);
83 }
84 inline int round_to_int(const double x) {
85         // return static_cast<int>(x+0.5);
86         // return static_cast<int>(rint(x));
87         if (x>=0) return static_cast<int>(x + 0.5);
88     else      return static_cast<int>(x - 0.5);
89 }
90
91 inline int ceil_to_int(const float x) { return static_cast<int>(ceil(x)); }
92 inline int ceil_to_int(const double x) { return static_cast<int>(ceil(x)); }
93
94 inline int floor_to_int(const float x) { return static_cast<int>(x); }
95 inline int floor_to_int(const double x) { return static_cast<int>(x); }
96
97 _ETL_END_NAMESPACE
98
99 /* === E X T E R N S ======================================================= */
100
101 /* === E N D =============================================================== */
102
103 #endif