GithubHelp home page GithubHelp logo

dice-group / dice-template-library Goto Github PK

View Code? Open in Web Editor NEW
2.0 7.0 0.0 179 KB

This template library is a collection of template-oriented code that we, the Data Science Group at UPB, found pretty handy.

Home Page: https://dice-research.org

License: MIT License

CMake 5.41% C++ 91.89% Python 2.70%
cpp20 switch-case template tuple

dice-template-library's Introduction

Dice Template Library

This template library is a collection of template-oriented code that we, the Data Science Group at UPB, found pretty handy. It contains:

  • switch_cases: Use runtime values in compile-time context.
  • integral_template_tuple: Create a tuple-like structure that instantiates a template for a range of values.
  • integral_template_variant: A wrapper type for std::variant guarantees to only contain variants of the form T<ix> where $\texttt{ix}\in [\texttt{first},\texttt{last}]$ (inclusive).
  • 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)

Usage

switch_cases

Use runtime values in compile-time context. This is realised by instantiating ranges of values at compile-time and dispatching to the correct version at runtime. You can add fallbacks for when the runtime value lies outside the range defined. By using switch_cases inside of switch_cases multidimensional ranges can be handled as well. Examples can be found here.

integral_template_tuple

Create a tuple-like structure that instantiates a template for a range of values. Let's say you have a type like

template <std::size_t N> struct my_type{...};

Then you can create a tuple consisting of my_type<i>, my_type<i+1>, ... up to my_type<j> for i<=j with this code. Negative indices, recasting to fewer values and non-default construction is also possible. Examples can be found here.

integral_template_variant

Creates a variant-like structure that instantiates a template for a range of values. Let's say you have a type like

template <std::size_t N> struct my_type{...};

Then you can create a variant consisting of my_type<i>, my_type<i+1>, ..., my_type<j> with the help of integral_template_variant<my_type, i, j>. Negative indices, and j <= i are also possible. Examples can be found here.

for_{types,values,range}

Different flavors of compile time loops that allow to iterate types, values or ranges at compile time. Types and values are provided as template arguments and a lambda to be called for each of them is passed as function argument, e.g. for_types<uint8_t, uint64_t>([]<typename T>() {}) and for_values<1, 1.1, 'c'>([](auto x) {}). Ranges are defined by template parameters for start and exclusive end and receive a function to be applied to each range element as function argument, e.g. for_range<3, 5>([](auto x) {}), including support for decreasing ranges and negative indices, e.g. for_range<2, -4>([](auto x) {}). Examples can be found here.

polymorphic_allocator

A std::pmr::polymorphic_allocator-like type that uses static dispatch instead of dynamic dispatch to choose the allocator. This allocator is primarily useful for situations where you have inhomogeneous memory and one of the memory types does not allow dynamic dispatch using vtables; but you still want to mix and match values from both memory types.

For example, you might have some allocations in persistent or shared memory (or generally: memory-mapped allocations) and others on the heap. The problem with mmap allocations is that they will be placed at an arbitrary position in virtual memory each time they are loaded, therefore absolute pointers will cause segfaults if the segment is reloaded. Which means: vtables will not work (because they use absolute pointers) and therefore you cannot use std::pmr::polymorphic_allocator.

DICE_DEFER/DICE_DEFER_TO_SUCCES/DICE_DEFER_TO_FAIL

A mechanism similar to go's defer keyword, which can be used to defer some action to scope exit. The primary use-case for this is on-the-fly RAII-like resource management for types that do not support RAII (for example C types). Usage examples can be found here.

tuple algorthims

Some algorithms for iterating tuples, for example tuple_fold a fold/reduce implementation for tuples.

flex_array

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

Further Examples

Compilable code examples can be found in examples. The example build requires the cmake option -DBUILD_EXAMPLES=ON to be added.

Requirements

A C++20 compatible compiler. Code was only tested on x86_64.

Include it into your projects

CMake

add

FetchContent_Declare(
        dice-template-library
        GIT_REPOSITORY "https://github.com/dice-group/dice-template-library.git"
        GIT_TAG v1.5.1
        GIT_SHALLOW TRUE)

FetchContent_MakeAvailable(dice-template-library)

to your CMakeLists.txt

You can now add it to your target with:

target_link_libraries(your_target
        dice-template-library::dice-template-library
        )

conan

You can use it with conan. To do so, you need to add dice-template-library/1.5.1 to the [requires] section of your conan file.

Build and Run Tests and Examples

# get it 
git clone https://github.com/dice-group/dice-template-library.git
cd dice-template-library
# build it
mkdir build
cd build
cmake -DBUILD_TESTING=ON -DBUILD_EXAMPLES=ON ..
make -j$(nproc)
# run tests
make run_tests
# run examples
./examples/examples_integral_template_tuple
./examples/examples_switch_cases

dice-template-library's People

Contributors

bigerl avatar clueliss avatar lukaskerk avatar mcb5637 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

dice-template-library's Issues

Checksum mismatch for version 1.0.0

Hello!

I would like to understand why the sha256 of https://github.com/dice-group/dice-template-library/archive/refs/tags/v1.0.0.tar.gz is now presenting a different result.

Before: 485505ad3f9fb033083e2952bd8b4e68f6b4f123746b20f4ec3af46f4ce66cfe
Now: d0c75ec4861e2480dc7d6533125f9c2dfb3574fc2cffd15c66665b1cdfa66561

Steps to reproduce:

wget https://github.com/dice-group/dice-template-library/archive/refs/tags/v1.0.0.tar.gz
sha256sum v1.0.0.tar.gz

Did you re-upload the same package?

Add `variant2`

Add variant2 a replacement type for std::variant<T, U> (2 types).
Similar to the specializations for polymorphic_allocator it might make sense to add a variant2 type that is implemented as union + bool discriminant, to avoid vfunction calls of std::variant.

update package on conan

This will probably involve bigger changes to the files at conan center because our last release there was still conan 1.x and now accept only conan 2.x packages AFAIK.

When updatingplease also address this: #7

Feature: `switch_cases` for booleans

Currently switch_cases does not really work for booleans as the upper index is exclusive (and true + 1 doesn't really have a meaning).

Suggestion: add switch_bool that switches over the 2 values of a bool.

switch_bool(some_boolean, []<bool B>(std::bool_constant<B>) {
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.