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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines