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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines