xref: /AOO41X/main/soltools/inc/st_list.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef SOLTOOLS_ST_LIST_HXX
29*cdf0e10cSrcweir #define SOLTOOLS_ST_LIST_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <string.h>
32*cdf0e10cSrcweir #include <iostream>
33*cdf0e10cSrcweir #include <stdlib.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir template <class XX>
36*cdf0e10cSrcweir class ST_List            /// Soltools-List.
37*cdf0e10cSrcweir {
38*cdf0e10cSrcweir   public :
39*cdf0e10cSrcweir 	typedef XX * 		iterator;
40*cdf0e10cSrcweir 	typedef const XX *	const_iterator;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 	// LIFECYCLE
43*cdf0e10cSrcweir 						ST_List();
44*cdf0e10cSrcweir                         ST_List(
45*cdf0e10cSrcweir                             const ST_List<XX> & i_rList );
46*cdf0e10cSrcweir 	virtual				~ST_List() { }
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 	// OPERATORS
49*cdf0e10cSrcweir 	ST_List<XX> &       operator=(
50*cdf0e10cSrcweir 							const ST_List<XX> & i_rList );
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 	const XX &        	operator[](
53*cdf0e10cSrcweir 							unsigned 			n) const
54*cdf0e10cSrcweir 												{ return elem(n); }
55*cdf0e10cSrcweir 	XX &              	operator[](
56*cdf0e10cSrcweir 							unsigned 			n)
57*cdf0e10cSrcweir 												{ return elem(n); }
58*cdf0e10cSrcweir 	// OPERATIONS
59*cdf0e10cSrcweir 	void	            reserve(
60*cdf0e10cSrcweir 							unsigned			i_nSize )
61*cdf0e10cSrcweir 												{ alloc(i_nSize,true); }
62*cdf0e10cSrcweir 	void  	 	        insert(
63*cdf0e10cSrcweir                             iterator            i_aPos,
64*cdf0e10cSrcweir 							const XX &          elem_ )
65*cdf0e10cSrcweir                                                 { Insert(i_aPos-begin(), elem_); }
66*cdf0e10cSrcweir 	virtual	void  	 	Insert(
67*cdf0e10cSrcweir 							unsigned 			pos,
68*cdf0e10cSrcweir 							const XX &          elem );
69*cdf0e10cSrcweir 	void              	push_back(
70*cdf0e10cSrcweir 							const XX & 			elem_)
71*cdf0e10cSrcweir 												{ Insert(size(),elem_); }
72*cdf0e10cSrcweir 	void  	 	        remove(
73*cdf0e10cSrcweir 							iterator            i_aPos )
74*cdf0e10cSrcweir                                                 { Remove(i_aPos-begin()); }
75*cdf0e10cSrcweir 	virtual	void  	 	Remove(
76*cdf0e10cSrcweir 							unsigned 			pos );
77*cdf0e10cSrcweir 	void              	pop_back()				{ Remove(size()-1); }
78*cdf0e10cSrcweir 	void 				erase_all()            	{ while (size()) Remove(size()-1); }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	// INQUIRY
81*cdf0e10cSrcweir     const_iterator      begin() const           { return &inhalt[0]; }
82*cdf0e10cSrcweir     const_iterator      end() const             { return &inhalt[len]; }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	const XX &          front() const       	{ return elem(0); }
85*cdf0e10cSrcweir 	const XX &          back() const        	{ return elem(len-1); }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	unsigned			size() const            { return len; }
88*cdf0e10cSrcweir 	unsigned			space() const           { return allocated; }
89*cdf0e10cSrcweir 	bool              	is_valid_index(
90*cdf0e10cSrcweir 							unsigned			n) const
91*cdf0e10cSrcweir 												{ return n < len; }
92*cdf0e10cSrcweir 	// ACCESS
93*cdf0e10cSrcweir     iterator            begin()                 { return &inhalt[0]; }
94*cdf0e10cSrcweir     iterator            end()                   { return &inhalt[len]; }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	XX &                front()                 { return elem(0); }
97*cdf0e10cSrcweir 	XX &                back()                  { return elem(len-1); }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir   protected:
100*cdf0e10cSrcweir 	void  				checkSize(
101*cdf0e10cSrcweir 							unsigned 			newLength);
102*cdf0e10cSrcweir 	void				alloc(
103*cdf0e10cSrcweir 							unsigned			newSpace,
104*cdf0e10cSrcweir 							bool                re = false );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 	const XX &		   	elem(
107*cdf0e10cSrcweir 							unsigned 			n ) const
108*cdf0e10cSrcweir 												{ return inhalt[n]; }
109*cdf0e10cSrcweir 	XX &				elem(
110*cdf0e10cSrcweir 							unsigned 			n )
111*cdf0e10cSrcweir 												{ return inhalt[n]; }
112*cdf0e10cSrcweir   // DATA
113*cdf0e10cSrcweir 	XX *				inhalt;
114*cdf0e10cSrcweir 	unsigned            len;
115*cdf0e10cSrcweir     unsigned			allocated;
116*cdf0e10cSrcweir };
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir template <class XY>
121*cdf0e10cSrcweir class DynamicList : public ST_List< XY* >
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir   public:
124*cdf0e10cSrcweir                         DynamicList();
125*cdf0e10cSrcweir                         DynamicList(
126*cdf0e10cSrcweir                             const DynamicList<XY> &
127*cdf0e10cSrcweir                                                 i_rList );
128*cdf0e10cSrcweir 	virtual				~DynamicList();         /// Deletes all member pointers
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	DynamicList<XY> &   operator=(
131*cdf0e10cSrcweir 							const DynamicList<XY> &
132*cdf0e10cSrcweir                                                 i_rList );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	virtual	void  	 	Insert(
135*cdf0e10cSrcweir 							unsigned 			pos,
136*cdf0e10cSrcweir 							XY * const &        elem );
137*cdf0e10cSrcweir 	virtual	void  	 	Remove(
138*cdf0e10cSrcweir 							unsigned 			pos );
139*cdf0e10cSrcweir };
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir template <class XX>
144*cdf0e10cSrcweir ST_List<XX>::ST_List()
145*cdf0e10cSrcweir 	:	inhalt(0),
146*cdf0e10cSrcweir 		len(0),
147*cdf0e10cSrcweir         allocated(0)
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir     alloc(1);
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir template <class XX>
153*cdf0e10cSrcweir ST_List<XX>::ST_List( const ST_List<XX> & i_rList )
154*cdf0e10cSrcweir 	:	inhalt(0),
155*cdf0e10cSrcweir 		len(0),
156*cdf0e10cSrcweir         allocated(0)
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	alloc(i_rList.size());
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     for ( const_iterator it = i_rList.begin();
161*cdf0e10cSrcweir           it != i_rList.end();
162*cdf0e10cSrcweir           ++it )
163*cdf0e10cSrcweir     {
164*cdf0e10cSrcweir      	push_back(*it);
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir template <class XX>
169*cdf0e10cSrcweir ST_List<XX> &
170*cdf0e10cSrcweir ST_List<XX>::operator=( const ST_List<XX> & i_rList )
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir     for ( const_iterator it = i_rList.begin();
173*cdf0e10cSrcweir           it != i_rList.end();
174*cdf0e10cSrcweir           ++it )
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir      	push_back(*it);
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir     return *this;
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir template <class XX>
182*cdf0e10cSrcweir void
183*cdf0e10cSrcweir ST_List<XX>::Insert(unsigned pos, const XX & elem_)
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir 	if ( pos > len )
186*cdf0e10cSrcweir 	  return;
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 	checkSize(len+2);
189*cdf0e10cSrcweir 	for ( unsigned p = len; p > pos; --p)
190*cdf0e10cSrcweir 	{
191*cdf0e10cSrcweir 		inhalt[p] = inhalt[p-1];
192*cdf0e10cSrcweir 	}
193*cdf0e10cSrcweir 	inhalt[pos] = elem_;
194*cdf0e10cSrcweir 	len++;
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir template <class XX>
199*cdf0e10cSrcweir void
200*cdf0e10cSrcweir ST_List<XX>::Remove(unsigned pos)
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir 	if ( pos >= len )
203*cdf0e10cSrcweir 	  return;
204*cdf0e10cSrcweir 	len--;
205*cdf0e10cSrcweir 	for ( unsigned p = pos; p < len; ++p)
206*cdf0e10cSrcweir 	{
207*cdf0e10cSrcweir 		inhalt[p] = inhalt[p+1];
208*cdf0e10cSrcweir 	}
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir // Protected:
213*cdf0e10cSrcweir template <class XX>
214*cdf0e10cSrcweir void
215*cdf0e10cSrcweir ST_List<XX>::checkSize(unsigned newLength)
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir    // neuen Platzbedarf pruefen:
218*cdf0e10cSrcweir    unsigned newSpace = space();
219*cdf0e10cSrcweir    if (newLength >= newSpace)
220*cdf0e10cSrcweir    {
221*cdf0e10cSrcweir 	  if (!newSpace)
222*cdf0e10cSrcweir 		 newSpace = 1;
223*cdf0e10cSrcweir 	  const unsigned nBorder = 2000000000;
224*cdf0e10cSrcweir 	  while(newLength >= newSpace)
225*cdf0e10cSrcweir 	  {
226*cdf0e10cSrcweir 		if (newSpace < nBorder)
227*cdf0e10cSrcweir 			newSpace <<= 1;
228*cdf0e10cSrcweir 		else
229*cdf0e10cSrcweir 		{
230*cdf0e10cSrcweir 			std::cerr << "List becomes too big" << std::endl;
231*cdf0e10cSrcweir 			exit(1);
232*cdf0e10cSrcweir 		}
233*cdf0e10cSrcweir 	  }
234*cdf0e10cSrcweir    }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir    // Veraenderung ?:
237*cdf0e10cSrcweir    if (newSpace != space())
238*cdf0e10cSrcweir 	  alloc(newSpace,true);
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir template <class XX>
242*cdf0e10cSrcweir void
243*cdf0e10cSrcweir ST_List<XX>::alloc( unsigned			newSpace,
244*cdf0e10cSrcweir 				    bool               re )
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir 	XX * pNew = new XX[newSpace];
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 	if (inhalt != 0)
249*cdf0e10cSrcweir 	{
250*cdf0e10cSrcweir 		if (re)
251*cdf0e10cSrcweir 		{
252*cdf0e10cSrcweir 			for (unsigned i = 0; i < len; ++i)
253*cdf0e10cSrcweir 			{
254*cdf0e10cSrcweir 				pNew[i] = inhalt[i];
255*cdf0e10cSrcweir 			}  // end for
256*cdf0e10cSrcweir 		}
257*cdf0e10cSrcweir 		delete [] inhalt;
258*cdf0e10cSrcweir 	}
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 	inhalt = pNew;
261*cdf0e10cSrcweir 	allocated = newSpace;
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir template <class XY>
266*cdf0e10cSrcweir DynamicList<XY>::DynamicList()
267*cdf0e10cSrcweir {
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir template <class XY>
271*cdf0e10cSrcweir DynamicList<XY>::DynamicList( const DynamicList<XY> & i_rList )
272*cdf0e10cSrcweir     :   ST_List< XY* >(i_rList)
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir     for ( typename DynamicList<XY>::iterator it = this->begin();
275*cdf0e10cSrcweir           it != DynamicList<XY>::end();
276*cdf0e10cSrcweir           ++it )
277*cdf0e10cSrcweir     {
278*cdf0e10cSrcweir         // Copying the contents the pointers point at:
279*cdf0e10cSrcweir      	(*it) = new XY( *(*it) );
280*cdf0e10cSrcweir     }
281*cdf0e10cSrcweir }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir template <class XY>
284*cdf0e10cSrcweir DynamicList<XY>::~DynamicList()
285*cdf0e10cSrcweir {
286*cdf0e10cSrcweir 	this->erase_all();
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir template <class XY>
290*cdf0e10cSrcweir DynamicList<XY> &
291*cdf0e10cSrcweir DynamicList<XY>::operator=( const DynamicList<XY> & i_rList )
292*cdf0e10cSrcweir {
293*cdf0e10cSrcweir     for ( typename DynamicList<XY>::const_iterator it = i_rList.begin();
294*cdf0e10cSrcweir           it != i_rList.end();
295*cdf0e10cSrcweir           ++it )
296*cdf0e10cSrcweir     {
297*cdf0e10cSrcweir      	push_back( new XY(*(*it)) );
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir     return *this;
300*cdf0e10cSrcweir }
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir template <class XY>
304*cdf0e10cSrcweir void
305*cdf0e10cSrcweir DynamicList<XY>::Insert(unsigned pos, XY * const & elem_)
306*cdf0e10cSrcweir {
307*cdf0e10cSrcweir 	if ( pos > this->len )
308*cdf0e10cSrcweir 	  return;
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir 	checkSize(DynamicList<XY>::len+2);
311*cdf0e10cSrcweir 	memmove( DynamicList<XY>::inhalt+pos+1, DynamicList<XY>::inhalt+pos, (DynamicList<XY>::len-pos) * sizeof(XY*) );
312*cdf0e10cSrcweir 	this->inhalt[pos] = elem_;
313*cdf0e10cSrcweir 	this->len++;
314*cdf0e10cSrcweir }
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir template <class XY>
317*cdf0e10cSrcweir void
318*cdf0e10cSrcweir DynamicList<XY>::Remove( unsigned pos )
319*cdf0e10cSrcweir {
320*cdf0e10cSrcweir 	if (!this->is_valid_index(pos) )
321*cdf0e10cSrcweir 		return;
322*cdf0e10cSrcweir 	this->len--;
323*cdf0e10cSrcweir 	delete DynamicList<XY>::inhalt[pos];
324*cdf0e10cSrcweir 	memmove(DynamicList<XY>::inhalt+pos, DynamicList<XY>::inhalt+pos+1, (DynamicList<XY>::len-pos) * sizeof(XY*) );
325*cdf0e10cSrcweir }
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir #endif
330*cdf0e10cSrcweir 
331