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.11 by root, Tue Dec 20 19:30:59 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines