summaryrefslogtreecommitdiff
path: root/sc/source/core/data/sortparam.cxx
blob: cd98dd07a9827113881c8a97d0c40085b844a34f (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
/* -*- 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 <sortparam.hxx>
#include <global.hxx>
#include <address.hxx>
#include <queryparam.hxx>
#include <subtotalparam.hxx>

#include <osl/diagnose.h>

#include <algorithm>

ScSortParam::ScSortParam()
{
    Clear();
}

ScSortParam::ScSortParam( const ScSortParam& r ) :
        nCol1(r.nCol1),nRow1(r.nRow1),nCol2(r.nCol2),nRow2(r.nRow2),nUserIndex(r.nUserIndex),
        bHasHeader(r.bHasHeader),bByRow(r.bByRow),bCaseSens(r.bCaseSens),
        bNaturalSort(r.bNaturalSort),bIncludeComments(r.bIncludeComments),
        bIncludeGraphicObjects(r.bIncludeGraphicObjects),bUserDef(r.bUserDef),
        bIncludePattern(r.bIncludePattern),bInplace(r.bInplace),
        nDestTab(r.nDestTab),nDestCol(r.nDestCol),nDestRow(r.nDestRow),
        maKeyState( r.maKeyState ),
        aCollatorLocale( r.aCollatorLocale ), aCollatorAlgorithm( r.aCollatorAlgorithm ),
        nCompatHeader( r.nCompatHeader )
{
}

ScSortParam::~ScSortParam() {}

void ScSortParam::Clear()
{
    ScSortKeyState aKeyState;

    nCol1=nCol2=nDestCol = 0;
    nRow1=nRow2=nDestRow = 0;
    nCompatHeader = 2;
    nDestTab = 0;
    nUserIndex = 0;
    bHasHeader=bCaseSens=bUserDef=bNaturalSort = false;
    bIncludeComments = false;
    bIncludeGraphicObjects = true;
    bByRow=bIncludePattern=bInplace = true;
    aCollatorLocale = css::lang::Locale();
    aCollatorAlgorithm.clear();

    aKeyState.bDoSort = false;
    aKeyState.nField = 0;
    aKeyState.bAscending = true;

    // Initialize to default size
    maKeyState.assign( DEFSORT, aKeyState );
}

ScSortParam& ScSortParam::operator=( const ScSortParam& r )
{
    nCol1           = r.nCol1;
    nRow1           = r.nRow1;
    nCol2           = r.nCol2;
    nRow2           = r.nRow2;
    nUserIndex      = r.nUserIndex;
    bHasHeader      = r.bHasHeader;
    bByRow          = r.bByRow;
    bCaseSens       = r.bCaseSens;
    bNaturalSort    = r.bNaturalSort;
    bIncludeComments= r.bIncludeComments;
    bIncludeGraphicObjects = r.bIncludeGraphicObjects;
    bUserDef        = r.bUserDef;
    bIncludePattern = r.bIncludePattern;
    bInplace        = r.bInplace;
    nDestTab        = r.nDestTab;
    nDestCol        = r.nDestCol;
    nDestRow        = r.nDestRow;
    maKeyState      = r.maKeyState;
    aCollatorLocale         = r.aCollatorLocale;
    aCollatorAlgorithm      = r.aCollatorAlgorithm;
    nCompatHeader   = r.nCompatHeader;

    return *this;
}

bool ScSortParam::operator==( const ScSortParam& rOther ) const
{
    bool bEqual = false;
    // Number of Sorts the same?
    sal_uInt16 nLast      = 0;
    sal_uInt16 nOtherLast = 0;
    sal_uInt16 nSortSize = GetSortKeyCount();

    if ( !maKeyState.empty() )
    {
        while ( maKeyState[nLast++].bDoSort && nLast < nSortSize ) ;
        nLast--;
    }

    if ( !rOther.maKeyState.empty() )
    {
        while ( rOther.maKeyState[nOtherLast++].bDoSort && nOtherLast < nSortSize ) ;
        nOtherLast--;
    }

    if (   (nLast           == nOtherLast)
        && (nCol1           == rOther.nCol1)
        && (nRow1           == rOther.nRow1)
        && (nCol2           == rOther.nCol2)
        && (nRow2           == rOther.nRow2)
        && (bHasHeader      == rOther.bHasHeader)
        && (bByRow          == rOther.bByRow)
        && (bCaseSens       == rOther.bCaseSens)
        && (bNaturalSort    == rOther.bNaturalSort)
        && (bIncludeComments== rOther.bIncludeComments)
        && (bIncludeGraphicObjects == rOther.bIncludeGraphicObjects)
        && (bUserDef        == rOther.bUserDef)
        && (nUserIndex      == rOther.nUserIndex)
        && (bIncludePattern == rOther.bIncludePattern)
        && (bInplace        == rOther.bInplace)
        && (nDestTab        == rOther.nDestTab)
        && (nDestCol        == rOther.nDestCol)
        && (nDestRow        == rOther.nDestRow)
        && (aCollatorLocale.Language    == rOther.aCollatorLocale.Language)
        && (aCollatorLocale.Country     == rOther.aCollatorLocale.Country)
        && (aCollatorLocale.Variant     == rOther.aCollatorLocale.Variant)
        && (aCollatorAlgorithm          == rOther.aCollatorAlgorithm)
        && ( !maKeyState.empty() || !rOther.maKeyState.empty() )
        )
    {
        bEqual = true;
        for ( sal_uInt16 i=0; i<=nLast && bEqual; i++ )
            bEqual = ( maKeyState[i].nField == rOther.maKeyState[i].nField ) &&
                ( maKeyState[i].bAscending  == rOther.maKeyState[i].bAscending );
    }
    if ( maKeyState.empty() && rOther.maKeyState.empty() )
        bEqual = true;

    return bEqual;
}

ScSortParam::ScSortParam( const ScSubTotalParam& rSub, const ScSortParam& rOld ) :
        nCol1(rSub.nCol1),nRow1(rSub.nRow1),nCol2(rSub.nCol2),nRow2(rSub.nRow2),nUserIndex(rSub.nUserIndex),
        bHasHeader(true),bByRow(true),bCaseSens(rSub.bCaseSens),bNaturalSort(rOld.bNaturalSort),
        bIncludeComments(rOld.bIncludeComments),bIncludeGraphicObjects(rOld.bIncludeGraphicObjects),
        bUserDef(rSub.bUserDef),bIncludePattern(rSub.bIncludePattern),
        bInplace(true),
        nDestTab(0),nDestCol(0),nDestRow(0),
        aCollatorLocale( rOld.aCollatorLocale ), aCollatorAlgorithm( rOld.aCollatorAlgorithm ),
        nCompatHeader( rOld.nCompatHeader )
{
    sal_uInt16 i;

    //  first the groups from the partial results
    if (rSub.bDoSort)
        for (i=0; i<MAXSUBTOTAL; i++)
            if (rSub.bGroupActive[i])
            {
                ScSortKeyState key;
                key.bDoSort = true;
                key.nField = rSub.nField[i];
                key.bAscending = rSub.bAscending;
                maKeyState.push_back(key);
            }

    //  then the old settings
    for (i=0; i < rOld.GetSortKeyCount(); i++)
        if (rOld.maKeyState[i].bDoSort)
        {
            SCCOLROW nThisField = rOld.maKeyState[i].nField;
            bool bDouble = false;
            for (sal_uInt16 j = 0; j < GetSortKeyCount(); j++)
                if ( maKeyState[j].nField == nThisField )
                    bDouble = true;
            if (!bDouble)               // do not enter a field twice
            {
                ScSortKeyState key;
                key.bDoSort = true;
                key.nField = nThisField;
                key.bAscending = rOld.maKeyState[i].bAscending;
                maKeyState.push_back(key);
            }
        }
}

ScSortParam::ScSortParam( const ScQueryParam& rParam, SCCOL nCol ) :
        nCol1(nCol),nRow1(rParam.nRow1),nCol2(nCol),nRow2(rParam.nRow2),nUserIndex(0),
        bHasHeader(rParam.bHasHeader),bByRow(true),bCaseSens(rParam.bCaseSens),
        bNaturalSort(false),bIncludeComments(false),bIncludeGraphicObjects(true),
//TODO: what about Locale and Algorithm?
        bUserDef(false),bIncludePattern(false),
        bInplace(true),
        nDestTab(0),nDestCol(0),nDestRow(0), nCompatHeader(2)
{
    ScSortKeyState aKeyState;
    aKeyState.bDoSort = true;
    aKeyState.nField = nCol;
    aKeyState.bAscending = true;

    maKeyState.push_back( aKeyState );

    // Set the rest
    aKeyState.bDoSort = false;
    aKeyState.nField = 0;

    for (sal_uInt16 i=1; i<GetSortKeyCount(); i++)
        maKeyState.push_back( aKeyState );
}

void ScSortParam::MoveToDest()
{
    if (!bInplace)
    {
        SCCOL nDifX = nDestCol - nCol1;
        SCROW nDifY = nDestRow - nRow1;

        nCol1 = sal::static_int_cast<SCCOL>( nCol1 + nDifX );
        nRow1 = sal::static_int_cast<SCROW>( nRow1 + nDifY );
        nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nDifX );
        nRow2 = sal::static_int_cast<SCROW>( nRow2 + nDifY );
        for (sal_uInt16 i=0; i<GetSortKeyCount(); i++)
            if (bByRow)
                maKeyState[i].nField += nDifX;
            else
                maKeyState[i].nField += nDifY;

        bInplace = true;
    }
    else
    {
        OSL_FAIL("MoveToDest, bInplace == TRUE");
    }
}

namespace sc {

namespace {

struct ReorderIndex
{
    struct LessByPos2
    {
        bool operator() ( const ReorderIndex& r1, const ReorderIndex& r2 ) const
        {
            return r1.mnPos2 < r2.mnPos2;
        }
    };

    SCCOLROW mnPos1;
    SCCOLROW mnPos2;

    ReorderIndex( SCCOLROW nPos1, SCCOLROW nPos2 ) : mnPos1(nPos1), mnPos2(nPos2) {}
};

}

void ReorderParam::reverse()
{
    SCCOLROW nStart;
    if (mbByRow)
        nStart = maSortRange.aStart.Row();
    else
        nStart = maSortRange.aStart.Col();

    size_t n = maOrderIndices.size();
    std::vector<ReorderIndex> aBucket;
    aBucket.reserve(n);
    for (size_t i = 0; i < n; ++i)
    {
        SCCOLROW nPos1 = i + nStart;
        SCCOLROW nPos2 = maOrderIndices[i];
        aBucket.emplace_back(nPos1, nPos2);
    }

    std::sort(aBucket.begin(), aBucket.end(), ReorderIndex::LessByPos2());
    std::vector<SCCOLROW> aNew;
    aNew.reserve(n);
    for (size_t i = 0; i < n; ++i)
        aNew.push_back(aBucket[i].mnPos1);

    maOrderIndices.swap(aNew);
}

}

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