LibreOffice Module connectivity (master) 1
TSortIndex.cxx
Go to the documentation of this file.
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#include <o3tl/functional.hxx>
24
25using namespace connectivity;
26
27namespace {
28
30struct TKeyValueFunc
31{
32 OSortIndex* pIndex;
33
34 explicit TKeyValueFunc(OSortIndex* _pIndex) : pIndex(_pIndex)
35 {
36 }
37 // return false if compared values are equal otherwise true
38 bool operator()(const OSortIndex::TIntValuePairVector::value_type& lhs,const OSortIndex::TIntValuePairVector::value_type& rhs) const
39 {
40 const std::vector<OKeyType>& aKeyType = pIndex->getKeyType();
41 size_t i = 0;
42 for (auto const& elem : aKeyType)
43 {
44 const bool bGreater = pIndex->getAscending(i) != TAscendingOrder::ASC;
45 const bool bLess = !bGreater;
46
47 // compare depending for type
48 switch (elem)
49 {
50 case OKeyType::String:
51 {
52 sal_Int32 nRes = lhs.second->getKeyString(i).compareTo(rhs.second->getKeyString(i));
53 if (nRes < 0)
54 return bLess;
55 else if (nRes > 0)
56 return bGreater;
57 }
58 break;
59 case OKeyType::Double:
60 {
61 double d1 = lhs.second->getKeyDouble(i);
62 double d2 = rhs.second->getKeyDouble(i);
63
64 if (d1 < d2)
65 return bLess;
66 else if (d1 > d2)
67 return bGreater;
68 }
69 break;
70 case OKeyType::NONE:
71 break;
72 }
73 ++i;
74 }
75
76 // know we know that the values are equal
77 return false;
78 }
79};
80
81}
82
83::rtl::Reference<OKeySet> OSortIndex::CreateKeySet()
84{
85 Freeze();
86
87 ::rtl::Reference<OKeySet> pKeySet = new OKeySet();
88 pKeySet->reserve(m_aKeyValues.size());
89 std::transform(m_aKeyValues.begin()
90 ,m_aKeyValues.end()
91 ,std::back_inserter(*pKeySet)
93 pKeySet->setFrozen();
94 return pKeySet;
95}
96
97OSortIndex::OSortIndex( std::vector<OKeyType>&& _aKeyType,
98 std::vector<TAscendingOrder>&& _aAscending)
99 :m_aKeyType(std::move(_aKeyType))
100 ,m_aAscending(std::move(_aAscending))
101 ,m_bFrozen(false)
102{
103}
104
106{
107}
108
109void OSortIndex::AddKeyValue(std::unique_ptr<OKeyValue> pKeyValue)
110{
111 assert(pKeyValue && "Can not be null here!");
112 if(m_bFrozen)
113 {
114 m_aKeyValues.push_back({pKeyValue->getValue(),nullptr});
115 }
116 else
117 m_aKeyValues.push_back({pKeyValue->getValue(),std::move(pKeyValue)});
118}
119
121{
122 OSL_ENSURE(! m_bFrozen,"OSortIndex::Freeze: already frozen!");
123 // sorting:
124 if (m_aKeyType[0] != OKeyType::NONE)
125 // we will sort ourself when the first keyType say so
126 std::sort(m_aKeyValues.begin(),m_aKeyValues.end(),TKeyValueFunc(this));
127
128 for (auto & keyValue : m_aKeyValues)
129 {
130 keyValue.second.reset();
131 }
132
133 m_bFrozen = true;
134}
135
136
137OKeyValue::OKeyValue(sal_Int32 nVal)
138: m_nValue(nVal)
139{
140}
141
143{
144}
145
146std::unique_ptr<OKeyValue> OKeyValue::createKeyValue(sal_Int32 _nVal)
147{
148 return std::unique_ptr<OKeyValue>(new OKeyValue(_nVal));
149}
150
151
152/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
The class OKeySet is a refcountable vector which also has a state.
Definition: TSortIndex.hxx:99
OKeyValue(sal_Int32 nVal)
Definition: TSortIndex.cxx:137
static std::unique_ptr< OKeyValue > createKeyValue(sal_Int32 nVal)
Definition: TSortIndex.cxx:146
The class OSortIndex can be used to implement a sorted index.
Definition: TSortIndex.hxx:47
TKeyTypeVector m_aKeyType
Definition: TSortIndex.hxx:54
void Freeze()
Freeze freezes the sortindex so that new values could only be appended by their value.
Definition: TSortIndex.cxx:120
OSortIndex(std::vector< OKeyType > &&_aKeyType, std::vector< TAscendingOrder > &&_aAscending)
Definition: TSortIndex.cxx:97
TIntValuePairVector m_aKeyValues
Definition: TSortIndex.hxx:53
const std::vector< OKeyType > & getKeyType() const
Definition: TSortIndex.hxx:86
TAscendingOrder getAscending(std::vector< TAscendingOrder >::size_type _nPos) const
Definition: TSortIndex.hxx:87
void AddKeyValue(std::unique_ptr< OKeyValue > pKeyValue)
AddKeyValue appends a new value.
Definition: TSortIndex.cxx:109
Type m_aKeyType
int i