1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sortparam.hxx"
22 #include "address.hxx"
23 #include "queryparam.hxx"
24 #include "subtotalparam.hxx"
26 #include <osl/diagnose.h>
28 ScSortParam::ScSortParam()
33 ScSortParam::ScSortParam( const ScSortParam
& r
) :
34 nCol1(r
.nCol1
),nRow1(r
.nRow1
),nCol2(r
.nCol2
),nRow2(r
.nRow2
),nUserIndex(r
.nUserIndex
),
35 bHasHeader(r
.bHasHeader
),bByRow(r
.bByRow
),bCaseSens(r
.bCaseSens
),
36 bNaturalSort(r
.bNaturalSort
),bUserDef(r
.bUserDef
),
37 bIncludePattern(r
.bIncludePattern
),bInplace(r
.bInplace
),
38 nDestTab(r
.nDestTab
),nDestCol(r
.nDestCol
),nDestRow(r
.nDestRow
),
39 maKeyState( r
.maKeyState
),
40 aCollatorLocale( r
.aCollatorLocale
), aCollatorAlgorithm( r
.aCollatorAlgorithm
),
41 nCompatHeader( r
.nCompatHeader
)
45 ScSortParam::~ScSortParam() {}
47 void ScSortParam::Clear()
49 ScSortKeyState aKeyState
;
51 nCol1
=nCol2
=nDestCol
= 0;
52 nRow1
=nRow2
=nDestRow
= 0;
56 bHasHeader
=bCaseSens
=bUserDef
=bNaturalSort
= false;
57 bByRow
=bIncludePattern
=bInplace
= true;
58 aCollatorLocale
= ::com::sun::star::lang::Locale();
59 aCollatorAlgorithm
.clear();
61 aKeyState
.bDoSort
= false;
63 aKeyState
.bAscending
= true;
65 // Initialize to default size
66 maKeyState
.assign( DEFSORT
, aKeyState
);
69 ScSortParam
& ScSortParam::operator=( const ScSortParam
& r
)
75 nUserIndex
= r
.nUserIndex
;
76 bHasHeader
= r
.bHasHeader
;
78 bCaseSens
= r
.bCaseSens
;
79 bNaturalSort
= r
.bNaturalSort
;
80 bUserDef
= r
.bUserDef
;
81 bIncludePattern
= r
.bIncludePattern
;
82 bInplace
= r
.bInplace
;
83 nDestTab
= r
.nDestTab
;
84 nDestCol
= r
.nDestCol
;
85 nDestRow
= r
.nDestRow
;
86 maKeyState
= r
.maKeyState
;
87 aCollatorLocale
= r
.aCollatorLocale
;
88 aCollatorAlgorithm
= r
.aCollatorAlgorithm
;
89 nCompatHeader
= r
.nCompatHeader
;
94 bool ScSortParam::operator==( const ScSortParam
& rOther
) const
97 // Number of Sorts the same?
99 sal_uInt16 nOtherLast
= 0;
100 sal_uInt16 nSortSize
= GetSortKeyCount();
102 if ( !maKeyState
.empty() )
104 while ( maKeyState
[nLast
++].bDoSort
&& nLast
< nSortSize
) ;
108 if ( !rOther
.maKeyState
.empty() )
110 while ( rOther
.maKeyState
[nOtherLast
++].bDoSort
&& nOtherLast
< nSortSize
) ;
114 if ( (nLast
== nOtherLast
)
115 && (nCol1
== rOther
.nCol1
)
116 && (nRow1
== rOther
.nRow1
)
117 && (nCol2
== rOther
.nCol2
)
118 && (nRow2
== rOther
.nRow2
)
119 && (bHasHeader
== rOther
.bHasHeader
)
120 && (bByRow
== rOther
.bByRow
)
121 && (bCaseSens
== rOther
.bCaseSens
)
122 && (bNaturalSort
== rOther
.bNaturalSort
)
123 && (bUserDef
== rOther
.bUserDef
)
124 && (nUserIndex
== rOther
.nUserIndex
)
125 && (bIncludePattern
== rOther
.bIncludePattern
)
126 && (bInplace
== rOther
.bInplace
)
127 && (nDestTab
== rOther
.nDestTab
)
128 && (nDestCol
== rOther
.nDestCol
)
129 && (nDestRow
== rOther
.nDestRow
)
130 && (aCollatorLocale
.Language
== rOther
.aCollatorLocale
.Language
)
131 && (aCollatorLocale
.Country
== rOther
.aCollatorLocale
.Country
)
132 && (aCollatorLocale
.Variant
== rOther
.aCollatorLocale
.Variant
)
133 && (aCollatorAlgorithm
== rOther
.aCollatorAlgorithm
)
134 && ( !maKeyState
.empty() || !rOther
.maKeyState
.empty() )
138 for ( sal_uInt16 i
=0; i
<=nLast
&& bEqual
; i
++ )
139 bEqual
= ( maKeyState
[i
].nField
== rOther
.maKeyState
[i
].nField
) &&
140 ( maKeyState
[i
].bAscending
== rOther
.maKeyState
[i
].bAscending
);
142 if ( maKeyState
.empty() && rOther
.maKeyState
.empty() )
148 ScSortParam::ScSortParam( const ScSubTotalParam
& rSub
, const ScSortParam
& rOld
) :
149 nCol1(rSub
.nCol1
),nRow1(rSub
.nRow1
),nCol2(rSub
.nCol2
),nRow2(rSub
.nRow2
),nUserIndex(rSub
.nUserIndex
),
150 bHasHeader(true),bByRow(true),bCaseSens(rSub
.bCaseSens
),bNaturalSort(rOld
.bNaturalSort
),
151 bUserDef(rSub
.bUserDef
),bIncludePattern(rSub
.bIncludePattern
),
153 nDestTab(0),nDestCol(0),nDestRow(0),
154 aCollatorLocale( rOld
.aCollatorLocale
), aCollatorAlgorithm( rOld
.aCollatorAlgorithm
),
155 nCompatHeader( rOld
.nCompatHeader
)
159 // first the groups from the partial results
161 for (i
=0; i
<MAXSUBTOTAL
; i
++)
162 if (rSub
.bGroupActive
[i
])
166 key
.nField
= rSub
.nField
[i
];
167 key
.bAscending
= rSub
.bAscending
;
168 maKeyState
.push_back(key
);
171 // then the old settings
172 for (i
=0; i
< rOld
.GetSortKeyCount(); i
++)
173 if (rOld
.maKeyState
[i
].bDoSort
)
175 SCCOLROW nThisField
= rOld
.maKeyState
[i
].nField
;
176 bool bDouble
= false;
177 for (sal_uInt16 j
= 0; j
< GetSortKeyCount(); j
++)
178 if ( maKeyState
[j
].nField
== nThisField
)
180 if (!bDouble
) // do not enter a field twice
184 key
.nField
= nThisField
;
185 key
.bAscending
= rOld
.maKeyState
[i
].bAscending
;
186 maKeyState
.push_back(key
);
191 ScSortParam::ScSortParam( const ScQueryParam
& rParam
, SCCOL nCol
) :
192 nCol1(nCol
),nRow1(rParam
.nRow1
),nCol2(nCol
),nRow2(rParam
.nRow2
),nUserIndex(0),
193 bHasHeader(rParam
.bHasHeader
),bByRow(true),bCaseSens(rParam
.bCaseSens
),
195 //TODO: what about Locale and Algorithm?
196 bUserDef(false),bIncludePattern(false),
198 nDestTab(0),nDestCol(0),nDestRow(0), nCompatHeader(2)
200 ScSortKeyState aKeyState
;
201 aKeyState
.bDoSort
= true;
202 aKeyState
.nField
= nCol
;
203 aKeyState
.bAscending
= true;
205 maKeyState
.push_back( aKeyState
);
208 aKeyState
.bDoSort
= false;
209 aKeyState
.nField
= 0;
211 for (sal_uInt16 i
=1; i
<GetSortKeyCount(); i
++)
212 maKeyState
.push_back( aKeyState
);
215 void ScSortParam::MoveToDest()
219 SCsCOL nDifX
= ((SCsCOL
) nDestCol
) - ((SCsCOL
) nCol1
);
220 SCsROW nDifY
= ((SCsROW
) nDestRow
) - ((SCsROW
) nRow1
);
222 nCol1
= sal::static_int_cast
<SCCOL
>( nCol1
+ nDifX
);
223 nRow1
= sal::static_int_cast
<SCROW
>( nRow1
+ nDifY
);
224 nCol2
= sal::static_int_cast
<SCCOL
>( nCol2
+ nDifX
);
225 nRow2
= sal::static_int_cast
<SCROW
>( nRow2
+ nDifY
);
226 for (sal_uInt16 i
=0; i
<GetSortKeyCount(); i
++)
228 maKeyState
[i
].nField
+= nDifX
;
230 maKeyState
[i
].nField
+= nDifY
;
236 OSL_FAIL("MoveToDest, bInplace == TRUE");
246 struct LessByPos2
: std::binary_function
<ReorderIndex
, ReorderIndex
, bool>
248 bool operator() ( const ReorderIndex
& r1
, const ReorderIndex
& r2
) const
250 return r1
.mnPos2
< r2
.mnPos2
;
257 ReorderIndex( SCCOLROW nPos1
, SCCOLROW nPos2
) : mnPos1(nPos1
), mnPos2(nPos2
) {}
262 void ReorderParam::reverse()
266 nStart
= maSortRange
.aStart
.Row();
268 nStart
= maSortRange
.aStart
.Col();
270 size_t n
= maOrderIndices
.size();
271 std::vector
<ReorderIndex
> aBucket
;
273 for (size_t i
= 0; i
< n
; ++i
)
275 SCCOLROW nPos1
= i
+ nStart
;
276 SCCOLROW nPos2
= maOrderIndices
[i
];
277 aBucket
.push_back(ReorderIndex(nPos1
, nPos2
));
280 std::sort(aBucket
.begin(), aBucket
.end(), ReorderIndex::LessByPos2());
281 std::vector
<SCCOLROW
> aNew
;
283 for (size_t i
= 0; i
< n
; ++i
)
284 aNew
.push_back(aBucket
[i
].mnPos1
);
286 maOrderIndices
.swap(aNew
);
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */