Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / ETL / tags / ETL_0_04_10_rc3 / 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         /*!     \fixme 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);
79 }
80 inline int round_to_int(const double x) { return static_cast<int>(x+0.5); }
81
82 inline int ceil_to_int(const float x) { return static_cast<int>(ceil(x)); }
83 inline int ceil_to_int(const double x) { return static_cast<int>(ceil(x)); }
84
85 inline int floor_to_int(const float x) { return static_cast<int>(x); }
86 inline int floor_to_int(const double x) { return static_cast<int>(x); }
87
88 _ETL_END_NAMESPACE
89
90 /* === E X T E R N S ======================================================= */
91
92 /* === E N D =============================================================== */
93
94 #endif