Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
factor_result.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_FACTOR_RESULT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_FACTOR_RESULT_HPP_
7
8// IWYU pragma: private, include "arene/base/linalg.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
12#include "arene/base/stdlib_choice/cstddef.hpp"
13#include "arene/base/stdlib_choice/numeric_limits.hpp"
14
15// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
16
17namespace arene {
18namespace base {
19namespace linalg {
20
21/// @brief Stores the result of factorization type algorithms
22///
23/// A @c factor_result contains an optional pivot index signaling either success or failure of the corresponding
24/// factorization algorithm. On success, a @c factor_result does not have a pivot index. On failure, a
25/// @c factor_result has a pivot index which specifies the first bad pivot encountered during factorization, that is
26/// the pivot that caused factorization to fail.
27///
28/// @note A @c factor_result cannot represent a pivot index with value <c>std::numeric_limits<std::size_t>::max()</c>.
29// parasoft-begin-suppress AUTOSAR-A3_1_5-a "False positive: all functions are constexpr so are intended to be
30// inlined"
32 public:
33 /// @brief Construct a result denoting success
34 /// @post @c has_pivot_index() is @c false
35 constexpr factor_result() = default;
36
37 /// @brief Construct a result denoting failure
38 /// @param idx Index of the first bad pivot encountered during factorization
39 /// @pre <c>idx != std::numeric_limits<std::size_t>::max()</c>, otherwise @c ARENE_PRECONDITION violation
40 /// @post @c has_pivot_index() is @c true and @c pivot_index() is @c idx
41 constexpr explicit factor_result(std::size_t idx) noexcept
42 : index_{idx} {
43 ARENE_PRECONDITION(idx != no_pivot_index);
44 }
45
46 /// @brief Query whether the factorization succeeded
47 /// @return @c true if the factorization succeeded, that is if there is no pivot index
48 constexpr explicit operator bool() const noexcept { return index_ == no_pivot_index; }
49
50 /// @brief Query whether the factorization failed
51 /// @return @c true if the factorization failed, that is if there is a pivot index
52 constexpr auto has_pivot_index() const noexcept -> bool { return !(*this); }
53
54 /// @brief Obtain the pivot index
55 /// @pre @c has_pivot_index() is @c true, otherwise @c ARENE_PRECONDITION violation
56 /// @return Index of the first bad pivot encountered during factorization
57 constexpr auto pivot_index() const noexcept -> std::size_t {
59 return index_;
60 }
61
62 private:
63 /// @brief The index value used to denote the absence of a pivot index
64 static constexpr std::size_t no_pivot_index{std::numeric_limits<std::size_t>::max()};
65
66 /// @brief The pivot index, or @c no_pivot_index if there is no pivot index
67 std::size_t index_{no_pivot_index};
68};
69// parasoft-end-suppress AUTOSAR-A3_1_5-a
70
71} // namespace linalg
72} // namespace base
73} // namespace arene
74
75// parasoft-end-suppress AUTOSAR-A7_1_5-a
76
77#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_FACTOR_RESULT_HPP_
constexpr auto has_pivot_index() const noexcept -> bool
Query whether the factorization failed.
Definition factor_result.hpp:52
constexpr auto pivot_index() const noexcept -> std::size_t
Obtain the pivot index.
Definition factor_result.hpp:57
constexpr factor_result()=default
Construct a result denoting success.
constexpr factor_result(std::size_t idx) noexcept
Construct a result denoting failure.
Definition factor_result.hpp:41
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition add.hpp:20
Definition accumulate.hpp:37
Definition accumulate.hpp:36
STL namespace.
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29