… | |
… | |
6 | #else |
6 | #else |
7 | # define is_constant(c) 0 |
7 | # define is_constant(c) 0 |
8 | #endif |
8 | #endif |
9 | |
9 | |
10 | #include <cstddef> |
10 | #include <cstddef> |
|
|
11 | #include <new> |
|
|
12 | #include <vector> |
11 | |
13 | |
12 | #include <glib.h> |
14 | #include <glib.h> |
|
|
15 | |
|
|
16 | #include <shstr.h> |
|
|
17 | #include <traits.h> |
13 | |
18 | |
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
19 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
20 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
21 | |
|
|
22 | // very ugly macro that basicaly declares and initialises a variable |
|
|
23 | // that is in scope for the next statement only |
|
|
24 | // works only for stuff that can be assigned 0 and converts to false |
|
|
25 | // (note: works great for pointers) |
|
|
26 | // most ugly macro I ever wrote |
|
|
27 | #define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
16 | |
28 | |
17 | // makes dynamically allocated objects zero-initialised |
29 | // makes dynamically allocated objects zero-initialised |
18 | struct zero_initialised |
30 | struct zero_initialised |
19 | { |
31 | { |
20 | void *operator new (size_t s, void *p) |
32 | void *operator new (size_t s, void *p) |
… | |
… | |
191 | { |
203 | { |
192 | return !strcmp (a, b); |
204 | return !strcmp (a, b); |
193 | } |
205 | } |
194 | }; |
206 | }; |
195 | |
207 | |
196 | #include <vector> |
|
|
197 | |
|
|
198 | template<class obj> |
208 | template<class T> |
199 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
209 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
200 | { |
210 | { |
201 | typedef typename unordered_vector::iterator iterator; |
211 | typedef typename unordered_vector::iterator iterator; |
202 | |
212 | |
203 | void erase (unsigned int pos) |
213 | void erase (unsigned int pos) |
204 | { |
214 | { |
… | |
… | |
209 | } |
219 | } |
210 | |
220 | |
211 | void erase (iterator i) |
221 | void erase (iterator i) |
212 | { |
222 | { |
213 | erase ((unsigned int )(i - this->begin ())); |
223 | erase ((unsigned int )(i - this->begin ())); |
|
|
224 | } |
|
|
225 | }; |
|
|
226 | |
|
|
227 | template<class T, int T::* index> |
|
|
228 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
|
|
229 | { |
|
|
230 | void insert (T *obj) |
|
|
231 | { |
|
|
232 | assert (!(obj->*index)); |
|
|
233 | push_back (obj); |
|
|
234 | obj->*index = this->size (); |
|
|
235 | } |
|
|
236 | |
|
|
237 | void insert (T &obj) |
|
|
238 | { |
|
|
239 | insert (&obj); |
|
|
240 | } |
|
|
241 | |
|
|
242 | void erase (T *obj) |
|
|
243 | { |
|
|
244 | assert (obj->*index); |
|
|
245 | int pos = obj->*index; |
|
|
246 | obj->*index = 0; |
|
|
247 | |
|
|
248 | if (pos < this->size ()) |
|
|
249 | { |
|
|
250 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
251 | (*this)[pos - 1]->*index = pos; |
|
|
252 | } |
|
|
253 | |
|
|
254 | this->pop_back (); |
|
|
255 | } |
|
|
256 | |
|
|
257 | void erase (T &obj) |
|
|
258 | { |
|
|
259 | errase (&obj); |
214 | } |
260 | } |
215 | }; |
261 | }; |
216 | |
262 | |
217 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
263 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
218 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
264 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
… | |
… | |
233 | typedef double tstamp; |
279 | typedef double tstamp; |
234 | |
280 | |
235 | // return current time as timestampe |
281 | // return current time as timestampe |
236 | tstamp now (); |
282 | tstamp now (); |
237 | |
283 | |
|
|
284 | int similar_direction (int a, int b); |
|
|
285 | |
238 | #endif |
286 | #endif |
239 | |
287 | |