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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines