LCOV - code coverage report
Current view: top level - libreoffice/connectivity/source/commontools - TSortIndex.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 45 59 76.3 %
Date: 2012-12-17 Functions: 9 10 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "TSortIndex.hxx"
      21             : #include <algorithm>
      22             : #include <iterator>
      23             : 
      24             : #include <o3tl/compat_functional.hxx>
      25             : 
      26             : using namespace connectivity;
      27             : //------------------------------------------------------------------
      28             : /// binary_function Functor object for class OSortIndex::TIntValuePairVector::value_type returntype is bool
      29             : struct TKeyValueFunc : ::std::binary_function<OSortIndex::TIntValuePairVector::value_type,OSortIndex::TIntValuePairVector::value_type,bool>
      30             : {
      31             :     OSortIndex* pIndex;
      32             : 
      33           2 :     TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex)
      34             :     {
      35           2 :     }
      36             :     // return false if compared values are equal otherwise true
      37          52 :     inline bool operator()(const OSortIndex::TIntValuePairVector::value_type& lhs,const OSortIndex::TIntValuePairVector::value_type& rhs)   const
      38             :     {
      39          52 :         const ::std::vector<OKeyType>& aKeyType = pIndex->getKeyType();
      40          52 :         ::std::vector<OKeyType>::const_iterator aIter = aKeyType.begin();
      41          52 :         for (::std::vector<sal_Int16>::size_type i=0;aIter != aKeyType.end(); ++aIter,++i)
      42             :         {
      43          52 :             const bool nGreater = (pIndex->getAscending(i) == SQL_ASC) ? false : true;
      44          52 :             const bool nLess = !nGreater;
      45             : 
      46             :             // compare depending for type
      47          52 :             switch (*aIter)
      48             :             {
      49             :                 case SQL_ORDERBYKEY_STRING:
      50             :                 {
      51          52 :                     sal_Int32 nRes = lhs.second->getKeyString(i).compareTo(rhs.second->getKeyString(i));
      52          52 :                     if (nRes < 0)
      53          16 :                         return nLess;
      54          36 :                     else if (nRes > 0)
      55          36 :                         return nGreater;
      56             :                 }
      57           0 :                 break;
      58             :                 case SQL_ORDERBYKEY_DOUBLE:
      59             :                 {
      60           0 :                     double d1 = lhs.second->getKeyDouble(i);
      61           0 :                     double d2 = rhs.second->getKeyDouble(i);
      62             : 
      63           0 :                     if (d1 < d2)
      64           0 :                         return nLess;
      65           0 :                     else if (d1 > d2)
      66           0 :                         return nGreater;
      67             :                 }
      68           0 :                 break;
      69             :                 case SQL_ORDERBYKEY_NONE:
      70           0 :                     break;
      71             :             }
      72             :         }
      73             : 
      74             :         // know we know that the values are equal
      75           0 :         return false;
      76             :     }
      77             : };
      78             : 
      79             : // -----------------------------------------------------------------------------
      80           2 : ::rtl::Reference<OKeySet> OSortIndex::CreateKeySet()
      81             : {
      82           2 :     Freeze();
      83             : 
      84           2 :     ::rtl::Reference<OKeySet> pKeySet = new OKeySet();
      85           2 :     pKeySet->get().reserve(m_aKeyValues.size());
      86             :     ::std::transform(m_aKeyValues.begin()
      87             :                     ,m_aKeyValues.end()
      88           2 :                     ,::std::back_inserter(pKeySet->get())
      89           2 :                     ,::o3tl::select1st<TIntValuePairVector::value_type>());
      90           2 :     pKeySet->setFrozen();
      91           2 :     return pKeySet;
      92             : }
      93             : // -----------------------------------------------------------------------------
      94           2 : OSortIndex::OSortIndex( const ::std::vector<OKeyType>& _aKeyType,
      95             :                         const ::std::vector<TAscendingOrder>& _aAscending)
      96             :     :m_aKeyType(_aKeyType)
      97             :     ,m_aAscending(_aAscending)
      98           2 :     ,m_bFrozen(sal_False)
      99             : {
     100           2 : }
     101             : //------------------------------------------------------------------
     102           0 : OSortIndex::~OSortIndex()
     103             : {
     104           0 : }
     105             : //------------------------------------------------------------------
     106          20 : void OSortIndex::AddKeyValue(OKeyValue * pKeyValue)
     107             : {
     108             :     OSL_ENSURE(pKeyValue,"Can not be null here!");
     109          20 :     if(m_bFrozen)
     110             :     {
     111           0 :         m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),(OKeyValue *)NULL));
     112           0 :         delete pKeyValue;
     113             :     }
     114             :     else
     115          20 :         m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),pKeyValue));
     116          20 : }
     117             : 
     118             : 
     119             : //------------------------------------------------------------------
     120           2 : void OSortIndex::Freeze()
     121             : {
     122             :     OSL_ENSURE(! m_bFrozen,"OSortIndex::Freeze: already frozen!");
     123             :     // Sortierung:
     124           2 :     if (m_aKeyType[0] != SQL_ORDERBYKEY_NONE)
     125             :         // we will sort ourself when the first keyType say so
     126           2 :         ::std::sort(m_aKeyValues.begin(),m_aKeyValues.end(),TKeyValueFunc(this));
     127             : 
     128           2 :     TIntValuePairVector::iterator aIter = m_aKeyValues.begin();
     129          22 :     for(;aIter != m_aKeyValues.end();++aIter)
     130             :     {
     131          20 :         delete aIter->second;
     132          20 :         aIter->second = NULL;
     133             :     }
     134             : 
     135           2 :     m_bFrozen = sal_True;
     136           2 : }
     137             : 
     138             : // -----------------------------------------------------------------------------
     139          20 : OKeyValue::OKeyValue(sal_Int32 nVal)
     140          20 : : m_nValue(nVal)
     141             : {
     142          20 : }
     143             : // -----------------------------------------------------------------------------
     144          20 : OKeyValue::~OKeyValue()
     145             : {
     146          20 : }
     147             : // -----------------------------------------------------------------------------
     148          20 : OKeyValue* OKeyValue::createKeyValue(sal_Int32 _nVal)
     149             : {
     150          20 :     return new OKeyValue(_nVal);
     151             : }
     152             : // -----------------------------------------------------------------------------
     153             : 
     154             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10