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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines