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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines