mini-llvm 0.1.0
Loading...
Searching...
No Matches
BigInteger.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <compare>
6#include <cstddef>
7#include <cstdint>
8#include <format>
9#include <functional>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <utility>
14#include <vector>
15
17
18namespace mini_llvm {
19
21public:
22 BigInteger() noexcept;
23 BigInteger(int64_t value);
24 explicit BigInteger(std::string_view str, int base = 10);
25
26 BigInteger(const BigInteger &other) = default;
27 BigInteger &operator=(const BigInteger &other) = default;
28
29 BigInteger(BigInteger &&other) noexcept = default;
30 BigInteger &operator=(BigInteger &&other) noexcept = default;
31
32 BigInteger operator+() const &;
33 BigInteger operator+() &&;
34 BigInteger operator-() const &;
35 BigInteger operator-() &&;
36
37 BigInteger &operator+=(const BigInteger &lhs);
38 BigInteger &operator-=(const BigInteger &lhs);
39 BigInteger &operator*=(const BigInteger &lhs);
40 BigInteger &operator/=(int32_t lhs);
41
42 BigInteger &operator++();
43 BigInteger operator++(int);
44 BigInteger &operator--();
45 BigInteger operator--(int);
46
47 int64_t toInt64() const noexcept;
48 std::string toString(int base = 10) const;
49
50 size_t hashCode() const noexcept;
51
52 static std::optional<BigInteger> parse(std::string_view str, int base = 10);
53
54private:
55 int sign_{};
56 std::vector<uint32_t> digits_;
57
58 BigInteger(int sign, std::vector<uint32_t> digits);
59
60 friend MINI_LLVM_EXPORT bool operator==(const BigInteger &lhs, const BigInteger &rhs) noexcept;
61 friend MINI_LLVM_EXPORT std::strong_ordering operator<=>(const BigInteger &lhs, const BigInteger &rhs) noexcept;
62
66 friend MINI_LLVM_EXPORT BigInteger operator/(const BigInteger &lhs, int32_t rhs);
67 friend MINI_LLVM_EXPORT int32_t operator%(const BigInteger &lhs, int32_t rhs);
68
71 friend MINI_LLVM_EXPORT std::pair<BigInteger, int32_t> divRem(const BigInteger &lhs, int32_t rhs);
72};
73
74MINI_LLVM_EXPORT bool operator==(const BigInteger &lhs, const BigInteger &rhs) noexcept;
75MINI_LLVM_EXPORT std::strong_ordering operator<=>(const BigInteger &lhs, const BigInteger &rhs) noexcept;
76
81MINI_LLVM_EXPORT int32_t operator%(const BigInteger &lhs, int32_t rhs);
82
85MINI_LLVM_EXPORT std::pair<BigInteger, int32_t> divRem(const BigInteger &lhs, int32_t rhs);
86
87} // namespace mini_llvm
88
89template <>
90struct std::hash<mini_llvm::BigInteger> {
91 size_t operator()(const mini_llvm::BigInteger &value) const noexcept {
92 return value.hashCode();
93 }
94};
95
96template <>
97struct std::formatter<mini_llvm::BigInteger> {
98 constexpr auto parse(std::format_parse_context &ctx) {
99 return ctx.begin();
100 }
101
102 template <typename FormatContext>
103 auto format(const mini_llvm::BigInteger &value, FormatContext &ctx) const {
104 return std::format(ctx.out(), "{}", value.toString());
105 }
106};
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
Definition BigInteger.h:20
friend MINI_LLVM_EXPORT BigInteger abs(const BigInteger &value)
friend MINI_LLVM_EXPORT int32_t operator%(const BigInteger &lhs, int32_t rhs)
std::string toString(int base=10) const
friend MINI_LLVM_EXPORT std::pair< BigInteger, int32_t > divRem(const BigInteger &lhs, int32_t rhs)
friend MINI_LLVM_EXPORT BigInteger abs(BigInteger &&value)
friend MINI_LLVM_EXPORT BigInteger operator+(const BigInteger &lhs, const BigInteger &rhs)
static std::optional< BigInteger > parse(std::string_view str, int base=10)
friend MINI_LLVM_EXPORT std::strong_ordering operator<=>(const BigInteger &lhs, const BigInteger &rhs) noexcept
friend MINI_LLVM_EXPORT BigInteger operator-(const BigInteger &lhs, const BigInteger &rhs)
friend MINI_LLVM_EXPORT bool operator==(const BigInteger &lhs, const BigInteger &rhs) noexcept
size_t hashCode() const noexcept
int64_t toInt64() const noexcept
friend MINI_LLVM_EXPORT BigInteger operator/(const BigInteger &lhs, int32_t rhs)
friend MINI_LLVM_EXPORT BigInteger operator*(const BigInteger &lhs, const BigInteger &rhs)
Definition GraphColoringAllocator.h:13
MINI_LLVM_EXPORT BigInteger operator*(const BigInteger &lhs, const BigInteger &rhs)
MINI_LLVM_EXPORT BigInteger operator/(const BigInteger &lhs, int32_t rhs)
MINI_LLVM_EXPORT BigInteger abs(const BigInteger &value)
MINI_LLVM_EXPORT int32_t operator%(const BigInteger &lhs, int32_t rhs)
MINI_LLVM_EXPORT std::pair< BigInteger, int32_t > divRem(const BigInteger &lhs, int32_t rhs)
MINI_LLVM_EXPORT bool operator==(const BigInteger &lhs, const BigInteger &rhs) noexcept
MINI_LLVM_EXPORT BigInteger operator-(const BigInteger &lhs, const BigInteger &rhs)
MINI_LLVM_EXPORT BigInteger operator+(const BigInteger &lhs, const BigInteger &rhs)
MINI_LLVM_EXPORT std::strong_ordering operator<=>(const BigInteger &lhs, const BigInteger &rhs) noexcept
auto format(const mini_llvm::BigInteger &value, FormatContext &ctx) const
Definition BigInteger.h:103
constexpr auto parse(std::format_parse_context &ctx)
Definition BigInteger.h:98
size_t operator()(const mini_llvm::BigInteger &value) const noexcept
Definition BigInteger.h:91