summaryrefslogtreecommitdiff
path: root/svl/source/memtools/svarray.cxx
blob: 984a6ef03e6d8c453b15df698f106f2df6f48365 (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
/* -*- 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 .
 */

#include <svl/svarray.hxx>

SvPtrarr::SvPtrarr( sal_uInt16 nInit )
    : pData (0),
      nFree (nInit),
      nA    (0)
{
    if( nInit )
    {
        pData = (VoidPtr*)(rtl_allocateMemory(sizeof(VoidPtr) * nInit));
        OSL_ENSURE( pData, "CTOR, allocate");
    }
}

void SvPtrarr::_resize (size_t n)
{
    sal_uInt16 nL = ((n < USHRT_MAX) ? sal_uInt16(n) : USHRT_MAX);
    VoidPtr* pE = (VoidPtr*)(rtl_reallocateMemory (pData, sizeof(VoidPtr) * nL));
    if ((pE != 0) || (nL == 0))
    {
        pData = pE;
        nFree = nL - nA;
    }
}

void SvPtrarr::Insert( const VoidPtr& aE, sal_uInt16 nP )
{
    OSL_ENSURE(nP <= nA && nA < USHRT_MAX, "Ins 1");
    if (nFree < 1)
        _resize (nA + ((nA > 1) ? nA : 1));
    if( pData && nP < nA )
        memmove( pData+nP+1, pData+nP, (nA-nP) * sizeof( VoidPtr ));
    *(pData+nP) = (VoidPtr&)aE;
    ++nA; --nFree;
}

void SvPtrarr::Insert( const VoidPtr* pE, sal_uInt16 nL, sal_uInt16 nP )
{
    OSL_ENSURE(nP<=nA && ((long)nA+nL)<USHRT_MAX,"Ins n");
    if (nFree < nL)
        _resize (nA + ((nA > nL) ? nA : nL));
    if( pData && nP < nA )
        memmove( pData+nP+nL, pData+nP, (nA-nP) * sizeof( VoidPtr ));
    if( pE )
        memcpy( pData+nP, pE, nL * sizeof( VoidPtr ));
    nA = nA + nL; nFree = nFree - nL;
}

void SvPtrarr::Replace( const VoidPtr& aE, sal_uInt16 nP )
{
    if( nP < nA )
        *(pData+nP) = (VoidPtr&)aE;
}

void SvPtrarr::Replace( const VoidPtr *pE, sal_uInt16 nL, sal_uInt16 nP )
{
    if( pE && nP < nA )
    {
        if( nP + nL < nA )
            memcpy( pData + nP, pE, nL * sizeof( VoidPtr ));
        else if( nP + nL < nA + nFree )\
        {
            memcpy( pData + nP, pE, nL * sizeof( VoidPtr ));
            nP = nP + (nL - nA);
            nFree = nP;
        }
        else
        {
            sal_uInt16 nTmpLen = nA + nFree - nP;
            memcpy( pData + nP, pE, nTmpLen * sizeof( VoidPtr ));
            nA = nA + nFree;
            nFree = 0;
            Insert( pE + nTmpLen, nL - nTmpLen, nA );
        }
    }
}

void SvPtrarr::Remove( sal_uInt16 nP, sal_uInt16 nL )
{
    if( !nL )
        return;
    OSL_ENSURE( nP < nA && nP + nL <= nA,"Del");
    if( pData && nP+1 < nA )
        memmove( pData+nP, pData+nP+nL, (nA-nP-nL) * sizeof( VoidPtr ));
    nA = nA - nL; nFree = nFree + nL;
    if (nFree > nA)
        _resize (nA);
}

void SvPtrarr::_ForEach( sal_uInt16 nStt, sal_uInt16 nE,
            FnForEach_SvPtrarr fnCall, void* pArgs )
{
    if( nStt >= nE || nE > nA )
        return;
    for( ; nStt < nE && (*fnCall)( *(const VoidPtr*)(pData+nStt), pArgs ); nStt++)
        ;
}

sal_uInt16 SvPtrarr::GetPos( const VoidPtr& aElement ) const
{
    sal_uInt16 n;
    for( n=0; n < nA && *(GetData()+n) != aElement; ) n++;
    return ( n >= nA ? USHRT_MAX : n );
}

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