Branch data 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 : :
23 : : #include <o3tl/compat_functional.hxx>
24 : :
25 : : using namespace connectivity;
26 : : //------------------------------------------------------------------
27 : : /// binary_function Functor object for class OSortIndex::TIntValuePairVector::value_type returntype is bool
28 : : struct TKeyValueFunc : ::std::binary_function<OSortIndex::TIntValuePairVector::value_type,OSortIndex::TIntValuePairVector::value_type,bool>
29 : : {
30 : : OSortIndex* pIndex;
31 : :
32 : 2 : TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex)
33 : : {
34 : 2 : }
35 : : // return false if compared values are equal otherwise true
36 : 126 : inline bool operator()(const OSortIndex::TIntValuePairVector::value_type& lhs,const OSortIndex::TIntValuePairVector::value_type& rhs) const
37 : : {
38 : 126 : const ::std::vector<OKeyType>& aKeyType = pIndex->getKeyType();
39 : 126 : ::std::vector<OKeyType>::const_iterator aIter = aKeyType.begin();
40 [ # # ][ + - ]: 126 : for (::std::vector<sal_Int16>::size_type i=0;aIter != aKeyType.end(); ++aIter,++i)
[ + - ]
41 : : {
42 [ + - ]: 126 : const bool nGreater = (pIndex->getAscending(i) == SQL_ASC) ? false : true;
43 : 126 : const bool nLess = !nGreater;
44 : :
45 : : // compare depending for type
46 [ + - ]: 126 : switch (*aIter)
[ + - - - ]
47 : : {
48 : : case SQL_ORDERBYKEY_STRING:
49 : : {
50 [ + - ][ + - ]: 126 : sal_Int32 nRes = lhs.second->getKeyString(i).compareTo(rhs.second->getKeyString(i));
51 [ + + ]: 126 : if (nRes < 0)
52 : 62 : return nLess;
53 [ + - ]: 64 : else if (nRes > 0)
54 : 64 : return nGreater;
55 : : }
56 : 0 : break;
57 : : case SQL_ORDERBYKEY_DOUBLE:
58 : : {
59 [ # # ]: 0 : double d1 = lhs.second->getKeyDouble(i);
60 [ # # ]: 0 : double d2 = rhs.second->getKeyDouble(i);
61 : :
62 [ # # ]: 0 : if (d1 < d2)
63 : 0 : return nLess;
64 [ # # ]: 0 : else if (d1 > d2)
65 : 0 : return nGreater;
66 : : }
67 : 0 : break;
68 : : case SQL_ORDERBYKEY_NONE:
69 : 0 : break;
70 : : }
71 : : }
72 : :
73 : : // know we know that the values are equal
74 : 126 : return false;
75 : : }
76 : : };
77 : :
78 : : // -----------------------------------------------------------------------------
79 : 2 : ::rtl::Reference<OKeySet> OSortIndex::CreateKeySet()
80 : : {
81 : 2 : Freeze();
82 : :
83 [ + - ][ + - ]: 2 : ::rtl::Reference<OKeySet> pKeySet = new OKeySet();
84 [ + - ]: 2 : pKeySet->get().reserve(m_aKeyValues.size());
85 : : ::std::transform(m_aKeyValues.begin()
86 : : ,m_aKeyValues.end()
87 : 2 : ,::std::back_inserter(pKeySet->get())
88 [ + - ][ + - ]: 2 : ,::o3tl::select1st<TIntValuePairVector::value_type>());
89 : 2 : pKeySet->setFrozen();
90 : 2 : return pKeySet;
91 : : }
92 : : // -----------------------------------------------------------------------------
93 : 2 : OSortIndex::OSortIndex( const ::std::vector<OKeyType>& _aKeyType,
94 : : const ::std::vector<TAscendingOrder>& _aAscending)
95 : : :m_aKeyType(_aKeyType)
96 : : ,m_aAscending(_aAscending)
97 [ + - ][ + - ]: 2 : ,m_bFrozen(sal_False)
98 : : {
99 : 2 : }
100 : : //------------------------------------------------------------------
101 : 2 : OSortIndex::~OSortIndex()
102 : : {
103 : 2 : }
104 : : //------------------------------------------------------------------
105 : 40 : void OSortIndex::AddKeyValue(OKeyValue * pKeyValue)
106 : : {
107 : : OSL_ENSURE(pKeyValue,"Can not be null here!");
108 [ - + ]: 40 : if(m_bFrozen)
109 : : {
110 [ # # ][ # # ]: 0 : m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),NULL));
111 [ # # ]: 0 : delete pKeyValue;
112 : : }
113 : : else
114 [ + - ][ + - ]: 40 : m_aKeyValues.push_back(TIntValuePairVector::value_type(pKeyValue->getValue(),pKeyValue));
115 : 40 : }
116 : :
117 : :
118 : : //------------------------------------------------------------------
119 : 2 : void OSortIndex::Freeze()
120 : : {
121 : : OSL_ENSURE(! m_bFrozen,"OSortIndex::Freeze: already frozen!");
122 : : // Sortierung:
123 [ + - ][ + - ]: 2 : if (m_aKeyType[0] != SQL_ORDERBYKEY_NONE)
124 : : // we will sort ourself when the first keyType say so
125 [ + - ]: 2 : ::std::sort(m_aKeyValues.begin(),m_aKeyValues.end(),TKeyValueFunc(this));
126 : :
127 : 2 : TIntValuePairVector::iterator aIter = m_aKeyValues.begin();
128 [ + - ][ + - ]: 42 : for(;aIter != m_aKeyValues.end();++aIter)
[ + + ]
129 : : {
130 [ + - ][ + - ]: 40 : delete aIter->second;
[ + - ]
131 [ + - ]: 40 : aIter->second = NULL;
132 : : }
133 : :
134 : 2 : m_bFrozen = sal_True;
135 : 2 : }
136 : :
137 : : // -----------------------------------------------------------------------------
138 : 40 : OKeyValue::OKeyValue(sal_Int32 nVal)
139 : 40 : : m_nValue(nVal)
140 : : {
141 : 40 : }
142 : : // -----------------------------------------------------------------------------
143 : 40 : OKeyValue::~OKeyValue()
144 : : {
145 : 40 : }
146 : : // -----------------------------------------------------------------------------
147 : 40 : OKeyValue* OKeyValue::createKeyValue(sal_Int32 _nVal)
148 : : {
149 [ + - ]: 40 : return new OKeyValue(_nVal);
150 : : }
151 : : // -----------------------------------------------------------------------------
152 : :
153 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|