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.4 by root, Thu Sep 2 07:44:40 2004 UTC vs.
Revision 1.25 by root, Mon Jan 30 19:46:13 2006 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 <cstring> 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
15#ifndef __attribute__
16# if __GNUC__
17# if (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || (__GNUC__ < 2)
18# define __attribute__(x)
19# endif
20# endif
21# define __attribute__(x)
22#endif
23
24#define NORETURN __attribute__ ((noreturn))
25#define UNUSED __attribute__ ((unused))
26#define CONST __attribute__ ((const))
27
28// increases code size unless -fno-enforce-eh-specs
29#if __GNUC__
30# define NOTHROW
31# define THROW(x)
32#else
33# define NOTHROW throw()
34# define THROW(x) throw x
35#endif
5 36
6extern class byteorder { 37extern class byteorder {
7 static unsigned int e; // at least 32 bits 38 static unsigned int e; // at least 32 bits
8public: 39public:
9 byteorder (); 40 byteorder ();
12 static bool network () { return e == 0x11223344; }; 43 static bool network () { return e == 0x11223344; };
13 static bool little_endian () { return e == 0x44332211; }; 44 static bool little_endian () { return e == 0x44332211; };
14 static bool vax () { return e == 0x44332211; }; 45 static bool vax () { return e == 0x44332211; };
15} byteorder; 46} byteorder;
16 47
48// various utility functions
49template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
50template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
51template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
52template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
53
54template<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; }
55template<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; }
56
57template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
58
59template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); }
60
61// linear interpolation
17template<typename T, typename U> 62template<typename T, typename U, typename P>
18static inline T min (T a, U b) { return a < b ? a : (T)b; } 63static inline
19template<typename T, typename U> 64T lerp (T a, U b, P p)
20static inline T max (T a, U b) { return a > b ? a : (T)b; } 65{
66 return (int(a) * int(p) + int(b) * int(100 - p)) / 100;
67}
21 68
69// some bit functions, xft fuck me plenty
70#if HAVE_GCC_BUILTINS
71static inline int ctz (unsigned int x) CONST { return __builtin_ctz (x); }
72static inline int popcount (unsigned int x) CONST { return __builtin_popcount (x); }
73#else
74// count trailing zero bits and count # of one bits
75int ctz (unsigned int x) CONST;
76int popcount (unsigned int x) CONST;
77#endif
78
79// in range including end
80#define IN_RANGE_INC(val,beg,end) \
81 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
82
83// in range excluding end
84#define IN_RANGE_EXC(val,beg,end) \
85 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
86
87// makes dynamically allocated objects zero-initialised
22struct zero_initialized { 88struct zero_initialized {
23 void *operator new (size_t s); 89 void *operator new (size_t s);
24 void operator delete (void *p, size_t s); 90 void operator delete (void *p, size_t s);
25}; 91};
26 92
40 T *_buf; 106 T *_buf;
41 107
42public: 108public:
43 const_iterator begin () const 109 const_iterator begin () const
44 { 110 {
45 return &_buf[0]; 111 return &_buf[0];
46 } 112 }
47 iterator begin () 113 iterator begin ()
48 { 114 {
49 return &_buf[0]; 115 return &_buf[0];
50 } 116 }
51 const_iterator end () const 117 const_iterator end () const
52 { 118 {
53 return &_buf[_last]; 119 return &_buf[_last];
54 } 120 }
55 iterator end () 121 iterator end ()
56 { 122 {
57 return &_buf[_last]; 123 return &_buf[_last];
58 } 124 }
59 size_type capacity () const 125 size_type capacity () const
60 { 126 {
61 return _size; 127 return _size;
62 } 128 }
63 size_type size () const 129 size_type size () const
64 { 130 {
65 return _last; 131 return _last;
66 } 132 }
67 133
68private: 134private:
69 static T *alloc (size_type n) 135 static T *alloc (size_type n)
70 { 136 {
71 return (T *)::operator new ((size_t) (n * sizeof (T))); 137 return (T *)::operator new ((size_t) (n * sizeof (T)));
72 } 138 }
73 static void dealloc (T *buf) 139 static void dealloc (T *buf)
74 { 140 {
75 if (buf) 141 if (buf)
76 ::operator delete (buf); 142 ::operator delete (buf);
77 } 143 }
78 144
79 void reserve (iterator where, size_type n) 145 void reserve (iterator where, size_type n)
80 { 146 {
81 if (_last + n <= _size) { 147 if (_last + n <= _size) {
82 memmove (where+n, where, (end ()-where)*sizeof (T)); 148 memmove (where+n, where, (end ()-where)*sizeof (T));
83 } else { 149 } else {
84 size_type sz = _last+n; 150 size_type sz = _last+n;
85 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); 151 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
86 T *nbuf = alloc (sz); 152 T *nbuf = alloc (sz);
87 if (_buf) { 153 if (_buf) {
88 memcpy (nbuf, begin (), (where-begin ())*sizeof (T)); 154 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
89 memcpy (nbuf + (where-begin ()) + n, where, 155 memcpy (nbuf + (where-begin ()) + n, where,
90 (end ()-where)*sizeof (T)); 156 (end ()-where)*sizeof (T));
91 dealloc (_buf); 157 dealloc (_buf);
92 } 158 }
93 _buf = nbuf; 159 _buf = nbuf;
94 _size = sz; 160 _size = sz;
95 } 161 }
96 } 162 }
97 163
98public: 164public:
99 void reserve (size_type sz) 165 void reserve (size_type sz)
100 { 166 {
101 if (_size < sz) { 167 if (_size < sz) {
102 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); 168 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
103 T *nbuf = alloc (sz); 169 T *nbuf = alloc (sz);
104 if (_buf) { 170 if (_buf) {
105 memcpy (nbuf, begin (), size ()*sizeof (T)); 171 memcpy (nbuf, begin (), size ()*sizeof (T));
106 dealloc (_buf); 172 dealloc (_buf);
107 } 173 }
108 _buf = nbuf; 174 _buf = nbuf;
109 _size = sz; 175 _size = sz;
110 } 176 }
111 } 177 }
112 simplevec () 178 simplevec ()
113 : _last(0), _size(0), _buf(0) 179 : _last(0), _size(0), _buf(0)
114 { 180 {
115 } 181 }
116 simplevec (size_type n, const T& t = T ()) 182 simplevec (size_type n, const T& t = T ())
117 : _last(0), _size(0), _buf(0) 183 : _last(0), _size(0), _buf(0)
118 { 184 {
119 insert (begin (), n, t); 185 insert (begin (), n, t);
120 } 186 }
121 simplevec (const_iterator first, const_iterator last) 187 simplevec (const_iterator first, const_iterator last)
122 : _last(0), _size(0), _buf(0) 188 : _last(0), _size(0), _buf(0)
123 { 189 {
124 insert (begin (), first, last); 190 insert (begin (), first, last);
125 } 191 }
126 simplevec (const simplevec<T> &v) 192 simplevec (const simplevec<T> &v)
127 : _last(0), _size(0), _buf(0) 193 : _last(0), _size(0), _buf(0)
128 { 194 {
129 reserve (v._last); 195 reserve (v._last);
130 memcpy (_buf, v.begin (), v.size ()*sizeof (T)); 196 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
131 _last = v._last; 197 _last = v._last;
132 } 198 }
133 simplevec<T> &operator= (const simplevec<T> &v) 199 simplevec<T> &operator= (const simplevec<T> &v)
134 { 200 {
135 if (this != &v) { 201 if (this != &v) {
136 _last = 0; 202 _last = 0;
137 reserve (v._last); 203 reserve (v._last);
138 memcpy (_buf, v.begin (), v.size ()*sizeof (T)); 204 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
139 _last = v._last; 205 _last = v._last;
140 } 206 }
141 return *this; 207 return *this;
142 } 208 }
143 ~simplevec () 209 ~simplevec ()
144 { 210 {
145 dealloc (_buf); 211 dealloc (_buf);
146 } 212 }
147 const T &front () const 213 const T &front () const
148 { 214 {
149 //ministl_assert (size () > 0); 215 //ministl_assert (size () > 0);
150 return _buf[0]; 216 return _buf[0];
151 } 217 }
152 T &front () 218 T &front ()
153 { 219 {
154 //ministl_assert (size () > 0); 220 //ministl_assert (size () > 0);
155 return _buf[0]; 221 return _buf[0];
156 } 222 }
157 const T &back () const 223 const T &back () const
158 { 224 {
159 //ministl_assert (size () > 0); 225 //ministl_assert (size () > 0);
160 return _buf[_last-1]; 226 return _buf[_last-1];
161 } 227 }
162 T &back () 228 T &back ()
163 { 229 {
164 //ministl_assert (size () > 0); 230 //ministl_assert (size () > 0);
165 return _buf[_last-1]; 231 return _buf[_last-1];
166 } 232 }
167 bool empty () const 233 bool empty () const
168 { 234 {
169 return _last == 0; 235 return _last == 0;
170 } 236 }
171 void clear () 237 void clear ()
172 { 238 {
173 _last = 0; 239 _last = 0;
174 } 240 }
175 void push_back (const T &t) 241 void push_back (const T &t)
176 { 242 {
177 reserve (_last+1); 243 reserve (_last+1);
178 *end () = t; 244 *end () = t;
179 ++_last; 245 ++_last;
180 } 246 }
181 void push_back (T &t) 247 void push_back (T &t)
182 { 248 {
183 reserve (_last+1); 249 reserve (_last+1);
184 *end () = t; 250 *end () = t;
185 ++_last; 251 ++_last;
186 } 252 }
187 void pop_back () 253 void pop_back ()
188 { 254 {
189 //ministl_assert (size () > 0); 255 //ministl_assert (size () > 0);
190 --_last; 256 --_last;
191 } 257 }
192 const T &operator[] (size_type idx) const 258 const T &operator[] (size_type idx) const
193 { 259 {
194 //ministl_assert (idx < size ()); 260 //ministl_assert (idx < size ());
195 return _buf[idx]; 261 return _buf[idx];
196 } 262 }
197 T &operator[] (size_type idx) 263 T &operator[] (size_type idx)
198 { 264 {
199 //ministl_assert (idx < size ()); 265 //ministl_assert (idx < size ());
200 return _buf[idx]; 266 return _buf[idx];
201 } 267 }
202 iterator insert (iterator pos, const T &t) 268 iterator insert (iterator pos, const T &t)
203 { 269 {
204 //ministl_assert (pos <= end ()); 270 //ministl_assert (pos <= end ());
205 long at = pos - begin (); 271 long at = pos - begin ();
206 reserve (pos, 1); 272 reserve (pos, 1);
207 pos = begin ()+at; 273 pos = begin ()+at;
208 *pos = t; 274 *pos = t;
209 ++_last; 275 ++_last;
210 return pos; 276 return pos;
211 } 277 }
212 iterator insert (iterator pos, const_iterator first, const_iterator last) 278 iterator insert (iterator pos, const_iterator first, const_iterator last)
213 { 279 {
214 //ministl_assert (pos <= end ()); 280 //ministl_assert (pos <= end ());
215 long n = last - first; 281 long n = last - first;
216 long at = pos - begin (); 282 long at = pos - begin ();
217 if (n > 0) { 283 if (n > 0) {
218 reserve (pos, n); 284 reserve (pos, n);
219 pos = begin ()+at; 285 pos = begin ()+at;
220 memcpy (pos, first, (last-first)*sizeof (T)); 286 memcpy (pos, first, (last-first)*sizeof (T));
221 _last += n; 287 _last += n;
222 } 288 }
223 return pos; 289 return pos;
224 } 290 }
225 iterator insert (iterator pos, size_type n, const T &t) 291 iterator insert (iterator pos, size_type n, const T &t)
226 { 292 {
227 //ministl_assert (pos <= end ()); 293 //ministl_assert (pos <= end ());
228 long at = pos - begin (); 294 long at = pos - begin ();
229 if (n > 0) { 295 if (n > 0) {
230 reserve (pos, n); 296 reserve (pos, n);
231 pos = begin ()+at; 297 pos = begin ()+at;
232 for (int i = 0; i < n; ++i) 298 for (int i = 0; i < n; ++i)
233 pos[i] = t; 299 pos[i] = t;
234 _last += n; 300 _last += n;
235 } 301 }
236 return pos; 302 return pos;
237 } 303 }
238 void erase (iterator first, iterator last) 304 void erase (iterator first, iterator last)
239 { 305 {
240 if (last != first) { 306 if (last != first) {
241 memmove (first, last, (end ()-last)*sizeof (T)); 307 memmove (first, last, (end () - last) * sizeof (T));
242 _last -= last - first; 308 _last -= last - first;
243 } 309 }
244 } 310 }
245 void erase (iterator pos) 311 void erase (iterator pos)
246 { 312 {
247 if (pos != end ()) { 313 if (pos != end ()) {
248 memmove (pos, pos+1, (end ()- (pos+1))*sizeof (T)); 314 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
249 --_last; 315 --_last;
250 } 316 }
317 }
318 void swap (simplevec<T> &t)
319 {
320 ::swap(_last, t._last);
321 ::swap(_size, t._size);
322 ::swap(_buf, t._buf);
251 } 323 }
252}; 324};
253 325
254template<class T> 326template<class T>
255bool operator== (const simplevec<T> &v1, const simplevec<T> &v2) 327bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
256{ 328{
257 if (v1.size () != v2.size ()) 329 if (v1.size () != v2.size ())
258 return false; 330 return false;
259 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T)); 331 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
260} 332}
261 333
262template<class T> 334template<class T>
263bool operator< (const simplevec<T> &v1, const simplevec<T> &v2) 335bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
264{ 336{
265 unsigned long minlast = min (v1.size (), v2.size ()); 337 unsigned long minlast = min (v1.size (), v2.size ());
266 for (unsigned long i = 0; i < minlast; ++i) { 338 for (unsigned long i = 0; i < minlast; ++i) {
267 if (v1[i] < v2[i]) 339 if (v1[i] < v2[i])
268 return true; 340 return true;
269 if (v2[i] < v1[i]) 341 if (v2[i] < v1[i])
270 return false; 342 return false;
271 } 343 }
272 return v1.size () < v2.size (); 344 return v1.size () < v2.size ();
273} 345}
274 346
275 347
365struct stringvec : simplevec<char *> 437struct stringvec : simplevec<char *>
366{ 438{
367 ~stringvec () 439 ~stringvec ()
368 { 440 {
369 for (char **c = begin (); c != end (); c++) 441 for (char **c = begin (); c != end (); c++)
370 delete [] *c; 442 free (*c);
371 } 443 }
372}; 444};
373 445
446// return a very temporary (and never deallocated) buffer. keep small.
447void *rxvt_temp_buf (int len);
448
449template<typename T>
450static inline T *
451rxvt_temp_buf (int len)
452{
453 return (T *)rxvt_temp_buf (len * sizeof (T));
454}
455
374#endif 456#endif
375 457

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines