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.1 by root, Sun Aug 15 00:37:04 2004 UTC vs.
Revision 1.15 by root, Mon Jan 2 15:35:43 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines