1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_connectivity.hxx" 30 #include "TSortIndex.hxx" 31 #include <algorithm> 32 #include <functional> 33 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 41 TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex) 42 { 43 } 44 // return false if compared values are equal otherwise true 45 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 // ----------------------------------------------------------------------------- 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 // ----------------------------------------------------------------------------- 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 //------------------------------------------------------------------ 110 OSortIndex::~OSortIndex() 111 { 112 } 113 //------------------------------------------------------------------ 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 //------------------------------------------------------------------ 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 //------------------------------------------------------------------ 147 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 // ----------------------------------------------------------------------------- 160 OKeyValue::OKeyValue() 161 { 162 } 163 // ----------------------------------------------------------------------------- 164 OKeyValue::OKeyValue(sal_Int32 nVal) 165 : m_nValue(nVal) 166 { 167 } 168 // ----------------------------------------------------------------------------- 169 OKeyValue::~OKeyValue() 170 { 171 } 172 // ----------------------------------------------------------------------------- 173 OKeyValue* OKeyValue::createKeyValue(sal_Int32 _nVal) 174 { 175 return new OKeyValue(_nVal); 176 } 177 // ----------------------------------------------------------------------------- 178 179