Skip to content

Commit

Permalink
Feature: generator (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
liss-h authored Jul 22, 2024
1 parent 943a92d commit 206d781
Show file tree
Hide file tree
Showing 8 changed files with 982 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/code_testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
fail-fast: false
matrix:
config:
- os: ubuntu-22.04
compiler: clang-15
- os: ubuntu-22.04
compiler: clang-16
- os: ubuntu-22.04
compiler: clang-17
- os: ubuntu-22.04
compiler: clang-18

- os: ubuntu-22.04
compiler: gcc-12
Expand All @@ -30,7 +30,7 @@ jobs:
shell: bash

steps:
- name: Add repos for for gcc-13 and clang-16
- name: Add repos for for gcc-13 and clang-16,..
uses: dice-group/cpp-conan-release-reusable-workflow/.github/actions/setup_apt@main

- name: Install CMake
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
uses: dice-group/cpp-conan-release-reusable-workflow/.github/actions/add_conan_provider@main

- name: Configure CMake
run: cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=On -DBUILD_EXAMPLES=On -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -DCONAN_INSTALL_ARGS="--build=missing;-o=boost/*:header_only=True" -G Ninja -B build .
run: cmake -DCMAKE_CXX_FLAGS="-fsanitize=address,undefined" -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=On -DBUILD_EXAMPLES=On -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -G Ninja -B build .
env:
CC: ${{ steps.install_cc.outputs.cc }}
CXX: ${{ steps.install_cc.outputs.cxx }}
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ project(
option(BUILD_TESTING "build tests" OFF)
option(BUILD_EXAMPLES "build examples" OFF)

if (PROJECT_IS_TOP_LEVEL)
set(CONAN_INSTALL_ARGS "${CONAN_INSTALL_ARGS};-o=boost/*:header_only=True")
endif ()

# conan requires cmake build type to be specified and it is generally a good idea
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/CMakeCache.txt)
if (NOT CMAKE_BUILD_TYPE)
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ It contains:
- `for_{types,values,range}`: Compile time for loops for types, values or ranges
- `polymorphic_allocator`: Like `std::pmr::polymorphic_allocator` but with static dispatch
- `DICE_DEFER`/`DICE_DEFER_TO_SUCCES`/`DICE_DEFER_TO_FAIL`: On-the-fly RAII for types that do not support it natively (similar to go's `defer` keyword)
- `overloaded`: Composition for `std::variant` visitor lambdas
- `flex_array`: A combination of `std::array` and `std::span`
- `tuple_algorithms`: Some algorithms for iterating tuples
- `generator`: The reference implementation of `std::generator` from [P2502R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2502r2.pdf)

## Usage

Expand Down Expand Up @@ -71,6 +75,13 @@ Some algorithms for iterating tuples, for example `tuple_fold` a fold/reduce imp
A combination of `std::array` and `std::span` where the size is either statically known or a runtime variable
depending on the `extent` template parameter

### `generator`
The reference implementation of `std::generator` from [P2502R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2502r2.pdf).
By default, the generator and corresponding utilities are exported under the `dice::template_library::` namespace.
If you want this generator to serve as a drop in replacement for `std::generator` until it arrives
use `#define DICE_TEMPLATELIBRARY_GENERATOR_STD_COMPAT 1` before including the generator header. That will export
all generator related things under namespace `std::`.

### Further Examples

Compilable code examples can be found in [examples](./examples). The example build requires the cmake
Expand Down
7 changes: 7 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ target_link_libraries(example_flex_array
PRIVATE
dice-template-library::dice-template-library
)

add_executable(example_generator
example_generator.cpp)
target_link_libraries(example_generator
PRIVATE
dice-template-library::dice-template-library
)
46 changes: 46 additions & 0 deletions examples/example_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <string>

#include <dice/template-library/generator.hpp>

namespace dtl = dice::template_library;

template<typename T>
struct Tree {
T value;
Tree *left = nullptr;
Tree *right = nullptr;

[[nodiscard]] dtl::generator<T const &> traverse_inorder() const {
if (left != nullptr) {
co_yield dtl::ranges::elements_of(left->traverse_inorder());
}

co_yield value;

if (right != nullptr) {
co_yield dtl::ranges::elements_of(right->traverse_inorder());
}
}
};

int main() {
// D
// B F
// A C E G
Tree<char> leaf1{'A'};
Tree<char> leaf2{'C'};
Tree<char> leaf3{'E'};
Tree<char> leaf4{'G'};
Tree<char> branch1{'B', &leaf1, &leaf2};
Tree<char> branch2{'F', &leaf3, &leaf4};
Tree<char> root{'D', &branch1, &branch2};

std::string output;
for (char const x : root.traverse_inorder()) {
output.push_back(x);
}

assert(output == "ABCDEFG");
std::cout << output << '\n';
}
Loading

0 comments on commit 206d781

Please sign in to comment.