1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 30*cdf0e10cSrcweir #include "TSortIndex.hxx" 31*cdf0e10cSrcweir #include <algorithm> 32*cdf0e10cSrcweir #include <functional> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir using namespace connectivity; 35*cdf0e10cSrcweir //------------------------------------------------------------------ 36*cdf0e10cSrcweir /// binary_function Functor object for class OSortIndex::TIntValuePairVector::value_type returntype is bool 37*cdf0e10cSrcweir struct TKeyValueFunc : ::std::binary_function<OSortIndex::TIntValuePairVector::value_type,OSortIndex::TIntValuePairVector::value_type,bool> 38*cdf0e10cSrcweir { 39*cdf0e10cSrcweir OSortIndex* pIndex; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex) 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir } 44*cdf0e10cSrcweir // return false if compared values are equal otherwise true 45*cdf0e10cSrcweir inline bool operator()(const OSortIndex::TIntValuePairVector::value_type& lhs,const OSortIndex::TIntValuePairVector::value_type& rhs) const 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir const ::std::vector<OKeyType>& aKeyType = pIndex->getKeyType(); 48*cdf0e10cSrcweir ::std::vector<OKeyType>::const_iterator aIter = aKeyType.begin(); 49*cdf0e10cSrcweir for (::std::vector<sal_Int16>::size_type i=0;aIter != aKeyType.end(); ++aIter,++i) 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir const bool nGreater = (pIndex->getAscending(i) == SQL_ASC) ? false : true; 52*cdf0e10cSrcweir const bool nLess = !nGreater; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir // compare depending for type 55*cdf0e10cSrcweir switch (*aIter) 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir case SQL_ORDERBYKEY_STRING: 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir sal_Int32 nRes = lhs.second->getKeyString(i).compareTo(rhs.second->getKeyString(i)); 60*cdf0e10cSrcweir if (nRes < 0) 61*cdf0e10cSrcweir return nLess; 62*cdf0e10cSrcweir else if (nRes > 0) 63*cdf0e10cSrcweir return nGreater; 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir break; 66*cdf0e10cSrcweir case SQL_ORDERBYKEY_DOUBLE: 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir double d1 = lhs.second->getKeyDouble(i); 69*cdf0e10cSrcweir double d2 = rhs.second->getKeyDouble(i); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir if (d1 < d2) 72*cdf0e10cSrcweir return nLess; 73*cdf0e10cSrcweir else if (d1 > d2) 74*cdf0e10cSrcweir return nGreater; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir break; 77*cdf0e10cSrcweir case SQL_ORDERBYKEY_NONE: 78*cdf0e10cSrcweir break; 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir // know we know that the values are equal 83*cdf0e10cSrcweir return false; 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir }; 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 88*cdf0e10cSrcweir ::vos::ORef<OKeySet> OSortIndex::CreateKeySet() 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir Freeze(); 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir ::vos::ORef<OKeySet> pKeySet = new OKeySet(); 93*cdf0e10cSrcweir pKeySet->get().reserve(m_aKeyValues.size()); 94*cdf0e10cSrcweir ::std::transform(m_aKeyValues.begin() 95*cdf0e10cSrcweir ,m_aKeyValues.end() 96*cdf0e10cSrcweir ,::std::back_inserter(pKeySet->get()) 97*cdf0e10cSrcweir ,::std::select1st<TIntValuePairVector::value_type>()); 98*cdf0e10cSrcweir pKeySet->setFrozen(); 99*cdf0e10cSrcweir return pKeySet; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 102*cdf0e10cSrcweir OSortIndex::OSortIndex( const ::std::vector<OKeyType>& _aKeyType, 103*cdf0e10cSrcweir const ::std::vector<TAscendingOrder>& _aAscending) 104*cdf0e10cSrcweir :m_aKeyType(_aKeyType) 105*cdf0e10cSrcweir ,m_aAscending(_aAscending) 106*cdf0e10cSrcweir ,m_bFrozen(sal_False) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir //------------------------------------------------------------------ 110*cdf0e10cSrcweir OSortIndex::~OSortIndex() 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir //------------------------------------------------------------------ 114*cdf0e10cSrcweir void OSortIndex::AddKeyValue(OKeyValue * pKeyValue) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir OSL_ENSURE(pKeyValue,"Can not be null here!"); 117*cdf0e10cSrcweir if(m_bFrozen) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),NULL)); 120*cdf0e10cSrcweir delete pKeyValue; 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir else 123*cdf0e10cSrcweir m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),pKeyValue)); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir //------------------------------------------------------------------ 128*cdf0e10cSrcweir void OSortIndex::Freeze() 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir OSL_ENSURE(! m_bFrozen,"OSortIndex::Freeze: already frozen!"); 131*cdf0e10cSrcweir // Sortierung: 132*cdf0e10cSrcweir if (m_aKeyType[0] != SQL_ORDERBYKEY_NONE) 133*cdf0e10cSrcweir // we will sort ourself when the first keyType say so 134*cdf0e10cSrcweir ::std::sort(m_aKeyValues.begin(),m_aKeyValues.end(),TKeyValueFunc(this)); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir TIntValuePairVector::iterator aIter = m_aKeyValues.begin(); 137*cdf0e10cSrcweir for(;aIter != m_aKeyValues.end();++aIter) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir delete aIter->second; 140*cdf0e10cSrcweir aIter->second = NULL; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir m_bFrozen = sal_True; 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir //------------------------------------------------------------------ 147*cdf0e10cSrcweir sal_Int32 OSortIndex::GetValue(sal_Int32 nPos) const 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir OSL_ENSURE(nPos > 0,"OSortIndex::GetValue: nPos == 0"); 150*cdf0e10cSrcweir OSL_ENSURE((size_t)nPos <= m_aKeyValues.size(),"OSortIndex::GetValue: Zugriff ausserhalb der Array-Grenzen"); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir if (!m_bFrozen && m_aKeyType[0] != SQL_ORDERBYKEY_NONE) 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir OSL_ASSERT("OSortIndex::GetValue: Invalid use of index!"); 155*cdf0e10cSrcweir return 0; 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir return m_aKeyValues[nPos-1].first; 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 160*cdf0e10cSrcweir OKeyValue::OKeyValue() 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 164*cdf0e10cSrcweir OKeyValue::OKeyValue(sal_Int32 nVal) 165*cdf0e10cSrcweir : m_nValue(nVal) 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 169*cdf0e10cSrcweir OKeyValue::~OKeyValue() 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 173*cdf0e10cSrcweir OKeyValue* OKeyValue::createKeyValue(sal_Int32 _nVal) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir return new OKeyValue(_nVal); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 178*cdf0e10cSrcweir 179