ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
Revision: 1.1
Committed: Sat Jan 21 13:40:29 2012 UTC (12 years, 5 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Log Message:
Port find/vector implementation from urxvt and always use it in place of
stl.

File Contents

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