3 * MICO --- a free CORBA implementation
4 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
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.
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.
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.
20 * Send comments and/or bug reports to:
21 * mico@informatik.uni-frankfurt.de
24 #ifndef __ministl_simplevec_h__
25 #define __ministl_simplevec_h__
33 typedef const T* const_iterator;
34 typedef unsigned long size_type;
36 size_type _last, _size;
40 const_iterator begin () const
48 const_iterator end () const
56 size_type capacity () const
60 size_type size () const
66 static T *alloc (size_type n)
68 return (T *)::operator new ((size_t)(n * sizeof (T)));
70 static void dealloc (T *buf)
73 ::operator delete (buf);
76 void reserve (iterator where, size_type n)
78 if (_last + n <= _size) {
79 memmove (where+n, where, (end()-where)*sizeof(T));
82 sz = (_size == 0) ? max(sz, 5) : max(sz, 2*_size);
85 memcpy (nbuf, begin(), (where-begin())*sizeof(T));
86 memcpy (nbuf + (where-begin()) + n, where,
87 (end()-where)*sizeof(T));
95 void reserve (size_type sz)
98 sz = (_size == 0) ? max(sz, 5) : max(sz, 2*_size);
101 memcpy (nbuf, begin(), size()*sizeof(T));
109 : _last (0), _size (0), _buf (0)
112 simplevec (size_type n, const T& t = T())
113 : _last (0), _size (0), _buf (0)
115 insert (begin(), n, t);
117 simplevec (const_iterator first, const_iterator last)
118 : _last (0), _size (0), _buf (0)
120 insert (begin(), first, last);
122 simplevec (const simplevec<T> &v)
123 : _last (0), _size (0), _buf (0)
126 memcpy (_buf, v.begin(), v.size()*sizeof(T));
129 simplevec<T> &operator= (const simplevec<T> &v)
134 memcpy (_buf, v.begin(), v.size()*sizeof(T));
143 const T &front () const
145 //ministl_assert (size() > 0);
150 //ministl_assert (size() > 0);
153 const T &back () const
155 //ministl_assert (size() > 0);
156 return _buf[_last-1];
160 //ministl_assert (size() > 0);
161 return _buf[_last-1];
171 void push_back (const T &t)
179 //ministl_assert (size() > 0);
182 const T &operator[] (size_type idx) const
184 //ministl_assert (idx < size());
187 T &operator[] (size_type idx)
189 //ministl_assert (idx < size());
192 iterator insert (iterator pos, const T &t)
194 //ministl_assert (pos <= end());
195 long at = pos - begin();
202 iterator insert (iterator pos, const_iterator first, const_iterator last)
204 //ministl_assert (pos <= end());
205 long n = last - first;
206 long at = pos - begin();
210 memcpy (pos, first, (last-first)*sizeof(T));
215 iterator insert (iterator pos, size_type n, const T &t)
217 //ministl_assert (pos <= end());
218 long at = pos - begin();
222 for (int i = 0; i < n; ++i)
228 void erase (iterator first, iterator last)
231 memmove (first, last, (end()-last)*sizeof(T));
232 _last -= last - first;
235 void erase (iterator pos)
238 memmove (pos, pos+1, (end()-(pos+1))*sizeof(T));
245 bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
247 if (v1.size() != v2.size())
249 return !v1.size() || !memcmp (&v1[0], &v2[0], v1.size()*sizeof(T));
253 bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
255 unsigned long minlast = min (v1.size(), v2.size());
256 for (unsigned long i = 0; i < minlast; ++i) {
262 return v1.size() < v2.size();