1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*9b5730f6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*9b5730f6SAndrew Rist * distributed with this work for additional information
6*9b5730f6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*9b5730f6SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist * with the License. You may obtain a copy of the License at
10*9b5730f6SAndrew Rist *
11*9b5730f6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist *
13*9b5730f6SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist * KIND, either express or implied. See the License for the
17*9b5730f6SAndrew Rist * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist * under the License.
19*9b5730f6SAndrew Rist *
20*9b5730f6SAndrew Rist *************************************************************/
21*9b5730f6SAndrew Rist
22*9b5730f6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include "TSortIndex.hxx"
27cdf0e10cSrcweir #include <algorithm>
28cdf0e10cSrcweir #include <functional>
29cdf0e10cSrcweir
30cdf0e10cSrcweir using namespace connectivity;
31cdf0e10cSrcweir //------------------------------------------------------------------
32cdf0e10cSrcweir /// binary_function Functor object for class OSortIndex::TIntValuePairVector::value_type returntype is bool
33cdf0e10cSrcweir struct TKeyValueFunc : ::std::binary_function<OSortIndex::TIntValuePairVector::value_type,OSortIndex::TIntValuePairVector::value_type,bool>
34cdf0e10cSrcweir {
35cdf0e10cSrcweir OSortIndex* pIndex;
36cdf0e10cSrcweir
TKeyValueFuncTKeyValueFunc37cdf0e10cSrcweir TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex)
38cdf0e10cSrcweir {
39cdf0e10cSrcweir }
40cdf0e10cSrcweir // return false if compared values are equal otherwise true
operator ()TKeyValueFunc41cdf0e10cSrcweir inline bool operator()(const OSortIndex::TIntValuePairVector::value_type& lhs,const OSortIndex::TIntValuePairVector::value_type& rhs) const
42cdf0e10cSrcweir {
43cdf0e10cSrcweir const ::std::vector<OKeyType>& aKeyType = pIndex->getKeyType();
44cdf0e10cSrcweir ::std::vector<OKeyType>::const_iterator aIter = aKeyType.begin();
45cdf0e10cSrcweir for (::std::vector<sal_Int16>::size_type i=0;aIter != aKeyType.end(); ++aIter,++i)
46cdf0e10cSrcweir {
47cdf0e10cSrcweir const bool nGreater = (pIndex->getAscending(i) == SQL_ASC) ? false : true;
48cdf0e10cSrcweir const bool nLess = !nGreater;
49cdf0e10cSrcweir
50cdf0e10cSrcweir // compare depending for type
51cdf0e10cSrcweir switch (*aIter)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir case SQL_ORDERBYKEY_STRING:
54cdf0e10cSrcweir {
55cdf0e10cSrcweir sal_Int32 nRes = lhs.second->getKeyString(i).compareTo(rhs.second->getKeyString(i));
56cdf0e10cSrcweir if (nRes < 0)
57cdf0e10cSrcweir return nLess;
58cdf0e10cSrcweir else if (nRes > 0)
59cdf0e10cSrcweir return nGreater;
60cdf0e10cSrcweir }
61cdf0e10cSrcweir break;
62cdf0e10cSrcweir case SQL_ORDERBYKEY_DOUBLE:
63cdf0e10cSrcweir {
64cdf0e10cSrcweir double d1 = lhs.second->getKeyDouble(i);
65cdf0e10cSrcweir double d2 = rhs.second->getKeyDouble(i);
66cdf0e10cSrcweir
67cdf0e10cSrcweir if (d1 < d2)
68cdf0e10cSrcweir return nLess;
69cdf0e10cSrcweir else if (d1 > d2)
70cdf0e10cSrcweir return nGreater;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir break;
73cdf0e10cSrcweir case SQL_ORDERBYKEY_NONE:
74cdf0e10cSrcweir break;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir }
77cdf0e10cSrcweir
78cdf0e10cSrcweir // know we know that the values are equal
79cdf0e10cSrcweir return false;
80cdf0e10cSrcweir }
81cdf0e10cSrcweir };
82cdf0e10cSrcweir
83cdf0e10cSrcweir // -----------------------------------------------------------------------------
CreateKeySet()84cdf0e10cSrcweir ::vos::ORef<OKeySet> OSortIndex::CreateKeySet()
85cdf0e10cSrcweir {
86cdf0e10cSrcweir Freeze();
87cdf0e10cSrcweir
88cdf0e10cSrcweir ::vos::ORef<OKeySet> pKeySet = new OKeySet();
89cdf0e10cSrcweir pKeySet->get().reserve(m_aKeyValues.size());
90cdf0e10cSrcweir ::std::transform(m_aKeyValues.begin()
91cdf0e10cSrcweir ,m_aKeyValues.end()
92cdf0e10cSrcweir ,::std::back_inserter(pKeySet->get())
93cdf0e10cSrcweir ,::std::select1st<TIntValuePairVector::value_type>());
94cdf0e10cSrcweir pKeySet->setFrozen();
95cdf0e10cSrcweir return pKeySet;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir // -----------------------------------------------------------------------------
OSortIndex(const::std::vector<OKeyType> & _aKeyType,const::std::vector<TAscendingOrder> & _aAscending)98cdf0e10cSrcweir OSortIndex::OSortIndex( const ::std::vector<OKeyType>& _aKeyType,
99cdf0e10cSrcweir const ::std::vector<TAscendingOrder>& _aAscending)
100cdf0e10cSrcweir :m_aKeyType(_aKeyType)
101cdf0e10cSrcweir ,m_aAscending(_aAscending)
102cdf0e10cSrcweir ,m_bFrozen(sal_False)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir }
105cdf0e10cSrcweir //------------------------------------------------------------------
~OSortIndex()106cdf0e10cSrcweir OSortIndex::~OSortIndex()
107cdf0e10cSrcweir {
108cdf0e10cSrcweir }
109cdf0e10cSrcweir //------------------------------------------------------------------
AddKeyValue(OKeyValue * pKeyValue)110cdf0e10cSrcweir void OSortIndex::AddKeyValue(OKeyValue * pKeyValue)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir OSL_ENSURE(pKeyValue,"Can not be null here!");
113cdf0e10cSrcweir if(m_bFrozen)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),NULL));
116cdf0e10cSrcweir delete pKeyValue;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir else
119cdf0e10cSrcweir m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),pKeyValue));
120cdf0e10cSrcweir }
121cdf0e10cSrcweir
122cdf0e10cSrcweir
123cdf0e10cSrcweir //------------------------------------------------------------------
Freeze()124cdf0e10cSrcweir void OSortIndex::Freeze()
125cdf0e10cSrcweir {
126cdf0e10cSrcweir OSL_ENSURE(! m_bFrozen,"OSortIndex::Freeze: already frozen!");
127cdf0e10cSrcweir // Sortierung:
128cdf0e10cSrcweir if (m_aKeyType[0] != SQL_ORDERBYKEY_NONE)
129cdf0e10cSrcweir // we will sort ourself when the first keyType say so
130cdf0e10cSrcweir ::std::sort(m_aKeyValues.begin(),m_aKeyValues.end(),TKeyValueFunc(this));
131cdf0e10cSrcweir
132cdf0e10cSrcweir TIntValuePairVector::iterator aIter = m_aKeyValues.begin();
133cdf0e10cSrcweir for(;aIter != m_aKeyValues.end();++aIter)
134cdf0e10cSrcweir {
135cdf0e10cSrcweir delete aIter->second;
136cdf0e10cSrcweir aIter->second = NULL;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir
139cdf0e10cSrcweir m_bFrozen = sal_True;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir
142cdf0e10cSrcweir //------------------------------------------------------------------
GetValue(sal_Int32 nPos) const143cdf0e10cSrcweir sal_Int32 OSortIndex::GetValue(sal_Int32 nPos) const
144cdf0e10cSrcweir {
145cdf0e10cSrcweir OSL_ENSURE(nPos > 0,"OSortIndex::GetValue: nPos == 0");
146cdf0e10cSrcweir OSL_ENSURE((size_t)nPos <= m_aKeyValues.size(),"OSortIndex::GetValue: Zugriff ausserhalb der Array-Grenzen");
147cdf0e10cSrcweir
148cdf0e10cSrcweir if (!m_bFrozen && m_aKeyType[0] != SQL_ORDERBYKEY_NONE)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir OSL_ASSERT("OSortIndex::GetValue: Invalid use of index!");
151cdf0e10cSrcweir return 0;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir return m_aKeyValues[nPos-1].first;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir // -----------------------------------------------------------------------------
OKeyValue()156cdf0e10cSrcweir OKeyValue::OKeyValue()
157cdf0e10cSrcweir {
158cdf0e10cSrcweir }
159cdf0e10cSrcweir // -----------------------------------------------------------------------------
OKeyValue(sal_Int32 nVal)160cdf0e10cSrcweir OKeyValue::OKeyValue(sal_Int32 nVal)
161cdf0e10cSrcweir : m_nValue(nVal)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir }
164cdf0e10cSrcweir // -----------------------------------------------------------------------------
~OKeyValue()165cdf0e10cSrcweir OKeyValue::~OKeyValue()
166cdf0e10cSrcweir {
167cdf0e10cSrcweir }
168cdf0e10cSrcweir // -----------------------------------------------------------------------------
createKeyValue(sal_Int32 _nVal)169cdf0e10cSrcweir OKeyValue* OKeyValue::createKeyValue(sal_Int32 _nVal)
170cdf0e10cSrcweir {
171cdf0e10cSrcweir return new OKeyValue(_nVal);
172cdf0e10cSrcweir }
173cdf0e10cSrcweir // -----------------------------------------------------------------------------
174cdf0e10cSrcweir
175