Skip to content

Commit

Permalink
Enable ifdef for visualizations for better use in cp
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Sep 25, 2024
1 parent 3d4f407 commit e804853
Show file tree
Hide file tree
Showing 21 changed files with 216 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/classes/disjoint_set/disjoint_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class dsu {
}

/**
* @brief
* @brief same function
*
* @param i first element
* @param j second element
Expand Down
6 changes: 6 additions & 0 deletions src/classes/graph/graph.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef GRAPH_H
#define GRAPH_H

#ifdef GRAPH_VISUALIZATION_H
#include "../../visualization/graph_visual/graph_visualization.h"
#endif

#ifdef __cplusplus
#include <algorithm>
Expand Down Expand Up @@ -493,6 +495,7 @@ template <typename T> int graph<T>::eulerian() {
return (odd) ? 1 : 2;
}

#ifdef GRAPH_VISUALIZATION_H
template <typename T> void graph<T>::visualize() {
std::string s;
if (_type == "directed") {
Expand Down Expand Up @@ -543,6 +546,7 @@ template <typename T> void graph<T>::visualize() {
graph_visualization::visualize(s);
}
}
#endif

/**
* @brief class for weighted graph
Expand Down Expand Up @@ -1161,6 +1165,7 @@ std::unordered_map<T, double> weighted_graph<T>::bellman_ford(T start) {
return dist;
}

#ifdef GRAPH_VISUALIZATION_H
template <typename T> void weighted_graph<T>::visualize() {
std::string s;
if (_type == "directed") {
Expand Down Expand Up @@ -1234,5 +1239,6 @@ template <typename T> void weighted_graph<T>::visualize() {
graph_visualization::visualize(s);
}
}
#endif

#endif
13 changes: 9 additions & 4 deletions src/classes/list/circular_linked_list.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef CIRCULAR_LINKED_LIST_H
#define CIRCULAR_LINKED_LIST_H

#ifdef __cplusplus
#ifdef LINKED_LIST_VISUALIZATION_H
#include "../../visualization/list_visual/linked_list_visualization.h"
#endif

#ifdef __cplusplus
#include <iostream>
#include <memory>
#include <type_traits>
Expand Down Expand Up @@ -33,9 +36,9 @@ template <typename T> class circular_linked_list {
* @param c the list we want to copy
*/
explicit circular_linked_list(const circular_linked_list &c) : root(c.root), tail(c.tail), _size(c._size) {



}

/**
Expand Down Expand Up @@ -304,10 +307,12 @@ template <typename T> std::string circular_linked_list<T>::generate() {
return gen;
}

#ifdef LINKED_LIST_VISUALIZATION_H
template <typename T> void circular_linked_list<T>::visualize() {
std::string generated = this->generate();
linked_list_visualization::visualize(generated);
}
#endif

/**
* @brief Iterator class
Expand Down
14 changes: 10 additions & 4 deletions src/classes/list/doubly_linked_list.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef DOUBLY_LINKED_LIST_H
#define DOUBLY_LINKED_LIST_H

#ifdef __cplusplus
#ifdef LINKED_LIST_VISUALIZATION_H
#include "../../visualization/list_visual/linked_list_visualization.h"
#endif

#ifdef __cplusplus
#include <iostream>
#include <memory>
#include <string>
Expand Down Expand Up @@ -34,9 +37,9 @@ template <typename T> class doubly_linked_list {
* @param l the list we want to copy
*/
explicit doubly_linked_list(const doubly_linked_list &l) : root(l.root), tail(l.tail), _size(l._size) {



}

/**
Expand Down Expand Up @@ -309,10 +312,13 @@ template <typename T> std::string doubly_linked_list<T>::generate() {
return gen;
}


#ifdef LINKED_LIST_VISUALIZATION_H
template <typename T> void doubly_linked_list<T>::visualize() {
std::string generated = this->generate();
linked_list_visualization::visualize(generated);
}
#endif

/**
* @brief Iterator class
Expand Down
10 changes: 8 additions & 2 deletions src/classes/list/frequency_list.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#ifndef FREQUENCY_LIST_H
#define FREQUENCY_LIST_H

#ifdef LINKED_LIST_VISUALIZATION_H
#include "../../visualization/list_visual/linked_list_visualization.h"
#endif

#ifdef __cplusplus
#include <cstdint>
#include <vector>
#include <memory>
#include <string>
#include "../../visualization/list_visual/linked_list_visualization.h"
#include <iostream>
#endif


Expand Down Expand Up @@ -121,7 +125,7 @@ class frequency_list {
*/
void reset_frequency();


/**
*@brief Returns all the elements of the list
*@return std::vector<T> the elements of the list
Expand Down Expand Up @@ -480,10 +484,12 @@ template <typename T> std::string frequency_list<T>::generate() {
return gen;
}

#ifdef LINKED_LIST_VISUALIZATION_H
template <typename T> void frequency_list<T>::visualize() {
std::string generated = this->generate();
linked_list_visualization::visualize(generated);
}
#endif



Expand Down
14 changes: 10 additions & 4 deletions src/classes/list/linked_list.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H

#ifdef __cplusplus
#ifdef LINKED_LIST_VISUALIZATION_H
#include "../../visualization/list_visual/linked_list_visualization.h"
#endif

#ifdef __cplusplus
#include <iostream>
#include <memory>
#include <string>
Expand Down Expand Up @@ -35,9 +38,9 @@ template <typename T> class linked_list {
* @param l the list we want to copy
*/
explicit linked_list(const linked_list &l) : root(l.root), tail(l.tail), _size(l._size) {



}

/**
Expand Down Expand Up @@ -291,10 +294,13 @@ template <typename T> std::string linked_list<T>::generate() {
return gen;
}

#ifdef LINKED_LIST_VISUALIZATION_H
template <typename T> void linked_list<T>::visualize() {
std::string generated = this->generate();
linked_list_visualization::visualize(generated);
}
#endif


/**
* @brief Iterator class
Expand Down
30 changes: 18 additions & 12 deletions src/classes/list/skip_list.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef SKIP_LIST_H
#define SKIP_LIST_H

#ifdef __cplusplus
#ifdef LINKED_LIST_VISUALIZATION_H
#include "../../visualization/list_visual/linked_list_visualization.h"
#endif

#ifdef __cplusplus
#include <iostream>
#include <memory>
#include <stdexcept>
Expand Down Expand Up @@ -51,10 +54,10 @@ template <typename T> class skip_list {
* @param s
*/
skip_list(const skip_list &s) : level(s.level), PROB(s.PROB), MAX_LEVEL(s.MAX_LEVEL), root(s.root) {




}

/**
Expand Down Expand Up @@ -176,10 +179,13 @@ template <typename T> class skip_list {
* @brief visualize function
* returns a .dot file that can be previewd with graphviz plugin in vscode
*/

#ifdef LINKED_LIST_VISUALIZATION_H
void visualize(){
std::string generated = this->generate();
linked_list_visualization::visualize(generated);
}
#endif

/**
*@brief operator << for skip_list<T> class.
Expand Down Expand Up @@ -231,17 +237,17 @@ template <typename T> class skip_list {

int level{0};
std::shared_ptr<node> root;

std:: string generate_node(std::string node_val, int levs){
std::string gen;
gen += node_val;
gen += " [label=\"<";
gen += to_string(levs);
gen += std::to_string(levs);
gen += "> ";
gen += node_val;
for(int i=levs-1;i>=0;i--){
gen += " | <";
gen += to_string(i);
gen += std::to_string(i);
gen += "> ";
gen += node_val;
}
Expand All @@ -254,11 +260,11 @@ template <typename T> class skip_list {
std::string gen;
gen += prev_val;
gen += ':';
gen += to_string(lev);
gen += std::to_string(lev);
gen += " -> ";
gen += curr_val;
gen += ':';
gen += to_string(lev);
gen += std::to_string(lev);
gen += " ;\n";
return gen;
}
Expand All @@ -269,8 +275,8 @@ template <typename T> class skip_list {
gen += '\n';
gen += "node [shape=record;]";
gen += '\n';
unordered_set<std::string> S;
int m_level = min(level, (int)root->next.size()); // See if this is a parameter
std::unordered_set<std::string> S;
int m_level = std::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);
Expand Down
14 changes: 7 additions & 7 deletions src/classes/tree/234_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ class ttf_tree {
* @brief default constructor of 234-tree class
* @param elements: by default empty, if you want to pass elements all in one to the tree
*/
explicit ttf_tree(const std::vector<T> &elements = {}) {
explicit ttf_tree(const std::vector<T> &elements = {}) {
if(!elements.empty()){
for(const T & x: elements){
this->insert(x);
}
}
}
}

/**
* @brief search function
* @param key the element we want to search
* @return true if key exists in the tree
* @return false otherwise
*/
bool search(const T &key) const;

/**
* @brief insert function
* @param key thet key we want to insert
*/
void insert(const T &key);

/**
* @brief level_order function
* @return vector<vector<vector<T> > > the level order of the 234-tree in a 3d array
Expand Down Expand Up @@ -93,9 +93,9 @@ class ttf_tree {

template <typename T>
void ttf_tree<T>::insert(const T &key) {
std::vector<std::shared_ptr<node> > null_children(4, nullptr);
std::vector<std::shared_ptr<node> > null_children(4, nullptr);
if(root == nullptr){
std::vector<int> keys = {key};
std::vector<int> keys = {key};
root = std::make_shared<node>(keys, null_children, 2);
root->index = 0;
parent[root] = nullptr;
Expand Down
10 changes: 8 additions & 2 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef AVL_TREE_H
#define AVL_TREE_H

#ifdef __cplusplus
#ifdef TREE_VISUALIZATION_H
#include "../../visualization/tree_visual/tree_visualization.h"
#endif

#ifdef __cplusplus
#include <functional>
#include <memory>
#include <queue>
Expand Down Expand Up @@ -196,15 +199,18 @@ template <typename T> class avl_tree {
*@brief visualize function
*@returns .dot file that can be previewed using graphviz in vscode.
*/

#ifdef TREE_VISUALIZATION_H
void visualize() {
std::string _generated = generate_visualization();
tree_visualization::visualize(_generated);
}
#endif

/**
* @brief operator << for avl_tree class
*/
friend ostream & operator << (ostream &out, avl_tree<T> &t){
friend std::ostream & operator << (std::ostream &out, avl_tree<T> &t){
std::vector<std::vector<T> > order = t.inorder();
for(int i = 0; i<order.size(); i++){
if(i != order.size() - 1){
Expand Down
Loading

0 comments on commit e804853

Please sign in to comment.