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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines