23#ifndef VCL_SPACE_COMPLEX_GRAPH_BIPARTITE_GRAPH_H
24#define VCL_SPACE_COMPLEX_GRAPH_BIPARTITE_GRAPH_H
26#include "bipartite_iterators/adjacent_left_node_iterator.h"
27#include "bipartite_iterators/adjacent_right_node_iterator.h"
28#include "bipartite_iterators/node_iterator.h"
29#include "nodes/undirected_node.h"
31#include <vclib/types.h>
42template<
typename Graph,
typename Iterator>
43class AdjacentLeftNodeIterator;
45template<
typename Graph,
typename Iterator>
46class AdjacentRightNodeIterator;
50template<
class T1,
class T2>
53 template<
typename Graph,
typename Iterator>
54 friend class detail::AdjacentLeftNodeIterator;
56 template<
typename Graph,
typename Iterator>
57 friend class detail::AdjacentRightNodeIterator;
60 std::map<T1, unsigned int> mMapL;
61 std::map<T2, unsigned int> mMapR;
63 std::vector<UndirectedNode<T1>> mNodesL;
64 std::vector<UndirectedNode<T2>> mNodesR;
66 std::set<unsigned int> mUnusedLNodes;
67 std::set<unsigned int> mUnusedRNodes;
83 using AdjacentLeftNodeIterator = detail::AdjacentLeftNodeIterator<
85 std::unordered_set<unsigned int>::const_iterator>;
86 using AdjacentRightNodeIterator = detail::AdjacentRightNodeIterator<
88 std::unordered_set<unsigned int>::const_iterator>;
105 return mMapL.find(
lNode) != mMapL.end();
115 return mMapR.find(
rNode) != mMapR.end();
124 return (
unsigned int) (mNodesL.size() - mUnusedLNodes.size());
133 return (
unsigned int) (mNodesR.size() - mUnusedRNodes.size());
144 return mNodesL[
uid].sizeAdjacentNodes();
155 return mNodesR[
vid].sizeAdjacentNodes();
166 if (mMapL.find(info) == mMapL.end()) {
167 if (mUnusedLNodes.size() == 0) {
168 mMapL[info] = (
unsigned int) mNodesL.size();
169 mNodesL.emplace_back(info);
172 unsigned int id = *(mUnusedLNodes.begin());
173 mUnusedLNodes.erase(mUnusedLNodes.begin());
191 if (mMapR.find(info) == mMapR.end()) {
192 if (mUnusedRNodes.size() == 0) {
193 mMapR[info] = (
unsigned int) mNodesR.size();
194 mNodesR.emplace_back(info);
197 unsigned int id = *(mUnusedRNodes.begin());
198 mUnusedRNodes.erase(mUnusedRNodes.begin());
216 mUnusedLNodes.insert(mMapL[
lNode]);
232 mUnusedRNodes.insert(mMapR[
rNode]);
252 assert((
unsigned int)
uid < mNodesL.size());
253 assert((
unsigned int)
vid < mNodesR.size());
254 mNodesL[
uid].addAdjacent(
vid);
255 mNodesR[
vid].addAdjacent(
uid);
275 assert((
unsigned int)
uid < mNodesL.size());
276 assert((
unsigned int)
vid < mNodesR.size());
277 mNodesL[
uid].deleteAdjacent(
vid);
278 mNodesR[
vid].deleteAdjacent(
uid);
296 for (
unsigned int adj : mNodesL[
uid]) {
297 mNodesR[
adj].deleteAdjacent(
uid);
299 mNodesL[
uid].clearAdjacentNodes();
317 for (
unsigned int adj : mNodesR[
vid]) {
318 mNodesL[
adj].deleteAdjacent(
vid);
320 mNodesR[
vid].clearAdjacentNodes();
337 int uid = getIdLeftNode(
old);
357 int vid = getIdRightNode(
old);
368 AdjacentLeftNodeIterator adjacentLeftNodeBegin(
const T1&
lNode)
const
371 return AdjacentLeftNodeIterator(*
this, mNodesL[
uid].begin());
374 AdjacentLeftNodeIterator adjacentLeftNodeEnd(
const T1& lNode)
const
376 int uid = getIdLeftNode(lNode);
377 return AdjacentLeftNodeIterator(*
this, mNodesL[uid].end());
380 AdjacentRightNodeIterator adjacentRightNodeBegin(
const T2& rNode)
const
382 int vid = getIdRightNode(rNode);
383 return AdjacentRightNodeIterator(*
this, mNodesR[vid].begin());
386 AdjacentRightNodeIterator adjacentRightNodeEnd(
const T2& rNode)
const
388 int vid = getIdRightNode(rNode);
389 return AdjacentRightNodeIterator(*
this, mNodesR[vid].end());
392 LeftNodeIterator leftNodeBegin()
const
394 return LeftNodeIterator(mNodesL.begin());
397 LeftNodeIterator leftNodeEnd()
const
399 return LeftNodeIterator(mNodesL.end());
402 RightNodeIterator rightNodeBegin()
const
404 return RightNodeIterator(mNodesR.begin());
407 RightNodeIterator rightNodeEnd()
const
409 return RightNodeIterator(mNodesR.end());
412 LeftNodeView leftNodes()
const
414 return LeftNodeView(leftNodeBegin(), leftNodeEnd());
417 RightNodeView rightNodes()
const
419 return RightNodeView(rightNodeBegin(), rightNodeEnd());
422 AdjacentLeftNodeView adjacentLeftNodes(
const T1& lNode)
const
424 return AdjacentLeftNodeView(
425 adjacentLeftNodeBegin(lNode), adjacentLeftNodeEnd(lNode));
428 AdjacentRightNodeView adjacentRightNodes(
const T2& rNode)
const
430 return AdjacentRightNodeView(
431 adjacentRightNodeBegin(rNode), adjacentRightNodeEnd(rNode));
435 int getIdLeftNode(
const T1& uNode)
const {
return mMapL.at(uNode); }
437 int getIdRightNode(
const T2& vNode)
const {
return mMapR.at(vNode); }
Definition bipartite_graph.h:52
bool setRightNode(const T2 &old, const T2 &newInfo)
Sets the key of a rNode.
Definition bipartite_graph.h:354
bool setLeftNode(const T1 &old, const T1 &newInfo)
Sets the key of an lNode.
Definition bipartite_graph.h:334
bool addArc(const T1 &lNode, const T2 &rNode)
Creates an arc between lNode and rNode.
Definition bipartite_graph.h:247
BipartiteGraph()
Default constructor. It creates an empty Bipartite Graph.
Definition bipartite_graph.h:96
bool leftNodeExists(const T1 &lNode) const
Checks if a node exists on the left side of the graph.
Definition bipartite_graph.h:103
uint leftNodesNumber() const
Returns the number of left nodes of the graph.
Definition bipartite_graph.h:122
bool rightNodeExists(const T2 &rNode) const
Checks if a node exists on the right side of the graph.
Definition bipartite_graph.h:113
bool deleteLeftNode(const T1 &lNode)
Removes lNode and all its arcs from the graph.
Definition bipartite_graph.h:213
bool addRightNode(const T2 &info)
Adds a new node on the right side of the graph.
Definition bipartite_graph.h:189
uint adjacentLeftNodeNumber(const T1 &lNode) const
Returns the number of adjacent nodes to lNode.
Definition bipartite_graph.h:141
bool clearAdjacenciesRightNode(const T2 &rNode)
Removes all the arcs connected to rNode (lNode won't have adjacent nodes)
Definition bipartite_graph.h:313
bool clearAdjacenciesLeftNode(const T1 &lNode)
Removes all the arcs connected to lNode (lNode won't have adjacent nodes)
Definition bipartite_graph.h:292
bool deleteRightNode(const T2 &rNode)
Removes rNode and all its arcs from the graph.
Definition bipartite_graph.h:229
bool deleteArc(const T1 &lNode, const T2 &rNode)
Removes the arc between lNode and rNode.
Definition bipartite_graph.h:270
uint adjacentRightNodeNumber(const T2 &rNode) const
Returns the number of adjacent nodes to rNode.
Definition bipartite_graph.h:152
uint rightNodesNumber() const
Returns the number of right nodes of the graph.
Definition bipartite_graph.h:131
bool addLeftNode(const T1 &info)
Adds a new node on the left side of the graph.
Definition bipartite_graph.h:164
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43