update dev300-m58
[ooovba.git] / connectivity / source / commontools / TSortIndex.cxx
blob5809d8e59e188b5673ee65f8d84120485e2d44f3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TSortIndex.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "TSortIndex.hxx"
34 #include <algorithm>
35 #include <functional>
37 using namespace connectivity;
38 //------------------------------------------------------------------
39 /// binary_function Functor object for class OSortIndex::TIntValuePairVector::value_type returntype is bool
40 struct TKeyValueFunc : ::std::binary_function<OSortIndex::TIntValuePairVector::value_type,OSortIndex::TIntValuePairVector::value_type,bool>
42 OSortIndex* pIndex;
44 TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex)
47 // return false if compared values are equal otherwise true
48 inline bool operator()(const OSortIndex::TIntValuePairVector::value_type& lhs,const OSortIndex::TIntValuePairVector::value_type& rhs) const
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)
54 const bool nGreater = (pIndex->getAscending(i) == SQL_ASC) ? false : true;
55 const bool nLess = !nGreater;
57 // compare depending for type
58 switch (*aIter)
60 case SQL_ORDERBYKEY_STRING:
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;
68 break;
69 case SQL_ORDERBYKEY_DOUBLE:
71 double d1 = lhs.second->getKeyDouble(i);
72 double d2 = rhs.second->getKeyDouble(i);
74 if (d1 < d2)
75 return nLess;
76 else if (d1 > d2)
77 return nGreater;
79 break;
80 case SQL_ORDERBYKEY_NONE:
81 break;
85 // know we know that the values are equal
86 return false;
90 // -----------------------------------------------------------------------------
91 ::vos::ORef<OKeySet> OSortIndex::CreateKeySet()
93 Freeze();
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;
104 // -----------------------------------------------------------------------------
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)
112 //------------------------------------------------------------------
113 OSortIndex::~OSortIndex()
116 //------------------------------------------------------------------
117 void OSortIndex::AddKeyValue(OKeyValue * pKeyValue)
119 OSL_ENSURE(pKeyValue,"Can not be null here!");
120 if(m_bFrozen)
122 m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),NULL));
123 delete pKeyValue;
125 else
126 m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),pKeyValue));
130 //------------------------------------------------------------------
131 void OSortIndex::Freeze()
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));
139 TIntValuePairVector::iterator aIter = m_aKeyValues.begin();
140 for(;aIter != m_aKeyValues.end();++aIter)
142 delete aIter->second;
143 aIter->second = NULL;
146 m_bFrozen = sal_True;
149 //------------------------------------------------------------------
150 sal_Int32 OSortIndex::GetValue(sal_Int32 nPos) const
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");
155 if (!m_bFrozen && m_aKeyType[0] != SQL_ORDERBYKEY_NONE)
157 OSL_ASSERT("OSortIndex::GetValue: Invalid use of index!");
158 return 0;
160 return m_aKeyValues[nPos-1].first;
162 // -----------------------------------------------------------------------------
163 OKeyValue::OKeyValue()
166 // -----------------------------------------------------------------------------
167 OKeyValue::OKeyValue(sal_Int32 nVal)
168 : m_nValue(nVal)
171 // -----------------------------------------------------------------------------
172 OKeyValue::~OKeyValue()
175 // -----------------------------------------------------------------------------
176 OKeyValue* OKeyValue::createKeyValue(sal_Int32 _nVal)
178 return new OKeyValue(_nVal);
180 // -----------------------------------------------------------------------------