LibreOffice Module sc (master) 1
sortparam.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sortparam.hxx>
21#include <global.hxx>
22#include <address.hxx>
23#include <queryparam.hxx>
24#include <subtotalparam.hxx>
25
26#include <osl/diagnose.h>
27
28#include <algorithm>
29
31{
32 Clear();
33}
34
36 nCol1(r.nCol1),nRow1(r.nRow1),nCol2(r.nCol2),nRow2(r.nRow2),
37 aDataAreaExtras(r.aDataAreaExtras),
38 nUserIndex(r.nUserIndex),
39 bHasHeader(r.bHasHeader),bByRow(r.bByRow),bCaseSens(r.bCaseSens),
40 bNaturalSort(r.bNaturalSort),
41 bUserDef(r.bUserDef),
42 bInplace(r.bInplace),
43 nDestTab(r.nDestTab),nDestCol(r.nDestCol),nDestRow(r.nDestRow),
44 maKeyState( r.maKeyState ),
45 aCollatorLocale( r.aCollatorLocale ), aCollatorAlgorithm( r.aCollatorAlgorithm ),
46 nCompatHeader( r.nCompatHeader )
47{
48}
49
51
53{
54 ScSortKeyState aKeyState;
55
61 nCompatHeader = 2;
62 nDestTab = 0;
63 nUserIndex = 0;
65 bByRow = bInplace = true;
66 aCollatorLocale = css::lang::Locale();
67 aCollatorAlgorithm.clear();
68
69 aKeyState.bDoSort = false;
70 aKeyState.nField = 0;
71 aKeyState.bAscending = true;
73
74 // Initialize to default size
75 maKeyState.assign( DEFSORT, aKeyState );
76}
77
79{
80 nCol1 = r.nCol1;
81 nRow1 = r.nRow1;
82 nCol2 = r.nCol2;
83 nRow2 = r.nRow2;
87 bByRow = r.bByRow;
99
100 return *this;
101}
102
103bool ScSortParam::operator==( const ScSortParam& rOther ) const
104{
105 bool bEqual = false;
106 // Number of Sorts the same?
107 sal_uInt16 nLast = 0;
108 sal_uInt16 nOtherLast = 0;
109 sal_uInt16 nSortSize = GetSortKeyCount();
110
111 if ( !maKeyState.empty() )
112 {
113 while ( maKeyState[nLast++].bDoSort && nLast < nSortSize ) ;
114 nLast--;
115 }
116
117 if ( !rOther.maKeyState.empty() )
118 {
119 while ( rOther.maKeyState[nOtherLast++].bDoSort && nOtherLast < nSortSize ) ;
120 nOtherLast--;
121 }
122
123 if ( (nLast == nOtherLast)
124 && (nCol1 == rOther.nCol1)
125 && (nRow1 == rOther.nRow1)
126 && (nCol2 == rOther.nCol2)
127 && (nRow2 == rOther.nRow2)
128 && (aDataAreaExtras == rOther.aDataAreaExtras)
129 && (bHasHeader == rOther.bHasHeader)
130 && (bByRow == rOther.bByRow)
131 && (bCaseSens == rOther.bCaseSens)
132 && (bNaturalSort == rOther.bNaturalSort)
133 && (bUserDef == rOther.bUserDef)
134 && (nUserIndex == rOther.nUserIndex)
135 && (bInplace == rOther.bInplace)
136 && (nDestTab == rOther.nDestTab)
137 && (nDestCol == rOther.nDestCol)
138 && (nDestRow == rOther.nDestRow)
139 && (aCollatorLocale.Language == rOther.aCollatorLocale.Language)
140 && (aCollatorLocale.Country == rOther.aCollatorLocale.Country)
141 && (aCollatorLocale.Variant == rOther.aCollatorLocale.Variant)
143 && ( !maKeyState.empty() || !rOther.maKeyState.empty() )
144 )
145 {
146 bEqual = true;
147 for ( sal_uInt16 i=0; i<=nLast && bEqual; i++ )
148 bEqual = ( maKeyState[i].nField == rOther.maKeyState[i].nField ) &&
149 ( maKeyState[i].bAscending == rOther.maKeyState[i].bAscending );
150 }
151 if ( maKeyState.empty() && rOther.maKeyState.empty() )
152 bEqual = true;
153
154 return bEqual;
155}
156
158 nCol1(rSub.nCol1),nRow1(rSub.nRow1),nCol2(rSub.nCol2),nRow2(rSub.nRow2),
159 aDataAreaExtras(rOld.aDataAreaExtras),
160 nUserIndex(rSub.nUserIndex),
161 bHasHeader(true),bByRow(true),bCaseSens(rSub.bCaseSens),bNaturalSort(rOld.bNaturalSort),
162 bUserDef(rSub.bUserDef),
163 bInplace(true),
164 nDestTab(0),nDestCol(0),nDestRow(0),
165 aCollatorLocale( rOld.aCollatorLocale ), aCollatorAlgorithm( rOld.aCollatorAlgorithm ),
166 nCompatHeader( rOld.nCompatHeader )
167{
170
171 sal_uInt16 i;
172
173 // first the groups from the partial results
174 if (rSub.bDoSort)
175 for (i=0; i<MAXSUBTOTAL; i++)
176 if (rSub.bGroupActive[i])
177 {
178 ScSortKeyState key;
179 key.bDoSort = true;
180 key.nField = rSub.nField[i];
181 key.bAscending = rSub.bAscending;
183 maKeyState.push_back(key);
184 }
185
186 // then the old settings
187 for (i=0; i < rOld.GetSortKeyCount(); i++)
188 if (rOld.maKeyState[i].bDoSort)
189 {
190 SCCOLROW nThisField = rOld.maKeyState[i].nField;
191 bool bDouble = false;
192 for (sal_uInt16 j = 0; j < GetSortKeyCount(); j++)
193 if ( maKeyState[j].nField == nThisField )
194 bDouble = true;
195 if (!bDouble) // do not enter a field twice
196 {
197 ScSortKeyState key;
198 key.bDoSort = true;
199 key.nField = nThisField;
200 key.bAscending = rOld.maKeyState[i].bAscending;
202 maKeyState.push_back(key);
203 }
204 }
205}
206
208 nCol1(nCol),nRow1(rParam.nRow1),nCol2(nCol),nRow2(rParam.nRow2),nUserIndex(0),
209 bHasHeader(rParam.bHasHeader),bByRow(true),bCaseSens(rParam.bCaseSens),
210 bNaturalSort(false),
211//TODO: what about Locale and Algorithm?
212 bUserDef(false),
213 bInplace(true),
214 nDestTab(0),nDestCol(0),nDestRow(0), nCompatHeader(2)
215{
217
218 ScSortKeyState aKeyState;
219 aKeyState.bDoSort = true;
220 aKeyState.nField = nCol;
221 aKeyState.bAscending = true;
223
224 maKeyState.push_back( aKeyState );
225
226 // Set the rest
227 aKeyState.bDoSort = false;
228 aKeyState.nField = 0;
229
230 for (sal_uInt16 i=1; i<GetSortKeyCount(); i++)
231 maKeyState.push_back( aKeyState );
232}
233
235{
236 if (!bInplace)
237 {
238 SCCOL nDifX = nDestCol - nCol1;
239 SCROW nDifY = nDestRow - nRow1;
240
241 nCol1 = sal::static_int_cast<SCCOL>( nCol1 + nDifX );
242 nRow1 = sal::static_int_cast<SCROW>( nRow1 + nDifY );
243 nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nDifX );
244 nRow2 = sal::static_int_cast<SCROW>( nRow2 + nDifY );
245 for (sal_uInt16 i=0; i<GetSortKeyCount(); i++)
246 if (bByRow)
247 maKeyState[i].nField += nDifX;
248 else
249 maKeyState[i].nField += nDifY;
250
251 bInplace = true;
252 }
253 else
254 {
255 OSL_FAIL("MoveToDest, bInplace == TRUE");
256 }
257}
258
259namespace sc {
260
261namespace {
262
263struct ReorderIndex
264{
265 struct LessByPos2
266 {
267 bool operator() ( const ReorderIndex& r1, const ReorderIndex& r2 ) const
268 {
269 return r1.mnPos2 < r2.mnPos2;
270 }
271 };
272
273 SCCOLROW mnPos1;
274 SCCOLROW mnPos2;
275
276 ReorderIndex( SCCOLROW nPos1, SCCOLROW nPos2 ) : mnPos1(nPos1), mnPos2(nPos2) {}
277};
278
279}
280
282{
283 SCCOLROW nStart;
284 if (mbByRow)
285 nStart = maSortRange.aStart.Row();
286 else
287 nStart = maSortRange.aStart.Col();
288
289 size_t n = maOrderIndices.size();
290 std::vector<ReorderIndex> aBucket;
291 aBucket.reserve(n);
292 for (size_t i = 0; i < n; ++i)
293 {
294 SCCOLROW nPos1 = i + nStart;
295 SCCOLROW nPos2 = maOrderIndices[i];
296 aBucket.emplace_back(nPos1, nPos2);
297 }
298
299 std::sort(aBucket.begin(), aBucket.end(), ReorderIndex::LessByPos2());
300 std::vector<SCCOLROW> aNew;
301 aNew.reserve(n);
302 for (size_t i = 0; i < n; ++i)
303 aNew.push_back(aBucket[i].mnPos1);
304
305 maOrderIndices.swap(aNew);
306}
307
308}
309
310/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SCROW Row() const
Definition: address.hxx:274
SCCOL Col() const
Definition: address.hxx:279
ScAddress aStart
Definition: address.hxx:497
const SCSIZE MAXSUBTOTAL
Definition: global.hxx:82
sal_Int64 n
int i
CAUTION! The following defines must be in the same namespace as the respective type.
Definition: broadcast.cxx:15
#define DEFSORT
Definition: sortparam.hxx:22
Struct to hold non-data extended area, used with ScDocument::ShrinkToUsedDataArea().
Definition: sortparam.hxx:53
bool mbCellDrawObjects
If TRUE, consider the presence of draw objects anchored to the cell.
Definition: sortparam.hxx:57
bool mbCellFormats
If TRUE, consider the presence of cell formats.
Definition: sortparam.hxx:59
ScColorSortMode aColorSortMode
Definition: sortparam.hxx:45
SCCOLROW nField
Definition: sortparam.hxx:42
SCTAB nDestTab
Definition: sortparam.hxx:126
SCROW nDestRow
Definition: sortparam.hxx:128
sal_uInt16 nCompatHeader
Definition: sortparam.hxx:133
SCCOL nDestCol
Definition: sortparam.hxx:127
bool bNaturalSort
Definition: sortparam.hxx:123
::std::vector< ScSortKeyState > maKeyState
Definition: sortparam.hxx:130
void MoveToDest()
Definition: sortparam.cxx:234
OUString aCollatorAlgorithm
Definition: sortparam.hxx:132
bool bHasHeader
Definition: sortparam.hxx:120
ScSortParam & operator=(const ScSortParam &r)
Definition: sortparam.cxx:78
ScDataAreaExtras aDataAreaExtras
Definition: sortparam.hxx:118
css::lang::Locale aCollatorLocale
Definition: sortparam.hxx:131
sal_uInt16 nUserIndex
Definition: sortparam.hxx:119
void Clear()
Definition: sortparam.cxx:52
bool operator==(const ScSortParam &rOther) const
Definition: sortparam.cxx:103
sal_uInt16 GetSortKeyCount() const
Definition: sortparam.hxx:148
bool bCaseSens
Definition: sortparam.hxx:122
bool bAscending
sort ascending
bool bGroupActive[MAXSUBTOTAL]
active groups
SCCOL nField[MAXSUBTOTAL]
associated field
bool bDoSort
presort
bool bIncludePattern
sort formats
void reverse()
Reorder the position indices such that it can be used to undo the original reordering.
Definition: sortparam.cxx:281
std::vector< SCCOLROW > maOrderIndices
List of original column / row positions after reordering.
Definition: sortparam.hxx:166
ScRange maSortRange
This sort range already takes into account the presence or absence of header row / column i....
Definition: sortparam.hxx:160
sal_Int32 SCCOLROW
a type capable of holding either SCCOL or SCROW
Definition: types.hxx:23
sal_Int16 SCCOL
Definition: types.hxx:21
sal_Int32 SCROW
Definition: types.hxx:17