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