Skip to content

Commit f23ed3b

Browse files
feat: add examples directory with first example (#97)
* feat: add examples directory with first example * docs: add info regarding examples build * refactor: cleanup the aoc example
1 parent fc95fa0 commit f23ed3b

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ if(${COMPILE_BENCHMARKS})
3939
set(CMAKE_BUILD_TYPE "Release")
4040
add_subdirectory(benchmarks)
4141
endif()
42+
43+
if (${COMPILE_EXAMPLES})
44+
add_subdirectory(examples)
45+
endif()

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ We provide a small utility script to build the project. We use both CMake and Ni
3535

3636
## Examples
3737

38-
Some simple examples of usage.
38+
Some actual real-life inspired examples can be found in the `examples/` directory. You can build them using provided build script:
39+
40+
```bash
41+
./tools/build.sh --compile-examples
42+
```
43+
44+
Here we will list some of the simple examples, to show you the power of lazy iteration!
3945

4046
### Count all even numbers in the iterator
4147

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(aoc)

examples/aoc/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_executable(aoc main.cpp)
2+
target_compile_features(aoc PRIVATE cxx_std_23)
3+
target_link_libraries(aoc PRIVATE rusty_iterators)

examples/aoc/input.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
7 6 4 2 1
2+
1 2 7 8 9
3+
9 7 6 2 1
4+
1 3 2 4 5
5+
8 6 4 4 1
6+
1 3 6 7 9

examples/aoc/main.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <rusty_iterators/file_iterator.hpp>
2+
#include <rusty_iterators/iterator.hpp>
3+
4+
#include <cassert>
5+
#include <iostream>
6+
#include <sstream>
7+
#include <string>
8+
9+
using ::rusty_iterators::iterator::FileIterator;
10+
using ::rusty_iterators::iterator::FIterType;
11+
using ::rusty_iterators::iterator::LazyIterator;
12+
13+
auto splitByWhitespaceAndCastToInt(std::string str) -> std::vector<int>
14+
{
15+
std::istringstream iss{str};
16+
std::vector<int> tokens{};
17+
std::string token;
18+
19+
while (std::getline(iss, token, ' '))
20+
{
21+
if (!token.empty())
22+
tokens.push_back(std::atoi(token.c_str()));
23+
}
24+
return std::move(tokens);
25+
}
26+
27+
auto isReportSafe(std::vector<int> vec) -> bool
28+
{
29+
auto signum = (vec.at(1) - vec.at(0)) > 0 ? 1 : -1;
30+
31+
return LazyIterator{vec}.movingWindow(2).all([&signum](auto x) {
32+
auto result = x.at(1) - x.at(0);
33+
auto currSignum = result > 0 ? 1 : -1;
34+
auto absResult = std::abs(result);
35+
36+
return absResult >= 1 && absResult <= 3 && currSignum == signum;
37+
});
38+
}
39+
40+
/*
41+
* An example solution to Advent of Code 2024: day 2, part 1.
42+
* https://adventofcode.com/2024/day/2
43+
*/
44+
auto main() -> int
45+
{
46+
auto result = FileIterator<FIterType::Lazy>{std::string{"./examples/aoc/input.txt"}}
47+
.map(splitByWhitespaceAndCastToInt)
48+
.filter(isReportSafe)
49+
.count();
50+
51+
std::cout << "Expected amount of safe reports: 2" << std::endl;
52+
std::cout << "Calculated amount of safe reports: " << result << std::endl;
53+
54+
assert(result == 2);
55+
}

tools/build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ clean_run=false
77
compile_tests=false
88
cxx_compiler="g++"
99
compile_benchmarks=false
10+
compile_examples=false
1011

1112
while [[ $# -gt 0 ]]; do
1213
case $1 in
@@ -27,6 +28,10 @@ while [[ $# -gt 0 ]]; do
2728
compile_benchmarks=true
2829
shift
2930
;;
31+
--compile-examples)
32+
compile_examples=true
33+
shift
34+
;;
3035
*)
3136
echo "Unknown option $1"
3237
exit 1
@@ -41,13 +46,15 @@ echo "C++ Compiler = ${cxx_compiler}"
4146
echo "Clean Run = ${clean_run}"
4247
echo "Compile Tests = ${compile_tests}"
4348
echo "Compile Benchmarks = ${compile_benchmarks}"
49+
echo "Compile Examples = ${compile_examples}"
4450
echo
4551

4652
cmake \
4753
-G Ninja \
4854
-D CMAKE_CXX_COMPILER="${cxx_compiler}" \
4955
-D COMPILE_TESTS="${compile_tests}" \
5056
-D COMPILE_BENCHMARKS="${compile_benchmarks}" \
57+
-D COMPILE_EXAMPLES="${compile_examples}" \
5158
-S "${repo_root}" \
5259
-B build
5360

0 commit comments

Comments
 (0)