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.13 by root, Wed Dec 21 14:19:19 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines