Update ooo320-m1
[ooovba.git] / svtools / source / memtools / svarray.cxx
blobb786a186ac96b0071d1f0a61b9886837b185988b
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: svarray.cxx,v $
10 * $Revision: 1.8 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #define _SVARRAY_CXX
36 #define _SVSTDARR_BOOLS
37 #define _SVSTDARR_BYTES
38 #define _SVSTDARR_ULONGS
39 #define _SVSTDARR_ULONGSSORT
40 #define _SVSTDARR_USHORTS
41 #define _SVSTDARR_LONGS
42 #define _SVSTDARR_LONGSSORT
43 #define _SVSTDARR_SHORTS
44 #define _SVSTDARR_STRINGS
45 #define _SVSTDARR_STRINGSDTOR
46 #define _SVSTDARR_STRINGSSORT
47 #define _SVSTDARR_STRINGSSORTDTOR
48 #define _SVSTDARR_STRINGSISORT
49 #define _SVSTDARR_STRINGSISORTDTOR
50 #define _SVSTDARR_USHORTSSORT
52 #define _SVSTDARR_BYTESTRINGS
53 #define _SVSTDARR_BYTESTRINGSDTOR
54 #define _SVSTDARR_BYTESTRINGSSORT
55 #define _SVSTDARR_BYTESTRINGSSORTDTOR
56 #define _SVSTDARR_BYTESTRINGSISORT
57 #define _SVSTDARR_BYTESTRINGSISORTDTOR
59 #define _SVSTDARR_XUB_STRLEN
60 #define _SVSTDARR_XUB_STRLENSORT
62 #include <svtools/svstdarr.hxx>
63 #include <tools/string.hxx>
64 #include <tools/debug.hxx>
66 SV_IMPL_VARARR(SvPtrarr,VoidPtr)
67 SV_IMPL_VARARR_PLAIN(SvPtrarrPlain,VoidPtr)
69 USHORT SvPtrarr::GetPos( const VoidPtr& aElement ) const
70 { USHORT n;
71 for( n=0; n < nA && *(GetData()+n) != aElement; ) n++;
72 return ( n >= nA ? USHRT_MAX : n );
75 USHORT SvPtrarrPlain::GetPos( const VoidPtr aElement ) const
76 { USHORT n;
77 for( n=0; n < nA && *(GetData()+n) != aElement; ) n++;
78 return ( n >= nA ? USHRT_MAX : n );
82 SV_IMPL_VARARR( SvBools, BOOL )
83 SV_IMPL_VARARR( SvBytes, BYTE )
84 SV_IMPL_VARARR( SvULongs, ULONG )
85 SV_IMPL_VARARR( SvUShorts, USHORT )
86 SV_IMPL_VARARR( SvLongs, long)
87 SV_IMPL_VARARR( SvShorts, short )
89 SV_IMPL_VARARR_SORT( SvULongsSort, ULONG )
90 SV_IMPL_VARARR_SORT( SvLongsSort, long )
91 SV_IMPL_VARARR_SORT( SvXub_StrLensSort, xub_StrLen )
93 SV_IMPL_VARARR( SvXub_StrLens, xub_StrLen )
95 SV_IMPL_PTRARR( SvStrings, StringPtr )
96 SV_IMPL_PTRARR( SvStringsDtor, StringPtr )
97 SV_IMPL_OP_PTRARR_SORT( SvStringsSort, StringPtr )
98 SV_IMPL_OP_PTRARR_SORT( SvStringsSortDtor, StringPtr )
100 SV_IMPL_PTRARR( SvByteStrings, ByteStringPtr )
101 SV_IMPL_PTRARR( SvByteStringsDtor, ByteStringPtr )
102 SV_IMPL_OP_PTRARR_SORT( SvByteStringsSort, ByteStringPtr )
103 SV_IMPL_OP_PTRARR_SORT( SvByteStringsSortDtor, ByteStringPtr )
107 // ---------------- strings -------------------------------------
109 // Array mit anderer Seek-Methode!
110 _SV_IMPL_SORTAR_ALG( SvStringsISort, StringPtr )
111 void SvStringsISort::DeleteAndDestroy( USHORT nP, USHORT nL )
113 if( nL )
115 DBG_ASSERT( nP < nA && nP + nL <= nA, "ERR_VAR_DEL" );
116 for( USHORT n=nP; n < nP + nL; n++ )
117 delete *((StringPtr*)pData+n);
118 SvPtrarr::Remove( nP, nL );
121 BOOL SvStringsISort::Seek_Entry( const StringPtr aE, USHORT* pP ) const
123 register USHORT nO = SvStringsISort_SAR::Count(),
125 nU = 0;
126 if( nO > 0 )
128 nO--;
129 while( nU <= nO )
131 nM = nU + ( nO - nU ) / 2;
132 StringCompare eCmp = (*((StringPtr*)pData + nM))->
133 CompareIgnoreCaseToAscii( *(aE) );
134 if( COMPARE_EQUAL == eCmp )
136 if( pP ) *pP = nM;
137 return TRUE;
139 else if( COMPARE_LESS == eCmp )
140 nU = nM + 1;
141 else if( nM == 0 )
143 if( pP ) *pP = nU;
144 return FALSE;
146 else
147 nO = nM - 1;
150 if( pP ) *pP = nU;
151 return FALSE;
154 // ---------------- strings -------------------------------------
156 // Array mit anderer Seek-Methode!
157 _SV_IMPL_SORTAR_ALG( SvStringsISortDtor, StringPtr )
158 void SvStringsISortDtor::DeleteAndDestroy( USHORT nP, USHORT nL )
160 if( nL )
162 DBG_ASSERT( nP < nA && nP + nL <= nA, "ERR_VAR_DEL" );
163 for( USHORT n=nP; n < nP + nL; n++ )
164 delete *((StringPtr*)pData+n);
165 SvPtrarr::Remove( nP, nL );
168 BOOL SvStringsISortDtor::Seek_Entry( const StringPtr aE, USHORT* pP ) const
170 register USHORT nO = SvStringsISortDtor_SAR::Count(),
172 nU = 0;
173 if( nO > 0 )
175 nO--;
176 while( nU <= nO )
178 nM = nU + ( nO - nU ) / 2;
179 StringCompare eCmp = (*((StringPtr*)pData + nM))->
180 CompareIgnoreCaseToAscii( *(aE) );
181 if( COMPARE_EQUAL == eCmp )
183 if( pP ) *pP = nM;
184 return TRUE;
186 else if( COMPARE_LESS == eCmp )
187 nU = nM + 1;
188 else if( nM == 0 )
190 if( pP ) *pP = nU;
191 return FALSE;
193 else
194 nO = nM - 1;
197 if( pP ) *pP = nU;
198 return FALSE;
201 // ---------------- Ushorts -------------------------------------
203 /* SortArray fuer UShorts */
204 BOOL SvUShortsSort::Seek_Entry( const USHORT aE, USHORT* pP ) const
206 register USHORT nO = SvUShorts::Count(),
208 nU = 0;
209 if( nO > 0 )
211 nO--;
212 while( nU <= nO )
214 nM = nU + ( nO - nU ) / 2;
215 if( *(pData + nM) == aE )
217 if( pP ) *pP = nM;
218 return TRUE;
220 else if( *(pData + nM) < aE )
221 nU = nM + 1;
222 else if( nM == 0 )
224 if( pP ) *pP = nU;
225 return FALSE;
227 else
228 nO = nM - 1;
231 if( pP ) *pP = nU;
232 return FALSE;
235 void SvUShortsSort::Insert( const SvUShortsSort * pI, USHORT nS, USHORT nE )
237 if( USHRT_MAX == nE )
238 nE = pI->Count();
239 USHORT nP;
240 const USHORT * pIArr = pI->GetData();
241 for( ; nS < nE; ++nS )
243 if( ! Seek_Entry( *(pIArr+nS), &nP) )
244 SvUShorts::Insert( *(pIArr+nS), nP );
245 if( ++nP >= Count() )
247 SvUShorts::Insert( pI, nP, nS+1, nE );
248 nS = nE;
253 BOOL SvUShortsSort::Insert( const USHORT aE )
255 USHORT nP;
256 BOOL bExist = Seek_Entry( aE, &nP );
257 if( !bExist )
258 SvUShorts::Insert( aE, nP );
259 return !bExist;
262 BOOL SvUShortsSort::Insert( const USHORT aE, USHORT& rP )
264 BOOL bExist = Seek_Entry( aE, &rP );
265 if( !bExist )
266 SvUShorts::Insert( aE, rP );
267 return !bExist;
270 void SvUShortsSort::Insert( const USHORT* pE, USHORT nL)
272 USHORT nP;
273 for( USHORT n = 0; n < nL; ++n )
274 if( ! Seek_Entry( *(pE+n), &nP ))
275 SvUShorts::Insert( *(pE+n), nP );
278 // remove ab Pos
279 void SvUShortsSort::RemoveAt( const USHORT nP, USHORT nL )
281 if( nL )
282 SvUShorts::Remove( nP, nL);
285 // remove ab dem Eintrag
286 void SvUShortsSort::Remove( const USHORT aE, USHORT nL )
288 USHORT nP;
289 if( nL && Seek_Entry( aE, &nP ) )
290 SvUShorts::Remove( nP, nL);
293 // ---------------- bytestrings -------------------------------------
295 // Array mit anderer Seek-Methode!
296 _SV_IMPL_SORTAR_ALG( SvByteStringsISort, ByteStringPtr )
297 void SvByteStringsISort::DeleteAndDestroy( USHORT nP, USHORT nL )
299 if( nL )
301 DBG_ASSERT( nP < nA && nP + nL <= nA, "ERR_VAR_DEL" );
302 for( USHORT n=nP; n < nP + nL; n++ )
303 delete *((ByteStringPtr*)pData+n);
304 SvPtrarr::Remove( nP, nL );
307 BOOL SvByteStringsISort::Seek_Entry( const ByteStringPtr aE, USHORT* pP ) const
309 register USHORT nO = SvByteStringsISort_SAR::Count(),
311 nU = 0;
312 if( nO > 0 )
314 nO--;
315 while( nU <= nO )
317 nM = nU + ( nO - nU ) / 2;
318 StringCompare eCmp = (*((ByteStringPtr*)pData + nM))->
319 CompareIgnoreCaseToAscii( *(aE) );
320 if( COMPARE_EQUAL == eCmp )
322 if( pP ) *pP = nM;
323 return TRUE;
325 else if( COMPARE_LESS == eCmp )
326 nU = nM + 1;
327 else if( nM == 0 )
329 if( pP ) *pP = nU;
330 return FALSE;
332 else
333 nO = nM - 1;
336 if( pP ) *pP = nU;
337 return FALSE;
341 // Array mit anderer Seek-Methode!
342 _SV_IMPL_SORTAR_ALG( SvByteStringsISortDtor, ByteStringPtr )
343 void SvByteStringsISortDtor::DeleteAndDestroy( USHORT nP, USHORT nL )
345 if( nL )
347 DBG_ASSERT( nP < nA && nP + nL <= nA, "ERR_VAR_DEL" );
348 for( USHORT n=nP; n < nP + nL; n++ )
349 delete *((ByteStringPtr*)pData+n);
350 SvPtrarr::Remove( nP, nL );
353 BOOL SvByteStringsISortDtor::Seek_Entry( const ByteStringPtr aE, USHORT* pP ) const
355 register USHORT nO = SvByteStringsISortDtor_SAR::Count(),
357 nU = 0;
358 if( nO > 0 )
360 nO--;
361 while( nU <= nO )
363 nM = nU + ( nO - nU ) / 2;
364 StringCompare eCmp = (*((ByteStringPtr*)pData + nM))->
365 CompareIgnoreCaseToAscii( *(aE) );
366 if( COMPARE_EQUAL == eCmp )
368 if( pP ) *pP = nM;
369 return TRUE;
371 else if( COMPARE_LESS == eCmp )
372 nU = nM + 1;
373 else if( nM == 0 )
375 if( pP ) *pP = nU;
376 return FALSE;
378 else
379 nO = nM - 1;
382 if( pP ) *pP = nU;
383 return FALSE;