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