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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines