Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / soltools / inc / st_list.hxx
blob8ede2ef1f949dce6436c583d867b975e8a630295
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #ifndef SOLTOOLS_ST_LIST_HXX
30 #define SOLTOOLS_ST_LIST_HXX
32 #include <string.h>
33 #include <iostream>
34 #include <stdlib.h>
36 template <class XX>
37 class ST_List /// Soltools-List.
39 public :
40 typedef XX * iterator;
41 typedef const XX * const_iterator;
43 // LIFECYCLE
44 ST_List();
45 ST_List(
46 const ST_List<XX> & i_rList );
47 virtual ~ST_List() { delete[] inhalt; }
49 // OPERATORS
50 ST_List<XX> & operator=(
51 const ST_List<XX> & i_rList );
53 const XX & operator[](
54 unsigned n) const
55 { return elem(n); }
56 XX & operator[](
57 unsigned n)
58 { return elem(n); }
59 // OPERATIONS
60 void reserve(
61 unsigned i_nSize )
62 { alloc(i_nSize,true); }
63 void insert(
64 iterator i_aPos,
65 const XX & elem_ )
66 { Insert((unsigned)(i_aPos-begin()), elem_); }
67 virtual void Insert(
68 unsigned pos,
69 const XX & elem );
70 void push_back(
71 const XX & elem_)
72 { Insert(size(),elem_); }
73 void remove(
74 iterator i_aPos )
75 { Remove((int)(i_aPos-begin())); }
76 virtual void Remove(
77 unsigned pos );
78 void pop_back() { Remove(size()-1); }
79 void erase_all() { while (size()) Remove(size()-1); }
81 // INQUIRY
82 const_iterator begin() const { return &inhalt[0]; }
83 const_iterator end() const { return &inhalt[len]; }
85 const XX & front() const { return elem(0); }
86 const XX & back() const { return elem(len-1); }
88 unsigned size() const { return len; }
89 unsigned space() const { return allocated; }
90 bool is_valid_index(
91 unsigned n) const
92 { return n < len; }
93 // ACCESS
94 iterator begin() { return &inhalt[0]; }
95 iterator end() { return &inhalt[len]; }
97 XX & front() { return elem(0); }
98 XX & back() { return elem(len-1); }
100 protected:
101 void checkSize(
102 unsigned newLength);
103 void alloc(
104 unsigned newSpace,
105 bool re = false );
107 const XX & elem(
108 unsigned n ) const
109 { return inhalt[n]; }
110 XX & elem(
111 unsigned n )
112 { return inhalt[n]; }
113 // DATA
114 XX * inhalt;
115 unsigned len;
116 unsigned allocated;
121 template <class XY>
122 class DynamicList : public ST_List< XY* >
124 public:
125 DynamicList();
126 DynamicList(
127 const DynamicList<XY> &
128 i_rList );
129 virtual ~DynamicList(); /// Deletes all member pointers
131 DynamicList<XY> & operator=(
132 const DynamicList<XY> &
133 i_rList );
135 virtual void Insert(
136 unsigned pos,
137 XY * const & elem );
138 virtual void Remove(
139 unsigned pos );
144 template <class XX>
145 ST_List<XX>::ST_List()
146 : inhalt(0),
147 len(0),
148 allocated(0)
150 alloc(1);
153 template <class XX>
154 ST_List<XX>::ST_List( const ST_List<XX> & i_rList )
155 : inhalt(0),
156 len(0),
157 allocated(0)
159 alloc(i_rList.size());
161 for ( const_iterator it = i_rList.begin();
162 it != i_rList.end();
163 ++it )
165 push_back(*it);
169 template <class XX>
170 ST_List<XX> &
171 ST_List<XX>::operator=( const ST_List<XX> & i_rList )
173 for ( const_iterator it = i_rList.begin();
174 it != i_rList.end();
175 ++it )
177 push_back(*it);
179 return *this;
182 template <class XX>
183 void
184 ST_List<XX>::Insert(unsigned pos, const XX & elem_)
186 if ( pos > len )
187 return;
189 checkSize(len+2);
190 for ( unsigned p = len; p > pos; --p)
192 inhalt[p] = inhalt[p-1];
194 inhalt[pos] = elem_;
195 len++;
199 template <class XX>
200 void
201 ST_List<XX>::Remove(unsigned pos)
203 if ( pos >= len )
204 return;
205 len--;
206 for ( unsigned p = pos; p < len; ++p)
208 inhalt[p] = inhalt[p+1];
213 // Protected:
214 template <class XX>
215 void
216 ST_List<XX>::checkSize(unsigned newLength)
218 // neuen Platzbedarf pruefen:
219 unsigned newSpace = space();
220 if (newLength >= newSpace)
222 if (!newSpace)
223 newSpace = 1;
224 const unsigned nBorder = 2000000000;
225 while(newLength >= newSpace)
227 if (newSpace < nBorder)
228 newSpace <<= 1;
229 else
231 std::cerr << "List becomes too big" << std::endl;
232 exit(1);
237 // Veraenderung ?:
238 if (newSpace != space())
239 alloc(newSpace,true);
242 template <class XX>
243 void
244 ST_List<XX>::alloc( unsigned newSpace,
245 bool re )
247 XX * pNew = new XX[newSpace];
249 if (inhalt != 0)
251 if (re)
253 for (unsigned i = 0; i < len; ++i)
255 pNew[i] = inhalt[i];
256 } // end for
258 delete [] inhalt;
261 inhalt = pNew;
262 allocated = newSpace;
266 template <class XY>
267 DynamicList<XY>::DynamicList()
271 template <class XY>
272 DynamicList<XY>::DynamicList( const DynamicList<XY> & i_rList )
273 : ST_List< XY* >(i_rList)
275 for ( typename DynamicList<XY>::iterator it = this->begin();
276 it != DynamicList<XY>::end();
277 ++it )
279 // Copying the contents the pointers point at:
280 (*it) = new XY( *(*it) );
284 template <class XY>
285 DynamicList<XY>::~DynamicList()
287 this->erase_all();
290 template <class XY>
291 DynamicList<XY> &
292 DynamicList<XY>::operator=( const DynamicList<XY> & i_rList )
294 for ( typename DynamicList<XY>::const_iterator it = i_rList.begin();
295 it != i_rList.end();
296 ++it )
298 this->push_back( new XY(*(*it)) );
300 return *this;
304 template <class XY>
305 void
306 DynamicList<XY>::Insert(unsigned pos, XY * const & elem_)
308 if ( pos > this->len )
309 return;
311 this->checkSize(DynamicList<XY>::len+2);
312 memmove( DynamicList<XY>::inhalt+pos+1, DynamicList<XY>::inhalt+pos, (DynamicList<XY>::len-pos) * sizeof(XY*) );
313 this->inhalt[pos] = elem_;
314 this->len++;
317 template <class XY>
318 void
319 DynamicList<XY>::Remove( unsigned pos )
321 if (!this->is_valid_index(pos) )
322 return;
323 this->len--;
324 delete DynamicList<XY>::inhalt[pos];
325 memmove(DynamicList<XY>::inhalt+pos, DynamicList<XY>::inhalt+pos+1, (DynamicList<XY>::len-pos) * sizeof(XY*) );
330 #endif
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */