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_connectivity.hxx"
26 #include "TSortIndex.hxx"
27 #include <algorithm>
28 #include <functional>
29
30 using namespace connectivity;
31 //------------------------------------------------------------------
32 /// binary_function Functor object for class OSortIndex::TIntValuePairVector::value_type returntype is bool
33 struct TKeyValueFunc : ::std::binary_function<OSortIndex::TIntValuePairVector::value_type,OSortIndex::TIntValuePairVector::value_type,bool>
34 {
35 OSortIndex* pIndex;
36
TKeyValueFuncTKeyValueFunc37 TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex)
38 {
39 }
40 // return false if compared values are equal otherwise true
operator ()TKeyValueFunc41 inline bool operator()(const OSortIndex::TIntValuePairVector::value_type& lhs,const OSortIndex::TIntValuePairVector::value_type& rhs) const
42 {
43 const ::std::vector<OKeyType>& aKeyType = pIndex->getKeyType();
44 ::std::vector<OKeyType>::const_iterator aIter = aKeyType.begin();
45 for (::std::vector<sal_Int16>::size_type i=0;aIter != aKeyType.end(); ++aIter,++i)
46 {
47 const bool nGreater = (pIndex->getAscending(i) == SQL_ASC) ? false : true;
48 const bool nLess = !nGreater;
49
50 // compare depending for type
51 switch (*aIter)
52 {
53 case SQL_ORDERBYKEY_STRING:
54 {
55 sal_Int32 nRes = lhs.second->getKeyString(i).compareTo(rhs.second->getKeyString(i));
56 if (nRes < 0)
57 return nLess;
58 else if (nRes > 0)
59 return nGreater;
60 }
61 break;
62 case SQL_ORDERBYKEY_DOUBLE:
63 {
64 double d1 = lhs.second->getKeyDouble(i);
65 double d2 = rhs.second->getKeyDouble(i);
66
67 if (d1 < d2)
68 return nLess;
69 else if (d1 > d2)
70 return nGreater;
71 }
72 break;
73 case SQL_ORDERBYKEY_NONE:
74 break;
75 }
76 }
77
78 // know we know that the values are equal
79 return false;
80 }
81 };
82
83 // -----------------------------------------------------------------------------
CreateKeySet()84 ::vos::ORef<OKeySet> OSortIndex::CreateKeySet()
85 {
86 Freeze();
87
88 ::vos::ORef<OKeySet> pKeySet = new OKeySet();
89 pKeySet->get().reserve(m_aKeyValues.size());
90 ::std::transform(m_aKeyValues.begin()
91 ,m_aKeyValues.end()
92 ,::std::back_inserter(pKeySet->get())
93 ,::std::select1st<TIntValuePairVector::value_type>());
94 pKeySet->setFrozen();
95 return pKeySet;
96 }
97 // -----------------------------------------------------------------------------
OSortIndex(const::std::vector<OKeyType> & _aKeyType,const::std::vector<TAscendingOrder> & _aAscending)98 OSortIndex::OSortIndex( const ::std::vector<OKeyType>& _aKeyType,
99 const ::std::vector<TAscendingOrder>& _aAscending)
100 :m_aKeyType(_aKeyType)
101 ,m_aAscending(_aAscending)
102 ,m_bFrozen(sal_False)
103 {
104 }
105 //------------------------------------------------------------------
~OSortIndex()106 OSortIndex::~OSortIndex()
107 {
108 }
109 //------------------------------------------------------------------
AddKeyValue(OKeyValue * pKeyValue)110 void OSortIndex::AddKeyValue(OKeyValue * pKeyValue)
111 {
112 OSL_ENSURE(pKeyValue,"Can not be null here!");
113 if(m_bFrozen)
114 {
115 m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),NULL));
116 delete pKeyValue;
117 }
118 else
119 m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),pKeyValue));
120 }
121
122
123 //------------------------------------------------------------------
Freeze()124 void OSortIndex::Freeze()
125 {
126 OSL_ENSURE(! m_bFrozen,"OSortIndex::Freeze: already frozen!");
127 // Sortierung:
128 if (m_aKeyType[0] != SQL_ORDERBYKEY_NONE)
129 // we will sort ourself when the first keyType say so
130 ::std::sort(m_aKeyValues.begin(),m_aKeyValues.end(),TKeyValueFunc(this));
131
132 TIntValuePairVector::iterator aIter = m_aKeyValues.begin();
133 for(;aIter != m_aKeyValues.end();++aIter)
134 {
135 delete aIter->second;
136 aIter->second = NULL;
137 }
138
139 m_bFrozen = sal_True;
140 }
141
142 //------------------------------------------------------------------
GetValue(sal_Int32 nPos) const143 sal_Int32 OSortIndex::GetValue(sal_Int32 nPos) const
144 {
145 OSL_ENSURE(nPos > 0,"OSortIndex::GetValue: nPos == 0");
146 OSL_ENSURE((size_t)nPos <= m_aKeyValues.size(),"OSortIndex::GetValue: Zugriff ausserhalb der Array-Grenzen");
147
148 if (!m_bFrozen && m_aKeyType[0] != SQL_ORDERBYKEY_NONE)
149 {
150 OSL_ASSERT("OSortIndex::GetValue: Invalid use of index!");
151 return 0;
152 }
153 return m_aKeyValues[nPos-1].first;
154 }
155 // -----------------------------------------------------------------------------
OKeyValue()156 OKeyValue::OKeyValue()
157 {
158 }
159 // -----------------------------------------------------------------------------
OKeyValue(sal_Int32 nVal)160 OKeyValue::OKeyValue(sal_Int32 nVal)
161 : m_nValue(nVal)
162 {
163 }
164 // -----------------------------------------------------------------------------
~OKeyValue()165 OKeyValue::~OKeyValue()
166 {
167 }
168 // -----------------------------------------------------------------------------
createKeyValue(sal_Int32 _nVal)169 OKeyValue* OKeyValue::createKeyValue(sal_Int32 _nVal)
170 {
171 return new OKeyValue(_nVal);
172 }
173 // -----------------------------------------------------------------------------
174
175