9 /** Field containing 2^i on i-th position, defined in modules.cpp */
10 extern const int powers
[31];
12 /** Helper template function computing the square of a number */
13 template<typename T
> inline T
sqr(T i
)
15 /** Helper template function computing the cube of a number */
16 template<typename T
> inline T
cube(T i
)
19 /** Returns i*2^bits */
20 template<typename T
> inline T
lShift(T i
,T bits
)
21 { ASSERT(bits
>=0); return i
<<bits
; }
23 /** Returns i/2^bits */
24 template<typename T
> inline T
rShift(T i
,T bits
)
25 { ASSERT(bits
>=0 && i
>=0); return i
>>bits
; }
27 /** Returns ceil(log2(i)) */
28 inline int log2ceil(int i
) {
39 /** A wrapper around isnan() because of compiler support */
40 template<class T
> inline bool isNaN(T num
) {
48 /** How many short intervals (shifted by density) can fit into a long interval (discrete) */
49 inline int getCountForDensity(int longLength
,int density
,int shortLength
)
50 { return (longLength
-shortLength
)/density
+1; }
52 /** The same as above, but in 2D for squares fitting into a rectangle */
53 inline int getCountForDensity2D(int width
,int height
,int density
,int sideSize
) {
54 return getCountForDensity(width
,density
,sideSize
)
55 * getCountForDensity(height
,density
,sideSize
);
58 /** General bounds-checking template routine - returns max(low,min(value,high)) */
59 template<class T
> inline T
checkBoundsFunc(T low
,T value
,T high
) {
67 /** Struct for conversion between 0-1 Real and 0-(2^power-1) integer */
68 template<int power
,class R
> struct Float2int
{
69 static R
convert(int i
)
70 { return std::ldexp( i
+R(0.5), -power
); }
72 static int convert(R r
)
73 { return (int)trunc(std::ldexp( r
, power
)); }
74 static int convertCheck(R r
)
75 { return checkBoundsFunc( 0, convert(r
), powers
[power
]-1 ); }
78 /** Counts the number of '\n' characters in a C-string */
79 inline int countEOLs(const char *s
) {
87 /** Converts any type to std::string via std::stringstream */
88 template<class T
> inline std::string
toString(const T
&what
) {
89 std::stringstream stream
;
96 /** Type convertor - NonConstType<T>::Result is a non-const variant of T or T itself if N/A */
97 template<class T
> struct NonConstType
{ typedef T Result
; };
98 template<class T
> struct NonConstType
<const T
> { typedef T Result
; };
100 /** Automatic version of const_cast for pointers */
101 template <class T
> inline T
* constCast(const T
* toCast
) { return const_cast<T
*>(toCast
); }
102 /** Automatic version of const_cast for references */
103 template <class T
> inline T
& constCast(const T
& toCast
) { return const_cast<T
&>(toCast
); }
105 /** Checking a condition - throws std::exception if false */
106 inline void checkThrow(bool check
) { if (!check
) throw std::exception(); }
109 /** Template object for automated deletion of pointers (useful in for_each) */
110 struct SingleDeleter
{
111 template <class T
> void operator()(T
*toDelete
) const { delete toDelete
; }
113 /** Template object for automated deletion of field pointers (useful in for_each) */
114 struct MultiDeleter
{
115 template <class T
> void operator()(T
*toDelete
) const { delete[] toDelete
; }
119 /** Deletes all pointers in a container (it has to support \c begin and \c end methods) */
120 template<class C
> void clearContainer(const C
&container
)
121 { for_each( container
.begin(), container
.end(), SingleDeleter() ); }
124 template <class T
,int bulkKb
=64>
125 class BulkAllocator
{
126 enum { bulkCount
=(bulkKb
*1024)/sizeof(T
) };
128 std::vector
<T
*> pools
;
133 : nextIndex(bulkCount
) {}
134 BulkAllocator(const BulkAllocator
&DEBUG_ONLY(copy
))
135 { nextIndex
=bulkCount
; ASSERT(copy
.pools
.empty()); }
137 { for_each( pools
.begin(), pools
.end(), MultiDeleter() ); }
141 ASSERT(nextIndex
<=bulkCount
);
142 // allocate a new bulk if needed
143 if (nextIndex
==bulkCount
) {
145 pools
.push_back( new T
[bulkCount
] );
147 return & (pools
.back()[nextIndex
++]);
149 T
* makeField(PtrInt count
) {
151 ASSERT(nextIndex
<=bulkCount
);
153 if (count
>bulkCount
/2) {
154 T
*result
= new T
[count
];
156 pools
.push_back(result
);
158 pools
.push_back(pools
.back());
159 *(pools
.end()-2)= result
;
164 if (nextIndex
+count
>bulkCount
) {
165 // some space will be wasted
167 pools
.push_back(new T
[bulkCount
]);
169 T
*result
=&pools
.back()[nextIndex
];
173 }; // BulkAllocator class
175 /** Structure providing support for progress update and interruption (used for encoding) */
177 typedef void (*IncInt
)(int increment
); ///< Type for used functions -> more readable code
179 static void emptyFunction(int) {} ///< does nothing, default for IncInt functions
180 static const bool noTerminate
= false; ///< default for #terminate, defined in modules.cpp
181 static const UpdateInfo none
; ///< empty UpdateInfo instance, in modules.cpp
183 volatile const bool *terminate
; ///< true if the action should be terminated
184 IncInt incMaxProgress
/// function for increasing the maximum progress (100%)
185 , incProgress
; ///< function for increasing the current progress
187 /** Initializes the structure from supplied parametres */
188 UpdateInfo( const bool &terminate_
, IncInt incMaxProgress_
, IncInt incProgress_
)
189 : terminate(&terminate_
), incMaxProgress(incMaxProgress_
), incProgress(incProgress_
)
190 { ASSERT(isValid()); }
192 bool isValid() const { return terminate
&& incMaxProgress
&& incProgress
; }
195 #endif // UTIL_HEADER_