Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload slides. #69

Merged
merged 1 commit into from
Oct 23, 2021
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
Binary file added A bite of pattern matching in C++.pdf
Binary file not shown.
22 changes: 0 additions & 22 deletions Abstract.md

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright [2021] [Bowen Fu]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
50 changes: 50 additions & 0 deletions sample/Red-black-Tree-Rebalancing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bool operator==(Node<T> const &lhs, Node<T> const &rhs)
lhs.value == rhs.value && lhs.rhs == rhs.rhs;
}

#if 0
template <typename T>
void Node<T>::balance()
{
Expand Down Expand Up @@ -81,6 +82,55 @@ void Node<T>::balance()
);
}

#else

template <typename T>
void Node<T>::balance()
{
using namespace matchit;

constexpr auto dsN = [](auto &&color, auto &&lhs, auto &&value, auto &&rhs)
{
return and_(app(&Node<T>::color, color), app(&Node<T>::lhs, lhs),
app(&Node<T>::value, value), app(&Node<T>::rhs, rhs));
};

constexpr auto blackN = [dsN](auto &&lhs, auto &&value, auto &&rhs)
{
return dsN(Black, lhs, value, rhs);
};

constexpr auto redN = [dsN](auto &&lhs, auto &&value, auto &&rhs)
{
return dsN(Red, lhs, value, rhs);
};

Id<std::shared_ptr<Node<T>>> a, b, c, d;
Id<T> x, y, z;
Id<Node> self;
*this = match(*this)(
pattern | blackN(some(redN(some(redN(a, x, b)), y, c)), z, d) // left-left case
= [&]
{ return Node{Red, std::make_shared<Node>(Black, *a, *x, *b), *y,
std::make_shared<Node>(Black, *c, *z, *d)}; },
pattern | blackN(some(redN(a, x, some(redN(b, y, c)))), z, d) // left-right case
= [&]
{ return Node{Red, std::make_shared<Node>(Black, *a, *x, *b), *y,
std::make_shared<Node>(Black, *c, *z, *d)}; },
pattern | blackN(a, x, some(redN(some(redN(b, y, c)), z, d))) // right-left case
= [&]
{ return Node{Red, std::make_shared<Node>(Black, *a, *x, *b), *y,
std::make_shared<Node>(Black, *c, *z, *d)}; },
pattern | blackN(a, x, some(redN(b, y, some(redN(c, z, d))))) // right-right case
= [&]
{ return Node{Red, std::make_shared<Node>(Black, *a, *x, *b), *y,
std::make_shared<Node>(Black, *c, *z, *d)}; },
pattern | self = expr(self) // do nothing
);
}

#endif

int main()
{
auto x = std::make_shared<Node<int>>(Color::Red, std::shared_ptr<Node<int>>{},
Expand Down