… | |
… | |
11 | #define PP_STRINGIFY_(a) #a |
11 | #define PP_STRINGIFY_(a) #a |
12 | #define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
12 | #define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
13 | |
13 | |
14 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
14 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
15 | |
15 | |
16 | #if __GNUC__ >= 3 |
16 | #if __GNUC__ >= 4 |
17 | # define rxvt_attribute(x) __attribute__(x) |
17 | # define rxvt_attribute(x) __attribute__(x) |
18 | #else |
18 | #else |
19 | # define rxvt_attribute(x) |
19 | # define rxvt_attribute(x) |
20 | #endif |
20 | #endif |
21 | |
21 | |
… | |
… | |
60 | template<typename T, typename U, typename P> |
60 | template<typename T, typename U, typename P> |
61 | static inline |
61 | static inline |
62 | T lerp (T a, U b, P p) |
62 | T lerp (T a, U b, P p) |
63 | { |
63 | { |
64 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
64 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
|
|
65 | } |
|
|
66 | |
|
|
67 | template <typename I, typename T> |
|
|
68 | I find (I first, I last, const T& value) |
|
|
69 | { |
|
|
70 | while (first != last && *first != value) |
|
|
71 | ++first; |
|
|
72 | |
|
|
73 | return first; |
|
|
74 | } |
|
|
75 | |
|
|
76 | // return a very temporary (and never deallocated) buffer. keep small. |
|
|
77 | void *rxvt_temp_buf (int len); |
|
|
78 | |
|
|
79 | template<typename T> |
|
|
80 | static inline T * |
|
|
81 | rxvt_temp_buf (int len) |
|
|
82 | { |
|
|
83 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
65 | } |
84 | } |
66 | |
85 | |
67 | // some bit functions, xft fuck me plenty |
86 | // some bit functions, xft fuck me plenty |
68 | #if HAVE_GCC_BUILTINS |
87 | #if HAVE_GCC_BUILTINS |
69 | static inline int ctz (unsigned int x) { return __builtin_ctz (x); } |
88 | static inline int ctz (unsigned int x) { return __builtin_ctz (x); } |
… | |
… | |
79 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
98 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
80 | |
99 | |
81 | // in range excluding end |
100 | // in range excluding end |
82 | #define IN_RANGE_EXC(val,beg,end) \ |
101 | #define IN_RANGE_EXC(val,beg,end) \ |
83 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
102 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
103 | |
|
|
104 | // for m >= -n, ensure remainder lies between 0..n-1 |
|
|
105 | #define MOD(m,n) (((m) + (n)) % (n)) |
84 | |
106 | |
85 | // makes dynamically allocated objects zero-initialised |
107 | // makes dynamically allocated objects zero-initialised |
86 | struct zero_initialized { |
108 | struct zero_initialized { |
87 | void *operator new (size_t s); |
109 | void *operator new (size_t s); |
88 | void operator delete (void *p, size_t s); |
110 | void operator delete (void *p, size_t s); |
… | |
… | |
343 | } |
365 | } |
344 | |
366 | |
345 | |
367 | |
346 | template<typename T> |
368 | template<typename T> |
347 | struct vector : simplevec<T> |
369 | struct vector : simplevec<T> |
348 | { }; |
370 | { |
|
|
371 | }; |
|
|
372 | |
|
|
373 | struct stringvec : simplevec<char *> |
|
|
374 | { |
|
|
375 | ~stringvec () |
|
|
376 | { |
|
|
377 | for (char **c = begin (); c != end (); c++) |
|
|
378 | free (*c); |
|
|
379 | } |
|
|
380 | }; |
349 | |
381 | |
350 | #if 0 |
382 | #if 0 |
351 | template<typename T> |
383 | template<typename T> |
352 | struct rxvt_vec : simplevec<void *> { |
384 | struct rxvt_vec : simplevec<void *> { |
353 | typedef T *iterator; |
385 | typedef T *iterator; |
… | |
… | |
361 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
393 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
362 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
394 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
363 | }; |
395 | }; |
364 | #endif |
396 | #endif |
365 | |
397 | |
366 | template <typename I, typename T> |
|
|
367 | I find (I first, I last, const T& value) |
|
|
368 | { |
|
|
369 | while (first != last && *first != value) |
|
|
370 | ++first; |
|
|
371 | |
|
|
372 | return first; |
|
|
373 | } |
|
|
374 | |
|
|
375 | template<typename T> |
398 | template<typename T> |
376 | struct auto_ptr { |
399 | struct auto_ptr { |
377 | T *p; |
400 | T *p; |
378 | |
401 | |
379 | auto_ptr () : p (0) { } |
402 | auto_ptr () : p (0) { } |
… | |
… | |
430 | } |
453 | } |
431 | }; |
454 | }; |
432 | |
455 | |
433 | typedef auto_ptr<char> auto_str; |
456 | typedef auto_ptr<char> auto_str; |
434 | |
457 | |
435 | struct stringvec : simplevec<char *> |
|
|
436 | { |
|
|
437 | ~stringvec () |
|
|
438 | { |
|
|
439 | for (char **c = begin (); c != end (); c++) |
|
|
440 | free (*c); |
|
|
441 | } |
|
|
442 | }; |
|
|
443 | |
|
|
444 | // return a very temporary (and never deallocated) buffer. keep small. |
|
|
445 | void *rxvt_temp_buf (int len); |
|
|
446 | |
|
|
447 | template<typename T> |
|
|
448 | static inline T * |
|
|
449 | rxvt_temp_buf (int len) |
|
|
450 | { |
|
|
451 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
|
|
452 | } |
|
|
453 | |
|
|
454 | #endif |
458 | #endif |
455 | |
459 | |