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