Writing: fixed the year on the title page.
[fic.git] / util.h
blob12e90452cb47a6b73a325a526a5e573a77d476ac
1 #ifndef UTIL_HEADER_
2 #define UTIL_HEADER_
4 #include "headers.h"
5 #include "debug.h"
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)
12 { return i*i; }
13 /** Helper template function computing the cube of a number */
14 template<typename T> inline T cube(T i)
15 { return i*i*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) {
27 ASSERT(i>0);
28 --i;
29 int result= 0;
30 while (i) {
31 i/= 2;
32 ++result;
34 return result;
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) {
49 if (value>low)
50 if (value<high)
51 return value;
52 else
53 return high;
54 else
55 return low;
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) {
71 int result= 0;
72 for (; *s; ++s)
73 if (*s=='\n')
74 ++result;
75 return result;
78 template<class T> inline std::string toString(const T &what) {
79 std::stringstream stream;
80 stream << what;
81 std::string result;
82 stream >> result;
83 return result;
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> {
104 public:
105 Auto_ptr(T *t=0): std::auto_ptr<T>(t) {}
106 operator T*() const
107 { return this->get(); }
108 Auto_ptr& operator=(T *t) {
109 reset(t);
110 return *this;
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; }
125 #ifndef __ICC
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>() ); }
131 #else
132 #define clearContainer(cont_) \
133 do { \
134 typedef typeof(cont_) contType_; \
135 const contType_ &c_= cont_; \
136 for (contType_::const_iterator it_=c_.begin(); it_!=c_.end(); ++it_) \
137 delete *it_; \
138 } while (false)
139 #endif
141 template <class T,int bulkKb=64>
142 class BulkAllocator {
143 enum { bulkCount=(bulkKb*1024)/sizeof(T) };
145 std::vector<T*> pools;
146 size_t nextIndex;
148 public:
149 BulkAllocator()
150 : nextIndex(bulkCount) {}
151 BulkAllocator(const BulkAllocator &DEBUG_ONLY(copy))
152 { nextIndex=bulkCount; ASSERT(copy.pools.empty()); }
153 ~BulkAllocator()
154 { for_each( pools.begin(), pools.end(), MultiDeleter<T>() ); }
156 T* make() {
157 // check for errors
158 ASSERT(nextIndex<=bulkCount);
159 // allocate a new bulk if needed
160 if (nextIndex==bulkCount) {
161 nextIndex= 0;
162 pools.push_back( new T[bulkCount] );
164 return & (pools.back()[nextIndex++]);
166 T* makeField(size_t count) {
167 // check for errors
168 ASSERT(nextIndex<=bulkCount);
170 if (count>bulkCount/2) {
171 T *result=new T[count];
172 if (pools.empty())
173 pools.push_back(result);
174 else {
175 pools.push_back(pools.back());
176 *(pools.end()-2)=result;
178 return result;
181 if (nextIndex+count>bulkCount) {
182 // some space will be wasted
183 nextIndex=0;
184 pools.push_back(new T[bulkCount]);
186 T *result=&pools.back()[nextIndex];
187 nextIndex+=count;
188 return result;
192 struct UpdateInfo {
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_