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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines