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.2 by root, Sun Aug 15 07:20:16 2004 UTC vs.
Revision 1.13 by root, Wed Dec 21 14:19:19 2005 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>
3 5
4extern class byteorder { 6extern class byteorder {
5 static unsigned int e; // at least 32 bits 7 static unsigned int e; // at least 32 bits
6public: 8public:
7 byteorder (); 9 byteorder ();
10 static bool network () { return e == 0x11223344; }; 12 static bool network () { return e == 0x11223344; };
11 static bool little_endian () { return e == 0x44332211; }; 13 static bool little_endian () { return e == 0x44332211; };
12 static bool vax () { return e == 0x44332211; }; 14 static bool vax () { return e == 0x44332211; };
13} byteorder; 15} byteorder;
14 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 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; }
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
15template<typename T, typename U> 25template<typename T, typename U>
16static inline T min (T a, U b) { return a < b ? a : (T)b; } 26static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
17template<typename T, typename U>
18static inline T max (T a, U b) { return a > b ? a : (T)b; }
19 27
20#include "simplevec.h" 28// in range including end
29#define IN_RANGE_INC(val,beg,end) \
30 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
31
32// in range excluding end
33#define IN_RANGE_EXC(val,beg,end) \
34 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
35
36struct zero_initialized {
37 void *operator new (size_t s);
38 void operator delete (void *p, size_t s);
39};
40
41/* simplevec taken (and heavily modified), from:
42 *
43 * MICO --- a free CORBA implementation
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 {
59 return &_buf[0];
60 }
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};
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
21 295
22template<typename T> 296template<typename T>
23struct vector : simplevec<T> 297struct vector : simplevec<T>
24{ }; 298{ };
25 299
114 { 388 {
115 for (char **c = begin (); c != end (); c++) 389 for (char **c = begin (); c != end (); c++)
116 delete [] *c; 390 delete [] *c;
117 } 391 }
118}; 392};
393
119#endif 394#endif
120 395

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines