Skip to content

Commit

Permalink
Add slides. (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: Bowen Fu <missing>
  • Loading branch information
BowenFu committed Oct 23, 2021
1 parent cb68c92 commit 3802273
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
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

0 comments on commit 3802273

Please sign in to comment.