Skip to content
Open
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
23 changes: 23 additions & 0 deletions lib/utils/include/utils/bidict/algorithms/filter_bidict.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _FLEXFLOW_LIB_UTILS_INCLUDE_UTILS_BIDICT_ALGORITHMS_FILTER_BIDICT_H
#define _FLEXFLOW_LIB_UTILS_INCLUDE_UTILS_BIDICT_ALGORITHMS_FILTER_BIDICT_H

#include "utils/bidict/bidict.h"

namespace FlexFlow {

template <typename L, typename R, typename F>
bidict<L, R> filter_bidict(bidict<L, R> const &b, F &&f) {
bidict<L, R> result;

for (std::pair<L, R> const &p : b) {
if (f(p.first, p.second)) {
result.equate_strict(p.first, p.second);
}
}

return result;
}

} // namespace FlexFlow

#endif
12 changes: 12 additions & 0 deletions lib/utils/src/utils/bidict/algorithms/filter_bidict.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "utils/bidict/algorithms/filter_bidict.h"
#include "utils/archetypes/value_type.h"

namespace FlexFlow {

using L = value_type<0>;
using R = value_type<1>;
using F = std::function<bool(L const &, R const &)>;

template bidict<L, R> filter_bidict(bidict<L, R> const &, F &&);

} // namespace FlexFlow
28 changes: 28 additions & 0 deletions lib/utils/test/src/utils/bidict/algorithms/filter_bidict.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "utils/bidict/algorithms/filter_bidict.h"
#include <doctest/doctest.h>

using namespace ::FlexFlow;

TEST_SUITE(FF_TEST_SUITE) {
TEST_CASE("filter_bidict") {
bidict<int, std::string> b = {
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
};

auto filter_func = [](int k, std::string const &v) -> bool {
return (k % 2) == 0 || v.size() == 3;
};

bidict<int, std::string> result = filter_bidict(b, filter_func);
bidict<int, std::string> correct = {
{1, "one"},
{2, "two"},
{4, "four"},
};

CHECK(result == correct);
}
}
Loading