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)
11 extern class byteorder {
12 static unsigned int e; // at least 32 bits
16 static bool big_endian () { return e == 0x11223344; };
17 static bool network () { return e == 0x11223344; };
18 static bool little_endian () { return e == 0x44332211; };
19 static bool vax () { return e == 0x44332211; };
22 // various utility functions
23 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
24 template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
25 template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
26 template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
28 template<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; }
29 template<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; }
31 template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
33 // in range including end
34 #define IN_RANGE_INC(val,beg,end) \
35 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
37 // in range excluding end
38 #define IN_RANGE_EXC(val,beg,end) \
39 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
41 // makes dynamically allocated objects zero-initialised
42 struct zero_initialized {
43 void *operator new (size_t s);
44 void operator delete (void *p, size_t s);
47 /* simplevec taken (and heavily modified), from:
49 * MICO --- a free CORBA implementation
50 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
55 typedef const T* const_iterator;
56 typedef unsigned long size_type;
59 size_type _last, _size;
63 const_iterator begin () const
71 const_iterator end () const
79 size_type capacity () const
83 size_type size () const
89 static T *alloc (size_type n)
91 return (T *)::operator new ((size_t) (n * sizeof (T)));
93 static void dealloc (T *buf)
96 ::operator delete (buf);
99 void reserve (iterator where, size_type n)
101 if (_last + n <= _size) {
102 memmove (where+n, where, (end ()-where)*sizeof (T));
104 size_type sz = _last+n;
105 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
106 T *nbuf = alloc (sz);
108 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
109 memcpy (nbuf + (where-begin ()) + n, where,
110 (end ()-where)*sizeof (T));
119 void reserve (size_type sz)
122 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
123 T *nbuf = alloc (sz);
125 memcpy (nbuf, begin (), size ()*sizeof (T));
133 : _last(0), _size(0), _buf(0)
136 simplevec (size_type n, const T& t = T ())
137 : _last(0), _size(0), _buf(0)
139 insert (begin (), n, t);
141 simplevec (const_iterator first, const_iterator last)
142 : _last(0), _size(0), _buf(0)
144 insert (begin (), first, last);
146 simplevec (const simplevec<T> &v)
147 : _last(0), _size(0), _buf(0)
150 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
153 simplevec<T> &operator= (const simplevec<T> &v)
158 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
167 const T &front () const
169 //ministl_assert (size () > 0);
174 //ministl_assert (size () > 0);
177 const T &back () const
179 //ministl_assert (size () > 0);
180 return _buf[_last-1];
184 //ministl_assert (size () > 0);
185 return _buf[_last-1];
195 void push_back (const T &t)
201 void push_back (T &t)
209 //ministl_assert (size () > 0);
212 const T &operator[] (size_type idx) const
214 //ministl_assert (idx < size ());
217 T &operator[] (size_type idx)
219 //ministl_assert (idx < size ());
222 iterator insert (iterator pos, const T &t)
224 //ministl_assert (pos <= end ());
225 long at = pos - begin ();
232 iterator insert (iterator pos, const_iterator first, const_iterator last)
234 //ministl_assert (pos <= end ());
235 long n = last - first;
236 long at = pos - begin ();
240 memcpy (pos, first, (last-first)*sizeof (T));
245 iterator insert (iterator pos, size_type n, const T &t)
247 //ministl_assert (pos <= end ());
248 long at = pos - begin ();
252 for (int i = 0; i < n; ++i)
258 void erase (iterator first, iterator last)
261 memmove (first, last, (end () - last) * sizeof (T));
262 _last -= last - first;
265 void erase (iterator pos)
268 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
272 void swap (simplevec<T> &t)
274 ::swap(_last, t._last);
275 ::swap(_size, t._size);
276 ::swap(_buf, t._buf);
281 bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
283 if (v1.size () != v2.size ())
285 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
289 bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
291 unsigned long minlast = min (v1.size (), v2.size ());
292 for (unsigned long i = 0; i < minlast; ++i) {
298 return v1.size () < v2.size ();
303 struct vector : simplevec<T>
308 struct rxvt_vec : simplevec<void *> {
311 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
312 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
313 void erase (int i) { erase (begin () + i); }
314 void erase (iterator i) { simplevec<void *>::erase ((void **)i); }
315 iterator begin () const { return (iterator)simplevec<void *>::begin (); }
316 iterator end () const { return (iterator)simplevec<void *>::end (); }
317 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
318 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
322 template <typename I, typename T>
323 I find (I first, I last, const T& value)
325 while (first != last && *first != value)
335 auto_ptr () : p (0) { }
336 auto_ptr (T *a) : p (a) { }
338 auto_ptr (auto_ptr<T> &a)
345 auto_ptr (auto_ptr<A> &a)
356 // void because it makes sense in our context
357 void operator = (T *a)
363 void operator = (auto_ptr &a)
370 void operator = (auto_ptr<A> &a)
376 operator T * () const { return p; }
378 T *operator -> () const { return p; }
379 T &operator * () const { return *p; }
389 typedef auto_ptr<char> auto_str;
391 struct stringvec : simplevec<char *>
395 for (char **c = begin (); c != end (); c++)