Skip to content

Commit

Permalink
Implemented bit mask to memorize last used levels to beautify Dree ou…
Browse files Browse the repository at this point in the history
…tput (#7)

* print ascii changed

* bitmask memorisation added for clean dree output

* update .gitigore;make clean
  • Loading branch information
ujjwall-R authored Jul 1, 2023
1 parent 4e36587 commit f6886f1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/compile/main
.vscode
.vscode
obj
Binary file removed obj/controller/DirectoryGraph.o
Binary file not shown.
Binary file removed obj/main.o
Binary file not shown.
Binary file removed obj/model/DirectoryNode.o
Binary file not shown.
Binary file removed obj/view/Dree.o
Binary file not shown.
30 changes: 22 additions & 8 deletions src/controller/DirectoryGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ bool DirectoryGraph::isDirectory(const string &pathStr)
return filesystem::is_directory(path);
}

DirectoryNode *DirectoryGraph::BuildGraph(const string &directoryName, int depth)
DirectoryNode *DirectoryGraph::BuildGraph(const string &directoryName, long long depth)
{
DirectoryNode *graph = new DirectoryNode(directoryName);
TraverseDirectoriesDFS(graph, depth, 0);
return graph;
}

void DirectoryGraph::TraverseDirectoriesDFS(DirectoryNode *node, int depth, int currentDepth)
void DirectoryGraph::TraverseDirectoriesDFS(DirectoryNode *node, long long depth, long long currentDepth)
{
if (currentDepth > depth)
return;
Expand All @@ -40,19 +40,33 @@ void DirectoryGraph::TraverseDirectoriesDFS(DirectoryNode *node, int depth, int
}
}

void DirectoryGraph::PrintGraph(DirectoryNode *node, int depth, int currentDepth)
void DirectoryGraph::PrintGraph(DirectoryNode *node, long long depth, long long currentDepth, bool isLastChild, long long mask)
{
if (currentDepth == depth)
return;

for (int i = 0; i < currentDepth * depth; ++i)
cout << " ";
cout << "└── ";
for (long long i = 0; i < currentDepth; i++)
{
if (((mask >> i) & 1ll) == 0ll)
cout << "";
else
cout << " ";
}
isLastChild ? cout << "└── " : cout << "├── ";

cout << node->name << "\n";
for (size_t i = 0; i < node->children.size(); ++i)
for (size_t i = 0; i < node->children.size(); i++)
{
DirectoryNode *child = node->children[i];
PrintGraph(child, depth, currentDepth + 1);
if (i == node->children.size() - 1)
{
mask = mask | (1ll << (currentDepth + 1));
}
PrintGraph(child, depth, currentDepth + 1, i == node->children.size() - 1, mask);
}
}

void DirectoryGraph::PrintGraph(DirectoryNode *node, long long depth)
{
this->PrintGraph(node, depth, 0, true, 1);
}
9 changes: 4 additions & 5 deletions src/controller/DirectoryGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ using namespace std;
class DirectoryGraph
{
public:
DirectoryNode *BuildGraph(const string &directoryName, int depth);

void PrintGraph(DirectoryNode *node, int depth, int currentDepth = 0);
DirectoryNode *BuildGraph(const string &directoryName, long long depth);
void PrintGraph(DirectoryNode *node, long long depth);

private:
bool isDirectory(const std::string &pathStr);

void TraverseDirectoriesDFS(DirectoryNode *node, int depth, int currentDepth = 0);
void TraverseDirectoriesDFS(DirectoryNode *node, long long depth, long long currentDepth = 0);
void PrintGraph(DirectoryNode *node, long long depth, long long currentDepth, bool isLastChild, long long mask);
};

#endif
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ using namespace std;

int main()
{
long long depth = 3; // should not be more than 60
auto currentPath = filesystem::current_path();
DirectoryGraph builder;
auto root = builder.BuildGraph(currentPath, 4);
builder.PrintGraph(root, 4);
auto root = builder.BuildGraph(currentPath, depth);
builder.PrintGraph(root, depth);
return 0;
}

0 comments on commit f6886f1

Please sign in to comment.