6 #include <unordered_map> 15 namespace datastructure {
33 template<
typename K,
typename E1 = K,
typename E2 = E1>
34 class GraphAdjList :
public DataStructure {
38 unordered_map<K, Element<E1>* > vertices;
40 unordered_map<K, SLelement<Edge<K, E2> >*> adj_list;
43 static const int LargeGraphVertSize = 2000;
45 bool forceLargeViz =
false;
46 bool forceSmallViz =
false;
49 GraphAdjList(
const GraphAdjList& gr) =
delete;
50 const GraphAdjList& operator= (
const GraphAdjList& gr) =
delete;
53 GraphAdjList() =
default;
54 GraphAdjList(GraphAdjList&& gr) =
default;
57 for (
auto& v : vertices) {
58 if (adj_list[v.first]) {
62 adj_list[v.first]; sle !=
nullptr; ) {
67 adj_list[v.first] =
nullptr;
69 delete vertices[v.first];
70 vertices[v.first] =
nullptr;
78 if (forceLargeViz || (!forceSmallViz &&
79 vertices.size() > LargeGraphVertSize &&
80 areAllVerticesLocated())) {
83 return "GraphAdjacencyList";
99 if (vertices.find(k) == vertices.end()) {
102 adj_list[k] =
nullptr;
119 void addEdge(
const K& src,
const K& dest,
const E2& data = E2()) {
125 vertices[src]->links[vertices.at(dest)];
132 Edge<K, E2> (src, dest, &(vertices[src]->links[vertices.at(dest)]), data), conv.str());
135 catch (
const out_of_range& ) {
136 cerr <<
"addEdge(): Nonexistent vertex?" << endl <<
137 "Create vertices first prior to adding edges that use that vertex" << endl
138 <<
"Cannot add edge between non-existent verticies" 153 return (vertices.at(src))->getValue();
155 catch (
const out_of_range& ) {
156 cerr <<
"getVertexData(): vertex not found" << endl;
160 throw "getVertexData(): vertex not found";
174 catch (
const out_of_range& ) {
175 cerr <<
"setVertexData(): Nonexistent vertex" << endl;
178 catch (
const char* msg) {
197 if (ed.getVertex() == dest) {
202 throw "Edge not found!";
204 catch (
const out_of_range& ) {
205 cerr <<
"getEdgeData(): Edge not found" << endl;
208 catch (
const char* msg) {
212 throw "getEdgeData(): Edge not found";
229 if (ed.getVertex() == dest) {
234 throw "Edge not found!";
236 catch (
const out_of_range& ) {
237 cerr <<
"getEdgeData(): Edge not found" << endl;
240 catch (
const char* msg) {
244 throw "getEdgeData(): Edge not found";
264 if (ed.getVertex() == dest) {
271 throw "getEdgeData(): Edge not found!";
273 catch (
const out_of_range& ) {
274 cerr <<
"setEdgeData(): Nonexistent vertices or " <<
275 " edge not found" << endl;
278 catch (
const char* msg) {
282 throw "getEdgeData(): Edge not found";
307 return vertices.at(key);
309 catch (
const std::out_of_range& oor) {
324 return vertices.at(key);
326 catch (
const std::out_of_range& oor) {
338 const unordered_map<K, SLelement<Edge<K, E2> >*>&
353 return adj_list.at(k);
355 catch (
const out_of_range& ) {
356 cerr <<
"Cannot getAdjacencyList() of a non-existent vertex!" 372 return adj_list.at(k);
374 catch (
const out_of_range& ) {
375 cerr <<
"Cannot getAdjacencyList() of a non-existent vertex!" 396 catch (
const out_of_range& ) {
397 cerr <<
"Graph vertex " << k <<
" not found in graph!" << endl;
417 catch (
const out_of_range& ) {
418 cerr <<
"Either source or destination node not found in graph!" 429 virtual const string getDataStructureRepresentation()
const override {
439 vertices.size() > LargeGraphVertSize &&
440 areAllVerticesLocated())) {
441 return getDataStructureRepresentationLargeGraph();
444 unordered_map<K, int> node_map;
446 string nodes_JSON =
"", links_JSON =
"";
448 for (
const auto& v : vertices) {
449 if (node_map.emplace(v.first, i).second) {
451 nodes_JSON += v.second->getElementRepresentation() +
COMMA;
457 if (nodes_JSON.size()) {
458 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
463 for (
const auto& v : vertices) {
468 it = it->getNext()) {
469 Element<E1>* dest_vert = vertices.at(it->getValue().to() );
478 if (links_JSON.size()) {
479 links_JSON = links_JSON.erase(links_JSON.size() - 1);
482 string graph_alist_json =
490 return graph_alist_json;
502 string getDataStructureRepresentationLargeGraph ()
const {
509 unordered_map<K, int> node_map;
511 string nodes_JSON =
"";
513 for (
const auto& v : vertices) {
514 if (node_map.emplace(v.first, i).second) {
517 vertices.at(v.first)->getVisualizer();
535 if (nodes_JSON.size()) {
536 nodes_JSON = nodes_JSON.erase(nodes_JSON.size() - 1);
540 string links_JSON =
"";
541 for (
const auto& v : vertices) {
546 it = it->getNext()) {
547 Element<E1>* dest_vert = vertices.at(it->getValue().to() );
549 string src =
JSONencode(node_map.at(v.first));
550 string dest =
JSONencode(node_map.at(it->getValue().to()));
560 if (links_JSON.size())
561 links_JSON = links_JSON.erase(links_JSON.size() - 1);
563 string graph_alist_json =
570 return graph_alist_json;
576 bool areAllVerticesLocated()
const {
577 for (
const auto& v : vertices) {
591 forceLargeViz =
true;
592 forceSmallViz =
false;
595 forceLargeViz =
false;
601 forceSmallViz =
true;
602 forceLargeViz =
false;
605 forceSmallViz =
false;
611 std::unordered_map<K, Element<E1>* >
const & underlying_map;
626 return this->it != it.it;
665 typename std::unordered_map<K, Element<E1>* > & underlying_map;
674 typename std::unordered_map<K, Element<E1>* >
::iterator it;
681 return this->it != it.it;
703 return this->it != it.it;
718 return iterator(underlying_map.begin());
722 return iterator(underlying_map.end());
743 typename std::unordered_map<K, Element<E1>* >
const & underlying_map;
759 return this->it != it.it;
VertexElementSet_listhelper vertexSet()
returns a set of vertices (Element<E>) that conforms to STL list interface. That means we can use ran...
Definition: GraphAdjList.h:736
void forceSmallVisualization(bool f)
Definition: GraphAdjList.h:599
This is a helper class to return sets of vertices in a way that are iterable with range for loops...
Definition: GraphAdjList.h:673
double getLocationY() const
Definition: ElementVisualizer.h:153
const_iterator & operator++()
Definition: GraphAdjList.h:710
double getLocationX() const
Definition: ElementVisualizer.h:147
void setValue(const E &val)
Definition: Element.h:212
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:256
This is a helper class to return sets of vertices ina way that are iterable with range for loops...
Definition: GraphAdjList.h:751
void setEdgeData(const E2 &data)
Definition: Edge.h:89
ElementVisualizer * getVisualizer(const K &k)
Definition: GraphAdjList.h:390
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:702
E2 & getEdgeData(const K &src, const K &dest)
Definition: GraphAdjList.h:190
SLelement< Edge< K, E2 > > * getAdjacencyList(const K &k)
Definition: GraphAdjList.h:351
iterator end()
Definition: GraphAdjList.h:721
const string COLON
Definition: DataStructure.h:51
E const & getValue() const
Definition: Element.h:195
iterator(typename std::unordered_map< K, Element< E1 > * >::iterator i)
Definition: GraphAdjList.h:676
constVertexElementSet_listhelper(std::unordered_map< K, Element< E1 > * > const &um)
Definition: GraphAdjList.h:746
SLelement< Edge< K, E2 > >::SLelement_listhelper outgoingEdgeSetOf(K const &k)
Definition: GraphAdjList.h:654
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:28
const string OPEN_BOX
Definition: DataStructure.h:54
const_iterator end() const
Definition: GraphAdjList.h:776
const_iterator & operator++()
Definition: GraphAdjList.h:766
Element< E1 > const * operator*() const
Definition: GraphAdjList.h:706
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:754
const_iterator begin() const
Definition: GraphAdjList.h:639
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:698
const string CLOSE_CURLY
Definition: DataStructure.h:53
This is a helper class to return sets of vertices ina way that are iterable with range for loops...
Definition: GraphAdjList.h:610
const Element< E1 > * getVertex(const K &key) const
Definition: GraphAdjList.h:305
const E1 & getVertexData(const K &src) &
Definition: GraphAdjList.h:150
const_iterator end() const
Definition: GraphAdjList.h:643
Element< E1 > * getVertex(const K &key)
Definition: GraphAdjList.h:322
Color getColor() const
Return the link color.
Definition: LinkVisualizer.h:116
The singly linked list element, derived from Element.
Definition: SLelement.h:27
This is a helper class to return sets of vertices in a way that are iterable with range for loops...
Definition: GraphAdjList.h:695
const_iterator end() const
Definition: GraphAdjList.h:729
void forceLargeVisualization(bool f)
Definition: GraphAdjList.h:589
void addVertex(const K &k, const E1 &e=E1())
Definition: GraphAdjList.h:96
const_iterator(typename std::unordered_map< K, Element< E1 > * >::const_iterator i)
Definition: GraphAdjList.h:621
bool operator!=(const iterator &it) const
Definition: GraphAdjList.h:680
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const_iterator begin() const
Definition: GraphAdjList.h:772
virtual const string getDStype() const override
Definition: GraphAdjList.h:77
const string CLOSE_BOX
Definition: DataStructure.h:55
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:758
const E2 & getEdgeData() const
Definition: Edge.h:97
const string getCSSRepresentation() const
Definition: Color.h:404
bool operator!=(const const_iterator &it) const
Definition: GraphAdjList.h:625
Definition: GraphAdjList.h:618
This is a helper class to return sets of vertices ina way that are iterable with range for loops...
Definition: GraphAdjList.h:742
const_iterator begin() const
Definition: GraphAdjList.h:725
KeySet_helper(std::unordered_map< K, Element< E1 > * > const &um)
Definition: GraphAdjList.h:614
unordered_map< K, Element< E1 > * > * getVertices()
Definition: GraphAdjList.h:289
constVertexElementSet_listhelper vertexSet() const
returns a set of vertices (Element<E>) that conforms to STL list interface. That means we can use ran...
Definition: GraphAdjList.h:781
const_iterator & operator++()
Definition: GraphAdjList.h:633
void setVertexData(const K &vertID, E1 const &data)
Definition: GraphAdjList.h:169
virtual SLelement * getNext()
Definition: SLelement.h:70
iterator & operator++()
Definition: GraphAdjList.h:688
const unordered_map< K, Element< E1 > * > * getVertices() const
Definition: GraphAdjList.h:298
const K & operator*() const
Definition: GraphAdjList.h:629
This is a helper class to return sets of vertices in a way that are iterable with range for loops...
Definition: GraphAdjList.h:664
Element< E1 > const * operator*() const
Definition: GraphAdjList.h:762
const SLelement< Edge< K, E2 > > * getAdjacencyList(const K &k) const
Definition: GraphAdjList.h:370
This class maintains the visual properties of links within data structures.
Definition: LinkVisualizer.h:26
VertexElementSet_listhelper(std::unordered_map< K, Element< E1 > * > &um)
Definition: GraphAdjList.h:668
KeySet_helper keySet() const
Definition: GraphAdjList.h:650
Element< E1 > * operator*()
Definition: GraphAdjList.h:684
Color getColor() const
Definition: ElementVisualizer.h:95
virtual ~GraphAdjList() override
Definition: GraphAdjList.h:56
ElementVisualizer * getVisualizer()
Definition: Element.h:136
const string COMMA
Definition: DataStructure.h:50
iterator begin()
Definition: GraphAdjList.h:717
const string QUOTE
Definition: DataStructure.h:49
const unordered_map< K, SLelement< Edge< K, E2 > > * > & getAdjacencyList() const
Definition: GraphAdjList.h:339
LinkVisualizer * getLinkVisualizer(const K &k1, const K &k2)
Definition: GraphAdjList.h:410
SLelement< Edge< K, E2 > >::SLelement_constlisthelper outgoingEdgeSetOf(K const &k) const
Definition: GraphAdjList.h:658
E2 const & getEdgeData(const K &src, const K &dest) const
Definition: GraphAdjList.h:222
void addEdge(const K &src, const K &dest, const E2 &data=E2())
add an edge with data.
Definition: GraphAdjList.h:119
This helper class is used by the graph classes - GraphAdjList , GraphAdjMatrix - to keep track of edg...
Definition: Edge.h:34
void setEdgeData(const K &src, const K &dest, E2 &data)
Definition: GraphAdjList.h:257
std::string JSONencode(const T &d)
Definition: JSONutil.h:37
LinkVisualizer * getLinkVisualizer(const Element *el)
Definition: Element.h:154