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