ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/simplevec.h
Revision: 1.1
Committed: Mon Nov 24 17:28:08 2003 UTC (20 years, 6 months ago) by pcg
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.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     long 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 pop_back ()
178     {
179     //ministl_assert (size() > 0);
180     --_last;
181     }
182     const T &operator[] (size_type idx) const
183     {
184     //ministl_assert (idx < size());
185     return _buf[idx];
186     }
187     T &operator[] (size_type idx)
188     {
189     //ministl_assert (idx < size());
190     return _buf[idx];
191     }
192     iterator insert (iterator pos, const T &t)
193     {
194     //ministl_assert (pos <= end());
195     long at = pos - begin();
196     reserve (pos, 1);
197     pos = begin()+at;
198     *pos = t;
199     ++_last;
200     return pos;
201     }
202     iterator insert (iterator pos, const_iterator first, const_iterator last)
203     {
204     //ministl_assert (pos <= end());
205     long n = last - first;
206     long at = pos - begin();
207     if (n > 0) {
208     reserve (pos, n);
209     pos = begin()+at;
210     memcpy (pos, first, (last-first)*sizeof(T));
211     _last += n;
212     }
213     return pos;
214     }
215     iterator insert (iterator pos, size_type n, const T &t)
216     {
217     //ministl_assert (pos <= end());
218     long at = pos - begin();
219     if (n > 0) {
220     reserve (pos, n);
221     pos = begin()+at;
222     for (int i = 0; i < n; ++i)
223     pos[i] = t;
224     _last += n;
225     }
226     return pos;
227     }
228     void erase (iterator first, iterator last)
229     {
230     if (last != first) {
231     memmove (first, last, (end()-last)*sizeof(T));
232     _last -= last - first;
233     }
234     }
235     void erase (iterator pos)
236     {
237     if (pos != end()) {
238     memmove (pos, pos+1, (end()-(pos+1))*sizeof(T));
239     --_last;
240     }
241     }
242     };
243    
244     template<class T>
245     bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
246     {
247     if (v1.size() != v2.size())
248     return false;
249     return !v1.size() || !memcmp (&v1[0], &v2[0], v1.size()*sizeof(T));
250     }
251    
252     template<class T>
253     bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
254     {
255     unsigned long minlast = min (v1.size(), v2.size());
256     for (unsigned long i = 0; i < minlast; ++i) {
257     if (v1[i] < v2[i])
258     return true;
259     if (v2[i] < v1[i])
260     return false;
261     }
262     return v1.size() < v2.size();
263     }
264    
265     #endif