ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
(Generate patch)

Comparing rxvt-unicode/src/rxvtutil.h (file contents):
Revision 1.22 by root, Sun Jan 29 22:27:04 2006 UTC vs.
Revision 1.49 by sf-exg, Sat Jan 21 13:47:19 2012 UTC

1#ifndef RXVT_UTIL_H 1#ifndef RXVT_UTIL_H
2#define RXVT_UTIL_H 2#define RXVT_UTIL_H
3 3
4#include <cstdlib> 4#include <stdlib.h>
5#include <cstring> 5#include <string.h>
6#include "ecb.h"
7#include "estl.h"
6 8
7#define PP_CONCAT_(a, b) a ## b 9// increases code size unless -fno-enforce-eh-specs
8#define PP_CONCAT(a, b) PP_CONCAT_(a, b) 10#if __GNUC__
9#define PP_STRINGIFY_(a) #a 11# define NOTHROW
10#define PP_STRINGIFY(a) PP_STRINGIFY_(a) 12# define THROW(x)
11 13#else
12// actually, some gcc-3.x versions work, too 14# define NOTHROW throw()
13#define HAVE_GCC_BUILTINS (__GNUC__ >= 4) 15# define THROW(x) throw x
14#define HAVE_GCC_BUILTINS 0 16#endif
15
16extern class byteorder {
17 static unsigned int e; // at least 32 bits
18public:
19 byteorder ();
20
21 static bool big_endian () { return e == 0x11223344; };
22 static bool network () { return e == 0x11223344; };
23 static bool little_endian () { return e == 0x44332211; };
24 static bool vax () { return e == 0x44332211; };
25} byteorder;
26 17
27// various utility functions 18// various utility functions
28template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
29template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } 19template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
30template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
31template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } 20template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
32 21
33template<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; } 22template<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; }
34template<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; } 23template<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; }
35 24
36template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 25template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); }
37 26
38// linear interpolation 27// linear interpolation
39template<typename T, typename U, typename P> 28template<typename T, typename U, typename P>
40static inline 29static inline
41T lerp (T a, U b, P p) 30T lerp (T a, U b, P p)
42{ 31{
43 return (int(a) * int(p) + int(b) * int(100 - p)) / 100; 32 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
44} 33}
45 34
46// some bit functions, xft fuck me plenty 35// return a very temporary (and never deallocated) buffer. keep small.
47#if HAVE_GCC_BUILTINS 36void *rxvt_temp_buf (int len);
48static inline int ctz (unsigned int x) { return __builtin_ctz (x); } 37
49static inline int popcount (unsigned int x) { return __builtin_popcount (x); } 38template<typename T>
50#else 39static inline T *
51// count trailing zero bits and count # of one bits 40rxvt_temp_buf (int len)
52int ctz (unsigned int x); 41{
53int popcount (unsigned int x); 42 return (T *)rxvt_temp_buf (len * sizeof (T));
54#endif 43}
55 44
56// in range including end 45// in range including end
57#define IN_RANGE_INC(val,beg,end) \ 46#define IN_RANGE_INC(val,beg,end) \
58 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 47 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
59 48
60// in range excluding end 49// in range excluding end
61#define IN_RANGE_EXC(val,beg,end) \ 50#define IN_RANGE_EXC(val,beg,end) \
62 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 51 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
63 52
53// for m >= -n, ensure remainder lies between 0..n-1
54#define MOD(m,n) (((m) + (n)) % (n))
55
64// makes dynamically allocated objects zero-initialised 56// makes dynamically allocated objects zero-initialised
65struct zero_initialized { 57struct zero_initialized
58{
66 void *operator new (size_t s); 59 void *operator new (size_t s);
67 void operator delete (void *p, size_t s); 60 void operator delete (void *p, size_t s);
68}; 61};
69 62
70/* simplevec taken (and heavily modified), from: 63struct stringvec : simplevec<char *>
71 * 64{
72 * MICO --- a free CORBA implementation 65 ~stringvec ()
73 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
74 */
75template<class T>
76struct simplevec {
77 typedef T* iterator;
78 typedef const T* const_iterator;
79 typedef unsigned long size_type;
80
81private:
82 size_type _last, _size;
83 T *_buf;
84
85public:
86 const_iterator begin () const
87 { 66 {
88 return &_buf[0]; 67 for (char **c = begin (); c != end (); c++)
68 free (*c);
89 } 69 }
90 iterator begin ()
91 {
92 return &_buf[0];
93 }
94 const_iterator end () const
95 {
96 return &_buf[_last];
97 }
98 iterator end ()
99 {
100 return &_buf[_last];
101 }
102 size_type capacity () const
103 {
104 return _size;
105 }
106 size_type size () const
107 {
108 return _last;
109 }
110
111private:
112 static T *alloc (size_type n)
113 {
114 return (T *)::operator new ((size_t) (n * sizeof (T)));
115 }
116 static void dealloc (T *buf)
117 {
118 if (buf)
119 ::operator delete (buf);
120 }
121
122 void reserve (iterator where, size_type n)
123 {
124 if (_last + n <= _size) {
125 memmove (where+n, where, (end ()-where)*sizeof (T));
126 } else {
127 size_type sz = _last+n;
128 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
129 T *nbuf = alloc (sz);
130 if (_buf) {
131 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
132 memcpy (nbuf + (where-begin ()) + n, where,
133 (end ()-where)*sizeof (T));
134 dealloc (_buf);
135 }
136 _buf = nbuf;
137 _size = sz;
138 }
139 }
140
141public:
142 void reserve (size_type sz)
143 {
144 if (_size < sz) {
145 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
146 T *nbuf = alloc (sz);
147 if (_buf) {
148 memcpy (nbuf, begin (), size ()*sizeof (T));
149 dealloc (_buf);
150 }
151 _buf = nbuf;
152 _size = sz;
153 }
154 }
155 simplevec ()
156 : _last(0), _size(0), _buf(0)
157 {
158 }
159 simplevec (size_type n, const T& t = T ())
160 : _last(0), _size(0), _buf(0)
161 {
162 insert (begin (), n, t);
163 }
164 simplevec (const_iterator first, const_iterator last)
165 : _last(0), _size(0), _buf(0)
166 {
167 insert (begin (), first, last);
168 }
169 simplevec (const simplevec<T> &v)
170 : _last(0), _size(0), _buf(0)
171 {
172 reserve (v._last);
173 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
174 _last = v._last;
175 }
176 simplevec<T> &operator= (const simplevec<T> &v)
177 {
178 if (this != &v) {
179 _last = 0;
180 reserve (v._last);
181 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
182 _last = v._last;
183 }
184 return *this;
185 }
186 ~simplevec ()
187 {
188 dealloc (_buf);
189 }
190 const T &front () const
191 {
192 //ministl_assert (size () > 0);
193 return _buf[0];
194 }
195 T &front ()
196 {
197 //ministl_assert (size () > 0);
198 return _buf[0];
199 }
200 const T &back () const
201 {
202 //ministl_assert (size () > 0);
203 return _buf[_last-1];
204 }
205 T &back ()
206 {
207 //ministl_assert (size () > 0);
208 return _buf[_last-1];
209 }
210 bool empty () const
211 {
212 return _last == 0;
213 }
214 void clear ()
215 {
216 _last = 0;
217 }
218 void push_back (const T &t)
219 {
220 reserve (_last+1);
221 *end () = t;
222 ++_last;
223 }
224 void push_back (T &t)
225 {
226 reserve (_last+1);
227 *end () = t;
228 ++_last;
229 }
230 void pop_back ()
231 {
232 //ministl_assert (size () > 0);
233 --_last;
234 }
235 const T &operator[] (size_type idx) const
236 {
237 //ministl_assert (idx < size ());
238 return _buf[idx];
239 }
240 T &operator[] (size_type idx)
241 {
242 //ministl_assert (idx < size ());
243 return _buf[idx];
244 }
245 iterator insert (iterator pos, const T &t)
246 {
247 //ministl_assert (pos <= end ());
248 long at = pos - begin ();
249 reserve (pos, 1);
250 pos = begin ()+at;
251 *pos = t;
252 ++_last;
253 return pos;
254 }
255 iterator insert (iterator pos, const_iterator first, const_iterator last)
256 {
257 //ministl_assert (pos <= end ());
258 long n = last - first;
259 long at = pos - begin ();
260 if (n > 0) {
261 reserve (pos, n);
262 pos = begin ()+at;
263 memcpy (pos, first, (last-first)*sizeof (T));
264 _last += n;
265 }
266 return pos;
267 }
268 iterator insert (iterator pos, size_type n, const T &t)
269 {
270 //ministl_assert (pos <= end ());
271 long at = pos - begin ();
272 if (n > 0) {
273 reserve (pos, n);
274 pos = begin ()+at;
275 for (int i = 0; i < n; ++i)
276 pos[i] = t;
277 _last += n;
278 }
279 return pos;
280 }
281 void erase (iterator first, iterator last)
282 {
283 if (last != first) {
284 memmove (first, last, (end () - last) * sizeof (T));
285 _last -= last - first;
286 }
287 }
288 void erase (iterator pos)
289 {
290 if (pos != end ()) {
291 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
292 --_last;
293 }
294 }
295 void swap (simplevec<T> &t)
296 {
297 ::swap(_last, t._last);
298 ::swap(_size, t._size);
299 ::swap(_buf, t._buf);
300 }
301}; 70};
302
303template<class T>
304bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
305{
306 if (v1.size () != v2.size ())
307 return false;
308 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
309}
310
311template<class T>
312bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
313{
314 unsigned long minlast = min (v1.size (), v2.size ());
315 for (unsigned long i = 0; i < minlast; ++i) {
316 if (v1[i] < v2[i])
317 return true;
318 if (v2[i] < v1[i])
319 return false;
320 }
321 return v1.size () < v2.size ();
322}
323
324
325template<typename T>
326struct vector : simplevec<T>
327{ };
328 71
329#if 0 72#if 0
330template<typename T> 73template<typename T>
331struct rxvt_vec : simplevec<void *> { 74struct rxvt_vec : simplevec<void *>
75{
332 typedef T *iterator; 76 typedef T *iterator;
333 77
334 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 78 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
335 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 79 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
336 void erase (int i) { erase (begin () + i); } 80 void erase (int i) { erase (begin () + i); }
340 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 84 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
341 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } 85 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
342}; 86};
343#endif 87#endif
344 88
345template <typename I, typename T> 89template<typename T>
346I find (I first, I last, const T& value) 90struct auto_ptr
347{ 91{
348 while (first != last && *first != value)
349 ++first;
350
351 return first;
352}
353
354template<typename T>
355struct auto_ptr {
356 T *p; 92 T *p;
357 93
358 auto_ptr () : p (0) { } 94 auto_ptr () : p (0) { }
359 auto_ptr (T *a) : p (a) { } 95 auto_ptr (T *a) : p (a) { }
360 96
409 } 145 }
410}; 146};
411 147
412typedef auto_ptr<char> auto_str; 148typedef auto_ptr<char> auto_str;
413 149
414struct stringvec : simplevec<char *>
415{
416 ~stringvec ()
417 {
418 for (char **c = begin (); c != end (); c++)
419 free (*c);
420 }
421};
422
423// return a very temporary (and never deallocated) buffer. keep small.
424void *rxvt_temp_buf (int len);
425
426template<typename T>
427inline T *
428rxvt_temp_buf (int len)
429{
430 return (T *)rxvt_temp_buf (len * sizeof (T));
431}
432
433#endif 150#endif
434 151

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines