numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
custom_float.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstdint>
23#include <limits>
24
29
31
39template <index_type Radix, base::concepts::integral Digit = std::uint8_t,
40 base::concepts::signed_integral DigitCalc = std::int32_t>
42public:
43 static_assert(std::numeric_limits<Digit>::max() > Radix);
44 static_assert(std::numeric_limits<DigitCalc>::max() > Radix * Radix);
45 static_assert(std::numeric_limits<DigitCalc>::min() < -Radix * Radix);
46
48 static constexpr index_type radix = Radix;
49
51 using digit_type = Digit;
52
54 using digit_calc_type = DigitCalc;
55
60 : digits_(
61 typename util::bidirectional_vector<digit_type>::container_type{
62 0},
63 0) {}
64
70 [[nodiscard]] auto lowest_ind() const noexcept -> index_type {
71 return digits_.min_index();
72 }
73
79 [[nodiscard]] auto highest_ind() const noexcept -> index_type {
80 return digits_.max_index();
81 }
82
89 [[nodiscard]] auto at(index_type index) const -> digit_type {
90 return digits_.at(index);
91 }
92
101 [[nodiscard]] auto operator[](index_type index) const -> digit_type {
102 return digits_[index];
103 }
104
113 [[nodiscard]] auto operator[](index_type index) -> digit_type& {
114 return digits_.get_or_prepare(index);
115 }
116
123
130
136 void move_digits(index_type offset) { digits_.move_position(offset); }
137
144 void resize(index_type lowest, index_type highest) {
145 digits_.resize(lowest, highest);
146 }
147
148private:
151};
152
153} // namespace num_collect::numbers
Definition of bidirectional_vector class.
Class of floating numbers in a custom number system with given radix.
auto lowest_ind() const noexcept -> index_type
Get the index of the lowest digits.
void push_to_lowest(digit_type digit)
Add a digit to the lowest end.
util::bidirectional_vector< digit_type > digits_
Digits.
DigitCalc digit_calc_type
Type of digits for calculation.
auto operator[](index_type index) -> digit_type &
Access a digit preparing it if needed.
void resize(index_type lowest, index_type highest)
Change the range of digits.
auto at(index_type index) const -> digit_type
Access a digit with checks.
void push_to_highest(digit_type digit)
Add a digit to the highest end.
static constexpr index_type radix
Radix of the number system.
auto highest_ind() const noexcept -> index_type
Get the index of the highest digit.
auto operator[](index_type index) const -> digit_type
Access a digit without checks.
void move_digits(index_type offset)
Move digits.
Digit digit_type
Type of digits.
Class to save data in a sequence which can be extended even toward negative direction.
void resize(index_type min_index, index_type max_index, const value_type &value=value_type())
Change the position of this object.
auto max_index() const noexcept -> index_type
Get the maximum index.
auto min_index() const noexcept -> index_type
Get the minimum index. (Equal to the index of the origin.)
auto get_or_prepare(index_type index) -> value_type &
Access a value preparing it if needed.
auto at(index_type index) const -> const value_type &
Access a value with checks.
void push_front(const value_type &value)
Add a value to the beginning.
void push_back(const value_type &value)
Add a value to the end.
void move_position(index_type offset)
Move the position of this object.
Definition of index_type type.
Definition of integral concept.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of classes of numbers.
Definition of signed_integral concept.