1 |
root |
1.1 |
#ifndef RXVT_UTIL_H |
2 |
|
|
#define RXVT_UTIL_H |
3 |
|
|
|
4 |
root |
1.19 |
#include <cstdlib> |
5 |
root |
1.4 |
#include <cstring> |
6 |
|
|
|
7 |
root |
1.27 |
using namespace std; |
8 |
|
|
|
9 |
root |
1.15 |
#define PP_CONCAT_(a, b) a ## b |
10 |
|
|
#define PP_CONCAT(a, b) PP_CONCAT_(a, b) |
11 |
|
|
#define PP_STRINGIFY_(a) #a |
12 |
|
|
#define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
13 |
|
|
|
14 |
ayin |
1.28 |
#define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
15 |
root |
1.22 |
|
16 |
root |
1.31 |
#if __GNUC__ >= 4 |
17 |
root |
1.30 |
# define rxvt_attribute(x) __attribute__(x) |
18 |
root |
1.37 |
# define expect(expr,value) __builtin_expect ((expr),(value)) |
19 |
root |
1.30 |
#else |
20 |
|
|
# define rxvt_attribute(x) |
21 |
root |
1.37 |
# define expect(expr,value) (expr) |
22 |
root |
1.24 |
#endif |
23 |
|
|
|
24 |
root |
1.37 |
// put into ifs if you are very sure that the expression |
25 |
|
|
// is mostly true or mosty false. note that these return |
26 |
|
|
// booleans, not the expression. |
27 |
|
|
#define expect_false(expr) expect ((expr) != 0, 0) |
28 |
|
|
#define expect_true(expr) expect ((expr) != 0, 1) |
29 |
|
|
|
30 |
root |
1.30 |
#define NORETURN rxvt_attribute ((noreturn)) |
31 |
|
|
#define UNUSED rxvt_attribute ((unused)) |
32 |
|
|
#define CONST rxvt_attribute ((const)) |
33 |
root |
1.24 |
|
34 |
|
|
// increases code size unless -fno-enforce-eh-specs |
35 |
|
|
#if __GNUC__ |
36 |
|
|
# define NOTHROW |
37 |
|
|
# define THROW(x) |
38 |
|
|
#else |
39 |
|
|
# define NOTHROW throw() |
40 |
|
|
# define THROW(x) throw x |
41 |
|
|
#endif |
42 |
|
|
|
43 |
root |
1.1 |
extern class byteorder { |
44 |
|
|
static unsigned int e; // at least 32 bits |
45 |
|
|
public: |
46 |
|
|
byteorder (); |
47 |
|
|
|
48 |
|
|
static bool big_endian () { return e == 0x11223344; }; |
49 |
|
|
static bool network () { return e == 0x11223344; }; |
50 |
|
|
static bool little_endian () { return e == 0x44332211; }; |
51 |
|
|
static bool vax () { return e == 0x44332211; }; |
52 |
|
|
} byteorder; |
53 |
|
|
|
54 |
root |
1.16 |
// various utility functions |
55 |
root |
1.13 |
template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
56 |
|
|
template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
57 |
|
|
template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
58 |
|
|
template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
59 |
|
|
|
60 |
|
|
template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
61 |
|
|
template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = v < (T)a ? a : v >(T)b ? b : v; } |
62 |
|
|
|
63 |
root |
1.14 |
template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
64 |
root |
1.8 |
|
65 |
root |
1.25 |
template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); } |
66 |
|
|
|
67 |
root |
1.21 |
// linear interpolation |
68 |
|
|
template<typename T, typename U, typename P> |
69 |
|
|
static inline |
70 |
|
|
T lerp (T a, U b, P p) |
71 |
|
|
{ |
72 |
root |
1.26 |
return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
73 |
root |
1.21 |
} |
74 |
|
|
|
75 |
ayin |
1.32 |
template <typename I, typename T> |
76 |
|
|
I find (I first, I last, const T& value) |
77 |
|
|
{ |
78 |
|
|
while (first != last && *first != value) |
79 |
|
|
++first; |
80 |
|
|
|
81 |
|
|
return first; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
// return a very temporary (and never deallocated) buffer. keep small. |
85 |
|
|
void *rxvt_temp_buf (int len); |
86 |
|
|
|
87 |
|
|
template<typename T> |
88 |
|
|
static inline T * |
89 |
|
|
rxvt_temp_buf (int len) |
90 |
|
|
{ |
91 |
|
|
return (T *)rxvt_temp_buf (len * sizeof (T)); |
92 |
|
|
} |
93 |
|
|
|
94 |
root |
1.22 |
// some bit functions, xft fuck me plenty |
95 |
|
|
#if HAVE_GCC_BUILTINS |
96 |
root |
1.30 |
static inline int ctz (unsigned int x) { return __builtin_ctz (x); } |
97 |
|
|
static inline int popcount (unsigned int x) { return __builtin_popcount (x); } |
98 |
root |
1.22 |
#else |
99 |
|
|
// count trailing zero bits and count # of one bits |
100 |
root |
1.24 |
int ctz (unsigned int x) CONST; |
101 |
|
|
int popcount (unsigned int x) CONST; |
102 |
root |
1.22 |
#endif |
103 |
|
|
|
104 |
root |
1.11 |
// in range including end |
105 |
|
|
#define IN_RANGE_INC(val,beg,end) \ |
106 |
root |
1.9 |
((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
107 |
root |
1.1 |
|
108 |
root |
1.11 |
// in range excluding end |
109 |
|
|
#define IN_RANGE_EXC(val,beg,end) \ |
110 |
|
|
((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
111 |
|
|
|
112 |
ayin |
1.36 |
// for m >= -n, ensure remainder lies between 0..n-1 |
113 |
|
|
#define MOD(m,n) (((m) + (n)) % (n)) |
114 |
|
|
|
115 |
root |
1.16 |
// makes dynamically allocated objects zero-initialised |
116 |
root |
1.38 |
struct zero_initialized |
117 |
|
|
{ |
118 |
root |
1.4 |
void *operator new (size_t s); |
119 |
|
|
void operator delete (void *p, size_t s); |
120 |
|
|
}; |
121 |
|
|
|
122 |
|
|
/* simplevec taken (and heavily modified), from: |
123 |
ayin |
1.29 |
* |
124 |
root |
1.4 |
* MICO --- a free CORBA implementation |
125 |
|
|
* Copyright (C) 1997-98 Kay Roemer & Arno Puder |
126 |
|
|
*/ |
127 |
|
|
template<class T> |
128 |
root |
1.38 |
struct simplevec |
129 |
|
|
{ |
130 |
root |
1.4 |
typedef T* iterator; |
131 |
|
|
typedef const T* const_iterator; |
132 |
|
|
typedef unsigned long size_type; |
133 |
|
|
|
134 |
|
|
private: |
135 |
|
|
size_type _last, _size; |
136 |
|
|
T *_buf; |
137 |
|
|
|
138 |
|
|
public: |
139 |
|
|
const_iterator begin () const |
140 |
|
|
{ |
141 |
root |
1.8 |
return &_buf[0]; |
142 |
root |
1.4 |
} |
143 |
|
|
iterator begin () |
144 |
|
|
{ |
145 |
root |
1.8 |
return &_buf[0]; |
146 |
root |
1.4 |
} |
147 |
|
|
const_iterator end () const |
148 |
|
|
{ |
149 |
root |
1.8 |
return &_buf[_last]; |
150 |
root |
1.4 |
} |
151 |
|
|
iterator end () |
152 |
|
|
{ |
153 |
root |
1.8 |
return &_buf[_last]; |
154 |
root |
1.4 |
} |
155 |
|
|
size_type capacity () const |
156 |
|
|
{ |
157 |
root |
1.8 |
return _size; |
158 |
root |
1.4 |
} |
159 |
|
|
size_type size () const |
160 |
|
|
{ |
161 |
root |
1.8 |
return _last; |
162 |
root |
1.4 |
} |
163 |
|
|
|
164 |
|
|
private: |
165 |
|
|
static T *alloc (size_type n) |
166 |
|
|
{ |
167 |
root |
1.8 |
return (T *)::operator new ((size_t) (n * sizeof (T))); |
168 |
root |
1.4 |
} |
169 |
|
|
static void dealloc (T *buf) |
170 |
|
|
{ |
171 |
root |
1.8 |
if (buf) |
172 |
|
|
::operator delete (buf); |
173 |
root |
1.4 |
} |
174 |
|
|
|
175 |
|
|
void reserve (iterator where, size_type n) |
176 |
|
|
{ |
177 |
root |
1.8 |
if (_last + n <= _size) { |
178 |
|
|
memmove (where+n, where, (end ()-where)*sizeof (T)); |
179 |
|
|
} else { |
180 |
|
|
size_type sz = _last+n; |
181 |
|
|
sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); |
182 |
|
|
T *nbuf = alloc (sz); |
183 |
|
|
if (_buf) { |
184 |
|
|
memcpy (nbuf, begin (), (where-begin ())*sizeof (T)); |
185 |
|
|
memcpy (nbuf + (where-begin ()) + n, where, |
186 |
|
|
(end ()-where)*sizeof (T)); |
187 |
|
|
dealloc (_buf); |
188 |
|
|
} |
189 |
|
|
_buf = nbuf; |
190 |
|
|
_size = sz; |
191 |
|
|
} |
192 |
root |
1.4 |
} |
193 |
|
|
|
194 |
|
|
public: |
195 |
|
|
void reserve (size_type sz) |
196 |
|
|
{ |
197 |
root |
1.8 |
if (_size < sz) { |
198 |
|
|
sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); |
199 |
|
|
T *nbuf = alloc (sz); |
200 |
|
|
if (_buf) { |
201 |
|
|
memcpy (nbuf, begin (), size ()*sizeof (T)); |
202 |
|
|
dealloc (_buf); |
203 |
|
|
} |
204 |
|
|
_buf = nbuf; |
205 |
|
|
_size = sz; |
206 |
|
|
} |
207 |
root |
1.4 |
} |
208 |
|
|
simplevec () |
209 |
|
|
: _last(0), _size(0), _buf(0) |
210 |
|
|
{ |
211 |
|
|
} |
212 |
|
|
simplevec (size_type n, const T& t = T ()) |
213 |
|
|
: _last(0), _size(0), _buf(0) |
214 |
|
|
{ |
215 |
root |
1.8 |
insert (begin (), n, t); |
216 |
root |
1.4 |
} |
217 |
|
|
simplevec (const_iterator first, const_iterator last) |
218 |
|
|
: _last(0), _size(0), _buf(0) |
219 |
|
|
{ |
220 |
root |
1.8 |
insert (begin (), first, last); |
221 |
root |
1.4 |
} |
222 |
|
|
simplevec (const simplevec<T> &v) |
223 |
|
|
: _last(0), _size(0), _buf(0) |
224 |
|
|
{ |
225 |
root |
1.8 |
reserve (v._last); |
226 |
|
|
memcpy (_buf, v.begin (), v.size ()*sizeof (T)); |
227 |
|
|
_last = v._last; |
228 |
root |
1.4 |
} |
229 |
|
|
simplevec<T> &operator= (const simplevec<T> &v) |
230 |
|
|
{ |
231 |
root |
1.8 |
if (this != &v) { |
232 |
|
|
_last = 0; |
233 |
|
|
reserve (v._last); |
234 |
|
|
memcpy (_buf, v.begin (), v.size ()*sizeof (T)); |
235 |
|
|
_last = v._last; |
236 |
|
|
} |
237 |
root |
1.4 |
return *this; |
238 |
|
|
} |
239 |
|
|
~simplevec () |
240 |
|
|
{ |
241 |
root |
1.8 |
dealloc (_buf); |
242 |
root |
1.4 |
} |
243 |
|
|
const T &front () const |
244 |
|
|
{ |
245 |
root |
1.8 |
//ministl_assert (size () > 0); |
246 |
|
|
return _buf[0]; |
247 |
root |
1.4 |
} |
248 |
|
|
T &front () |
249 |
|
|
{ |
250 |
root |
1.8 |
//ministl_assert (size () > 0); |
251 |
|
|
return _buf[0]; |
252 |
root |
1.4 |
} |
253 |
|
|
const T &back () const |
254 |
|
|
{ |
255 |
root |
1.8 |
//ministl_assert (size () > 0); |
256 |
|
|
return _buf[_last-1]; |
257 |
root |
1.4 |
} |
258 |
|
|
T &back () |
259 |
|
|
{ |
260 |
root |
1.8 |
//ministl_assert (size () > 0); |
261 |
|
|
return _buf[_last-1]; |
262 |
root |
1.4 |
} |
263 |
|
|
bool empty () const |
264 |
|
|
{ |
265 |
root |
1.8 |
return _last == 0; |
266 |
root |
1.4 |
} |
267 |
|
|
void clear () |
268 |
|
|
{ |
269 |
root |
1.8 |
_last = 0; |
270 |
root |
1.4 |
} |
271 |
|
|
void push_back (const T &t) |
272 |
|
|
{ |
273 |
root |
1.8 |
reserve (_last+1); |
274 |
|
|
*end () = t; |
275 |
|
|
++_last; |
276 |
root |
1.4 |
} |
277 |
|
|
void push_back (T &t) |
278 |
|
|
{ |
279 |
root |
1.8 |
reserve (_last+1); |
280 |
|
|
*end () = t; |
281 |
|
|
++_last; |
282 |
root |
1.4 |
} |
283 |
|
|
void pop_back () |
284 |
|
|
{ |
285 |
root |
1.8 |
//ministl_assert (size () > 0); |
286 |
|
|
--_last; |
287 |
root |
1.4 |
} |
288 |
|
|
const T &operator[] (size_type idx) const |
289 |
|
|
{ |
290 |
root |
1.8 |
//ministl_assert (idx < size ()); |
291 |
|
|
return _buf[idx]; |
292 |
root |
1.4 |
} |
293 |
|
|
T &operator[] (size_type idx) |
294 |
|
|
{ |
295 |
root |
1.8 |
//ministl_assert (idx < size ()); |
296 |
|
|
return _buf[idx]; |
297 |
root |
1.4 |
} |
298 |
|
|
iterator insert (iterator pos, const T &t) |
299 |
|
|
{ |
300 |
root |
1.8 |
//ministl_assert (pos <= end ()); |
301 |
|
|
long at = pos - begin (); |
302 |
|
|
reserve (pos, 1); |
303 |
|
|
pos = begin ()+at; |
304 |
|
|
*pos = t; |
305 |
|
|
++_last; |
306 |
|
|
return pos; |
307 |
root |
1.4 |
} |
308 |
|
|
iterator insert (iterator pos, const_iterator first, const_iterator last) |
309 |
|
|
{ |
310 |
|
|
//ministl_assert (pos <= end ()); |
311 |
root |
1.8 |
long n = last - first; |
312 |
|
|
long at = pos - begin (); |
313 |
|
|
if (n > 0) { |
314 |
|
|
reserve (pos, n); |
315 |
|
|
pos = begin ()+at; |
316 |
|
|
memcpy (pos, first, (last-first)*sizeof (T)); |
317 |
|
|
_last += n; |
318 |
|
|
} |
319 |
|
|
return pos; |
320 |
root |
1.4 |
} |
321 |
|
|
iterator insert (iterator pos, size_type n, const T &t) |
322 |
|
|
{ |
323 |
|
|
//ministl_assert (pos <= end ()); |
324 |
root |
1.8 |
long at = pos - begin (); |
325 |
|
|
if (n > 0) { |
326 |
|
|
reserve (pos, n); |
327 |
|
|
pos = begin ()+at; |
328 |
|
|
for (int i = 0; i < n; ++i) |
329 |
|
|
pos[i] = t; |
330 |
|
|
_last += n; |
331 |
|
|
} |
332 |
|
|
return pos; |
333 |
root |
1.4 |
} |
334 |
|
|
void erase (iterator first, iterator last) |
335 |
|
|
{ |
336 |
root |
1.8 |
if (last != first) { |
337 |
root |
1.18 |
memmove (first, last, (end () - last) * sizeof (T)); |
338 |
root |
1.8 |
_last -= last - first; |
339 |
|
|
} |
340 |
root |
1.4 |
} |
341 |
|
|
void erase (iterator pos) |
342 |
|
|
{ |
343 |
|
|
if (pos != end ()) { |
344 |
root |
1.18 |
memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T)); |
345 |
root |
1.4 |
--_last; |
346 |
|
|
} |
347 |
|
|
} |
348 |
root |
1.8 |
void swap (simplevec<T> &t) |
349 |
|
|
{ |
350 |
|
|
::swap(_last, t._last); |
351 |
|
|
::swap(_size, t._size); |
352 |
|
|
::swap(_buf, t._buf); |
353 |
|
|
} |
354 |
root |
1.4 |
}; |
355 |
|
|
|
356 |
|
|
template<class T> |
357 |
|
|
bool operator== (const simplevec<T> &v1, const simplevec<T> &v2) |
358 |
|
|
{ |
359 |
|
|
if (v1.size () != v2.size ()) |
360 |
root |
1.8 |
return false; |
361 |
root |
1.4 |
return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T)); |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
template<class T> |
365 |
|
|
bool operator< (const simplevec<T> &v1, const simplevec<T> &v2) |
366 |
|
|
{ |
367 |
|
|
unsigned long minlast = min (v1.size (), v2.size ()); |
368 |
|
|
for (unsigned long i = 0; i < minlast; ++i) { |
369 |
|
|
if (v1[i] < v2[i]) |
370 |
root |
1.8 |
return true; |
371 |
|
|
if (v2[i] < v1[i]) |
372 |
|
|
return false; |
373 |
root |
1.4 |
} |
374 |
|
|
return v1.size () < v2.size (); |
375 |
|
|
} |
376 |
|
|
|
377 |
root |
1.1 |
|
378 |
|
|
template<typename T> |
379 |
|
|
struct vector : simplevec<T> |
380 |
root |
1.35 |
{ |
381 |
|
|
}; |
382 |
root |
1.1 |
|
383 |
ayin |
1.32 |
struct stringvec : simplevec<char *> |
384 |
|
|
{ |
385 |
|
|
~stringvec () |
386 |
|
|
{ |
387 |
|
|
for (char **c = begin (); c != end (); c++) |
388 |
|
|
free (*c); |
389 |
|
|
} |
390 |
|
|
}; |
391 |
|
|
|
392 |
root |
1.34 |
#if 0 |
393 |
root |
1.1 |
template<typename T> |
394 |
root |
1.38 |
struct rxvt_vec : simplevec<void *> |
395 |
|
|
{ |
396 |
root |
1.1 |
typedef T *iterator; |
397 |
|
|
|
398 |
|
|
void push_back (T d) { simplevec<void *>::push_back ((void *)d); } |
399 |
|
|
T pop_back () { return (T*)simplevec<void *>::pop_back (); } |
400 |
|
|
void erase (int i) { erase (begin () + i); } |
401 |
|
|
void erase (iterator i) { simplevec<void *>::erase ((void **)i); } |
402 |
|
|
iterator begin () const { return (iterator)simplevec<void *>::begin (); } |
403 |
|
|
iterator end () const { return (iterator)simplevec<void *>::end (); } |
404 |
|
|
T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
405 |
|
|
const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
406 |
|
|
}; |
407 |
root |
1.34 |
#endif |
408 |
root |
1.1 |
|
409 |
|
|
template<typename T> |
410 |
root |
1.38 |
struct auto_ptr |
411 |
|
|
{ |
412 |
root |
1.1 |
T *p; |
413 |
|
|
|
414 |
|
|
auto_ptr () : p (0) { } |
415 |
|
|
auto_ptr (T *a) : p (a) { } |
416 |
|
|
|
417 |
|
|
auto_ptr (auto_ptr<T> &a) |
418 |
|
|
{ |
419 |
|
|
p = a.p; |
420 |
|
|
a.p = 0; |
421 |
|
|
} |
422 |
|
|
|
423 |
|
|
template<typename A> |
424 |
|
|
auto_ptr (auto_ptr<A> &a) |
425 |
|
|
{ |
426 |
|
|
p = a.p; |
427 |
|
|
a.p = 0; |
428 |
|
|
} |
429 |
|
|
|
430 |
|
|
~auto_ptr () |
431 |
|
|
{ |
432 |
|
|
delete p; |
433 |
|
|
} |
434 |
|
|
|
435 |
|
|
// void because it makes sense in our context |
436 |
|
|
void operator = (T *a) |
437 |
|
|
{ |
438 |
|
|
delete p; |
439 |
|
|
p = a; |
440 |
|
|
} |
441 |
|
|
|
442 |
|
|
void operator = (auto_ptr &a) |
443 |
|
|
{ |
444 |
|
|
*this = a.p; |
445 |
|
|
a.p = 0; |
446 |
|
|
} |
447 |
|
|
|
448 |
|
|
template<typename A> |
449 |
|
|
void operator = (auto_ptr<A> &a) |
450 |
|
|
{ |
451 |
|
|
*this = a.p; |
452 |
|
|
a.p = 0; |
453 |
|
|
} |
454 |
|
|
|
455 |
|
|
operator T * () const { return p; } |
456 |
|
|
|
457 |
|
|
T *operator -> () const { return p; } |
458 |
|
|
T &operator * () const { return *p; } |
459 |
|
|
|
460 |
|
|
T *get () |
461 |
|
|
{ |
462 |
|
|
T *r = p; |
463 |
|
|
p = 0; |
464 |
|
|
return r; |
465 |
|
|
} |
466 |
|
|
}; |
467 |
|
|
|
468 |
|
|
typedef auto_ptr<char> auto_str; |
469 |
root |
1.20 |
|
470 |
root |
1.1 |
#endif |
471 |
|
|
|