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),nUserIndex(r.nUserIndex),
37  bHasHeader(r.bHasHeader),bByRow(r.bByRow),bCaseSens(r.bCaseSens),
38  bNaturalSort(r.bNaturalSort),bIncludeComments(r.bIncludeComments),
39  bIncludeGraphicObjects(r.bIncludeGraphicObjects),bUserDef(r.bUserDef),
40  bIncludePattern(r.bIncludePattern),bInplace(r.bInplace),
41  nDestTab(r.nDestTab),nDestCol(r.nDestCol),nDestRow(r.nDestRow),
42  maKeyState( r.maKeyState ),
43  aCollatorLocale( r.aCollatorLocale ), aCollatorAlgorithm( r.aCollatorAlgorithm ),
44  nCompatHeader( r.nCompatHeader )
45 {
46 }
47 
49 
51 {
52  ScSortKeyState aKeyState;
53 
54  nCol1=nCol2=nDestCol = 0;
55  nRow1=nRow2=nDestRow = 0;
56  nCompatHeader = 2;
57  nDestTab = 0;
58  nUserIndex = 0;
60  bIncludeComments = false;
63  aCollatorLocale = css::lang::Locale();
64  aCollatorAlgorithm.clear();
65 
66  aKeyState.bDoSort = false;
67  aKeyState.nField = 0;
68  aKeyState.bAscending = true;
69 
70  // Initialize to default size
71  maKeyState.assign( DEFSORT, aKeyState );
72 }
73 
75 {
76  nCol1 = r.nCol1;
77  nRow1 = r.nRow1;
78  nCol2 = r.nCol2;
79  nRow2 = r.nRow2;
82  bByRow = r.bByRow;
83  bCaseSens = r.bCaseSens;
87  bUserDef = r.bUserDef;
89  bInplace = r.bInplace;
90  nDestTab = r.nDestTab;
91  nDestCol = r.nDestCol;
92  nDestRow = r.nDestRow;
97 
98  return *this;
99 }
100 
101 bool ScSortParam::operator==( const ScSortParam& rOther ) const
102 {
103  bool bEqual = false;
104  // Number of Sorts the same?
105  sal_uInt16 nLast = 0;
106  sal_uInt16 nOtherLast = 0;
107  sal_uInt16 nSortSize = GetSortKeyCount();
108 
109  if ( !maKeyState.empty() )
110  {
111  while ( maKeyState[nLast++].bDoSort && nLast < nSortSize ) ;
112  nLast--;
113  }
114 
115  if ( !rOther.maKeyState.empty() )
116  {
117  while ( rOther.maKeyState[nOtherLast++].bDoSort && nOtherLast < nSortSize ) ;
118  nOtherLast--;
119  }
120 
121  if ( (nLast == nOtherLast)
122  && (nCol1 == rOther.nCol1)
123  && (nRow1 == rOther.nRow1)
124  && (nCol2 == rOther.nCol2)
125  && (nRow2 == rOther.nRow2)
126  && (bHasHeader == rOther.bHasHeader)
127  && (bByRow == rOther.bByRow)
128  && (bCaseSens == rOther.bCaseSens)
129  && (bNaturalSort == rOther.bNaturalSort)
130  && (bIncludeComments== rOther.bIncludeComments)
132  && (bUserDef == rOther.bUserDef)
133  && (nUserIndex == rOther.nUserIndex)
134  && (bIncludePattern == rOther.bIncludePattern)
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),nUserIndex(rSub.nUserIndex),
159  bHasHeader(true),bByRow(true),bCaseSens(rSub.bCaseSens),bNaturalSort(rOld.bNaturalSort),
160  bIncludeComments(rOld.bIncludeComments),bIncludeGraphicObjects(rOld.bIncludeGraphicObjects),
161  bUserDef(rSub.bUserDef),bIncludePattern(rSub.bIncludePattern),
162  bInplace(true),
163  nDestTab(0),nDestCol(0),nDestRow(0),
164  aCollatorLocale( rOld.aCollatorLocale ), aCollatorAlgorithm( rOld.aCollatorAlgorithm ),
165  nCompatHeader( rOld.nCompatHeader )
166 {
167  sal_uInt16 i;
168 
169  // first the groups from the partial results
170  if (rSub.bDoSort)
171  for (i=0; i<MAXSUBTOTAL; i++)
172  if (rSub.bGroupActive[i])
173  {
174  ScSortKeyState key;
175  key.bDoSort = true;
176  key.nField = rSub.nField[i];
177  key.bAscending = rSub.bAscending;
178  maKeyState.push_back(key);
179  }
180 
181  // then the old settings
182  for (i=0; i < rOld.GetSortKeyCount(); i++)
183  if (rOld.maKeyState[i].bDoSort)
184  {
185  SCCOLROW nThisField = rOld.maKeyState[i].nField;
186  bool bDouble = false;
187  for (sal_uInt16 j = 0; j < GetSortKeyCount(); j++)
188  if ( maKeyState[j].nField == nThisField )
189  bDouble = true;
190  if (!bDouble) // do not enter a field twice
191  {
192  ScSortKeyState key;
193  key.bDoSort = true;
194  key.nField = nThisField;
195  key.bAscending = rOld.maKeyState[i].bAscending;
196  maKeyState.push_back(key);
197  }
198  }
199 }
200 
202  nCol1(nCol),nRow1(rParam.nRow1),nCol2(nCol),nRow2(rParam.nRow2),nUserIndex(0),
203  bHasHeader(rParam.bHasHeader),bByRow(true),bCaseSens(rParam.bCaseSens),
204  bNaturalSort(false),bIncludeComments(false),bIncludeGraphicObjects(true),
205 //TODO: what about Locale and Algorithm?
206  bUserDef(false),bIncludePattern(false),
207  bInplace(true),
208  nDestTab(0),nDestCol(0),nDestRow(0), nCompatHeader(2)
209 {
210  ScSortKeyState aKeyState;
211  aKeyState.bDoSort = true;
212  aKeyState.nField = nCol;
213  aKeyState.bAscending = true;
214 
215  maKeyState.push_back( aKeyState );
216 
217  // Set the rest
218  aKeyState.bDoSort = false;
219  aKeyState.nField = 0;
220 
221  for (sal_uInt16 i=1; i<GetSortKeyCount(); i++)
222  maKeyState.push_back( aKeyState );
223 }
224 
226 {
227  if (!bInplace)
228  {
229  SCCOL nDifX = nDestCol - nCol1;
230  SCROW nDifY = nDestRow - nRow1;
231 
232  nCol1 = sal::static_int_cast<SCCOL>( nCol1 + nDifX );
233  nRow1 = sal::static_int_cast<SCROW>( nRow1 + nDifY );
234  nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nDifX );
235  nRow2 = sal::static_int_cast<SCROW>( nRow2 + nDifY );
236  for (sal_uInt16 i=0; i<GetSortKeyCount(); i++)
237  if (bByRow)
238  maKeyState[i].nField += nDifX;
239  else
240  maKeyState[i].nField += nDifY;
241 
242  bInplace = true;
243  }
244  else
245  {
246  OSL_FAIL("MoveToDest, bInplace == TRUE");
247  }
248 }
249 
250 namespace sc {
251 
252 namespace {
253 
254 struct ReorderIndex
255 {
256  struct LessByPos2
257  {
258  bool operator() ( const ReorderIndex& r1, const ReorderIndex& r2 ) const
259  {
260  return r1.mnPos2 < r2.mnPos2;
261  }
262  };
263 
264  SCCOLROW mnPos1;
265  SCCOLROW mnPos2;
266 
267  ReorderIndex( SCCOLROW nPos1, SCCOLROW nPos2 ) : mnPos1(nPos1), mnPos2(nPos2) {}
268 };
269 
270 }
271 
273 {
274  SCCOLROW nStart;
275  if (mbByRow)
276  nStart = maSortRange.aStart.Row();
277  else
278  nStart = maSortRange.aStart.Col();
279 
280  size_t n = maOrderIndices.size();
281  std::vector<ReorderIndex> aBucket;
282  aBucket.reserve(n);
283  for (size_t i = 0; i < n; ++i)
284  {
285  SCCOLROW nPos1 = i + nStart;
286  SCCOLROW nPos2 = maOrderIndices[i];
287  aBucket.emplace_back(nPos1, nPos2);
288  }
289 
290  std::sort(aBucket.begin(), aBucket.end(), ReorderIndex::LessByPos2());
291  std::vector<SCCOLROW> aNew;
292  aNew.reserve(n);
293  for (size_t i = 0; i < n; ++i)
294  aNew.push_back(aBucket[i].mnPos1);
295 
296  maOrderIndices.swap(aNew);
297 }
298 
299 }
300 
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::vector< ScSortKeyState > maKeyState
Definition: sortparam.hxx:61
bool bGroupActive[MAXSUBTOTAL]
active groups
sal_uInt16 nUserIndex
Definition: sortparam.hxx:47
SCROW nRow1
Definition: sortparam.hxx:44
SCCOL nField[MAXSUBTOTAL]
associated field
ScAddress aStart
Definition: address.hxx:500
SCCOL nCol2
Definition: sortparam.hxx:45
SCROW Row() const
Definition: address.hxx:262
css::lang::Locale aCollatorLocale
Definition: sortparam.hxx:62
SCCOL nDestCol
Definition: sortparam.hxx:58
bool bIncludeComments
Definition: sortparam.hxx:52
ScSortParam & operator=(const ScSortParam &r)
Definition: sortparam.cxx:74
sal_Int64 n
bool bDoSort
presort
bool bUserDef
Definition: sortparam.hxx:54
SCROW nRow2
Definition: sortparam.hxx:46
ScRange maSortRange
This sort range already takes into account the presence or absence of header row / column i...
Definition: sortparam.hxx:91
sal_Int32 SCCOLROW
a type capable of holding either SCCOL or SCROW
Definition: types.hxx:24
bool bNaturalSort
Definition: sortparam.hxx:51
bool bIncludeGraphicObjects
Definition: sortparam.hxx:53
SCCOL nCol1
Definition: sortparam.hxx:43
int i
sal_Int16 SCCOL
Definition: types.hxx:22
bool bCaseSens
Definition: sortparam.hxx:50
bool operator==(const ScSortParam &rOther) const
Definition: sortparam.cxx:101
SCCOL Col() const
Definition: address.hxx:267
std::vector< SCCOLROW > maOrderIndices
List of original column / row positions after reordering.
Definition: sortparam.hxx:96
sal_Int32 SCROW
Definition: types.hxx:18
bool bInplace
Definition: sortparam.hxx:56
bool bHasHeader
Definition: sortparam.hxx:48
void reverse()
Reorder the position indices such that it can be used to undo the original reordering.
Definition: sortparam.cxx:272
sal_uInt16 GetSortKeyCount() const
Definition: sortparam.hxx:79
void Clear()
Definition: sortparam.cxx:50
SCCOLROW nField
Definition: sortparam.hxx:36
OUString aCollatorAlgorithm
Definition: sortparam.hxx:63
void MoveToDest()
Definition: sortparam.cxx:225
SCROW nDestRow
Definition: sortparam.hxx:59
bool bIncludePattern
Definition: sortparam.hxx:55
#define DEFSORT
Definition: sortparam.hxx:23
sal_uInt16 nCompatHeader
Definition: sortparam.hxx:64
bool bAscending
sort ascending
const SCSIZE MAXSUBTOTAL
Definition: global.hxx:79
SCTAB nDestTab
Definition: sortparam.hxx:57