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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines