https://github.com/zhongruoyu/metasort
Compile-time sorting implementations with C++ template metaprogramming.
https://github.com/zhongruoyu/metasort
cpp metaprogramming sorting template-metaprogramming
Last synced: 11 months ago
JSON representation
Compile-time sorting implementations with C++ template metaprogramming.
- Host: GitHub
- URL: https://github.com/zhongruoyu/metasort
- Owner: ZhongRuoyu
- License: mit
- Created: 2022-08-29T05:57:32.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-29T13:25:53.000Z (almost 4 years ago)
- Last Synced: 2025-02-24T06:46:51.952Z (over 1 year ago)
- Topics: cpp, metaprogramming, sorting, template-metaprogramming
- Language: C++
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# metasort
This is a collection of *compile-time* sorting implementations with C++
[template metaprogramming](https://en.wikipedia.org/wiki/Template_metaprogramming).
Currently, the following sorting algorithms are implemented:
- [Quicksort](https://en.wikipedia.org/wiki/Quicksort)
- [Mergesort](https://en.wikipedia.org/wiki/Merge_sort)
All the implementation details can be found in [`metasort.h`](metasort.h).
## Examples
The metasort algorithms work on an integer sequence template,
named `metasort::sequence`. Its use is analogous to
[`std::integer_sequence`](https://en.cppreference.com/w/cpp/utility/integer_sequence).
The following illustrates how sorting can be performed on a sequence.
```c++
using seq_original = metasort::sequence;
using seq_quicksort = metasort::quicksort::sort_t;
```
In this example, we create a sequence of `int`s, and perform quicksort on the
sequence. The results are computed and stored into `seq_quicksort` during
compilation. To verify that this works:
```c++
static_assert(
std::is_same_v>,
"quicksort assertion failed");
```
If the result `seq_quicksort` is not sorted, the `static_assert` assertion
would have failed during compilation.
The `metasort::sequence` type also comes with several helper templates. For
instance, the `metasort::get` template gets the value at a specified index. The
type also provides a `to_array()` member function, which turns the sequence
into an `std::array` for ease of further manipulation.
See [example.cc](example.cc) for a full example.
## License
Copyright (c) 2022 Zhong Ruoyu. Licensed under the [MIT License](LICENSE).