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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines