inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
rbtree.hpp
Go to the documentation of this file.
1#ifndef RB_TREE_HPP
2#define RB_TREE_HPP
3
5#include "songmap.hpp"
6#include <chrono>
7#include <cstdlib>
8#include <cstring>
9#include <ctime>
10#include <dirent.h>
11#include <fstream>
12#include <iostream>
13#include <sys/stat.h>
14#include <unordered_map>
15#include <vector>
16
17using namespace std;
18
19#define RED 'R'
20#define BLACK 'B'
21
23 PARENT_LIB, PARENT_LIB_FIELD_DIR)); // fetches the directory from the example config.toml for now
25
26// Node structure for the Red-Black Tree
27struct Node
28{
29 ino_t data; // Using inode type (ino_t) for directory entries
30 char color;
32
33 Node(ino_t data) : data(data), color(RED), left(nullptr), right(nullptr), parent(nullptr) {}
34};
35
36// Red-Black Tree class
38{
39private:
40 Node* root;
41 Node* NIL;
42 SongTree songTree;
43
44 void leftRotate(Node* x)
45 {
46 Node* y = x->right;
47 x->right = y->left;
48 if (y->left != NIL)
49 {
50 y->left->parent = x;
51 }
52 y->parent = x->parent;
53 if (x->parent == nullptr)
54 {
55 root = y;
56 }
57 else if (x == x->parent->left)
58 {
59 x->parent->left = y;
60 }
61 else
62 {
63 x->parent->right = y;
64 }
65 y->left = x;
66 x->parent = y;
67 }
68
69 void rightRotate(Node* x)
70 {
71 Node* y = x->left;
72 x->left = y->right;
73 if (y->right != NIL)
74 {
75 y->right->parent = x;
76 }
77 y->parent = x->parent;
78 if (x->parent == nullptr)
79 {
80 root = y;
81 }
82 else if (x == x->parent->right)
83 {
84 x->parent->right = y;
85 }
86 else
87 {
88 x->parent->left = y;
89 }
90 y->right = x;
91 x->parent = y;
92 }
93
94 void fixInsert(Node* k)
95 {
96 while (k != root && k->parent->color == RED)
97 {
98 if (k->parent == k->parent->parent->left)
99 {
100 Node* u = k->parent->parent->right; // uncle
101 if (u->color == RED)
102 {
103 k->parent->color = BLACK;
104 u->color = BLACK;
105 k->parent->parent->color = RED;
106 k = k->parent->parent;
107 }
108 else
109 {
110 if (k == k->parent->right)
111 {
112 k = k->parent;
113 leftRotate(k);
114 }
115 k->parent->color = BLACK;
116 k->parent->parent->color = RED;
117 rightRotate(k->parent->parent);
118 }
119 }
120 else
121 {
122 Node* u = k->parent->parent->left; // uncle
123 if (u->color == RED)
124 {
125 k->parent->color = BLACK;
126 u->color = BLACK;
127 k->parent->parent->color = RED;
128 k = k->parent->parent;
129 }
130 else
131 {
132 if (k == k->parent->left)
133 {
134 k = k->parent;
135 rightRotate(k);
136 }
137 k->parent->color = BLACK;
138 k->parent->parent->color = RED;
139 leftRotate(k->parent->parent);
140 }
141 }
142 }
143 root->color = BLACK;
144 }
145
146 void inorderHelper(Node* node)
147 {
149
150 if (node != NIL)
151 {
152 inorderHelper(node->left);
153 auto metadataMap = parser.parseFromInode(node->data, DIRECTORY_FIELD);
154 if (metadataMap.empty())
155 {
157 "Error: No files found matching the inode or no metadata extracted.");
158 }
159
160 for (const auto& pair : metadataMap)
161 {
162 /*std::cout << "File: " << pair.first << std::endl; */
163 songTree.addSong(Song(node->data, pair.second));
164 };
165 inorderHelper(node->right);
166 }
167 }
168
169public:
171 {
172 NIL = new Node(0);
173 NIL->color = BLACK;
174 NIL->left = NIL->right = NIL;
175 root = NIL;
176 }
177
178 void insert(ino_t data)
179 {
180 Node* new_node = new Node(data);
181 new_node->left = NIL;
182 new_node->right = NIL;
183
184 Node* parent = nullptr;
185 Node* current = root;
186
187 while (current != NIL)
188 {
189 parent = current;
190 if (new_node->data < current->data)
191 {
192 current = current->left;
193 }
194 else
195 {
196 current = current->right;
197 }
198 }
199
200 new_node->parent = parent;
201
202 if (parent == nullptr)
203 {
204 root = new_node;
205 }
206 else if (new_node->data < parent->data)
207 {
208 parent->left = new_node;
209 }
210 else
211 {
212 parent->right = new_node;
213 }
214
215 if (new_node->parent == nullptr)
216 {
217 new_node->color = BLACK;
218 return;
219 }
220
221 if (new_node->parent->parent == nullptr)
222 {
223 return;
224 }
225
226 fixInsert(new_node);
227 }
228
229 void inorderStoreMetadata() { inorderHelper(root); }
230 void printSongTree() { songTree.display(); }
231 SongTree returnSongTree() { return songTree; }
232};
233
234#endif
RedBlackTree()
Definition rbtree.hpp:170
void insert(ino_t data)
Definition rbtree.hpp:178
SongTree returnSongTree()
Definition rbtree.hpp:231
void printSongTree()
Definition rbtree.hpp:230
void inorderStoreMetadata()
Definition rbtree.hpp:229
Represents a hierarchical tree structure to store songs by artist, album, disc number,...
Definition songmap.hpp:78
A class for parsing metadata from audio files.
Definition taglib_parser.h:72
std::unordered_map< std::string, Metadata > parseFromInode(ino_t inode, const std::string &directory)
Parse metadata from files in a directory based on inode.
Definition taglib_parser.h:196
string DEBUG_LOG_PARSE
Definition rbtree.hpp:24
string DIRECTORY_FIELD
Definition rbtree.hpp:22
#define BLACK
Definition rbtree.hpp:20
#define RED
Definition rbtree.hpp:19
Definition rbtree.hpp:28
Node(ino_t data)
Definition rbtree.hpp:33
ino_t data
Definition rbtree.hpp:29
Node * left
Definition rbtree.hpp:31
Node * parent
Definition rbtree.hpp:31
char color
Definition rbtree.hpp:30
Node * right
Definition rbtree.hpp:31
Represents a song with associated metadata and inode.
Definition songmap.hpp:38
void sendErrMsg(std::string debugLogBoolStr, std::string errMsg)
Sends an error message based on the debug log setting.
Definition taglib_parser.h:117
#define PARENT_DBG
Definition toml_parser.hpp:26
string_view parseTOMLField(string parent, string field)
Parses a string field from the TOML configuration.
Definition toml_parser.hpp:121
#define PARENT_LIB_FIELD_DIR
Definition toml_parser.hpp:19
#define PARENT_LIB
Macros for parent and field names used in the TOML configuration.
Definition toml_parser.hpp:17
#define PARENT_DBG_FIELD_PARSER_LOG
Definition toml_parser.hpp:27