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 <TSortIndex.hxx>
23 #include <o3tl/functional.hxx>
25 using namespace connectivity
;
27 /// Functor object for class OSortIndex::TIntValuePairVector::value_type returntype is bool
32 explicit TKeyValueFunc(OSortIndex
* _pIndex
) : pIndex(_pIndex
)
35 // return false if compared values are equal otherwise true
36 bool operator()(const OSortIndex::TIntValuePairVector::value_type
& lhs
,const OSortIndex::TIntValuePairVector::value_type
& rhs
) const
38 const std::vector
<OKeyType
>& aKeyType
= pIndex
->getKeyType();
40 for (auto const& elem
: aKeyType
)
42 const bool bGreater
= pIndex
->getAscending(i
) != TAscendingOrder::ASC
;
43 const bool bLess
= !bGreater
;
45 // compare depending for type
48 case OKeyType::String
:
50 sal_Int32 nRes
= lhs
.second
->getKeyString(i
).compareTo(rhs
.second
->getKeyString(i
));
57 case OKeyType::Double
:
59 double d1
= lhs
.second
->getKeyDouble(i
);
60 double d2
= rhs
.second
->getKeyDouble(i
);
74 // know we know that the values are equal
80 ::rtl::Reference
<OKeySet
> OSortIndex::CreateKeySet()
84 ::rtl::Reference
<OKeySet
> pKeySet
= new OKeySet();
85 pKeySet
->get().reserve(m_aKeyValues
.size());
86 std::transform(m_aKeyValues
.begin()
88 ,std::back_inserter(pKeySet
->get())
89 ,::o3tl::select1st
<TIntValuePairVector::value_type
>());
94 OSortIndex::OSortIndex( const std::vector
<OKeyType
>& _aKeyType
,
95 const std::vector
<TAscendingOrder
>& _aAscending
)
96 :m_aKeyType(_aKeyType
)
97 ,m_aAscending(_aAscending
)
102 OSortIndex::~OSortIndex()
106 void OSortIndex::AddKeyValue(std::unique_ptr
<OKeyValue
> pKeyValue
)
108 assert(pKeyValue
&& "Can not be null here!");
111 m_aKeyValues
.push_back({pKeyValue
->getValue(),nullptr});
114 m_aKeyValues
.push_back({pKeyValue
->getValue(),std::move(pKeyValue
)});
117 void OSortIndex::Freeze()
119 OSL_ENSURE(! m_bFrozen
,"OSortIndex::Freeze: already frozen!");
121 if (m_aKeyType
[0] != OKeyType::NONE
)
122 // we will sort ourself when the first keyType say so
123 std::sort(m_aKeyValues
.begin(),m_aKeyValues
.end(),TKeyValueFunc(this));
125 for (auto & keyValue
: m_aKeyValues
)
127 keyValue
.second
.reset();
134 OKeyValue::OKeyValue(sal_Int32 nVal
)
139 OKeyValue::~OKeyValue()
143 std::unique_ptr
<OKeyValue
> OKeyValue::createKeyValue(sal_Int32 _nVal
)
145 return std::unique_ptr
<OKeyValue
>(new OKeyValue(_nVal
));
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */