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

# 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 pcg 1.4 return (T *)::operator new ((size_t) (n * sizeof (T)));
69 pcg 1.1 }
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 pcg 1.4 memmove (where+n, where, (end ()-where)*sizeof (T));
80 pcg 1.1 } else {
81 pcg 1.3 size_type sz = _last+n;
82 pcg 1.4 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
83 pcg 1.1 T *nbuf = alloc (sz);
84     if (_buf) {
85 pcg 1.4 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
86     memcpy (nbuf + (where-begin ()) + n, where,
87     (end ()-where)*sizeof (T));
88 pcg 1.1 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 pcg 1.4 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
99 pcg 1.1 T *nbuf = alloc (sz);
100     if (_buf) {
101 pcg 1.4 memcpy (nbuf, begin (), size ()*sizeof (T));
102 pcg 1.1 dealloc (_buf);
103     }
104     _buf = nbuf;
105     _size = sz;
106     }
107     }
108     simplevec ()
109     : _last (0), _size (0), _buf (0)
110     {
111     }
112 pcg 1.4 simplevec (size_type n, const T& t = T ())
113 pcg 1.1 : _last (0), _size (0), _buf (0)
114     {
115 pcg 1.4 insert (begin (), n, t);
116 pcg 1.1 }
117     simplevec (const_iterator first, const_iterator last)
118     : _last (0), _size (0), _buf (0)
119     {
120 pcg 1.4 insert (begin (), first, last);
121 pcg 1.1 }
122     simplevec (const simplevec<T> &v)
123     : _last (0), _size (0), _buf (0)
124     {
125     reserve (v._last);
126 pcg 1.4 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
127 pcg 1.1 _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 pcg 1.4 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
135 pcg 1.1 _last = v._last;
136     }
137     return *this;
138     }
139     ~simplevec ()
140     {
141     dealloc (_buf);
142     }
143     const T &front () const
144     {
145 pcg 1.4 //ministl_assert (size () > 0);
146 pcg 1.1 return _buf[0];
147     }
148     T &front ()
149     {
150 pcg 1.4 //ministl_assert (size () > 0);
151 pcg 1.1 return _buf[0];
152     }
153     const T &back () const
154     {
155 pcg 1.4 //ministl_assert (size () > 0);
156 pcg 1.1 return _buf[_last-1];
157     }
158     T &back ()
159     {
160 pcg 1.4 //ministl_assert (size () > 0);
161 pcg 1.1 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 pcg 1.4 *end () = t;
175 pcg 1.1 ++_last;
176     }
177 pcg 1.2 void push_back (T &t)
178     {
179     reserve (_last+1);
180 pcg 1.4 *end () = t;
181 pcg 1.2 ++_last;
182     }
183 pcg 1.1 void pop_back ()
184     {
185 pcg 1.4 //ministl_assert (size () > 0);
186 pcg 1.1 --_last;
187     }
188     const T &operator[] (size_type idx) const
189     {
190 pcg 1.4 //ministl_assert (idx < size ());
191 pcg 1.1 return _buf[idx];
192     }
193     T &operator[] (size_type idx)
194     {
195 pcg 1.4 //ministl_assert (idx < size ());
196 pcg 1.1 return _buf[idx];
197     }
198     iterator insert (iterator pos, const T &t)
199     {
200 pcg 1.4 //ministl_assert (pos <= end ());
201     long at = pos - begin ();
202 pcg 1.1 reserve (pos, 1);
203 pcg 1.4 pos = begin ()+at;
204 pcg 1.1 *pos = t;
205     ++_last;
206     return pos;
207     }
208     iterator insert (iterator pos, const_iterator first, const_iterator last)
209     {
210 pcg 1.4 //ministl_assert (pos <= end ());
211 pcg 1.1 long n = last - first;
212 pcg 1.4 long at = pos - begin ();
213 pcg 1.1 if (n > 0) {
214     reserve (pos, n);
215 pcg 1.4 pos = begin ()+at;
216     memcpy (pos, first, (last-first)*sizeof (T));
217 pcg 1.1 _last += n;
218     }
219     return pos;
220     }
221     iterator insert (iterator pos, size_type n, const T &t)
222     {
223 pcg 1.4 //ministl_assert (pos <= end ());
224     long at = pos - begin ();
225 pcg 1.1 if (n > 0) {
226     reserve (pos, n);
227 pcg 1.4 pos = begin ()+at;
228 pcg 1.1 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 pcg 1.4 memmove (first, last, (end ()-last)*sizeof (T));
238 pcg 1.1 _last -= last - first;
239     }
240     }
241     void erase (iterator pos)
242     {
243 pcg 1.4 if (pos != end ()) {
244     memmove (pos, pos+1, (end ()- (pos+1))*sizeof (T));
245 pcg 1.1 --_last;
246     }
247     }
248     };
249    
250     template<class T>
251     bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
252     {
253 pcg 1.4 if (v1.size () != v2.size ())
254 pcg 1.1 return false;
255 pcg 1.4 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
256 pcg 1.1 }
257    
258     template<class T>
259     bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
260     {
261 pcg 1.4 unsigned long minlast = min (v1.size (), v2.size ());
262 pcg 1.1 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 pcg 1.4 return v1.size () < v2.size ();
269 pcg 1.1 }
270    
271     #endif
272 root 1.5