From 03b81fe698bb01d54556aca82836082c6234b9de Mon Sep 17 00:00:00 2001 From: Ankith Veldandi <143205588+aveldan@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:15:00 -0400 Subject: [PATCH 1/3] Adding visualise for skiplist --- src/classes/list/skip_list.h | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/classes/list/skip_list.h b/src/classes/list/skip_list.h index d38ebc86..63587e80 100644 --- a/src/classes/list/skip_list.h +++ b/src/classes/list/skip_list.h @@ -3,10 +3,12 @@ #define SKIP_LIST_H #ifdef __cplusplus +#include "../../visualization/list_visual/linked_list_visualization.h" #include #include #include #include +#include #endif /** @@ -171,6 +173,15 @@ template class skip_list { return false; } + /** + * @brief visualize function + * returns a .dot file that can be previewd with graphviz plugin in vscode + */ + void visualize(){ + std::string generated = this->generate(); + linked_list_visualization::visualize(generated); + } + /** *@brief operator << for skip_list class. */ @@ -221,6 +232,75 @@ template class skip_list { int level; std::shared_ptr root; + + std:: string generate_node(std::string node_val, int levs){ + std::string gen; + gen += node_val; + gen += " [label=\"<"; + gen += to_string(levs); + gen += "> "; + gen += node_val; + for(int i=levs-1;i>=0;i--){ + gen += " | <"; + gen += to_string(i); + gen += "> "; + gen += node_val; + } + gen += "\"];"; + gen += '\n'; + return gen; + } + + std::string generate_edge(std::string prev_val, std::string curr_val, int lev){ + std::string gen; + gen += prev_val; + gen += ':'; + gen += to_string(lev); + gen += " -> "; + gen += curr_val; + gen += ':'; + gen += to_string(lev); + gen += " ;\n"; + return gen; + } + + std::string generate() { + std::string gen; + gen += "rankdir=LR;"; + gen += '\n'; + gen += "node [shape=record;]"; + gen += '\n'; + unordered_set S; + int m_level = min(level, (int)root->next.size()); // See if this is a parameter + gen += generate_node("root", m_level+1); + gen += generate_node("NULL", m_level+1); + gen += generate_edge("root", "NULL", m_level+1); + for(int i=m_level;i>=0;i--){ + std::shared_ptr head = root; + std::string prev_val; + std::string head_key; + while(head){ + if(std::is_same_v){ + head_key = head->key; + } else { + head_key = to_string(head->key); + } + if(S.find(head_key) == S.end()){ + S.insert(head_key); + gen += generate_node(head_key, i); + } + if(head != root){ + gen += generate_edge(prev_val, head_key, i); + } else { + gen += generate_edge("root", head_key, i); + } + prev_val = head_key; + head = head->next[i]; + } + gen += generate_edge(prev_val, "NULL", i); + } + return gen; + } }; /** From 8aac0d154b2a52e8c2209ebd38fcadabaf8b0387 Mon Sep 17 00:00:00 2001 From: Spiros Maggioros Date: Sat, 6 Jul 2024 12:58:47 +0300 Subject: [PATCH 2/3] Looks fine | Zeros at front needs to be deteleted --- examples/list/skip_list.cc | 3 ++- examples/list/unnamed.dot | 37 ++++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/examples/list/skip_list.cc b/examples/list/skip_list.cc index 2cb8b520..7ae072de 100644 --- a/examples/list/skip_list.cc +++ b/examples/list/skip_list.cc @@ -11,4 +11,5 @@ int main() { s.insert(5); s.insert(6); std::cout << s << '\n'; -} \ No newline at end of file + s.visualize(); +} diff --git a/examples/list/unnamed.dot b/examples/list/unnamed.dot index 66419673..29817c88 100644 --- a/examples/list/unnamed.dot +++ b/examples/list/unnamed.dot @@ -1,17 +1,28 @@ digraph list { rankdir=LR; node [shape=record;] -3 [label=<{ 3 | 3 | }>] ; -2 [label=<{ 2 | 2 | }>] ; -4 [label=<{ 4 | 2 | }>] ; -1 [label=<{ 1 | 1 | }>] ; -5 [label=<{ 5 | 1 | }>] ; -3:ref -> 2:data [arrowhead=vee, arrowtail=dot, dir=both]; -2:data -> 3:ref [arrowhead=vee, arrowtail=dot, dir=both]; -2:ref -> 4:data [arrowhead=vee, arrowtail=dot, dir=both]; -4:data -> 2:ref [arrowhead=vee, arrowtail=dot, dir=both]; -4:ref -> 1:data [arrowhead=vee, arrowtail=dot, dir=both]; -1:data -> 4:ref [arrowhead=vee, arrowtail=dot, dir=both]; -1:ref -> 5:data [arrowhead=vee, arrowtail=dot, dir=both]; -5:data -> 1:ref [arrowhead=vee, arrowtail=dot, dir=both]; +root [label="<3> root | <2> root | <1> root | <0> root"]; +NULL [label="<3> NULL | <2> NULL | <1> NULL | <0> NULL"]; +root:3 -> NULL:3 ; +0 [label="<2> 0 | <1> 0 | <0> 0"]; +root:2 -> 0:2 ; +1 [label="<2> 1 | <1> 1 | <0> 1"]; +0:2 -> 1:2 ; +5 [label="<2> 5 | <1> 5 | <0> 5"]; +1:2 -> 5:2 ; +5:2 -> NULL:2 ; +root:1 -> 0:1 ; +0:1 -> 1:1 ; +1:1 -> 5:1 ; +5:1 -> NULL:1 ; +root:0 -> 0:0 ; +0:0 -> 1:0 ; +4 [label="<0> 4"]; +1:0 -> 4:0 ; +4:0 -> 5:0 ; +6 [label="<0> 6"]; +5:0 -> 6:0 ; +13 [label="<0> 13"]; +6:0 -> 13:0 ; +13:0 -> NULL:0 ; } From 1798973bdbb40536e1e1cc04d1292e38e3b6447f Mon Sep 17 00:00:00 2001 From: Ankith Veldandi <143205588+aveldan@users.noreply.github.com> Date: Sat, 6 Jul 2024 13:48:05 -0400 Subject: [PATCH 3/3] resolved a bug where zero nodes were created in the beginning --- src/classes/list/skip_list.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/classes/list/skip_list.h b/src/classes/list/skip_list.h index 63587e80..3c08c7eb 100644 --- a/src/classes/list/skip_list.h +++ b/src/classes/list/skip_list.h @@ -277,7 +277,8 @@ template class skip_list { gen += generate_edge("root", "NULL", m_level+1); for(int i=m_level;i>=0;i--){ std::shared_ptr head = root; - std::string prev_val; + head = head->next[i]; + std::string prev_val = "root"; std::string head_key; while(head){ if(std::is_same_v){ @@ -289,11 +290,7 @@ template class skip_list { S.insert(head_key); gen += generate_node(head_key, i); } - if(head != root){ - gen += generate_edge(prev_val, head_key, i); - } else { - gen += generate_edge("root", head_key, i); - } + gen += generate_edge(prev_val, head_key, i); prev_val = head_key; head = head->next[i]; }