Skip to content

Commit 40c4d0c

Browse files
authored
Add factorial and more (#30)
2 parents 83e7cae + ec9175c commit 40c4d0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+623
-847
lines changed

.github/workflows/benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
shell: bash
8383

8484
- name: Benchmark
85-
run: python tools/benchmark.py -g
85+
run: python -m tools.benchmark -g
8686
shell: bash
8787

8888
- name: Upload Graphs

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ jobs:
5050
shell: bash
5151

5252
- name: Lint
53-
run: python tools/run_clang_tidy.py -source-filter='.*lib.*' -p build
53+
run: python -m tools.run_clang_tidy -source-filter='.*lib.*' -p build

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ dist/
8383
downloads/
8484
eggs/
8585
.eggs/
86-
lib/
8786
lib64/
8887
parts/
8988
sdist/

CMakeLists.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.20)
2424
PROJECT(Steppable)
2525

2626
# Ensure that Python is available to run the development scripts, and build with bindings.
27-
FIND_PACKAGE(
28-
Python
29-
COMPONENTS Interpreter Development
30-
REQUIRED
31-
)
32-
FIND_PACKAGE(
33-
Python3
34-
COMPONENTS Interpreter Development
35-
REQUIRED
36-
)
27+
set(Python_FIND_VIRTUALENV FIRST)
28+
29+
find_package(
30+
Python
31+
COMPONENTS Development Interpreter
32+
REQUIRED)
33+
34+
find_package(
35+
Python3
36+
COMPONENTS Development Interpreter
37+
REQUIRED)
3738

3839
SET(CMAKE_CXX_STANDARD 20)
3940
SET(CMAKE_CXX_EXTENSIONS OFF)
@@ -98,6 +99,7 @@ SET(COMPONENTS
9899
power
99100
division
100101
root
102+
factorial
101103
)
102104
# NEW_COMPONENT: PATCH Do NOT remove the previous comment.
103105

Steppable.sublime-project

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
{
55
"path": ".",
66
},
7+
{
8+
"path": "~/Desktop/Steppable.wiki"
9+
},
710
],
811
"debugger_configurations":
912
[

__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#####################################################################################################
2+
# Copyright (c) 2023-2024 NWSOFT #
3+
# #
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy #
5+
# of this software and associated documentation files (the "Software"), to deal #
6+
# in the Software without restriction, including without limitation the rights #
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
8+
# copies of the Software, and to permit persons to whom the Software is #
9+
# furnished to do so, subject to the following conditions: #
10+
# #
11+
# The above copyright notice and this permission notice shall be included in all #
12+
# copies or substantial portions of the Software. #
13+
# #
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
20+
# SOFTWARE. #
21+
#####################################################################################################
22+
23+
pass

cpp.hint

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,60 @@
2323
// Hint files help the Visual Studio IDE interpret Visual C++ identifiers
2424
// such as names of functions and macros.
2525
// For more information see https://go.microsoft.com/fwlink/?linkid=865984
26-
#define SECTION(__VA_ARGS__) \
27-
{ \
28-
const char* nameSection = #__VA_ARGS__; \
29-
std::cout << colors::brightBlue << std::setw(80) << std::setfill('-') << '\0' << reset << '\n'; \
30-
std::cout << colors::brightBlue << "[Testing: " << nameSection << ']' << reset << '\n'; \
31-
auto start = std::chrono::high_resolution_clock::now(); \
32-
auto testCase = TestCase(static_cast<std::string>(nameSection));
26+
#define TEST_START() \
27+
/* NOLINTNEXTLINE(bugprone-exception-escape) */ \
28+
int main() \
29+
{ \
30+
using namespace steppable::__internals::stringUtils; \
31+
using namespace steppable::__internals::utils; \
32+
using namespace steppable::testing; \
33+
using namespace steppable::output; \
34+
Utf8CodePage use_utf8; \
35+
int errors = 0;
36+
37+
#define SECTION(...) \
38+
{ \
39+
const std::string& nameSection = #__VA_ARGS__; \
40+
std::cout << colors::brightBlue << std::setw(80) << std::setfill('-') << reset << '\n'; \
41+
std::cout << colors::brightBlue << "[Testing: " << nameSection << ']' << reset << '\n'; \
42+
auto start = std::chrono::high_resolution_clock::now(); \
43+
auto _ = TestCase(nameSection);
44+
45+
#define SECTION_END() \
46+
auto end = std::chrono::high_resolution_clock::now(); \
47+
auto duration = \
48+
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start) \
49+
.count(); \
50+
_.summarize(); \
51+
std::cout << colors::brightBlue << '[' << nameSection << " took " << duration << "(microseconds) to finish]" \
52+
<< reset << '\n'; \
53+
std::cout << reset << '\n'; \
54+
errors += _.errorCount; \
55+
}
56+
57+
#define TEST_END() \
58+
if (errors) \
59+
error("TEST_END", "Not all tests passed. There are %i errors."s, errors); \
60+
else \
61+
info("All tests passed."); \
62+
std::cout << colors::brightBlue << std::setw(80) << std::setfill('=') << reset << '\n'; \
63+
if (errors) \
64+
return 1; \
65+
}
66+
67+
#define TIC(...) \
68+
{ \
69+
const char* nameSection = #__VA_ARGS__; \
70+
std::cout << colors::brightBlue << std::setw(80) << std::setfill('-') << reset << '\n'; \
71+
std::cout << colors::brightBlue << "[Profiling: " << nameSection << ']' << reset << '\n'; \
72+
auto start = std::chrono::high_resolution_clock::now();
73+
74+
#define TOC() \
75+
auto duration = \
76+
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start) \
77+
.count(); \
78+
std::cout << colors::brightBlue << '[' << nameSection << " took " << duration << "(microseconds) to execute]" \
79+
<< reset << '\n'; \
80+
std::cout << colors::brightBlue << std::setw(80) << std::setfill('-'); \
81+
std::cout << reset << '\n'; \
82+
}

include/fn/basicArithm.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ namespace steppable::__internals::arithmetic
212212
*/
213213
std::string rootIntPart(const std::string& _number, const std::string& base);
214214

215+
/**
216+
* @brief Calculates the factorial of a number.
217+
*
218+
* @param _number The number to calculate the factorial of.
219+
* @param steps The number of steps to calculate the factorial.
220+
*
221+
* @return The factorial of the number.
222+
*/
223+
std::string factorial(const std::string& _number, int steps = 2);
224+
215225
/**
216226
* @brief Executes a given predicate function a specified number of times.
217227
*

include/fraction.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ namespace steppable
224224
*/
225225
bool operator>=(const Fraction& rhs) const;
226226

227+
/**
228+
* @brief Converts a fraction to its reciprocal.
229+
* This function converts the fraction to its reciprocal.
230+
*/
231+
void reciprocal();
232+
227233
/**
228234
* @brief Simplifies a fraction.
229235
* This function simplifies the fraction by dividing the top and bottom components by their greatest common

lib/CMakeLists.txt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,7 @@
2020
# SOFTWARE. #
2121
#####################################################################################################
2222

23-
set(Python_FIND_VIRTUALENV ONLY)
24-
25-
if (NOT DEFINED Python_EXECUTABLE)
26-
find_package(
27-
Python
28-
COMPONENTS Development Interpreter
29-
REQUIRED)
30-
else ()
31-
set(Python_EXECUTABLE ${PYTHON_EXECUTABLE})
32-
endif ()
23+
set(Python_EXECUTABLE ${Python3_EXECUTABLE})
3324

3425
# Detect the installed nanobind package and import it into CMake
3526
execute_process(

lib/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#####################################################################################################
2+
# Copyright (c) 2023-2024 NWSOFT #
3+
# #
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy #
5+
# of this software and associated documentation files (the "Software"), to deal #
6+
# in the Software without restriction, including without limitation the rights #
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
8+
# copies of the Software, and to permit persons to whom the Software is #
9+
# furnished to do so, subject to the following conditions: #
10+
# #
11+
# The above copyright notice and this permission notice shall be included in all #
12+
# copies or substantial portions of the Software. #
13+
# #
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
20+
# SOFTWARE. #
21+
#####################################################################################################
22+
23+
pass

lib/constants.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#####################################################################################################
2+
# Copyright (c) 2023-2024 NWSOFT #
3+
# #
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy #
5+
# of this software and associated documentation files (the "Software"), to deal #
6+
# in the Software without restriction, including without limitation the rights #
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
8+
# copies of the Software, and to permit persons to whom the Software is #
9+
# furnished to do so, subject to the following conditions: #
10+
# #
11+
# The above copyright notice and this permission notice shall be included in all #
12+
# copies or substantial portions of the Software. #
13+
# #
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
20+
# SOFTWARE. #
21+
#####################################################################################################
22+
23+
import platform
24+
25+
WINDOWS = platform.system() == "Windows"
26+
MACOSX = platform.system() == "Darwin"
27+
LINUX = platform.system() == "Linux"

tools/building/paths.py renamed to lib/paths.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626

2727
from pathlib import Path
2828

29+
# section Project paths
2930
PROJECT = "Steppable"
3031

31-
PROJECT_PATH = Path(__file__).parent.parent.parent
32+
PROJECT_PATH = Path(__file__).parent.parent
3233
SRC_DIR = PROJECT_PATH / "src"
3334
TESTS_DIR = PROJECT_PATH / "tests"
3435
INCLUDE_DIR = PROJECT_PATH / "include"
@@ -37,7 +38,4 @@
3738
OBJ_DIR = BUILD_DIR / "obj.temp"
3839
BIN_DIR = BUILD_DIR / "bin"
3940
LIB_DIR = BUILD_DIR / "lib"
40-
41-
STATIC = 0
42-
SHARED = 1
43-
EXECUTABLE = 2
41+
# endsection

tools/building/printing.py renamed to lib/printing.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#####################################################################################################
2222

2323
import sys
24+
import os
2425

25-
from building.directories import WINDOWS
26+
from lib.constants import WINDOWS
2627

2728

2829
def green() -> str:
@@ -79,3 +80,34 @@ def bold() -> str:
7980
if sys.stdout.isatty():
8081
return "\033[1m"
8182
return ""
83+
84+
85+
def print_progressbar(
86+
iteration: int,
87+
total: int,
88+
prefix: str = "",
89+
suffix: str = "",
90+
decimals: int = 1,
91+
length: int = 100,
92+
fill: str = "\u2588",
93+
) -> None:
94+
"""
95+
Call in a loop to create terminal progress bar.
96+
:param iteration: Current iteration.
97+
:param total: Total iterations.
98+
:param prefix: Prefix string.
99+
:param suffix: Suffix string.
100+
:param decimals: Positive number of decimals in percent complete.
101+
:param length: Character length of bar.
102+
:param fill: Bar fill character.
103+
"""
104+
if not os.isatty(sys.stdout.fileno()):
105+
print(f"{prefix} {iteration}/{total} {suffix}")
106+
return
107+
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
108+
filled_length = int(length * iteration // total)
109+
bar = fill * filled_length + " " * (length - filled_length)
110+
print("\r%s |%s| %s%% %s" % (prefix, bar, percent, suffix), end="")
111+
# Print New Line on Complete
112+
if iteration == total:
113+
print()

0 commit comments

Comments
 (0)