Created
December 22, 2020 11:07
-
-
Save qis/b9f05b2ad7a5f6aeb1ff2d35edd82dc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fmt/format.h> | |
#include <range/v3/view.hpp> | |
#include <vector> | |
struct event { | |
int created = 0; | |
int status = 0; | |
}; | |
std::vector<event> create_events() { | |
std::vector<event> v; | |
v.push_back({ 101, 1 }); | |
v.push_back({ 102, 2 }); | |
v.push_back({ 103, 1 }); | |
v.push_back({ 104, 2 }); | |
v.push_back({ 201, 2 }); | |
v.push_back({ 202, 1 }); | |
v.push_back({ 203, 1 }); | |
v.push_back({ 204, 2 }); | |
v.push_back({ 205, 2 }); | |
v.push_back({ 206, 1 }); | |
v.push_back({ 207, 1 }); | |
return v; | |
} | |
int main() { | |
const auto events = create_events(); | |
constexpr auto group_events = ranges::views::group_by([](const auto& lhs, const auto& rhs) { | |
return lhs.created / 100 == rhs.created / 100; | |
}); | |
constexpr auto drop_1 = ranges::views::drop_while([](const auto& event) { | |
return event.status == 1; | |
}); | |
constexpr auto drop_2 = ranges::views::drop_while([](const auto& event) { | |
return event.status == 2; | |
}); | |
constexpr auto filter_order = ranges::views::adjacent_filter([](const event& lhs, const event& rhs) { | |
return lhs.status != rhs.status; | |
}); | |
constexpr auto reverse = ranges::views::reverse; | |
constexpr auto filter_group = drop_2 | reverse | drop_1 | reverse | filter_order; | |
for (const auto& group : events | group_events) { | |
fmt::print("group: {}\n", group.front().created / 100); | |
for (const auto& event : group | filter_group) { | |
fmt::print("{} -> {}\n", event.created, event.status); | |
} | |
fmt::print("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: