From f20969174a7c3d55cb8c210988ea794cc457ed44 Mon Sep 17 00:00:00 2001 From: WiktorNowak Date: Fri, 10 Jan 2025 20:12:39 +0100 Subject: [PATCH 1/3] feat: add examples directory with first example --- CMakeLists.txt | 4 +++ examples/CMakeLists.txt | 1 + examples/aoc/CMakeLists.txt | 3 +++ examples/aoc/input.txt | 6 +++++ examples/aoc/main.cpp | 53 +++++++++++++++++++++++++++++++++++++ tools/build.sh | 7 +++++ 6 files changed, 74 insertions(+) create mode 100644 examples/CMakeLists.txt create mode 100644 examples/aoc/CMakeLists.txt create mode 100644 examples/aoc/input.txt create mode 100644 examples/aoc/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 960e60d..39d7476 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,3 +39,7 @@ if(${COMPILE_BENCHMARKS}) set(CMAKE_BUILD_TYPE "Release") add_subdirectory(benchmarks) endif() + +if (${COMPILE_EXAMPLES}) + add_subdirectory(examples) +endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..e264456 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(aoc) diff --git a/examples/aoc/CMakeLists.txt b/examples/aoc/CMakeLists.txt new file mode 100644 index 0000000..f7eb8c1 --- /dev/null +++ b/examples/aoc/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(aoc main.cpp) +target_compile_features(aoc PRIVATE cxx_std_23) +target_link_libraries(aoc PRIVATE rusty_iterators) diff --git a/examples/aoc/input.txt b/examples/aoc/input.txt new file mode 100644 index 0000000..b49c10d --- /dev/null +++ b/examples/aoc/input.txt @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 diff --git a/examples/aoc/main.cpp b/examples/aoc/main.cpp new file mode 100644 index 0000000..15d327f --- /dev/null +++ b/examples/aoc/main.cpp @@ -0,0 +1,53 @@ +#include +#include + +#include +#include +#include +#include + +using ::rusty_iterators::iterator::FileIterator; +using ::rusty_iterators::iterator::FIterType; +using ::rusty_iterators::iterator::LazyIterator; + +/* + * An example solution to Advent of Code 2024: day 2, part 1. + * https://adventofcode.com/2024/day/2 + */ +auto main() -> int +{ + auto split = [](auto str) { + std::istringstream iss(str); + std::vector tokens; + std::string token; + + while (std::getline(iss, token, ' ')) + { + if (!token.empty()) + tokens.push_back(std::atoi(token.c_str())); + } + return std::move(tokens); + }; + + auto filterSafeReports = [](std::vector vec) { + auto signum = (vec.at(1) - vec.at(0)) > 0 ? 1 : -1; + + return LazyIterator{vec}.movingWindow(2).all([&signum](auto x) { + int32_t result = x.at(1) - x.at(0); + auto currSignum = result > 0 ? 1 : -1; + auto absResult = std::abs(result); + + return absResult >= 1 && absResult <= 3 && currSignum == signum; + }); + }; + + auto result = FileIterator{std::string{"./examples/aoc/input.txt"}} + .map(std::move(split)) + .filter(std::move(filterSafeReports)) + .count(); + + std::cout << "Expected amount of safe reports: 2" << std::endl; + std::cout << "Calculated amount of safe reports: " << result << std::endl; + + assert(result == 2); +} diff --git a/tools/build.sh b/tools/build.sh index 03c71c1..1b3034d 100755 --- a/tools/build.sh +++ b/tools/build.sh @@ -7,6 +7,7 @@ clean_run=false compile_tests=false cxx_compiler="g++" compile_benchmarks=false +compile_examples=false while [[ $# -gt 0 ]]; do case $1 in @@ -27,6 +28,10 @@ while [[ $# -gt 0 ]]; do compile_benchmarks=true shift ;; + --compile-examples) + compile_examples=true + shift + ;; *) echo "Unknown option $1" exit 1 @@ -41,6 +46,7 @@ echo "C++ Compiler = ${cxx_compiler}" echo "Clean Run = ${clean_run}" echo "Compile Tests = ${compile_tests}" echo "Compile Benchmarks = ${compile_benchmarks}" +echo "Compile Examples = ${compile_examples}" echo cmake \ @@ -48,6 +54,7 @@ cmake \ -D CMAKE_CXX_COMPILER="${cxx_compiler}" \ -D COMPILE_TESTS="${compile_tests}" \ -D COMPILE_BENCHMARKS="${compile_benchmarks}" \ + -D COMPILE_EXAMPLES="${compile_examples}" \ -S "${repo_root}" \ -B build From 001f08b87bf02902b153d9f9741d6edd6784f31e Mon Sep 17 00:00:00 2001 From: WiktorNowak Date: Fri, 10 Jan 2025 20:15:58 +0100 Subject: [PATCH 2/3] docs: add info regarding examples build --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a404a40..aafb705 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,13 @@ We provide a small utility script to build the project. We use both CMake and Ni ## Examples -Some simple examples of usage. +Some actual real-life inspired examples can be found in the `examples/` directory. You can build them using provided build script: + +```bash +./tools/build.sh --compile-examples +``` + +Here we will list some of the simple examples, to show you the power of lazy iteration! ### Count all even numbers in the iterator From 53eeb1a3e239c7c5997828187c8bf264f2b6f578 Mon Sep 17 00:00:00 2001 From: WiktorNowak Date: Sat, 11 Jan 2025 10:54:10 +0100 Subject: [PATCH 3/3] refactor: cleanup the aoc example --- examples/aoc/main.cpp | 56 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/examples/aoc/main.cpp b/examples/aoc/main.cpp index 15d327f..607592e 100644 --- a/examples/aoc/main.cpp +++ b/examples/aoc/main.cpp @@ -10,40 +10,42 @@ using ::rusty_iterators::iterator::FileIterator; using ::rusty_iterators::iterator::FIterType; using ::rusty_iterators::iterator::LazyIterator; +auto splitByWhitespaceAndCastToInt(std::string str) -> std::vector +{ + std::istringstream iss{str}; + std::vector tokens{}; + std::string token; + + while (std::getline(iss, token, ' ')) + { + if (!token.empty()) + tokens.push_back(std::atoi(token.c_str())); + } + return std::move(tokens); +} + +auto isReportSafe(std::vector vec) -> bool +{ + auto signum = (vec.at(1) - vec.at(0)) > 0 ? 1 : -1; + + return LazyIterator{vec}.movingWindow(2).all([&signum](auto x) { + auto result = x.at(1) - x.at(0); + auto currSignum = result > 0 ? 1 : -1; + auto absResult = std::abs(result); + + return absResult >= 1 && absResult <= 3 && currSignum == signum; + }); +} + /* * An example solution to Advent of Code 2024: day 2, part 1. * https://adventofcode.com/2024/day/2 */ auto main() -> int { - auto split = [](auto str) { - std::istringstream iss(str); - std::vector tokens; - std::string token; - - while (std::getline(iss, token, ' ')) - { - if (!token.empty()) - tokens.push_back(std::atoi(token.c_str())); - } - return std::move(tokens); - }; - - auto filterSafeReports = [](std::vector vec) { - auto signum = (vec.at(1) - vec.at(0)) > 0 ? 1 : -1; - - return LazyIterator{vec}.movingWindow(2).all([&signum](auto x) { - int32_t result = x.at(1) - x.at(0); - auto currSignum = result > 0 ? 1 : -1; - auto absResult = std::abs(result); - - return absResult >= 1 && absResult <= 3 && currSignum == signum; - }); - }; - auto result = FileIterator{std::string{"./examples/aoc/input.txt"}} - .map(std::move(split)) - .filter(std::move(filterSafeReports)) + .map(splitByWhitespaceAndCastToInt) + .filter(isReportSafe) .count(); std::cout << "Expected amount of safe reports: 2" << std::endl;