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/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 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..607592e --- /dev/null +++ b/examples/aoc/main.cpp @@ -0,0 +1,55 @@ +#include +#include + +#include +#include +#include +#include + +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 result = FileIterator{std::string{"./examples/aoc/input.txt"}} + .map(splitByWhitespaceAndCastToInt) + .filter(isReportSafe) + .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