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