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.4 by root, Thu Sep 2 07:44:40 2004 UTC vs.
Revision 1.17 by root, Thu Jan 12 00:35:39 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines