update dev300-m58
[ooovba.git] / soltools / inc / st_list.hxx
blob8cc891cd60764452d059ad7471fe5c2fa180742a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: st_list.hxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SOLTOOLS_ST_LIST_HXX
32 #define SOLTOOLS_ST_LIST_HXX
34 #include <string.h>
35 #include <iostream>
36 #include <stdlib.h>
38 template <class XX>
39 class ST_List /// Soltools-List.
41 public :
42 typedef XX * iterator;
43 typedef const XX * const_iterator;
45 // LIFECYCLE
46 ST_List();
47 ST_List(
48 const ST_List<XX> & i_rList );
49 virtual ~ST_List() { }
51 // OPERATORS
52 ST_List<XX> & operator=(
53 const ST_List<XX> & i_rList );
55 const XX & operator[](
56 unsigned n) const
57 { return elem(n); }
58 XX & operator[](
59 unsigned n)
60 { return elem(n); }
61 // OPERATIONS
62 void reserve(
63 unsigned i_nSize )
64 { alloc(i_nSize,true); }
65 void insert(
66 iterator i_aPos,
67 const XX & elem_ )
68 { Insert(i_aPos-begin(), elem_); }
69 virtual void Insert(
70 unsigned pos,
71 const XX & elem );
72 void push_back(
73 const XX & elem_)
74 { Insert(size(),elem_); }
75 void remove(
76 iterator i_aPos )
77 { Remove(i_aPos-begin()); }
78 virtual void Remove(
79 unsigned pos );
80 void pop_back() { Remove(size()-1); }
81 void erase_all() { while (size()) Remove(size()-1); }
83 // INQUIRY
84 const_iterator begin() const { return &inhalt[0]; }
85 const_iterator end() const { return &inhalt[len]; }
87 const XX & front() const { return elem(0); }
88 const XX & back() const { return elem(len-1); }
90 unsigned size() const { return len; }
91 unsigned space() const { return allocated; }
92 bool is_valid_index(
93 unsigned n) const
94 { return n < len; }
95 // ACCESS
96 iterator begin() { return &inhalt[0]; }
97 iterator end() { return &inhalt[len]; }
99 XX & front() { return elem(0); }
100 XX & back() { return elem(len-1); }
102 protected:
103 void checkSize(
104 unsigned newLength);
105 void alloc(
106 unsigned newSpace,
107 bool re = false );
109 const XX & elem(
110 unsigned n ) const
111 { return inhalt[n]; }
112 XX & elem(
113 unsigned n )
114 { return inhalt[n]; }
115 // DATA
116 XX * inhalt;
117 unsigned len;
118 unsigned allocated;
123 template <class XY>
124 class DynamicList : public ST_List< XY* >
126 public:
127 DynamicList();
128 DynamicList(
129 const DynamicList<XY> &
130 i_rList );
131 virtual ~DynamicList(); /// Deletes all member pointers
133 DynamicList<XY> & operator=(
134 const DynamicList<XY> &
135 i_rList );
137 virtual void Insert(
138 unsigned pos,
139 XY * const & elem );
140 virtual void Remove(
141 unsigned pos );
146 template <class XX>
147 ST_List<XX>::ST_List()
148 : inhalt(0),
149 len(0),
150 allocated(0)
152 alloc(1);
155 template <class XX>
156 ST_List<XX>::ST_List( const ST_List<XX> & i_rList )
157 : inhalt(0),
158 len(0),
159 allocated(0)
161 alloc(i_rList.size());
163 for ( const_iterator it = i_rList.begin();
164 it != i_rList.end();
165 ++it )
167 push_back(*it);
171 template <class XX>
172 ST_List<XX> &
173 ST_List<XX>::operator=( const ST_List<XX> & i_rList )
175 for ( const_iterator it = i_rList.begin();
176 it != i_rList.end();
177 ++it )
179 push_back(*it);
181 return *this;
184 template <class XX>
185 void
186 ST_List<XX>::Insert(unsigned pos, const XX & elem_)
188 if ( pos > len )
189 return;
191 checkSize(len+2);
192 for ( unsigned p = len; p > pos; --p)
194 inhalt[p] = inhalt[p-1];
196 inhalt[pos] = elem_;
197 len++;
201 template <class XX>
202 void
203 ST_List<XX>::Remove(unsigned pos)
205 if ( pos >= len )
206 return;
207 len--;
208 for ( unsigned p = pos; p < len; ++p)
210 inhalt[p] = inhalt[p+1];
215 // Protected:
216 template <class XX>
217 void
218 ST_List<XX>::checkSize(unsigned newLength)
220 // neuen Platzbedarf pruefen:
221 unsigned newSpace = space();
222 if (newLength >= newSpace)
224 if (!newSpace)
225 newSpace = 1;
226 const unsigned nBorder = 2000000000;
227 while(newLength >= newSpace)
229 if (newSpace < nBorder)
230 newSpace <<= 1;
231 else
233 std::cerr << "List becomes too big" << std::endl;
234 exit(1);
239 // Veraenderung ?:
240 if (newSpace != space())
241 alloc(newSpace,true);
244 template <class XX>
245 void
246 ST_List<XX>::alloc( unsigned newSpace,
247 bool re )
249 XX * pNew = new XX[newSpace];
251 if (inhalt != 0)
253 if (re)
255 for (unsigned i = 0; i < len; ++i)
257 pNew[i] = inhalt[i];
258 } // end for
260 delete [] inhalt;
263 inhalt = pNew;
264 allocated = newSpace;
268 template <class XY>
269 DynamicList<XY>::DynamicList()
273 template <class XY>
274 DynamicList<XY>::DynamicList( const DynamicList<XY> & i_rList )
275 : ST_List< XY* >(i_rList)
277 for ( typename DynamicList<XY>::iterator it = this->begin();
278 it != DynamicList<XY>::end();
279 ++it )
281 // Copying the contents the pointers point at:
282 (*it) = new XY( *(*it) );
286 template <class XY>
287 DynamicList<XY>::~DynamicList()
289 this->erase_all();
292 template <class XY>
293 DynamicList<XY> &
294 DynamicList<XY>::operator=( const DynamicList<XY> & i_rList )
296 for ( typename DynamicList<XY>::const_iterator it = i_rList.begin();
297 it != i_rList.end();
298 ++it )
300 push_back( new XY(*(*it)) );
302 return *this;
306 template <class XY>
307 void
308 DynamicList<XY>::Insert(unsigned pos, XY * const & elem_)
310 if ( pos > this->len )
311 return;
313 checkSize(DynamicList<XY>::len+2);
314 memmove( DynamicList<XY>::inhalt+pos+1, DynamicList<XY>::inhalt+pos, (DynamicList<XY>::len-pos) * sizeof(XY*) );
315 this->inhalt[pos] = elem_;
316 this->len++;
319 template <class XY>
320 void
321 DynamicList<XY>::Remove( unsigned pos )
323 if (!this->is_valid_index(pos) )
324 return;
325 this->len--;
326 delete DynamicList<XY>::inhalt[pos];
327 memmove(DynamicList<XY>::inhalt+pos, DynamicList<XY>::inhalt+pos+1, (DynamicList<XY>::len-pos) * sizeof(XY*) );
332 #endif