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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines