bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / commontools / TSortIndex.cxx
blob454cf06e56309a8e2779d5872c972b812c73cb47
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
21 #include <algorithm>
22 #include <iterator>
24 #include <o3tl/compat_functional.hxx>
26 using namespace connectivity;
28 /// binary_function Functor object for class OSortIndex::TIntValuePairVector::value_type returntype is bool
29 struct TKeyValueFunc : ::std::binary_function<OSortIndex::TIntValuePairVector::value_type,OSortIndex::TIntValuePairVector::value_type,bool>
31 OSortIndex* pIndex;
33 TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex)
36 // return false if compared values are equal otherwise true
37 inline bool operator()(const OSortIndex::TIntValuePairVector::value_type& lhs,const OSortIndex::TIntValuePairVector::value_type& rhs) const
39 const ::std::vector<OKeyType>& aKeyType = pIndex->getKeyType();
40 ::std::vector<OKeyType>::const_iterator aIter = aKeyType.begin();
41 for (::std::vector<sal_Int16>::size_type i=0;aIter != aKeyType.end(); ++aIter,++i)
43 const bool nGreater = pIndex->getAscending(i) != SQL_ASC;
44 const bool nLess = !nGreater;
46 // compare depending for type
47 switch (*aIter)
49 case SQL_ORDERBYKEY_STRING:
51 sal_Int32 nRes = lhs.second->getKeyString(i).compareTo(rhs.second->getKeyString(i));
52 if (nRes < 0)
53 return nLess;
54 else if (nRes > 0)
55 return nGreater;
57 break;
58 case SQL_ORDERBYKEY_DOUBLE:
60 double d1 = lhs.second->getKeyDouble(i);
61 double d2 = rhs.second->getKeyDouble(i);
63 if (d1 < d2)
64 return nLess;
65 else if (d1 > d2)
66 return nGreater;
68 break;
69 case SQL_ORDERBYKEY_NONE:
70 break;
74 // know we know that the values are equal
75 return false;
80 ::rtl::Reference<OKeySet> OSortIndex::CreateKeySet()
82 Freeze();
84 ::rtl::Reference<OKeySet> pKeySet = new OKeySet();
85 pKeySet->get().reserve(m_aKeyValues.size());
86 ::std::transform(m_aKeyValues.begin()
87 ,m_aKeyValues.end()
88 ,::std::back_inserter(pKeySet->get())
89 ,::o3tl::select1st<TIntValuePairVector::value_type>());
90 pKeySet->setFrozen();
91 return pKeySet;
94 OSortIndex::OSortIndex( const ::std::vector<OKeyType>& _aKeyType,
95 const ::std::vector<TAscendingOrder>& _aAscending)
96 :m_aKeyType(_aKeyType)
97 ,m_aAscending(_aAscending)
98 ,m_bFrozen(false)
102 OSortIndex::~OSortIndex()
106 void OSortIndex::AddKeyValue(OKeyValue * pKeyValue)
108 assert(pKeyValue && "Can not be null here!");
109 if(m_bFrozen)
111 m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),(OKeyValue *)NULL));
112 delete pKeyValue;
114 else
115 m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),pKeyValue));
118 void OSortIndex::Freeze()
120 OSL_ENSURE(! m_bFrozen,"OSortIndex::Freeze: already frozen!");
121 // Sortierung:
122 if (m_aKeyType[0] != SQL_ORDERBYKEY_NONE)
123 // we will sort ourself when the first keyType say so
124 ::std::sort(m_aKeyValues.begin(),m_aKeyValues.end(),TKeyValueFunc(this));
126 TIntValuePairVector::iterator aIter = m_aKeyValues.begin();
127 for(;aIter != m_aKeyValues.end();++aIter)
129 delete aIter->second;
130 aIter->second = NULL;
133 m_bFrozen = true;
137 OKeyValue::OKeyValue(sal_Int32 nVal)
138 : m_nValue(nVal)
142 OKeyValue::~OKeyValue()
146 OKeyValue* OKeyValue::createKeyValue(sal_Int32 _nVal)
148 return new OKeyValue(_nVal);
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */