Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ if(${COMPILE_BENCHMARKS})
set(CMAKE_BUILD_TYPE "Release")
add_subdirectory(benchmarks)
endif()

if (${COMPILE_EXAMPLES})
add_subdirectory(examples)
endif()
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(aoc)
3 changes: 3 additions & 0 deletions examples/aoc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(aoc main.cpp)
target_compile_features(aoc PRIVATE cxx_std_23)
target_link_libraries(aoc PRIVATE rusty_iterators)
6 changes: 6 additions & 0 deletions examples/aoc/input.txt
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions examples/aoc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <rusty_iterators/file_iterator.hpp>
#include <rusty_iterators/iterator.hpp>

#include <cassert>
#include <iostream>
#include <sstream>
#include <string>

using ::rusty_iterators::iterator::FileIterator;
using ::rusty_iterators::iterator::FIterType;
using ::rusty_iterators::iterator::LazyIterator;

auto splitByWhitespaceAndCastToInt(std::string str) -> std::vector<int>
{
std::istringstream iss{str};
std::vector<int> 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<int> 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<FIterType::Lazy>{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);
}
7 changes: 7 additions & 0 deletions tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +28,10 @@ while [[ $# -gt 0 ]]; do
compile_benchmarks=true
shift
;;
--compile-examples)
compile_examples=true
shift
;;
*)
echo "Unknown option $1"
exit 1
Expand All @@ -41,13 +46,15 @@ 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 \
-G Ninja \
-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

Expand Down