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.3 by root, Sat Aug 21 05:32:00 2004 UTC vs.
Revision 1.11 by root, Tue Dec 20 19:30:59 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>
22static inline void swap (T& a, T& b) { T t=a; a=b; b=t; }
19 23
20#include "simplevec.h" 24// in range including end
25#define IN_RANGE_INC(val,beg,end) \
26 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
27
28// in range excluding end
29#define IN_RANGE_EXC(val,beg,end) \
30 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
31
32struct zero_initialized {
33 void *operator new (size_t s);
34 void operator delete (void *p, size_t s);
35};
36
37/* simplevec taken (and heavily modified), from:
38 *
39 * MICO --- a free CORBA implementation
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 {
55 return &_buf[0];
56 }
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};
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
21 291
22template<typename T> 292template<typename T>
23struct vector : simplevec<T> 293struct vector : simplevec<T>
24{ }; 294{ };
25 295
115 for (char **c = begin (); c != end (); c++) 385 for (char **c = begin (); c != end (); c++)
116 delete [] *c; 386 delete [] *c;
117 } 387 }
118}; 388};
119 389
120struct zero_initialized {
121 void *operator new (size_t s);
122 void operator delete (void *p, size_t s);
123};
124
125#endif 390#endif
126 391

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines