1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <com/sun/star/sheet/XSolver.hpp>
23 #include <com/sun/star/sheet/XSolverDescription.hpp>
24 #include <com/sun/star/table/CellAddress.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <cppuhelper/implbase.hxx>
27 #include <comphelper/broadcasthelper.hxx>
28 #include <comphelper/propertycontainer.hxx>
29 #include <comphelper/proparrhlp.hxx>
31 #include <unordered_map>
33 namespace com::sun::star::table
{ class XCell
; }
35 // hash map for the coefficients of a dependent cell (objective or constraint)
36 // The size of each vector is the number of columns (variable cells) plus one, first entry is initial value.
38 struct ScSolverCellHash
40 size_t operator()( const css::table::CellAddress
& rAddress
) const;
43 inline bool AddressEqual( const css::table::CellAddress
& rAddr1
, const css::table::CellAddress
& rAddr2
)
45 return rAddr1
.Sheet
== rAddr2
.Sheet
&& rAddr1
.Column
== rAddr2
.Column
&& rAddr1
.Row
== rAddr2
.Row
;
48 struct ScSolverCellEqual
50 bool operator()( const css::table::CellAddress
& rAddr1
, const css::table::CellAddress
& rAddr2
) const;
53 typedef std::unordered_map
< css::table::CellAddress
, std::vector
<double>, ScSolverCellHash
, ScSolverCellEqual
> ScSolverCellHashMap
;
55 typedef cppu::WeakImplHelper
<
57 css::sheet::XSolverDescription
,
58 css::lang::XServiceInfo
>
61 class SolverComponent
: public comphelper::OMutexAndBroadcastHelper
,
62 public comphelper::OPropertyContainer
,
63 public comphelper::OPropertyArrayUsageHelper
< SolverComponent
>,
64 public SolverComponent_Base
68 css::uno::Reference
< css::sheet::XSpreadsheetDocument
> mxDoc
;
69 css::table::CellAddress maObjective
;
70 css::uno::Sequence
< css::table::CellAddress
> maVariables
;
71 css::uno::Sequence
< css::sheet::SolverConstraint
> maConstraints
;
73 // set via XPropertySet
77 sal_Int32 mnEpsilonLevel
;
82 css::uno::Sequence
< double > maSolution
;
85 static OUString
GetResourceString(const char* pId
);
86 static css::uno::Reference
<css::table::XCell
> GetCell(
87 const css::uno::Reference
<css::sheet::XSpreadsheetDocument
>& xDoc
,
88 const css::table::CellAddress
& rPos
);
90 const css::uno::Reference
<css::sheet::XSpreadsheetDocument
>& xDoc
,
91 const css::table::CellAddress
& rPos
, double fValue
);
92 static double GetValue(
93 const css::uno::Reference
<css::sheet::XSpreadsheetDocument
>& xDoc
,
94 const css::table::CellAddress
& rPos
);
98 virtual ~SolverComponent() override
;
101 DECLARE_XTYPEPROVIDER()
103 virtual css::uno::Reference
< css::beans::XPropertySetInfo
> SAL_CALL
getPropertySetInfo() override
;
104 virtual ::cppu::IPropertyArrayHelper
& SAL_CALL
getInfoHelper() override
; // from OPropertySetHelper
105 virtual ::cppu::IPropertyArrayHelper
* createArrayHelper() const override
; // from OPropertyArrayUsageHelper
108 virtual css::uno::Reference
< css::sheet::XSpreadsheetDocument
> SAL_CALL
getDocument() override
;
109 virtual void SAL_CALL
setDocument( const css::uno::Reference
<
110 css::sheet::XSpreadsheetDocument
>& _document
) override
;
111 virtual css::table::CellAddress SAL_CALL
getObjective() override
;
112 virtual void SAL_CALL
setObjective( const css::table::CellAddress
& _objective
) override
;
113 virtual css::uno::Sequence
< css::table::CellAddress
> SAL_CALL
getVariables() override
;
114 virtual void SAL_CALL
setVariables( const css::uno::Sequence
<
115 css::table::CellAddress
>& _variables
) override
;
116 virtual css::uno::Sequence
< css::sheet::SolverConstraint
> SAL_CALL
getConstraints() override
;
117 virtual void SAL_CALL
setConstraints( const css::uno::Sequence
<
118 css::sheet::SolverConstraint
>& _constraints
) override
;
119 virtual sal_Bool SAL_CALL
getMaximize() override
;
120 virtual void SAL_CALL
setMaximize( sal_Bool _maximize
) override
;
122 virtual sal_Bool SAL_CALL
getSuccess() override
;
123 virtual double SAL_CALL
getResultValue() override
;
124 virtual css::uno::Sequence
< double > SAL_CALL
getSolution() override
;
126 virtual void SAL_CALL
solve() override
= 0;
128 // XSolverDescription
129 virtual OUString SAL_CALL
getComponentDescription() override
= 0;
130 virtual OUString SAL_CALL
getStatusDescription() override
;
131 virtual OUString SAL_CALL
getPropertyDescription( const OUString
& aPropertyName
) override
;
134 virtual OUString SAL_CALL
getImplementationName() override
= 0;
135 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
136 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */