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.22 by root, Sun Jan 29 22:27:04 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)
14#define HAVE_GCC_BUILTINS 0
3 15
4extern class byteorder { 16extern class byteorder {
5 static unsigned int e; // at least 32 bits 17 static unsigned int e; // at least 32 bits
6public: 18public:
7 byteorder (); 19 byteorder ();
10 static bool network () { return e == 0x11223344; }; 22 static bool network () { return e == 0x11223344; };
11 static bool little_endian () { return e == 0x44332211; }; 23 static bool little_endian () { return e == 0x44332211; };
12 static bool vax () { return e == 0x44332211; }; 24 static bool vax () { return e == 0x44332211; };
13} byteorder; 25} byteorder;
14 26
27// 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; }
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; }
32
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; }
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; }
35
36template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
37
38// linear interpolation
15template<typename T, typename U> 39template<typename T, typename U, typename P>
16static inline T min (T a, U b) { return a < b ? a : (T)b; } 40static inline
17template<typename T, typename U> 41T lerp (T a, U b, P p)
18static inline T max (T a, U b) { return a > b ? a : (T)b; } 42{
43 return (int(a) * int(p) + int(b) * int(100 - p)) / 100;
44}
19 45
20#include "simplevec.h" 46// some bit functions, xft fuck me plenty
47#if HAVE_GCC_BUILTINS
48static inline int ctz (unsigned int x) { return __builtin_ctz (x); }
49static inline int popcount (unsigned int x) { return __builtin_popcount (x); }
50#else
51// count trailing zero bits and count # of one bits
52int ctz (unsigned int x);
53int popcount (unsigned int x);
54#endif
55
56// in range including end
57#define IN_RANGE_INC(val,beg,end) \
58 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
59
60// in range excluding end
61#define IN_RANGE_EXC(val,beg,end) \
62 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
63
64// makes dynamically allocated objects zero-initialised
65struct zero_initialized {
66 void *operator new (size_t s);
67 void operator delete (void *p, size_t s);
68};
69
70/* simplevec taken (and heavily modified), from:
71 *
72 * MICO --- a free CORBA implementation
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 {
88 return &_buf[0];
89 }
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};
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
21 324
22template<typename T> 325template<typename T>
23struct vector : simplevec<T> 326struct vector : simplevec<T>
24{ }; 327{ };
25 328
111struct stringvec : simplevec<char *> 414struct stringvec : simplevec<char *>
112{ 415{
113 ~stringvec () 416 ~stringvec ()
114 { 417 {
115 for (char **c = begin (); c != end (); c++) 418 for (char **c = begin (); c != end (); c++)
116 delete [] *c; 419 free (*c);
117 } 420 }
118}; 421};
119 422
120struct zero_initialized { 423// return a very temporary (and never deallocated) buffer. keep small.
121 void *operator new (size_t s); 424void *rxvt_temp_buf (int len);
122 void operator delete (void *p, size_t s); 425
123}; 426template<typename T>
427inline T *
428rxvt_temp_buf (int len)
429{
430 return (T *)rxvt_temp_buf (len * sizeof (T));
431}
124 432
125#endif 433#endif
126 434

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines