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.3 by root, Sat Aug 21 05:32:00 2004 UTC vs.
Revision 1.23 by root, Sun Jan 29 22:30:21 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines