summaryrefslogtreecommitdiff
path: root/svl/inc/svl/svarray.hxx
blob: 4aebb4a2dfeb7d2f236bf3b9dee337371510bd68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef _SVARRAY_HXX
#define _SVARRAY_HXX

/***********************************************************************
*
*   Hier folgt die Beschreibung fuer die exportierten Makros:
*
*       SV_DECL_PTRARR_SORT(nm, AE, IS, GS)
*       SV_IMPL_PTRARR_SORT( nm,AE )
*           defieniere/implementiere ein Sort-Array mit Pointern, das nach
*           Pointern sortiert ist. Basiert auf einem PTRARR
*
*       SV_DECL_PTRARR_SORT_DEL(nm, AE, IS, GS)
*       SV_IMPL_PTRARR_SORT( nm,AE )
*           defieniere/implementiere ein Sort-Array mit Pointern, das nach
*           Pointern sortiert ist. Basiert auf einem PTRARR_DEL
*
*       SV_DECL_PTRARR_SORT(nm, AE, IS, GS)
*       SV_IMPL_OP_PTRARR_SORT( nm,AE )
*           defieniere/implementiere ein Sort-Array mit Pointern, das nach
*           Objecten sortiert ist. Basiert auf einem PTRARR.
*           Sortierung mit Hilfe der Object-operatoren "<" und "=="
*
*       SV_DECL_PTRARR_SORT_DEL(nm, AE, IS, GS)
*       SV_IMPL_OP_PTRARR_SORT( nm,AE )
*           defieniere/implementiere ein Sort-Array mit Pointern, das nach
*           Objecten sortiert ist. Basiert auf einem PTRARR_DEL.
*           Sortierung mit Hilfe der Object-operatoren "<" und "=="
***********************************************************************/

#include "svl/svldllapi.h"

#include <string.h>     // memmove()
#include <limits.h>     // USHRT_MAX
#include <osl/diagnose.h>
#include <rtl/alloc.h>
#include <tools/solar.h>

class String;

class DummyType;
inline void* operator new( size_t, DummyType* pPtr )
{
    return pPtr;
}
inline void operator delete( void*, DummyType* ) {}

#define SV_DECL_PTRARR_GEN(nm, AE, IS, Base, AERef, VPRef, vis )\
class vis nm: public Base \
{\
public:\
    nm( sal_uInt16 nIni=IS )\
        : Base(nIni) {}\
    void Insert( const nm *pI, sal_uInt16 nP, \
            sal_uInt16 nS = 0, sal_uInt16 nE = USHRT_MAX ) {\
        Base::Insert((const Base*)pI, nP, nS, nE);\
    }\
    void Insert( const AERef aE, sal_uInt16 nP ) {\
        Base::Insert( (const VPRef )aE, nP );\
    }\
    void Insert( const AE *pE, sal_uInt16 nL, sal_uInt16 nP ) {\
        Base::Insert( (const VoidPtr*)pE, nL, nP );\
    }\
    void Replace( const AERef aE, sal_uInt16 nP ) {\
        Base::Replace( (const VPRef)aE, nP );\
    }\
    void Replace( const AE *pE, sal_uInt16 nL, sal_uInt16 nP ) {\
        Base::Replace( (const VoidPtr*)pE, nL, nP );\
    }\
    void Remove( sal_uInt16 nP, sal_uInt16 nL = 1) {\
        Base::Remove(nP,nL);\
    }\
    const AE* GetData() const {\
        return (const AE*)Base::GetData();\
    }\
    AE operator[]( sal_uInt16 nP )const  { \
        return (AE)Base::operator[](nP); }\
    AE GetObject(sal_uInt16 nP) const { \
        return (AE)Base::GetObject(nP); }\
    \
    sal_uInt16 GetPos( const AERef aE ) const { \
        return Base::GetPos((const VPRef)aE);\
    }\
    void DeleteAndDestroy( sal_uInt16 nP, sal_uInt16 nL=1 );\
private:\
    nm( const nm& );\
    nm& operator=( const nm& );\
};

#define SV_DECL_PTRARR_VISIBILITY(nm, AE, IS, vis)\
SV_DECL_PTRARR_GEN(nm, AE, IS, SvPtrarr, AE &, VoidPtr &, vis )

typedef void* VoidPtr;
typedef sal_Bool (*FnForEach_SvPtrarr)( const VoidPtr&, void* );
class SVL_DLLPUBLIC SvPtrarr
{
protected:
    VoidPtr    *pData;
    sal_uInt16 nFree;
    sal_uInt16 nA;

    void _resize(size_t n);

public:
    SvPtrarr( sal_uInt16= 0 );
    ~SvPtrarr() { rtl_freeMemory( pData ); }

    VoidPtr& operator[](sal_uInt16 nP) const { return *(pData+nP); }

    void Insert( const SvPtrarr * pI, sal_uInt16 nP,
                 sal_uInt16 nS = 0, sal_uInt16 nE = USHRT_MAX )
    {
        if( USHRT_MAX == nE )
            nE = pI->nA;
        if( nS < nE )
            Insert( (const VoidPtr*)pI->pData+nS, (sal_uInt16)nE-nS, nP );
    }

    VoidPtr& GetObject(sal_uInt16 nP) const { return (*this)[nP]; }

    void Insert( const VoidPtr& aE, sal_uInt16 nP );
    void Insert( const VoidPtr *pE, sal_uInt16 nL, sal_uInt16 nP );
    void Remove( sal_uInt16 nP, sal_uInt16 nL = 1 );
    void Replace( const VoidPtr& aE, sal_uInt16 nP );
    void Replace( const VoidPtr *pE, sal_uInt16 nL, sal_uInt16 nP );
    sal_uInt16 Count() const { return nA; }
    const VoidPtr* GetData() const { return (const VoidPtr*)pData; }

    void ForEach( FnForEach_SvPtrarr fnForEach, void* pArgs = 0 )
    {
        _ForEach( 0, nA, fnForEach, pArgs );
    }
    void ForEach( sal_uInt16 nS, sal_uInt16 nE,
                    FnForEach_SvPtrarr fnForEach, void* pArgs = 0 )
    {
        _ForEach( nS, nE, fnForEach, pArgs );
    }

    void _ForEach( sal_uInt16 nStt, sal_uInt16 nE,
            FnForEach_SvPtrarr fnCall, void* pArgs = 0 );

    sal_uInt16 GetPos( const VoidPtr & aE ) const;
};

// SORTARR - Begin

#define _SORT_CLASS_DEF(nm, AE, IS, vis)\
class vis nm : private nm##_SAR \
{\
public:\
    nm(sal_uInt16 nSize = IS)\
        : nm##_SAR(nSize) {}\
    void Insert( const nm *pI, sal_uInt16 nS=0, sal_uInt16 nE=USHRT_MAX );\
    sal_Bool Insert( const AE& aE );\
    sal_Bool Insert( const AE& aE, sal_uInt16& rP );\
    void Insert( const AE *pE, sal_uInt16 nL );\
    void Remove( sal_uInt16 nP, sal_uInt16 nL = 1 );\
    void Remove( const AE& aE, sal_uInt16 nL = 1 );\
    sal_uInt16 Count() const  {   return nm##_SAR::Count(); }\
    const AE* GetData() const { return (const AE*)pData; }\
\
/* Das Ende stehe im DECL-Makro !!! */

#define _SV_SEEK_PTR(nm,AE)\
sal_Bool nm::Seek_Entry( const AE aE, sal_uInt16* pP ) const\
{\
    register sal_uInt16 nO  = nm##_SAR::Count(),\
            nM, \
            nU = 0;\
    if( nO > 0 )\
    {\
        nO--;\
        register long rCmp = (long)aE;\
        while( nU <= nO )\
        {\
            nM = nU + ( nO - nU ) / 2;\
            if( (long)*(pData + nM) == rCmp )\
            {\
                if( pP ) *pP = nM;\
                return sal_True;\
            }\
            else if( (long)*(pData+ nM) < (long)aE  )\
                nU = nM + 1;\
            else if( nM == 0 )\
            {\
                if( pP ) *pP = nU;\
                return sal_False;\
            }\
            else\
                nO = nM - 1;\
        }\
    }\
    if( pP ) *pP = nU;\
    return sal_False;\
}

#define _SV_SEEK_PTR_TO_OBJECT( nm,AE )\
sal_Bool nm::Seek_Entry( const AE aE, sal_uInt16* pP ) const\
{\
    register sal_uInt16 nO  = nm##_SAR::Count(),\
            nM, \
            nU = 0;\
    if( nO > 0 )\
    {\
        nO--;\
        while( nU <= nO )\
        {\
            nM = nU + ( nO - nU ) / 2;\
            if( *(*((AE*)pData + nM)) == *(aE) )\
            {\
                if( pP ) *pP = nM;\
                return sal_True;\
            }\
            else if( *(*((AE*)pData + nM)) < *(aE) )\
                nU = nM + 1;\
            else if( nM == 0 )\
            {\
                if( pP ) *pP = nU;\
                return sal_False;\
            }\
            else\
                nO = nM - 1;\
        }\
    }\
    if( pP ) *pP = nU;\
    return sal_False;\
}

#define _SV_IMPL_SORTAR_ALG(nm, AE)\
void nm::Insert( const nm * pI, sal_uInt16 nS, sal_uInt16 nE )\
{\
    if( USHRT_MAX == nE )\
        nE = pI->Count();\
    sal_uInt16 nP;\
    const AE * pIArr = pI->GetData();\
    for( ; nS < nE; ++nS )\
    {\
        if( ! Seek_Entry( *(pIArr+nS), &nP) )\
                nm##_SAR::Insert( *(pIArr+nS), nP );\
        if( ++nP >= Count() )\
        {\
            nm##_SAR::Insert( pI, nP, nS+1, nE );\
            nS = nE;\
        }\
    }\
}\
\
sal_Bool nm::Insert( const AE & aE )\
{\
    sal_uInt16 nP;\
    sal_Bool bExist;\
    bExist = Seek_Entry( aE, &nP );\
    if( ! bExist )\
        nm##_SAR::Insert( aE, nP );\
    return !bExist;\
}\
sal_Bool nm::Insert( const AE & aE, sal_uInt16& rP )\
{\
    sal_Bool bExist;\
    bExist = Seek_Entry( aE, &rP );\
    if( ! bExist )\
        nm##_SAR::Insert( aE, rP );\
    return !bExist;\
}\
void nm::Insert( const AE* pE, sal_uInt16 nL)\
{\
    sal_uInt16 nP;\
    for( sal_uInt16 n = 0; n < nL; ++n )\
        if( ! Seek_Entry( *(pE+n), &nP ))\
            nm##_SAR::Insert( *(pE+n), nP );\
}\
void nm::Remove( sal_uInt16 nP, sal_uInt16 nL )\
{\
    if( nL )\
        nm##_SAR::Remove( nP, nL);\
}\
\
void nm::Remove( const AE &aE, sal_uInt16 nL )\
{\
    sal_uInt16 nP;\
    if( nL && Seek_Entry( aE, &nP ) )   \
        nm##_SAR::Remove( nP, nL);\
}\

#define _SORTARR_BLC_CASTS(nm, AE )\
    sal_uInt16 GetPos( const AE& aE ) const { \
        return SvPtrarr::GetPos((const VoidPtr&)aE);\
    }

#define _SV_DECL_PTRARR_SORT_ALG(nm, AE, IS, vis)\
SV_DECL_PTRARR_VISIBILITY(nm##_SAR, AE, IS, vis)\
_SORT_CLASS_DEF(nm, AE, IS, vis)\
    AE operator[](sal_uInt16 nP) const {\
        return nm##_SAR::operator[]( nP );\
    }\
    AE GetObject(sal_uInt16 nP) const {\
        return nm##_SAR::GetObject( nP );\
    }\
    sal_Bool Seek_Entry( const AE aE, sal_uInt16* pP = 0 ) const;\
    void DeleteAndDestroy( sal_uInt16 nP, sal_uInt16 nL=1 ); \
    _SORTARR_BLC_CASTS(nm, AE )\
\
/* Das Ende stehe im DECL-Makro !!! */

#define _SV_DECL_PTRARR_SORT(nm, AE, IS, vis)\
_SV_DECL_PTRARR_SORT_ALG(nm, AE, IS, vis)\
private:\
    nm( const nm& );\
    nm& operator=( const nm& );\
};

#define SV_DECL_PTRARR_SORT(nm, AE, IS)\
_SV_DECL_PTRARR_SORT(nm, AE, IS,)

#define _SV_DECL_PTRARR_SORT_DEL(nm, AE, IS, vis)\
_SV_DECL_PTRARR_SORT_ALG(nm, AE, IS, vis)\
    ~nm() { DeleteAndDestroy( 0, Count() ); }\
private:\
    nm( const nm& );\
    nm& operator=( const nm& );\
};

#define SV_DECL_PTRARR_SORT_DEL(nm, AE, IS)\
_SV_DECL_PTRARR_SORT_DEL(nm, AE, IS,)

#define SV_IMPL_PTRARR_SORT( nm,AE )\
_SV_IMPL_SORTAR_ALG( nm,AE )\
    void nm::DeleteAndDestroy( sal_uInt16 nP, sal_uInt16 nL ) { \
        if( nL ) {\
            OSL_ENSURE( nP < nA && nP + nL <= nA, "ERR_VAR_DEL" );\
            for( sal_uInt16 n=nP; n < nP + nL; n++ ) \
                delete *((AE*)pData+n); \
            SvPtrarr::Remove( nP, nL ); \
        } \
    } \
_SV_SEEK_PTR( nm, AE )

#define SV_IMPL_OP_PTRARR_SORT( nm,AE )\
_SV_IMPL_SORTAR_ALG( nm,AE )\
    void nm::DeleteAndDestroy( sal_uInt16 nP, sal_uInt16 nL ) { \
        if( nL ) {\
            OSL_ENSURE( nP < nA && nP + nL <= nA, "ERR_VAR_DEL" );\
            for( sal_uInt16 n=nP; n < nP + nL; n++ ) \
                delete *((AE*)pData+n); \
            SvPtrarr::Remove( nP, nL ); \
        } \
    } \
_SV_SEEK_PTR_TO_OBJECT( nm,AE )

#if defined(ICC) || defined(GCC) || (defined(WNT) && _MSC_VER >= 1400)
#define C40_INSERT( c, p, n ) Insert( (c const *&) p, n )
#define C40_GETPOS( c, r) GetPos( (c const *&) r )
#else
#define C40_INSERT( c, p, n ) Insert( p, n )
#define C40_GETPOS( c, r) GetPos( r )
#endif

#endif  //_SVARRAY_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */