Skip to content

Commit cb77319

Browse files
committed
Numeric module
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent e5b0503 commit cb77319

File tree

13 files changed

+1234
-5
lines changed

13 files changed

+1234
-5
lines changed

.github/workflows/website-build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
-DCMAKE_BUILD_TYPE:STRING=Release
1919
-DSOURCEMETA_CORE_LANG_IO:BOOL=OFF
2020
-DSOURCEMETA_CORE_LANG_PROCESS:BOOL=OFF
21+
-DSOURCEMETA_CORE_LANG_PARALLEL:BOOL=OFF
22+
-DSOURCEMETA_CORE_LANG_NUMERIC:BOOL=OFF
2123
-DSOURCEMETA_CORE_TIME:BOOL=OFF
2224
-DSOURCEMETA_CORE_UUID:BOOL=OFF
2325
-DSOURCEMETA_CORE_REGEX:BOOL=OFF

.github/workflows/website-deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
-DCMAKE_BUILD_TYPE:STRING=Release
2929
-DSOURCEMETA_CORE_LANG_IO:BOOL=OFF
3030
-DSOURCEMETA_CORE_LANG_PROCESS:BOOL=OFF
31+
-DSOURCEMETA_CORE_LANG_PARALLEL:BOOL=OFF
32+
-DSOURCEMETA_CORE_LANG_NUMERIC:BOOL=OFF
3133
-DSOURCEMETA_CORE_TIME:BOOL=OFF
3234
-DSOURCEMETA_CORE_UUID:BOOL=OFF
3335
-DSOURCEMETA_CORE_REGEX:BOOL=OFF

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
66
option(SOURCEMETA_CORE_LANG_IO "Build the Sourcemeta Core language I/O library" ON)
77
option(SOURCEMETA_CORE_LANG_PROCESS "Build the Sourcemeta Core language Process library" ON)
88
option(SOURCEMETA_CORE_LANG_PARALLEL "Build the Sourcemeta Core language parallel library" ON)
9+
option(SOURCEMETA_CORE_LANG_NUMERIC "Build the Sourcemeta Core language numeric library" ON)
910
option(SOURCEMETA_CORE_TIME "Build the Sourcemeta Core time library" ON)
1011
option(SOURCEMETA_CORE_UUID "Build the Sourcemeta Core UUID library" ON)
1112
option(SOURCEMETA_CORE_MD5 "Build the Sourcemeta Core MD5 library" ON)
@@ -68,6 +69,11 @@ if(SOURCEMETA_CORE_LANG_PARALLEL)
6869
add_subdirectory(src/lang/parallel)
6970
endif()
7071

72+
if(SOURCEMETA_CORE_LANG_NUMERIC)
73+
find_package(mpdecimal REQUIRED)
74+
add_subdirectory(src/lang/numeric)
75+
endif()
76+
7177
if(SOURCEMETA_CORE_TIME)
7278
add_subdirectory(src/core/time)
7379
endif()
@@ -90,7 +96,6 @@ if(SOURCEMETA_CORE_URI)
9096
endif()
9197

9298
if(SOURCEMETA_CORE_JSON)
93-
find_package(mpdecimal REQUIRED)
9499
add_subdirectory(src/core/json)
95100
endif()
96101

@@ -174,6 +179,10 @@ if(SOURCEMETA_CORE_TESTS)
174179
add_subdirectory(test/parallel)
175180
endif()
176181

182+
if(SOURCEMETA_CORE_LANG_NUMERIC)
183+
add_subdirectory(test/numeric)
184+
endif()
185+
177186
if(SOURCEMETA_CORE_TIME)
178187
add_subdirectory(test/time)
179188
endif()

config.cmake.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if(NOT SOURCEMETA_CORE_COMPONENTS)
77
list(APPEND SOURCEMETA_CORE_COMPONENTS io)
88
list(APPEND SOURCEMETA_CORE_COMPONENTS process)
99
list(APPEND SOURCEMETA_CORE_COMPONENTS parallel)
10+
list(APPEND SOURCEMETA_CORE_COMPONENTS numeric)
1011
list(APPEND SOURCEMETA_CORE_COMPONENTS time)
1112
list(APPEND SOURCEMETA_CORE_COMPONENTS uuid)
1213
list(APPEND SOURCEMETA_CORE_COMPONENTS md5)
@@ -34,6 +35,10 @@ foreach(component ${SOURCEMETA_CORE_COMPONENTS})
3435
elseif(component STREQUAL "parallel")
3536
find_dependency(Threads)
3637
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_core_parallel.cmake")
38+
elseif(component STREQUAL "numeric")
39+
find_dependency(mpdecimal CONFIG)
40+
find_dependency(mpdecimalxx CONFIG)
41+
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_core_numeric.cmake")
3742
elseif(component STREQUAL "time")
3843
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_core_time.cmake")
3944
elseif(component STREQUAL "uuid")
@@ -47,8 +52,6 @@ foreach(component ${SOURCEMETA_CORE_COMPONENTS})
4752
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_core_uri.cmake")
4853
elseif(component STREQUAL "json")
4954
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_core_io.cmake")
50-
find_dependency(mpdecimal CONFIG)
51-
find_dependency(mpdecimalxx CONFIG)
5255
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_core_json.cmake")
5356
elseif(component STREQUAL "jsonl")
5457
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_core_json.cmake")

src/core/json/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ if(SOURCEMETA_CORE_INSTALL)
77
endif()
88

99
target_link_libraries(sourcemeta_core_json PRIVATE sourcemeta::core::io)
10-
target_link_libraries(sourcemeta_core_json PRIVATE mpdecimal)
11-
target_link_libraries(sourcemeta_core_json PRIVATE mpdecimalxx)
10+
target_link_libraries(sourcemeta_core_json PUBLIC sourcemeta::core::numeric)

src/lang/numeric/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME numeric
2+
PRIVATE_HEADERS decimal.h SOURCES decimal.cc)
3+
4+
if(SOURCEMETA_CORE_INSTALL)
5+
sourcemeta_library_install(NAMESPACE sourcemeta PROJECT core NAME numeric)
6+
endif()
7+
8+
target_link_libraries(sourcemeta_core_numeric PUBLIC mpdecimal)
9+
target_link_libraries(sourcemeta_core_numeric PUBLIC mpdecimalxx)

src/lang/numeric/decimal.cc

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include <sourcemeta/core/numeric_decimal.h>
2+
3+
#include <cassert> // assert
4+
#include <limits> // std::numeric_limits
5+
#include <utility> // std::move
6+
7+
#include <decimal.hh> // decimal::Decimal
8+
#include <mpdecimal.h> // MPD_*
9+
10+
namespace sourcemeta::core {
11+
12+
Decimal::Decimal(const char *const value) : data{value} {}
13+
14+
Decimal::Decimal(const std::string &value) : data{value} {}
15+
16+
Decimal::Decimal(const std::string_view value) : data{std::string{value}} {}
17+
18+
auto Decimal::to_scientific_string() const -> std::string {
19+
return this->data.to_sci(true);
20+
}
21+
22+
auto Decimal::to_string() const -> std::string {
23+
const std::string repr{this->data.repr()};
24+
return repr.substr(9, repr.size() - 11);
25+
}
26+
27+
auto Decimal::to_int64() const -> std::int64_t { return this->data.i64(); }
28+
29+
auto Decimal::to_int32() const -> std::int32_t { return this->data.i32(); }
30+
31+
auto Decimal::to_uint64() const -> std::uint64_t { return this->data.u64(); }
32+
33+
auto Decimal::to_uint32() const -> std::uint32_t { return this->data.u32(); }
34+
35+
auto Decimal::is_zero() const -> bool { return this->data.iszero(); }
36+
37+
auto Decimal::is_integer() const -> bool { return this->data.isinteger(); }
38+
39+
auto Decimal::is_finite() const -> bool { return this->data.isfinite(); }
40+
41+
auto Decimal::is_int32() const -> bool {
42+
assert(this->data.isinteger());
43+
return *this >= std::numeric_limits<std::int32_t>::min() &&
44+
*this <= std::numeric_limits<std::int32_t>::max();
45+
}
46+
47+
auto Decimal::is_int64() const -> bool {
48+
assert(this->data.isinteger());
49+
return *this >= std::numeric_limits<std::int64_t>::min() &&
50+
*this <= std::numeric_limits<std::int64_t>::max();
51+
}
52+
53+
auto Decimal::is_uint32() const -> bool {
54+
assert(this->data.isinteger());
55+
return *this >= 0 && *this <= std::numeric_limits<std::uint32_t>::max();
56+
}
57+
58+
auto Decimal::is_uint64() const -> bool {
59+
assert(this->data.isinteger());
60+
return *this >= 0 && *this <= std::numeric_limits<std::uint64_t>::max();
61+
}
62+
63+
auto Decimal::operator+=(const Decimal &other) -> Decimal & {
64+
this->data += other.data;
65+
return *this;
66+
}
67+
68+
auto Decimal::operator-=(const Decimal &other) -> Decimal & {
69+
this->data -= other.data;
70+
return *this;
71+
}
72+
73+
auto Decimal::operator*=(const Decimal &other) -> Decimal & {
74+
this->data *= other.data;
75+
return *this;
76+
}
77+
78+
auto Decimal::operator/=(const Decimal &other) -> Decimal & {
79+
this->data /= other.data;
80+
return *this;
81+
}
82+
83+
auto Decimal::operator%=(const Decimal &other) -> Decimal & {
84+
this->data %= other.data;
85+
return *this;
86+
}
87+
88+
auto Decimal::operator+(const Decimal &other) const -> Decimal {
89+
Decimal result{*this};
90+
result.data = this->data + other.data;
91+
return result;
92+
}
93+
94+
auto Decimal::operator-(const Decimal &other) const -> Decimal {
95+
Decimal result{*this};
96+
result.data = this->data - other.data;
97+
return result;
98+
}
99+
100+
auto Decimal::operator*(const Decimal &other) const -> Decimal {
101+
Decimal result{*this};
102+
result.data = this->data * other.data;
103+
return result;
104+
}
105+
106+
auto Decimal::operator/(const Decimal &other) const -> Decimal {
107+
Decimal result{*this};
108+
result.data = this->data / other.data;
109+
return result;
110+
}
111+
112+
auto Decimal::operator%(const Decimal &other) const -> Decimal {
113+
Decimal result{*this};
114+
result.data = this->data % other.data;
115+
return result;
116+
}
117+
118+
auto Decimal::operator-() const -> Decimal {
119+
Decimal result{*this};
120+
result.data = -this->data;
121+
return result;
122+
}
123+
124+
auto Decimal::operator+() const -> Decimal {
125+
Decimal result{*this};
126+
result.data = +this->data;
127+
return result;
128+
}
129+
130+
auto Decimal::operator++() -> Decimal & {
131+
this->data += decimal::Decimal{1};
132+
return *this;
133+
}
134+
135+
auto Decimal::operator++(int) -> Decimal {
136+
Decimal result{*this};
137+
this->data += decimal::Decimal{1};
138+
return result;
139+
}
140+
141+
auto Decimal::operator--() -> Decimal & {
142+
this->data -= decimal::Decimal{1};
143+
return *this;
144+
}
145+
146+
auto Decimal::operator--(int) -> Decimal {
147+
Decimal result{*this};
148+
this->data -= decimal::Decimal{1};
149+
return result;
150+
}
151+
152+
auto Decimal::operator==(const Decimal &other) const -> bool {
153+
return this->data == other.data;
154+
}
155+
156+
auto Decimal::operator!=(const Decimal &other) const -> bool {
157+
return this->data != other.data;
158+
}
159+
160+
auto Decimal::operator<(const Decimal &other) const -> bool {
161+
return this->data < other.data;
162+
}
163+
164+
auto Decimal::operator<=(const Decimal &other) const -> bool {
165+
return this->data <= other.data;
166+
}
167+
168+
auto Decimal::operator>(const Decimal &other) const -> bool {
169+
return this->data > other.data;
170+
}
171+
172+
auto Decimal::operator>=(const Decimal &other) const -> bool {
173+
return this->data >= other.data;
174+
}
175+
176+
} // namespace sourcemeta::core
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef SOURCEMETA_CORE_NUMERIC_H_
2+
#define SOURCEMETA_CORE_NUMERIC_H_
3+
4+
// NOLINTBEGIN(misc-include-cleaner)
5+
#include <sourcemeta/core/numeric_decimal.h>
6+
// NOLINTEND(misc-include-cleaner)
7+
8+
#endif

0 commit comments

Comments
 (0)