ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/simplevec.h
Revision: 1.6
Committed: Thu Sep 2 07:44:40 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +0 -0 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

# Content
1 // -*- c++ -*-
2 /*
3 * MICO --- a free CORBA implementation
4 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Send comments and/or bug reports to:
21 * mico@informatik.uni-frankfurt.de
22 */
23
24 #ifndef __ministl_simplevec_h__
25 #define __ministl_simplevec_h__
26
27 #include <cstring>
28
29 template<class T>
30 class simplevec {
31 public:
32 typedef T* iterator;
33 typedef const T* const_iterator;
34 typedef unsigned long size_type;
35 private:
36 size_type _last, _size;
37 T *_buf;
38
39 public:
40 const_iterator begin () const
41 {
42 return &_buf[0];
43 }
44 iterator begin ()
45 {
46 return &_buf[0];
47 }
48 const_iterator end () const
49 {
50 return &_buf[_last];
51 }
52 iterator end ()
53 {
54 return &_buf[_last];
55 }
56 size_type capacity () const
57 {
58 return _size;
59 }
60 size_type size () const
61 {
62 return _last;
63 }
64
65 private:
66 static T *alloc (size_type n)
67 {
68 return (T *)::operator new ((size_t) (n * sizeof (T)));
69 }
70 static void dealloc (T *buf)
71 {
72 if (buf)
73 ::operator delete (buf);
74 }
75
76 void reserve (iterator where, size_type n)
77 {
78 if (_last + n <= _size) {
79 memmove (where+n, where, (end ()-where)*sizeof (T));
80 } else {
81 size_type sz = _last+n;
82 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
83 T *nbuf = alloc (sz);
84 if (_buf) {
85 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
86 memcpy (nbuf + (where-begin ()) + n, where,
87 (end ()-where)*sizeof (T));
88 dealloc (_buf);
89 }
90 _buf = nbuf;
91 _size = sz;
92 }
93 }
94 public:
95 void reserve (size_type sz)
96 {
97 if (_size < sz) {
98 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
99 T *nbuf = alloc (sz);
100 if (_buf) {
101 memcpy (nbuf, begin (), size ()*sizeof (T));
102 dealloc (_buf);
103 }
104 _buf = nbuf;
105 _size = sz;
106 }
107 }
108 simplevec ()
109 : _last (0), _size (0), _buf (0)
110 {
111 }
112 simplevec (size_type n, const T& t = T ())
113 : _last (0), _size (0), _buf (0)
114 {
115 insert (begin (), n, t);
116 }
117 simplevec (const_iterator first, const_iterator last)
118 : _last (0), _size (0), _buf (0)
119 {
120 insert (begin (), first, last);
121 }
122 simplevec (const simplevec<T> &v)
123 : _last (0), _size (0), _buf (0)
124 {
125 reserve (v._last);
126 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
127 _last = v._last;
128 }
129 simplevec<T> &operator= (const simplevec<T> &v)
130 {
131 if (this != &v) {
132 _last = 0;
133 reserve (v._last);
134 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
135 _last = v._last;
136 }
137 return *this;
138 }
139 ~simplevec ()
140 {
141 dealloc (_buf);
142 }
143 const T &front () const
144 {
145 //ministl_assert (size () > 0);
146 return _buf[0];
147 }
148 T &front ()
149 {
150 //ministl_assert (size () > 0);
151 return _buf[0];
152 }
153 const T &back () const
154 {
155 //ministl_assert (size () > 0);
156 return _buf[_last-1];
157 }
158 T &back ()
159 {
160 //ministl_assert (size () > 0);
161 return _buf[_last-1];
162 }
163 bool empty () const
164 {
165 return _last == 0;
166 }
167 void clear ()
168 {
169 _last = 0;
170 }
171 void push_back (const T &t)
172 {
173 reserve (_last+1);
174 *end () = t;
175 ++_last;
176 }
177 void push_back (T &t)
178 {
179 reserve (_last+1);
180 *end () = t;
181 ++_last;
182 }
183 void pop_back ()
184 {
185 //ministl_assert (size () > 0);
186 --_last;
187 }
188 const T &operator[] (size_type idx) const
189 {
190 //ministl_assert (idx < size ());
191 return _buf[idx];
192 }
193 T &operator[] (size_type idx)
194 {
195 //ministl_assert (idx < size ());
196 return _buf[idx];
197 }
198 iterator insert (iterator pos, const T &t)
199 {
200 //ministl_assert (pos <= end ());
201 long at = pos - begin ();
202 reserve (pos, 1);
203 pos = begin ()+at;
204 *pos = t;
205 ++_last;
206 return pos;
207 }
208 iterator insert (iterator pos, const_iterator first, const_iterator last)
209 {
210 //ministl_assert (pos <= end ());
211 long n = last - first;
212 long at = pos - begin ();
213 if (n > 0) {
214 reserve (pos, n);
215 pos = begin ()+at;
216 memcpy (pos, first, (last-first)*sizeof (T));
217 _last += n;
218 }
219 return pos;
220 }
221 iterator insert (iterator pos, size_type n, const T &t)
222 {
223 //ministl_assert (pos <= end ());
224 long at = pos - begin ();
225 if (n > 0) {
226 reserve (pos, n);
227 pos = begin ()+at;
228 for (int i = 0; i < n; ++i)
229 pos[i] = t;
230 _last += n;
231 }
232 return pos;
233 }
234 void erase (iterator first, iterator last)
235 {
236 if (last != first) {
237 memmove (first, last, (end ()-last)*sizeof (T));
238 _last -= last - first;
239 }
240 }
241 void erase (iterator pos)
242 {
243 if (pos != end ()) {
244 memmove (pos, pos+1, (end ()- (pos+1))*sizeof (T));
245 --_last;
246 }
247 }
248 };
249
250 template<class T>
251 bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
252 {
253 if (v1.size () != v2.size ())
254 return false;
255 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
256 }
257
258 template<class T>
259 bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
260 {
261 unsigned long minlast = min (v1.size (), v2.size ());
262 for (unsigned long i = 0; i < minlast; ++i) {
263 if (v1[i] < v2[i])
264 return true;
265 if (v2[i] < v1[i])
266 return false;
267 }
268 return v1.size () < v2.size ();
269 }
270
271 #endif
272