xref: /aoo42x/main/sfx2/source/bastyp/minarray.cxx (revision d119d52d)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifndef GCC
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <sfx2/minarray.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // -----------------------------------------------------------------------
33cdf0e10cSrcweir 
SfxPtrArr(sal_uInt8 nInitSize,sal_uInt8 nGrowSize)34cdf0e10cSrcweir SfxPtrArr::SfxPtrArr( sal_uInt8 nInitSize, sal_uInt8 nGrowSize ):
35cdf0e10cSrcweir 	nUsed( 0 ),
36cdf0e10cSrcweir 	nGrow( nGrowSize ? nGrowSize : 1 ),
37cdf0e10cSrcweir 	nUnused( nInitSize )
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 	DBG_MEMTEST();
40cdf0e10cSrcweir 	sal_uInt16 nMSCBug = nInitSize;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 	if ( nMSCBug > 0 )
43cdf0e10cSrcweir 		pData = new void*[nMSCBug];
44cdf0e10cSrcweir 	else
45cdf0e10cSrcweir 		pData = 0;
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // -----------------------------------------------------------------------
49cdf0e10cSrcweir 
SfxPtrArr(const SfxPtrArr & rOrig)50cdf0e10cSrcweir SfxPtrArr::SfxPtrArr( const SfxPtrArr& rOrig )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	DBG_MEMTEST();
53cdf0e10cSrcweir 	nUsed = rOrig.nUsed;
54cdf0e10cSrcweir 	nGrow = rOrig.nGrow;
55cdf0e10cSrcweir 	nUnused = rOrig.nUnused;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	if ( rOrig.pData != 0 )
58cdf0e10cSrcweir 	{
59cdf0e10cSrcweir 		pData = new void*[nUsed+nUnused];
60cdf0e10cSrcweir 		memcpy( pData, rOrig.pData, nUsed*sizeof(void*) );
61cdf0e10cSrcweir 	}
62cdf0e10cSrcweir 	else
63cdf0e10cSrcweir 		pData = 0;
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir // -----------------------------------------------------------------------
67cdf0e10cSrcweir 
~SfxPtrArr()68cdf0e10cSrcweir SfxPtrArr::~SfxPtrArr()
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	DBG_MEMTEST();
71cdf0e10cSrcweir 	delete [] pData;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir // -----------------------------------------------------------------------
75cdf0e10cSrcweir 
operator =(const SfxPtrArr & rOrig)76cdf0e10cSrcweir SfxPtrArr& SfxPtrArr::operator=( const SfxPtrArr& rOrig )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	DBG_MEMTEST();
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	delete [] pData;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	nUsed = rOrig.nUsed;
83cdf0e10cSrcweir 	nGrow = rOrig.nGrow;
84cdf0e10cSrcweir 	nUnused = rOrig.nUnused;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	if ( rOrig.pData != 0 )
87cdf0e10cSrcweir 	{
88cdf0e10cSrcweir 		pData = new void*[nUsed+nUnused];
89cdf0e10cSrcweir 		memcpy( pData, rOrig.pData, nUsed*sizeof(void*) );
90cdf0e10cSrcweir 	}
91cdf0e10cSrcweir 	else
92cdf0e10cSrcweir 		pData = 0;
93cdf0e10cSrcweir 	return *this;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir // -----------------------------------------------------------------------
97cdf0e10cSrcweir 
Append(void * aElem)98cdf0e10cSrcweir void SfxPtrArr::Append( void* aElem )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	DBG_MEMTEST();
101cdf0e10cSrcweir 	DBG_ASSERT( sal::static_int_cast< unsigned >(nUsed+1) < ( USHRT_MAX / sizeof(void*) ), "array too large" );
102cdf0e10cSrcweir 	// musz das Array umkopiert werden?
103cdf0e10cSrcweir 	if ( nUnused == 0 )
104cdf0e10cSrcweir 	{
105cdf0e10cSrcweir 		sal_uInt16 nNewSize = (nUsed == 1) ? (nGrow==1 ? 2 : nGrow) : nUsed+nGrow;
106cdf0e10cSrcweir 		void** pNewData = new void*[nNewSize];
107cdf0e10cSrcweir 		if ( pData )
108cdf0e10cSrcweir 		{
109cdf0e10cSrcweir 			DBG_ASSERT( nUsed <= nNewSize, "" );
110cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(void*)*nUsed );
111cdf0e10cSrcweir 			delete [] pData;
112cdf0e10cSrcweir 		}
113cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
114cdf0e10cSrcweir 		pData = pNewData;
115cdf0e10cSrcweir 	}
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	// jetzt hinten in den freien Raum schreiben
118cdf0e10cSrcweir 	pData[nUsed] = aElem;
119cdf0e10cSrcweir 	++nUsed;
120cdf0e10cSrcweir 	--nUnused;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir // -----------------------------------------------------------------------
124cdf0e10cSrcweir 
Remove(sal_uInt16 nPos,sal_uInt16 nLen)125cdf0e10cSrcweir sal_uInt16 SfxPtrArr::Remove( sal_uInt16 nPos, sal_uInt16 nLen )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	DBG_MEMTEST();
128cdf0e10cSrcweir 	// nLen adjustieren, damit nicht ueber das Ende hinaus geloescht wird
129cdf0e10cSrcweir 	nLen = Min( (sal_uInt16)(nUsed-nPos), nLen );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	// einfache Aufgaben erfordern einfache Loesungen!
132cdf0e10cSrcweir 	if ( nLen == 0 )
133cdf0e10cSrcweir 		return 0;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	// bleibt vielleicht keiner uebrig
136cdf0e10cSrcweir 	if ( (nUsed-nLen) == 0 )
137cdf0e10cSrcweir 	{
138cdf0e10cSrcweir 		delete [] pData;
139cdf0e10cSrcweir 		pData = 0;
140cdf0e10cSrcweir 		nUsed = 0;
141cdf0e10cSrcweir 		nUnused = 0;
142cdf0e10cSrcweir 		return nLen;
143cdf0e10cSrcweir 	}
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	// feststellen, ob das Array dadurch physikalisch schrumpft...
146cdf0e10cSrcweir 	if ( (nUnused+nLen) >= nGrow )
147cdf0e10cSrcweir 	{
148cdf0e10cSrcweir 		// auf die naechste Grow-Grenze aufgerundet verkleinern
149cdf0e10cSrcweir 		sal_uInt16 nNewUsed = nUsed-nLen;
150cdf0e10cSrcweir 		sal_uInt16 nNewSize = ((nNewUsed+nGrow-1)/nGrow) * nGrow;
151cdf0e10cSrcweir 		DBG_ASSERT( nNewUsed <= nNewSize && nNewUsed+nGrow > nNewSize,
152cdf0e10cSrcweir 					"shrink size computation failed" );
153cdf0e10cSrcweir 		void** pNewData = new void*[nNewSize];
154cdf0e10cSrcweir 		if ( nPos > 0 )
155cdf0e10cSrcweir 		{
156cdf0e10cSrcweir 			DBG_ASSERT( nPos <= nNewSize, "" );
157cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(void*)*nPos );
158cdf0e10cSrcweir 		}
159cdf0e10cSrcweir 		if ( nNewUsed != nPos )
160cdf0e10cSrcweir 			memmove( pNewData+nPos, pData+nPos+nLen,
161cdf0e10cSrcweir 					 sizeof(void*)*(nNewUsed-nPos) );
162cdf0e10cSrcweir 		delete [] pData;
163cdf0e10cSrcweir 		pData = pNewData;
164cdf0e10cSrcweir 		nUsed = nNewUsed;
165cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize - nNewUsed);
166cdf0e10cSrcweir 		return nLen;
167cdf0e10cSrcweir 	}
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	// in allen anderen Faellen nur zusammenschieben
170cdf0e10cSrcweir 	if ( nUsed-nPos-nLen > 0 )
171cdf0e10cSrcweir 		memmove( pData+nPos, pData+nPos+nLen, (nUsed-nPos-nLen)*sizeof(void*) );
172cdf0e10cSrcweir 	nUsed = nUsed - nLen;
173cdf0e10cSrcweir 	nUnused = sal::static_int_cast< sal_uInt8 >(nUnused + nLen);
174cdf0e10cSrcweir 	return nLen;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir // -----------------------------------------------------------------------
178cdf0e10cSrcweir 
Remove(void * aElem)179cdf0e10cSrcweir sal_Bool SfxPtrArr::Remove( void* aElem )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir 	DBG_MEMTEST();
182cdf0e10cSrcweir 	// einfache Aufgaben ...
183cdf0e10cSrcweir 	if ( nUsed == 0 )
184cdf0e10cSrcweir 		return sal_False;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	// rueckwaerts, da meist der letzte zuerst wieder entfernt wird
187cdf0e10cSrcweir 	void* *pIter = pData + nUsed - 1;
188cdf0e10cSrcweir 	for ( sal_uInt16 n = 0; n < nUsed; ++n, --pIter )
189cdf0e10cSrcweir 		if ( *pIter == aElem )
190cdf0e10cSrcweir 		{
191cdf0e10cSrcweir 			Remove(nUsed-n-1, 1);
192cdf0e10cSrcweir 			return sal_True;
193cdf0e10cSrcweir 		}
194cdf0e10cSrcweir 	return sal_False;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir // -----------------------------------------------------------------------
198cdf0e10cSrcweir 
Replace(void * aOldElem,void * aNewElem)199cdf0e10cSrcweir sal_Bool SfxPtrArr::Replace( void* aOldElem, void* aNewElem )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir 	DBG_MEMTEST();
202cdf0e10cSrcweir 	// einfache Aufgaben ...
203cdf0e10cSrcweir 	if ( nUsed == 0 )
204cdf0e10cSrcweir 		return sal_False;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 	// rueckwaerts, da meist der letzte zuerst wieder entfernt wird
207cdf0e10cSrcweir 	void* *pIter = pData + nUsed - 1;
208cdf0e10cSrcweir 	for ( sal_uInt16 n = 0; n < nUsed; ++n, --pIter )
209cdf0e10cSrcweir 		if ( *pIter == aOldElem )
210cdf0e10cSrcweir 		{
211cdf0e10cSrcweir 			pData[nUsed-n-1] = aNewElem;
212cdf0e10cSrcweir 			return sal_True;
213cdf0e10cSrcweir 		}
214cdf0e10cSrcweir 	return sal_False;
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir // -----------------------------------------------------------------------
218cdf0e10cSrcweir 
Contains(const void * rItem) const219cdf0e10cSrcweir sal_Bool SfxPtrArr::Contains( const void* rItem ) const
220cdf0e10cSrcweir {
221cdf0e10cSrcweir 	DBG_MEMTEST();
222cdf0e10cSrcweir 	if ( !nUsed )
223cdf0e10cSrcweir 		return sal_False;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	for ( sal_uInt16 n = 0; n < nUsed; ++n )
226cdf0e10cSrcweir 	{
227cdf0e10cSrcweir 		void* p = GetObject(n);
228cdf0e10cSrcweir 		if ( p == rItem )
229cdf0e10cSrcweir 			return sal_True;
230cdf0e10cSrcweir 	}
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 	return sal_False;
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir // -----------------------------------------------------------------------
236cdf0e10cSrcweir 
Insert(sal_uInt16 nPos,void * rElem)237cdf0e10cSrcweir void SfxPtrArr::Insert( sal_uInt16 nPos, void* rElem )
238cdf0e10cSrcweir {
239cdf0e10cSrcweir 	DBG_MEMTEST();
240cdf0e10cSrcweir 	DBG_ASSERT( sal::static_int_cast< unsigned >(nUsed+1) < ( USHRT_MAX / sizeof(void*) ), "array too large" );
241cdf0e10cSrcweir 	// musz das Array umkopiert werden?
242cdf0e10cSrcweir 	if ( nUnused == 0 )
243cdf0e10cSrcweir 	{
244cdf0e10cSrcweir 		// auf die naechste Grow-Grenze aufgerundet vergroeszern
245cdf0e10cSrcweir 		sal_uInt16 nNewSize = nUsed+nGrow;
246cdf0e10cSrcweir 		void** pNewData = new void*[nNewSize];
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 		if ( pData )
249cdf0e10cSrcweir 		{
250cdf0e10cSrcweir 			DBG_ASSERT( nUsed < nNewSize, "" );
251cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(void*)*nUsed );
252cdf0e10cSrcweir 			delete [] pData;
253cdf0e10cSrcweir 		}
254cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
255cdf0e10cSrcweir 		pData = pNewData;
256cdf0e10cSrcweir 	}
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	// jetzt den hinteren Teil verschieben
259cdf0e10cSrcweir 	if ( nPos < nUsed )
260cdf0e10cSrcweir 		memmove( pData+nPos+1, pData+nPos, (nUsed-nPos)*sizeof(void*) );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	// jetzt in den freien Raum schreiben
263cdf0e10cSrcweir 	memmove( pData+nPos, &rElem, sizeof(void*) );
264cdf0e10cSrcweir 	nUsed += 1;
265cdf0e10cSrcweir 	nUnused -= 1;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir // class ByteArr ---------------------------------------------------------
269cdf0e10cSrcweir 
ByteArr(sal_uInt8 nInitSize,sal_uInt8 nGrowSize)270cdf0e10cSrcweir ByteArr::ByteArr( sal_uInt8 nInitSize, sal_uInt8 nGrowSize ):
271cdf0e10cSrcweir 	nUsed( 0 ),
272cdf0e10cSrcweir 	nGrow( nGrowSize ? nGrowSize : 1 ),
273cdf0e10cSrcweir 	nUnused( nInitSize )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir 	DBG_MEMTEST();
276cdf0e10cSrcweir 	sal_uInt16 nMSCBug = nInitSize;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	if ( nInitSize > 0 )
279cdf0e10cSrcweir 		pData = new char[nMSCBug];
280cdf0e10cSrcweir 	else
281cdf0e10cSrcweir 		pData = 0;
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir // -----------------------------------------------------------------------
285cdf0e10cSrcweir 
ByteArr(const ByteArr & rOrig)286cdf0e10cSrcweir ByteArr::ByteArr( const ByteArr& rOrig )
287cdf0e10cSrcweir {
288cdf0e10cSrcweir 	DBG_MEMTEST();
289cdf0e10cSrcweir 	nUsed = rOrig.nUsed;
290cdf0e10cSrcweir 	nGrow = rOrig.nGrow;
291cdf0e10cSrcweir 	nUnused = rOrig.nUnused;
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 	if ( rOrig.pData != 0 )
294cdf0e10cSrcweir 	{
295cdf0e10cSrcweir 		pData = new char[nUsed+nUnused];
296cdf0e10cSrcweir 		memcpy( pData, rOrig.pData, nUsed*sizeof(char) );
297cdf0e10cSrcweir 	}
298cdf0e10cSrcweir 	else
299cdf0e10cSrcweir 		pData = 0;
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir // -----------------------------------------------------------------------
303cdf0e10cSrcweir 
~ByteArr()304cdf0e10cSrcweir ByteArr::~ByteArr()
305cdf0e10cSrcweir {
306cdf0e10cSrcweir 	DBG_MEMTEST();
307cdf0e10cSrcweir 	delete [] pData;
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir // -----------------------------------------------------------------------
311cdf0e10cSrcweir 
operator =(const ByteArr & rOrig)312cdf0e10cSrcweir ByteArr& ByteArr::operator=( const ByteArr& rOrig )
313cdf0e10cSrcweir {
314cdf0e10cSrcweir 	DBG_MEMTEST();
315cdf0e10cSrcweir 
316cdf0e10cSrcweir 	delete [] pData;
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 	nUsed = rOrig.nUsed;
319cdf0e10cSrcweir 	nGrow = rOrig.nGrow;
320cdf0e10cSrcweir 	nUnused = rOrig.nUnused;
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	if ( rOrig.pData != 0 )
323cdf0e10cSrcweir 	{
324cdf0e10cSrcweir 		pData = new char[nUsed+nUnused];
325cdf0e10cSrcweir 		memcpy( pData, rOrig.pData, nUsed*sizeof(char) );
326cdf0e10cSrcweir 	}
327cdf0e10cSrcweir 	else
328cdf0e10cSrcweir 		pData = 0;
329cdf0e10cSrcweir 	return *this;
330cdf0e10cSrcweir }
331cdf0e10cSrcweir 
332cdf0e10cSrcweir // -----------------------------------------------------------------------
333cdf0e10cSrcweir 
Append(char aElem)334cdf0e10cSrcweir void ByteArr::Append( char aElem )
335cdf0e10cSrcweir {
336cdf0e10cSrcweir 	DBG_MEMTEST();
337cdf0e10cSrcweir 	// musz das Array umkopiert werden?
338cdf0e10cSrcweir 	if ( nUnused == 0 )
339cdf0e10cSrcweir 	{
340cdf0e10cSrcweir 		sal_uInt16 nNewSize = (nUsed == 1) ? (nGrow==1 ? 2 : nGrow) : nUsed+nGrow;
341cdf0e10cSrcweir 		char* pNewData = new char[nNewSize];
342cdf0e10cSrcweir 		if ( pData )
343cdf0e10cSrcweir 		{
344cdf0e10cSrcweir 			DBG_ASSERT( nUsed <= nNewSize, "" );
345cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(char)*nUsed );
346cdf0e10cSrcweir 			delete [] pData;
347cdf0e10cSrcweir 		}
348cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
349cdf0e10cSrcweir 		pData = pNewData;
350cdf0e10cSrcweir 	}
351cdf0e10cSrcweir 
352cdf0e10cSrcweir 	// jetzt hinten in den freien Raum schreiben
353cdf0e10cSrcweir 	pData[nUsed] = aElem;
354cdf0e10cSrcweir 	++nUsed;
355cdf0e10cSrcweir 	--nUnused;
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir // -----------------------------------------------------------------------
359cdf0e10cSrcweir 
Remove(sal_uInt16 nPos,sal_uInt16 nLen)360cdf0e10cSrcweir sal_uInt16 ByteArr::Remove( sal_uInt16 nPos, sal_uInt16 nLen )
361cdf0e10cSrcweir {
362cdf0e10cSrcweir 	DBG_MEMTEST();
363cdf0e10cSrcweir 	// nLen adjustieren, damit nicht ueber das Ende hinaus geloescht wird
364cdf0e10cSrcweir 	nLen = Min( (sal_uInt16)(nUsed-nPos), nLen );
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 	// einfache Aufgaben erfordern einfache Loesungen!
367cdf0e10cSrcweir 	if ( nLen == 0 )
368cdf0e10cSrcweir 		return 0;
369cdf0e10cSrcweir 
370cdf0e10cSrcweir 	// bleibt vielleicht keiner uebrig
371cdf0e10cSrcweir 	if ( (nUsed-nLen) == 0 )
372cdf0e10cSrcweir 	{
373cdf0e10cSrcweir 		delete [] pData;
374cdf0e10cSrcweir 		pData = 0;
375cdf0e10cSrcweir 		nUsed = 0;
376cdf0e10cSrcweir 		nUnused = 0;
377cdf0e10cSrcweir 		return nLen;
378cdf0e10cSrcweir 	}
379cdf0e10cSrcweir 
380cdf0e10cSrcweir 	// feststellen, ob das Array dadurch physikalisch schrumpft...
381cdf0e10cSrcweir 	if ( (nUnused+nLen) >= nGrow )
382cdf0e10cSrcweir 	{
383cdf0e10cSrcweir 		// auf die naechste Grow-Grenze aufgerundet verkleinern
384cdf0e10cSrcweir 		sal_uInt16 nNewUsed = nUsed-nLen;
385cdf0e10cSrcweir 		sal_uInt16 nNewSize = ((nNewUsed+nGrow-1)/nGrow) * nGrow;
386cdf0e10cSrcweir 		DBG_ASSERT( nNewUsed <= nNewSize && nNewUsed+nGrow > nNewSize,
387cdf0e10cSrcweir 					"shrink size computation failed" );
388cdf0e10cSrcweir 		char* pNewData = new char[nNewSize];
389cdf0e10cSrcweir 		if ( nPos > 0 )
390cdf0e10cSrcweir 		{
391cdf0e10cSrcweir 			DBG_ASSERT( nPos <= nNewSize, "" );
392cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(char)*nPos );
393cdf0e10cSrcweir 		}
394cdf0e10cSrcweir 		if ( nNewUsed != nPos )
395cdf0e10cSrcweir 			memmove( pNewData+nPos, pData+nPos+nLen,
396cdf0e10cSrcweir 					 sizeof(char)*(nNewUsed-nPos) );
397cdf0e10cSrcweir 		delete [] pData;
398cdf0e10cSrcweir 		pData = pNewData;
399cdf0e10cSrcweir 		nUsed = nNewUsed;
400cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize - nNewUsed);
401cdf0e10cSrcweir 		return nLen;
402cdf0e10cSrcweir 	}
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 	// in allen anderen Faellen nur zusammenschieben
405cdf0e10cSrcweir 	if ( nUsed-nPos-nLen > 0 )
406cdf0e10cSrcweir 		memmove( pData+nPos, pData+nPos+nLen, (nUsed-nPos-nLen)*sizeof(char) );
407cdf0e10cSrcweir 	nUsed = nUsed - nLen;
408cdf0e10cSrcweir 	nUnused = sal::static_int_cast< sal_uInt8 >(nUnused + nLen);
409cdf0e10cSrcweir 	return nLen;
410cdf0e10cSrcweir }
411cdf0e10cSrcweir 
412cdf0e10cSrcweir // -----------------------------------------------------------------------
413cdf0e10cSrcweir 
Remove(char aElem)414cdf0e10cSrcweir sal_Bool ByteArr::Remove( char aElem )
415cdf0e10cSrcweir {
416cdf0e10cSrcweir 	DBG_MEMTEST();
417cdf0e10cSrcweir 	// einfache Aufgaben ...
418cdf0e10cSrcweir 	if ( nUsed == 0 )
419cdf0e10cSrcweir 		return sal_False;
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 	// rueckwaerts, da meist der letzte zuerst wieder entfernt wird
422cdf0e10cSrcweir 	char *pIter = pData + nUsed - 1;
423cdf0e10cSrcweir 	for ( sal_uInt16 n = 0; n < nUsed; ++n, --pIter )
424cdf0e10cSrcweir 		if ( *pIter == aElem )
425cdf0e10cSrcweir 		{
426cdf0e10cSrcweir 			Remove(nUsed-n-1, 1);
427cdf0e10cSrcweir 			return sal_True;
428cdf0e10cSrcweir 		}
429cdf0e10cSrcweir 	return sal_False;
430cdf0e10cSrcweir }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir // -----------------------------------------------------------------------
433cdf0e10cSrcweir 
Contains(const char rItem) const434cdf0e10cSrcweir sal_Bool ByteArr::Contains( const char rItem ) const
435cdf0e10cSrcweir {
436cdf0e10cSrcweir 	DBG_MEMTEST();
437cdf0e10cSrcweir 	if ( !nUsed )
438cdf0e10cSrcweir 		return sal_False;
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 	for ( sal_uInt16 n = 0; n < nUsed; ++n )
441cdf0e10cSrcweir 	{
442cdf0e10cSrcweir 		char p = GetObject(n);
443cdf0e10cSrcweir 		if ( p == rItem )
444cdf0e10cSrcweir 			return sal_True;
445cdf0e10cSrcweir 	}
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 	return sal_False;
448cdf0e10cSrcweir }
449cdf0e10cSrcweir 
450cdf0e10cSrcweir // -----------------------------------------------------------------------
451cdf0e10cSrcweir 
Insert(sal_uInt16 nPos,char rElem)452cdf0e10cSrcweir void ByteArr::Insert( sal_uInt16 nPos, char rElem )
453cdf0e10cSrcweir {
454cdf0e10cSrcweir 	DBG_MEMTEST();
455cdf0e10cSrcweir 	// musz das Array umkopiert werden?
456cdf0e10cSrcweir 	if ( nUnused == 0 )
457cdf0e10cSrcweir 	{
458cdf0e10cSrcweir 		// auf die naechste Grow-Grenze aufgerundet vergroeszern
459cdf0e10cSrcweir 		sal_uInt16 nNewSize = nUsed+nGrow;
460cdf0e10cSrcweir 		char* pNewData = new char[nNewSize];
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 		if ( pData )
463cdf0e10cSrcweir 		{
464cdf0e10cSrcweir 			DBG_ASSERT( nUsed < nNewSize, "" );
465cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(char)*nUsed );
466cdf0e10cSrcweir 			delete [] pData;
467cdf0e10cSrcweir 		}
468cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
469cdf0e10cSrcweir 		pData = pNewData;
470cdf0e10cSrcweir 	}
471cdf0e10cSrcweir 
472cdf0e10cSrcweir 	// jetzt den hinteren Teil verschieben
473cdf0e10cSrcweir 	if ( nPos < nUsed )
474cdf0e10cSrcweir 		memmove( pData+nPos+1, pData+nPos, (nUsed-nPos)*sizeof(char) );
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 	// jetzt in den freien Raum schreiben
477cdf0e10cSrcweir 	memmove( pData+nPos, &rElem, sizeof(char) );
478cdf0e10cSrcweir 	nUsed += 1;
479cdf0e10cSrcweir 	nUnused -= 1;
480cdf0e10cSrcweir }
481cdf0e10cSrcweir 
482cdf0e10cSrcweir // -----------------------------------------------------------------------
483cdf0e10cSrcweir 
operator [](sal_uInt16 nPos) const484cdf0e10cSrcweir char ByteArr::operator[]( sal_uInt16 nPos ) const
485cdf0e10cSrcweir {
486cdf0e10cSrcweir 	DBG_MEMTEST();
487cdf0e10cSrcweir 	DBG_ASSERT( nPos < nUsed, "" );
488cdf0e10cSrcweir 	return *(pData+nPos);
489cdf0e10cSrcweir }
490cdf0e10cSrcweir 
491cdf0e10cSrcweir // -----------------------------------------------------------------------
492cdf0e10cSrcweir 
operator [](sal_uInt16 nPos)493cdf0e10cSrcweir char& ByteArr::operator [] (sal_uInt16 nPos)
494cdf0e10cSrcweir {
495cdf0e10cSrcweir 	DBG_MEMTEST();
496cdf0e10cSrcweir 	DBG_ASSERT( nPos < nUsed, "" );
497cdf0e10cSrcweir 	return *(pData+nPos);
498cdf0e10cSrcweir }
499cdf0e10cSrcweir 
500cdf0e10cSrcweir // class WordArr ---------------------------------------------------------
501cdf0e10cSrcweir 
WordArr(sal_uInt8 nInitSize,sal_uInt8 nGrowSize)502cdf0e10cSrcweir WordArr::WordArr( sal_uInt8 nInitSize, sal_uInt8 nGrowSize ):
503cdf0e10cSrcweir 	nUsed( 0 ),
504cdf0e10cSrcweir 	nGrow( nGrowSize ? nGrowSize : 1 ),
505cdf0e10cSrcweir 	nUnused( nInitSize )
506cdf0e10cSrcweir {
507cdf0e10cSrcweir 	DBG_MEMTEST();
508cdf0e10cSrcweir 	sal_uInt16 nMSCBug = nInitSize;
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 	if ( nInitSize > 0 )
511cdf0e10cSrcweir 		pData = new short[nMSCBug];
512cdf0e10cSrcweir 	else
513cdf0e10cSrcweir 		pData = 0;
514cdf0e10cSrcweir }
515cdf0e10cSrcweir 
516cdf0e10cSrcweir // -----------------------------------------------------------------------
517cdf0e10cSrcweir 
WordArr(const WordArr & rOrig)518cdf0e10cSrcweir WordArr::WordArr( const WordArr& rOrig )
519cdf0e10cSrcweir {
520cdf0e10cSrcweir 	DBG_MEMTEST();
521cdf0e10cSrcweir 	nUsed = rOrig.nUsed;
522cdf0e10cSrcweir 	nGrow = rOrig.nGrow;
523cdf0e10cSrcweir 	nUnused = rOrig.nUnused;
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 	if ( rOrig.pData != 0 )
526cdf0e10cSrcweir 	{
527cdf0e10cSrcweir 		pData = new short[nUsed+nUnused];
528cdf0e10cSrcweir 		memcpy( pData, rOrig.pData, nUsed*sizeof(short) );
529cdf0e10cSrcweir 	}
530cdf0e10cSrcweir 	else
531cdf0e10cSrcweir 		pData = 0;
532cdf0e10cSrcweir }
533cdf0e10cSrcweir 
534cdf0e10cSrcweir // -----------------------------------------------------------------------
535cdf0e10cSrcweir 
~WordArr()536cdf0e10cSrcweir WordArr::~WordArr()
537cdf0e10cSrcweir {
538cdf0e10cSrcweir 	DBG_MEMTEST();
539cdf0e10cSrcweir 	delete [] pData;
540cdf0e10cSrcweir }
541cdf0e10cSrcweir 
542cdf0e10cSrcweir // -----------------------------------------------------------------------
543cdf0e10cSrcweir 
operator =(const WordArr & rOrig)544cdf0e10cSrcweir WordArr& WordArr::operator=( const WordArr& rOrig )
545cdf0e10cSrcweir {
546cdf0e10cSrcweir 	DBG_MEMTEST();
547cdf0e10cSrcweir 
548cdf0e10cSrcweir 	delete [] pData;
549cdf0e10cSrcweir 
550cdf0e10cSrcweir 	nUsed = rOrig.nUsed;
551cdf0e10cSrcweir 	nGrow = rOrig.nGrow;
552cdf0e10cSrcweir 	nUnused = rOrig.nUnused;
553cdf0e10cSrcweir 
554cdf0e10cSrcweir 	if ( rOrig.pData != 0 )
555cdf0e10cSrcweir 	{
556cdf0e10cSrcweir 		pData = new short[nUsed+nUnused];
557cdf0e10cSrcweir 		memcpy( pData, rOrig.pData, nUsed*sizeof(short) );
558cdf0e10cSrcweir 	}
559cdf0e10cSrcweir 	else
560cdf0e10cSrcweir 		pData = 0;
561cdf0e10cSrcweir 	return *this;
562cdf0e10cSrcweir }
563cdf0e10cSrcweir 
564cdf0e10cSrcweir // -----------------------------------------------------------------------
565cdf0e10cSrcweir 
Append(short aElem)566cdf0e10cSrcweir void WordArr::Append( short aElem )
567cdf0e10cSrcweir {
568cdf0e10cSrcweir 	DBG_MEMTEST();
569cdf0e10cSrcweir 	// musz das Array umkopiert werden?
570cdf0e10cSrcweir 	if ( nUnused == 0 )
571cdf0e10cSrcweir 	{
572cdf0e10cSrcweir 		sal_uInt16 nNewSize = (nUsed == 1) ? (nGrow==1 ? 2 : nGrow) : nUsed+nGrow;
573cdf0e10cSrcweir 		short* pNewData = new short[nNewSize];
574cdf0e10cSrcweir 		if ( pData )
575cdf0e10cSrcweir 		{
576cdf0e10cSrcweir 			DBG_ASSERT( nUsed <= nNewSize, " " );
577cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(short)*nUsed );
578cdf0e10cSrcweir 			delete [] pData;
579cdf0e10cSrcweir 		}
580cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
581cdf0e10cSrcweir 		pData = pNewData;
582cdf0e10cSrcweir 	}
583cdf0e10cSrcweir 
584cdf0e10cSrcweir 	// jetzt hinten in den freien Raum schreiben
585cdf0e10cSrcweir 	pData[nUsed] = aElem;
586cdf0e10cSrcweir 	++nUsed;
587cdf0e10cSrcweir 	--nUnused;
588cdf0e10cSrcweir }
589cdf0e10cSrcweir 
590cdf0e10cSrcweir // -----------------------------------------------------------------------
591cdf0e10cSrcweir 
Remove(sal_uInt16 nPos,sal_uInt16 nLen)592cdf0e10cSrcweir sal_uInt16 WordArr::Remove( sal_uInt16 nPos, sal_uInt16 nLen )
593cdf0e10cSrcweir {
594cdf0e10cSrcweir 	DBG_MEMTEST();
595cdf0e10cSrcweir 	// nLen adjustieren, damit nicht ueber das Ende hinaus geloescht wird
596cdf0e10cSrcweir 	nLen = Min( (sal_uInt16)(nUsed-nPos), nLen );
597cdf0e10cSrcweir 
598cdf0e10cSrcweir 	// einfache Aufgaben erfordern einfache Loesungen!
599cdf0e10cSrcweir 	if ( nLen == 0 )
600cdf0e10cSrcweir 		return 0;
601cdf0e10cSrcweir 
602cdf0e10cSrcweir 	// bleibt vielleicht keiner uebrig
603cdf0e10cSrcweir 	if ( (nUsed-nLen) == 0 )
604cdf0e10cSrcweir 	{
605cdf0e10cSrcweir 		delete [] pData;
606cdf0e10cSrcweir 		pData = 0;
607cdf0e10cSrcweir 		nUsed = 0;
608cdf0e10cSrcweir 		nUnused = 0;
609cdf0e10cSrcweir 		return nLen;
610cdf0e10cSrcweir 	}
611cdf0e10cSrcweir 
612cdf0e10cSrcweir 	// feststellen, ob das Array dadurch physikalisch schrumpft...
613cdf0e10cSrcweir 	if ( (nUnused+nLen) >= nGrow )
614cdf0e10cSrcweir 	{
615cdf0e10cSrcweir 		// auf die naechste Grow-Grenze aufgerundet verkleinern
616cdf0e10cSrcweir 		sal_uInt16 nNewUsed = nUsed-nLen;
617cdf0e10cSrcweir 		sal_uInt16 nNewSize = ((nNewUsed+nGrow-1)/nGrow) * nGrow;
618cdf0e10cSrcweir 		DBG_ASSERT( nNewUsed <= nNewSize && nNewUsed+nGrow > nNewSize,
619cdf0e10cSrcweir 					"shrink size computation failed" );
620cdf0e10cSrcweir 		short* pNewData = new short[nNewSize];
621cdf0e10cSrcweir 		if ( nPos > 0 )
622cdf0e10cSrcweir 		{
623cdf0e10cSrcweir 			DBG_ASSERT( nPos <= nNewSize, "" );
624cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(short)*nPos );
625cdf0e10cSrcweir 		}
626cdf0e10cSrcweir 		if ( nNewUsed != nPos )
627cdf0e10cSrcweir 			memmove( pNewData+nPos, pData+nPos+nLen,
628cdf0e10cSrcweir 					 sizeof(short)*(nNewUsed-nPos) );
629cdf0e10cSrcweir 			delete [] pData;
630cdf0e10cSrcweir 		pData = pNewData;
631cdf0e10cSrcweir 		nUsed = nNewUsed;
632cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize - nNewUsed);
633cdf0e10cSrcweir 		return nLen;
634cdf0e10cSrcweir 	}
635cdf0e10cSrcweir 
636cdf0e10cSrcweir 	// in allen anderen Faellen nur zusammenschieben
637cdf0e10cSrcweir 	if ( nUsed-nPos-nLen > 0 )
638cdf0e10cSrcweir 		memmove( pData+nPos, pData+nPos+nLen, (nUsed-nPos-nLen)*sizeof(short) );
639cdf0e10cSrcweir 	nUsed = nUsed - nLen;
640cdf0e10cSrcweir 	nUnused = sal::static_int_cast< sal_uInt8 >(nUnused + nLen);
641cdf0e10cSrcweir 	return nLen;
642cdf0e10cSrcweir }
643cdf0e10cSrcweir 
644cdf0e10cSrcweir // -----------------------------------------------------------------------
645cdf0e10cSrcweir 
Remove(short aElem)646cdf0e10cSrcweir sal_Bool WordArr::Remove( short aElem )
647cdf0e10cSrcweir {
648cdf0e10cSrcweir 	DBG_MEMTEST();
649cdf0e10cSrcweir 	// einfache Aufgaben ...
650cdf0e10cSrcweir 	if ( nUsed == 0 )
651cdf0e10cSrcweir 		return sal_False;
652cdf0e10cSrcweir 
653cdf0e10cSrcweir 	// rueckwaerts, da meist der letzte zuerst wieder entfernt wird
654cdf0e10cSrcweir 	short *pIter = pData + nUsed - 1;
655cdf0e10cSrcweir 	for ( sal_uInt16 n = 0; n < nUsed; ++n, --pIter )
656cdf0e10cSrcweir 		if ( *pIter == aElem )
657cdf0e10cSrcweir 		{
658cdf0e10cSrcweir 			Remove(nUsed-n-1, 1);
659cdf0e10cSrcweir 			return sal_True;
660cdf0e10cSrcweir 		}
661cdf0e10cSrcweir 	return sal_False;
662cdf0e10cSrcweir }
663cdf0e10cSrcweir 
664cdf0e10cSrcweir // -----------------------------------------------------------------------
665cdf0e10cSrcweir 
Contains(const short rItem) const666cdf0e10cSrcweir sal_Bool WordArr::Contains( const short rItem ) const
667cdf0e10cSrcweir {
668cdf0e10cSrcweir 	DBG_MEMTEST();
669cdf0e10cSrcweir 	if ( !nUsed )
670cdf0e10cSrcweir 		return sal_False;
671cdf0e10cSrcweir 
672cdf0e10cSrcweir 	for ( sal_uInt16 n = 0; n < nUsed; ++n )
673cdf0e10cSrcweir 	{
674cdf0e10cSrcweir 		short p = GetObject(n);
675cdf0e10cSrcweir 		if ( p == rItem )
676cdf0e10cSrcweir 			return sal_True;
677cdf0e10cSrcweir 	}
678cdf0e10cSrcweir 
679cdf0e10cSrcweir 	return sal_False;
680cdf0e10cSrcweir }
681cdf0e10cSrcweir 
682cdf0e10cSrcweir // -----------------------------------------------------------------------
683cdf0e10cSrcweir 
Insert(sal_uInt16 nPos,short rElem)684cdf0e10cSrcweir void WordArr::Insert( sal_uInt16 nPos, short rElem )
685cdf0e10cSrcweir {
686cdf0e10cSrcweir 	DBG_MEMTEST();
687cdf0e10cSrcweir 	// musz das Array umkopiert werden?
688cdf0e10cSrcweir 	if ( nUnused == 0 )
689cdf0e10cSrcweir 	{
690cdf0e10cSrcweir 		// auf die naechste Grow-Grenze aufgerundet vergroeszern
691cdf0e10cSrcweir 		sal_uInt16 nNewSize = nUsed+nGrow;
692cdf0e10cSrcweir 		short* pNewData = new short[nNewSize];
693cdf0e10cSrcweir 
694cdf0e10cSrcweir 		if ( pData )
695cdf0e10cSrcweir 		{
696cdf0e10cSrcweir 			DBG_ASSERT( nUsed < nNewSize, "" );
697cdf0e10cSrcweir 			memmove( pNewData, pData, sizeof(short)*nUsed );
698cdf0e10cSrcweir 			delete [] pData;
699cdf0e10cSrcweir 		}
700cdf0e10cSrcweir 		nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
701cdf0e10cSrcweir 		pData = pNewData;
702cdf0e10cSrcweir 	}
703cdf0e10cSrcweir 
704cdf0e10cSrcweir 	// jetzt den hinteren Teil verschieben
705cdf0e10cSrcweir 	if ( nPos < nUsed )
706cdf0e10cSrcweir 		memmove( pData+nPos+1, pData+nPos, (nUsed-nPos)*sizeof(short) );
707cdf0e10cSrcweir 
708cdf0e10cSrcweir 	// jetzt in den freien Raum schreiben
709cdf0e10cSrcweir 	memmove( pData+nPos, &rElem, sizeof(short) );
710cdf0e10cSrcweir 	nUsed += 1;
711cdf0e10cSrcweir 	nUnused -= 1;
712cdf0e10cSrcweir }
713cdf0e10cSrcweir 
714cdf0e10cSrcweir // -----------------------------------------------------------------------
715cdf0e10cSrcweir 
operator [](sal_uInt16 nPos) const716cdf0e10cSrcweir short WordArr::operator[]( sal_uInt16 nPos ) const
717cdf0e10cSrcweir {
718cdf0e10cSrcweir 	DBG_MEMTEST();
719cdf0e10cSrcweir 	DBG_ASSERT( nPos < nUsed, "" );
720cdf0e10cSrcweir 	return *(pData+nPos);
721cdf0e10cSrcweir }
722cdf0e10cSrcweir 
723cdf0e10cSrcweir // -----------------------------------------------------------------------
724cdf0e10cSrcweir 
operator [](sal_uInt16 nPos)725cdf0e10cSrcweir short& WordArr::operator [] (sal_uInt16 nPos)
726cdf0e10cSrcweir {
727cdf0e10cSrcweir 	DBG_MEMTEST();
728cdf0e10cSrcweir 	DBG_ASSERT( nPos < nUsed, "" );
729cdf0e10cSrcweir 	return *(pData+nPos);
730cdf0e10cSrcweir }
731cdf0e10cSrcweir 
732cdf0e10cSrcweir 
733