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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines