7 /** Field containing 2^i on i-th position, defined in modules.cpp */
8 extern const int powers
[31];
10 /** Helper template function computing the square of a number */
11 template<typename T
> inline T
sqr(T i
)
13 /** Helper template function computing the cube of a number */
14 template<typename T
> inline T
cube(T i
)
17 /** Returns i*2^bits */
18 template<typename T
> inline T
lShift(T i
,T bits
)
19 { ASSERT(bits
>=0); return i
<<bits
; }
21 /** Returns i/2^bits */
22 template<typename T
> inline T
rShift(T i
,T bits
)
23 { ASSERT(bits
>=0 && i
>=0); return i
>>bits
; }
25 /** Returns ceil(log2(i)) */
26 inline int log2ceil(int i
) {
37 /** How many short intervals (shifted by density) can fit into a long interval (discrete) */
38 inline int getCountForDensity(int longLength
,int density
,int shortLength
)
39 { return (longLength
-shortLength
)/density
+1; }
41 /** The same as above, but in 2D for squares fitting into a rectangle */
42 inline int getCountForDensity2D(int width
,int height
,int density
,int sideSize
) {
43 return getCountForDensity(width
,density
,sideSize
)
44 * getCountForDensity(height
,density
,sideSize
);
47 /** General bounds-checking template routine - returns max(low,min(value,high)) */
48 template<class T
> inline T
checkBoundsFunc(T low
,T value
,T high
) {
58 /** Struct for conversion between [0..1] Real and 0..(2^power-1) integer */
59 template<int power
=8,class R
=MTypes::Real
> struct Float2int
{
60 static R
convert(int i
)
61 { return std::ldexp( i
+R(0.5), -power
); }
63 static int convert(R r
)
64 { return (int)trunc(std::ldexp( r
, power
)); }
65 static int convertCheck(R r
)
66 { return checkBoundsFunc( 0, convert(r
), powers
[power
]-1 ); }
69 /** Counts the number of '\n' characters in a C-string */
70 inline int countEOLs(const char *s
) {
78 template<class T
> inline std::string
toString(const T
&what
) {
79 std::stringstream stream
;
86 template<class T
> struct NonConstType
{ typedef T Result
; };
87 template<class T
> struct NonConstType
<const T
> { typedef T Result
; };
89 /** Automatic version of const_cast for pointers */
90 template <class T
> inline T
* constCast(const T
* toCast
) { return const_cast<T
*>(toCast
); }
91 /** Automatic version of const_cast for references */
92 template <class T
> inline T
& constCast(const T
& toCast
) { return const_cast<T
&>(toCast
); }
94 /** Automatic helper cast from "T**" to "const T**" */
95 template <class T
> inline const T
** bogoCast(T
** toCast
)
96 { return const_cast<const T
**>(toCast
); }
98 /** Checking a condition - throws an exception if false */
99 inline void checkThrow(bool check
) { if (!check
) throw std::exception(); }
102 /* Auto-release pointer template altered to work more like an ordinary pointer *//*
103 template<class T> class Auto_ptr: public std::auto_ptr<T> {
105 Auto_ptr(T *t=0): std::auto_ptr<T>(t) {}
107 { return this->get(); }
108 Auto_ptr& operator=(T *t) {
116 /** Template object for automated deletion of pointers (useful in for_each) */
117 template <class T
> struct SingleDeleter
{
118 void operator()(T
*toDelete
) { delete toDelete
; }
120 /** Template object for automated deletion of field pointers (useful in for_each) */
121 template <class T
> struct MultiDeleter
{
122 void operator()(T
*toDelete
) { delete[] toDelete
; }
126 /** Deletes all pointers in a container (it has to support \c begin and \c end methods) */
127 template < class T
, template<class> class C
> inline
128 void clearContainer(const C
<T
*> &container
)
129 { for_each( container
.begin(), container
.end(), SingleDeleter
<T
>() ); }
132 #define clearContainer(cont_) \
134 typedef typeof(cont_) contType_; \
135 const contType_ &c_= cont_; \
136 for (contType_::const_iterator it_=c_.begin(); it_!=c_.end(); ++it_) \
141 template <class T
,int bulkKb
=64>
142 class BulkAllocator
{
143 enum { bulkCount
=(bulkKb
*1024)/sizeof(T
) };
145 std::vector
<T
*> pools
;
150 : nextIndex(bulkCount
) {}
151 BulkAllocator(const BulkAllocator
&DEBUG_ONLY(copy
))
152 { nextIndex
=bulkCount
; ASSERT(copy
.pools
.empty()); }
154 { for_each( pools
.begin(), pools
.end(), MultiDeleter
<T
>() ); }
158 ASSERT(nextIndex
<=bulkCount
);
159 // allocate a new bulk if needed
160 if (nextIndex
==bulkCount
) {
162 pools
.push_back( new T
[bulkCount
] );
164 return & (pools
.back()[nextIndex
++]);
166 T
* makeField(size_t count
) {
168 ASSERT(nextIndex
<=bulkCount
);
170 if (count
>bulkCount
/2) {
171 T
*result
=new T
[count
];
173 pools
.push_back(result
);
175 pools
.push_back(pools
.back());
176 *(pools
.end()-2)=result
;
181 if (nextIndex
+count
>bulkCount
) {
182 // some space will be wasted
184 pools
.push_back(new T
[bulkCount
]);
186 T
*result
=&pools
.back()[nextIndex
];
193 typedef void (*IncInt
)(int increment
);
195 volatile const bool *terminate
;
196 IncInt incMaxProgress
, incProgress
;
198 static void emptyFunction(int) {}
199 static const bool noTerminate
= false; ///< defined in modules.cpp
201 UpdateInfo( const bool &terminate_
= noTerminate
, IncInt incMaxProgress_
=&emptyFunction
202 , IncInt incProgress_
=&emptyFunction
)
203 : terminate(&terminate_
), incMaxProgress(incMaxProgress_
), incProgress(incProgress_
) {}
206 #endif // UTIL_HEADER_