6 #include <unordered_map>
15 namespace datastructure {
109 template<
typename K,
typename E1 = K,
typename E2 = E1>
114 unordered_map<K, Element<E1>* > vertices;
116 unordered_map<K, SLelement<Edge<K, E2> >*> adj_list;
119 static const int LargeGraphVertSize = 2000;
121 bool forceLargeViz =
false;
122 bool forceSmallViz =
false;
132 for (
auto& v : vertices) {
133 if (adj_list[v.first]) {
137 adj_list[v.first]; sle !=
nullptr; ) {
142 adj_list[v.first] =
nullptr;
144 delete vertices[v.first];
145 vertices[v.first] =
nullptr;
155 if (forceLargeViz || (!forceSmallViz &&
156 vertices.size() > LargeGraphVertSize &&
157 areAllVerticesLocated())) {
160 return "GraphAdjacencyList";
178 if (vertices.find(k) == vertices.end()) {
181 adj_list[k] =
nullptr;
198 void addEdge(
const K& src,
const K& dest,
const E2& data = E2()) {
204 vertices[src]->links[vertices.at(dest)];
211 Edge<K, E2> (src, dest, &(vertices[src]->links[vertices.at(dest)]), data), conv.str());
214 catch (
const out_of_range& ) {
215 cerr <<
"addEdge(): Nonexistent vertex?" << endl <<
216 "Create vertices first prior to adding edges that use that vertex" << endl
217 <<
"Cannot add edge between non-existent verticies"
229 bool isEdge(
const K& src,
const K& dest) {
233 for (
auto& e : outgoingEdgeSetOf(src))
234 if (e.to() == dest) {
235 cout <<
"found it!" << src <<
"," << dest << endl;
239 catch (
const out_of_range& ) {
254 return (vertices.at(src))->
getValue();
256 catch (
const out_of_range& ) {
257 cerr <<
"getVertexData(): vertex not found" << endl;
261 throw "getVertexData(): vertex not found";
275 catch (
const out_of_range& ) {
276 cerr <<
"setVertexData(): Nonexistent vertex" << endl;
279 catch (
const char* msg) {
298 if (ed.
to() == dest) {
303 throw "Edge not found!";
305 catch (
const out_of_range& ) {
306 cerr <<
"getEdgeData(): Edge not found" << endl;
309 catch (
const char* msg) {
313 throw "getEdgeData(): Edge not found";
330 if (ed.getVertex() == dest) {
335 throw "Edge not found!";
337 catch (
const out_of_range& ) {
338 cerr <<
"getEdgeData(): Edge not found" << endl;
341 catch (
const char* msg) {
345 throw "getEdgeData(): Edge not found";
363 if (ed.
to() == dest) {
370 throw "getEdgeData(): Edge not found!";
372 catch (
const out_of_range& ) {
373 cerr <<
"setEdgeData(): Nonexistent vertices or " <<
374 " edge not found" << endl;
377 catch (
const char* msg) {
381 throw "getEdgeData(): Edge not found";
407 return vertices.at(key);
409 catch (
const std::out_of_range& oor) {
423 return vertices.at(key);
425 catch (
const std::out_of_range& oor) {
445 while (sle !=
nullptr) {
447 if (edge_dest == dest)
452 catch (
const std::out_of_range& oor) {
454 std::cout <<
"one or both vertices are likely missing from graph\n";
457 throw "Edge not found";
464 const unordered_map<K, SLelement<Edge<K, E2> >*>&
479 return adj_list.at(k);
481 catch (
const out_of_range& ) {
482 cerr <<
"Cannot get adjacencyList of a non-existent vertex -- " << k <<
"\n";
497 return adj_list.at(k);
499 catch (
const out_of_range& ) {
500 cerr <<
"Cannot getAdjacencyList() of a non-existent vertex!"
520 catch (
const out_of_range& ) {
521 cerr <<
"Graph vertex " << k <<
" not found in graph!" << endl;
537 return getEdge(k1, k2).getLinkVisualizer();
539 catch (
const out_of_range& ) {
540 cerr <<
"Either source or destination node not found in graph!\n";
550 virtual const string getDataStructureRepresentation()
const override {
560 vertices.size() > LargeGraphVertSize &&
561 areAllVerticesLocated())) {
562 return getDataStructureRepresentationLargeGraph();
565 unordered_map<K, int> node_map;
567 string nodes_JSON =
"", links_JSON =
"";
569 for (
const auto& v : vertices) {
570 if (node_map.emplace(v.first, i).second) {
572 nodes_JSON += v.second->getElementRepresentation() +
COMMA;
578 if (nodes_JSON.size()) {
579 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
584 for (
const auto& v : vertices) {
586 Element<E1>* src_vert = vertices.at(
v.first);
588 for (SLelement<Edge<K, E2 >> * it = adj_list.at(
v.first); it !=
nullptr;
589 it = it->getNext()) {
590 Element<E1>* dest_vert = vertices.at(it->getValue().to() );
591 links_JSON += src_vert->getLinkRepresentation(
592 *(src_vert->getLinkVisualizer(dest_vert)),
599 if (links_JSON.size()) {
600 links_JSON = links_JSON.erase(links_JSON.size() - 1);
603 string graph_alist_json =
610 return graph_alist_json;
622 string getDataStructureRepresentationLargeGraph ()
const {
629 unordered_map<K, int> node_map;
631 string nodes_JSON =
"";
633 for (
const auto& v : vertices) {
634 if (node_map.emplace(
v.first, i).second) {
636 const ElementVisualizer *elvis =
637 vertices.at(
v.first)->getVisualizer();
639 if ( (elvis->getLocationX() != INFINITY) &&
640 (elvis->getLocationY() != INFINITY) ) {
648 elvis->getColor().getCSSRepresentation() +
655 if (nodes_JSON.size()) {
656 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
660 string links_JSON =
"";
661 for (
const auto& v : vertices) {
663 Element<E1>* src_vert = vertices.at(
v.first);
665 for (SLelement<Edge<K, E2 >> * it = adj_list.at(
v.first); it !=
nullptr;
666 it = it->getNext()) {
667 Element<E1>* dest_vert = vertices.at(it->getValue().to() );
668 LinkVisualizer *lv = src_vert->getLinkVisualizer(dest_vert);
670 string dest =
JSONencode(node_map.at(it->getValue().to()));
674 lv->getColor().getCSSRepresentation() +
680 if (links_JSON.size())
681 links_JSON = links_JSON.erase(links_JSON.size() - 1);
683 string graph_alist_json =
690 return graph_alist_json;
696 bool areAllVerticesLocated()
const {
697 for (
const auto& v : vertices) {
698 ElementVisualizer *elvis =
v.second->getVisualizer();
699 if (elvis->getLocationX() == INFINITY
700 || elvis->getLocationY() == INFINITY)
726 forceLargeViz =
true;
727 forceSmallViz =
false;
730 forceLargeViz =
false;
752 forceSmallViz =
true;
753 forceLargeViz =
false;
756 forceSmallViz =
false;
764 std::unordered_map<K, Element<E1>* >
const & underlying_map;
779 return this->it != it.it;
838 typename std::unordered_map<K, Element<E1>* > & underlying_map;
851 typename std::unordered_map<K, Element<E1>* >
::iterator it;
858 return this->it != it.it;
884 return this->it != it.it;
898 return iterator(underlying_map.begin());
902 return iterator(underlying_map.end());
929 typename std::unordered_map<K, Element<E1>* >
const & underlying_map;
945 return this->it != it.it;
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:74
This helper class is used by the graph classes - GraphAdjList , GraphAdjMatrix - to keep track of edg...
Definition: Edge.h:35
const K & to() const
Destination vertex accessor.
Definition: Edge.h:84
const E2 & getEdgeData() const
Return the edge data.
Definition: Edge.h:100
void setEdgeData(const E2 &data)
Sets edge data to "data".
Definition: Edge.h:92
void setValue(const E &val)
Sets generic object to "val".
Definition: Element.h:226
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:207
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:141
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:31
Definition: GraphAdjList.h:771
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:774
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:778
const K & operator*() const
Definition: GraphAdjList.h:782
const_iterator & operator++()
Definition: GraphAdjList.h:786
Definition: GraphAdjList.h:763
const_iterator end() const
Definition: GraphAdjList.h:796
KeySet_helper(std::unordered_map< K, Element< E1 > * > const &um)
Definition: GraphAdjList.h:767
const_iterator begin() const
Definition: GraphAdjList.h:792
This is a helper class to return sets of vertices in a way that are iterable with range for loops....
Definition: GraphAdjList.h:876
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:879
Element< E1 > const * operator*() const
Definition: GraphAdjList.h:887
const_iterator & operator++()
Definition: GraphAdjList.h:891
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:883
This is a helper class to return sets of vertices in a way that are iterable with range for loops....
Definition: GraphAdjList.h:850
bool operator!=(const iterator &it) const
Definition: GraphAdjList.h:857
iterator & operator++()
Definition: GraphAdjList.h:865
Element< E1 > * operator*()
Definition: GraphAdjList.h:861
iterator(typename std::unordered_map< K, Element< E1 > * >::iterator i)
Definition: GraphAdjList.h:853
This is a helper class to return sets of vertices in a way that are iterable with range for loops....
Definition: GraphAdjList.h:837
iterator end()
Definition: GraphAdjList.h:901
VertexElementSet_listhelper(std::unordered_map< K, Element< E1 > * > &um)
Definition: GraphAdjList.h:841
iterator begin()
Definition: GraphAdjList.h:897
const_iterator begin() const
Definition: GraphAdjList.h:905
const_iterator end() const
Definition: GraphAdjList.h:909
This is a helper class to return sets of vertices ina way that are iterable with range for loops....
Definition: GraphAdjList.h:937
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:940
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:944
const_iterator & operator++()
Definition: GraphAdjList.h:952
Element< E1 > const * operator*() const
Definition: GraphAdjList.h:948
This is a helper class to return sets of vertices in a way that are iterable with range for loops....
Definition: GraphAdjList.h:928
const_iterator end() const
Definition: GraphAdjList.h:962
const_iterator begin() const
Definition: GraphAdjList.h:958
constVertexElementSet_listhelper(std::unordered_map< K, Element< E1 > * > const &um)
Definition: GraphAdjList.h:932
This class provides methods to represent adjacency list based graphs.
Definition: GraphAdjList.h:110
KeySet_helper keySet() const
Definition: GraphAdjList.h:810
ElementVisualizer * getVisualizer(const K &k)
Returns the visualizer corresponding to a graph vertex. convenient method to set attributes of the gr...
Definition: GraphAdjList.h:514
virtual ~GraphAdjList() override
Definition: GraphAdjList.h:131
const SLelement< Edge< K, E2 > > * getAdjacencyList(const K &k) const
Definition: GraphAdjList.h:495
void setEdgeData(const K &src, const K &dest, E2 &data)
Loads edge specific information for the edge from "src" to "dest".
Definition: GraphAdjList.h:356
const unordered_map< K, SLelement< Edge< K, E2 > > * > & getAdjacencyList() const
Return the adjacency list.
Definition: GraphAdjList.h:465
Edge< K, E2 > getEdge(const K &src, const K &dest)
Get the edge between src and dest vertices.
Definition: GraphAdjList.h:439
const E1 & getVertexData(const K &src) &
Gets vertex data for a graph vertex.
Definition: GraphAdjList.h:251
void addVertex(const K &k, const E1 &e=E1())
Adds a vertex to the graph.
Definition: GraphAdjList.h:175
SLelement< Edge< K, E2 > >::SLelement_listhelper outgoingEdgeSetOf(K const &k)
This method is useful for iterating through a set of outgoing edges from a vertex.
Definition: GraphAdjList.h:819
E2 const & getEdgeData(const K &src, const K &dest) const
Gets edge data for the edge from "src" to "dest" - const version.
Definition: GraphAdjList.h:323
constVertexElementSet_listhelper vertexSet() const
Definition: GraphAdjList.h:970
void addEdge(const K &src, const K &dest, const E2 &data=E2())
Definition: GraphAdjList.h:198
void forceLargeVisualization(bool f)
Force the rendering engine to use large graph visualization.
Definition: GraphAdjList.h:724
const unordered_map< K, Element< E1 > * > * getVertices() const
Return the graph nodes - const version.
Definition: GraphAdjList.h:397
bool isEdge(const K &src, const K &dest)
Definition: GraphAdjList.h:229
void forceSmallVisualization(bool f)
Force the rendering engine to use small graph visualization.
Definition: GraphAdjList.h:750
VertexElementSet_listhelper vertexSet()
Definition: GraphAdjList.h:919
SLelement< Edge< K, E2 > > * getAdjacencyList(const K &k)
Returns adjacency list of a vertex with name k.
Definition: GraphAdjList.h:477
Element< E1 > * getVertex(const K &key)
Definition: GraphAdjList.h:421
E2 & getEdgeData(const K &src, const K &dest)
Gets edge data for the edge from "src" to "dest".
Definition: GraphAdjList.h:291
void setVertexData(const K &vertID, E1 const &data)
Loads vertex specific information for a graph vertex.
Definition: GraphAdjList.h:270
GraphAdjList(GraphAdjList &&gr)=default
const Element< E1 > * getVertex(const K &key) const
Return the vertex corresponding to a key.
Definition: GraphAdjList.h:405
virtual const string getDStype() const override
Get the string representation of this data structure type.
Definition: GraphAdjList.h:154
LinkVisualizer * getLinkVisualizer(const K &k1, const K &k2)
Returns the link visualizer corresponding to an edge. Returns the link visualizer corresponding to tw...
Definition: GraphAdjList.h:535
SLelement< Edge< K, E2 > >::SLelement_constlisthelper outgoingEdgeSetOf(K const &k) const
This method is useful for iterating through a set of outgoing edges from a vertex - const version.
Definition: GraphAdjList.h:828
unordered_map< K, Element< E1 > * > * getVertices()
Return the graph nodes.
Definition: GraphAdjList.h:388
This class maintains the visual properties of links within data structures.
Definition: LinkVisualizer.h:26
The singly linked list element, derived from Element.
Definition: SLelement.h:26
virtual SLelement * getNext()
Returns the next element in the list.
Definition: SLelement.h:74
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
Support for drawing Bar charts.
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54
const string QUOTE
Definition: DataStructure.h:50