Skip to content

Commit

Permalink
fix the bug of query_set.h
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda7xx committed Jul 17, 2023
1 parent 66f72db commit 42178e7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
6 changes: 1 addition & 5 deletions lib/utils/include/utils/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ typename It::value_type product(It begin, It end) {
});
}

// template <typename Container>
// bool contains(Container const &c, typename Container::C::key_type const &e) {
// return find<Container>(c, e) != c.cend();
// }

template <typename Container>
bool contains(Container const &c, typename Container::value_type const &e) {
return find<Container>(c, e) != c.cend();
Expand Down Expand Up @@ -169,6 +164,7 @@ std::unordered_map<K, V> filter_keys(std::unordered_map<K, V> const &m,
result.insert(kv);
}
}
std::cout<<"filter_keys,result.size():"<<result.size()<<std::endl;
return result;
}

Expand Down
16 changes: 13 additions & 3 deletions lib/utils/include/utils/graph/query_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,26 @@ template <typename C,
typename V = typename C::mapped_type>
std::unordered_map<K, V> query_keys(query_set<K> const &q, C const &m) {
std::cout << "3" << std::endl;
if(is_matchall(q)) {
return m;
}
std::unordered_set<K> q_set = allowed_values(q);

auto filter_lambda = [&q_set](K const &key) {
return q_set.find(key) != q_set.end();
};

return filter_keys(m, filter_lambda);
} // TODO
}

template <typename K, typename V>
std::unordered_map<K, V> query_keys(query_set<V> const &q,
bidict<K, V> const &m) {
std::unordered_set<V> q_set = allowed_values(q);
if(is_matchall(q)) {
auto filter_lambda = [](V const &value) { return true; };
return filter_values(m, filter_lambda);
}

std::unordered_set<V> q_set = allowed_values(q);
auto filter_lambda = [&q_set](V const &value) {
return q_set.find(value) != q_set.end();
};
Expand All @@ -98,6 +104,10 @@ template <typename C,
typename V = typename C::mapped_type>
std::unordered_map<K, V> query_values(query_set<V> const &q, C const &m) {
std::cout << "4" << std::endl;
if(is_matchall(q)) {
return m;
}

std::unordered_set<V> q_set = allowed_values(q);

auto filter_lambda = [&q_set](V const &value) {
Expand Down
26 changes: 23 additions & 3 deletions lib/utils/src/graph/adjacency_multidigraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,38 @@ void AdjacencyMultiDiGraph::remove_edge(MultiDiEdge const &e) {
std::unordered_set<MultiDiEdge>
AdjacencyMultiDiGraph::query_edges(MultiDiEdgeQuery const &q) const {
std::unordered_set<MultiDiEdge> result;
// //std::cout<<"q.srcs:"<<q.srcs.value()<<" and q.dsts:"<<q.dsts.value()<< " and q.srcIdxs:"<<q.srcIdxs.value()<<" and q.dstIdxs:"<<q.dstIdxs.value()<<std::endl;
// std::cout<<"AdjacencyMultiDiGraph::query_edges"<<std::endl;
// for(auto kv : this->adjacency) {
// std::cout<<"this->adjacency node1:"<<kv.first.value()<<std::endl;
// for(auto node_nodeport: kv.second) {
// std::cout<<"this->adjacency node2:"<<node_nodeport.first.value()<<std::endl;
// for(auto nodeport_nodeport: node_nodeport.second) {
// std::cout<<"this->adjacency NodePort1:"<<nodeport_nodeport.first.value()<<std::endl;
// for(auto nodeport: nodeport_nodeport.second) {
// std::cout<<"this->adjacency NodePort2:"<<nodeport.value()<<std::endl;
// }
// }
// }
// std::cout<<"*********"<<std::endl;
// }

// auto src_kvs = query_keys(q.srcs, this->adjacency);
// std::cout<<"x.size:"<<src_kvs.size()<<std::endl;
// for(auto const &x:src_kvs) {
// std::cout<<"x.first:"<<x.first.value() <<" and x.second.size()"<<x.second.size()<<std::endl;
// }
for (auto const &src_kv : query_keys(q.srcs, this->adjacency)) {
for (auto const &dst_kv : query_keys(q.dsts, src_kv.second)) {
for (auto const &srcIdx_kv : query_keys(q.srcIdxs, dst_kv.second)) {
for (auto const &dstIdx : apply_query(q.dstIdxs, srcIdx_kv.second)) {
std::cout << "add edge " << src_kv.first.value() << " "
<< dst_kv.first.value() << " " << srcIdx_kv.first.value()
<< " " << dstIdx.value() << std::endl;
result.insert({src_kv.first, dst_kv.first, srcIdx_kv.first, dstIdx});
}
}
}

}
std::cout<<"query_edges, result.size():"<<result.size()<<std::endl;
return result;
}

Expand Down
36 changes: 19 additions & 17 deletions lib/utils/test/src/test_adjacency_multidigraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ TEST_CASE("AdjacencyMultiDiGraph:basic_test") {
query_set<Node> q{nodes};
CHECK(g.query_nodes(NodeQuery(q)) == std::unordered_set<Node>{n0, n2});

// CHECK(g.query_edges(MultiDiEdgeQuery::all()) ==
// std::unordered_set<MultiDiEdge>{e1, e2, e3, e4});

// CHECK(g.query_edges({}) == std::unordered_set<MultiDiEdge>{e1, e2, e3,
// e4});

CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_node(n1)) ==
std::unordered_set<MultiDiEdge>{});
CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_node(n1)) ==
Expand All @@ -39,22 +41,22 @@ TEST_CASE("AdjacencyMultiDiGraph:basic_test") {
std::unordered_set<MultiDiEdge>{});
CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idx(p1)) ==
std::unordered_set<MultiDiEdge>{e1, e4});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_nodes({n1, n2})) ==
// std::unordered_set<MultiDiEdge>{e3, e4});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_nodes({n0, n2})) ==
// std::unordered_set<MultiDiEdge>{e2, e3});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_idxs({p1, p2})) ==
// std::unordered_set<MultiDiEdge>{e3, e4});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idxs({p0, p2})) ==
// std::unordered_set<MultiDiEdge>{e2, e3});
// CHECK(g.query_edges(MultiDiEdgeQuery::all()
// .with_src_node(n1)
// .with_dst_node(n2)
// .with_src_idx(p1)
// .with_dst_idx(p2)) ==
// std::unordered_set<MultiDiEdge>{});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idx(p2)) ==
// std::unordered_set<MultiDiEdge>{e2});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_nodes({n1, n2})) ==
// std::unordered_set<MultiDiEdge>{e3, e4});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_nodes({n0, n2})) ==
// std::unordered_set<MultiDiEdge>{e2, e3});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_idxs({p1, p2})) ==
// std::unordered_set<MultiDiEdge>{e3, e4});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idxs({p0, p2})) ==
// std::unordered_set<MultiDiEdge>{e2, e3});
// CHECK(g.query_edges(MultiDiEdgeQuery::all()
// .with_src_node(n1)
// .with_dst_node(n2)
// .with_src_idx(p1)
// .with_dst_idx(p2)) ==
// std::unordered_set<MultiDiEdge>{});
// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idx(p2)) ==
// std::unordered_set<MultiDiEdge>{e2});
}

// TEST_CASE("AdjacencyMultiDiGraph:remove_node") {
Expand Down

0 comments on commit 42178e7

Please sign in to comment.