-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.hpp
More file actions
367 lines (329 loc) · 10.2 KB
/
tree.hpp
File metadata and controls
367 lines (329 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//================================================================================
// N-ary Tree for directory tree
// Auther: embedded-kiddie (https://github.com/embedded-kiddie)
// Released under the GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)
// https://www.geeksforgeeks.org/dsa/tree-data-structure/
// https://www.geeksforgeeks.org/dsa/generic-treesn-array-trees/
//================================================================================
#ifndef _TREE_HPP_
#define _TREE_HPP_
//----------------------------------------------------------------------
// SD file system configuration
//----------------------------------------------------------------------
#include "sdfs.h"
#include "debug.h"
#include "config.h"
#include <string>
#include <vector>
#include <exception>
#include <assert.h>
#include <string.h>
//----------------------------------------------------------------------
// Meta information for Node class
//----------------------------------------------------------------------
#define TYPE_NODE 0 // typically "folder"
#define TYPE_LEAF 1 // typically "file"
// States of Node and Leaf
#define NODE_HIDDEN true // for 'bool hidden'
#define NODE_REVEALED false // for 'bool hidden'
#define NODE_FOLDED true // for 'bool checked'
#define NODE_UNFOLDED false // for 'bool checked'
#define LEAF_SELECTED true // for 'bool checked'
#define LEAF_UNSELECTED false // for 'bool checked'
typedef struct {
uint8_t depth : 4; // hierarchy depth in N-ary tree
uint8_t type : 1; // 0: folder node, 1: leaf node
bool hidden : 1; // true: hidden, false: displayed
bool checked : 1; // true: folded (folder node) / selected (leaf node)
bool spare : 1; // spare
} NodeMeta_t;
//----------------------------------------------------------------------
// Class definition
//----------------------------------------------------------------------
class Node {
private:
static bool m_found; // node search flag
static uint16_t n_depth; // depth of tree
static uint16_t n_nodes; // number of all nodes
static uint16_t n_leafs; // number of leaf nodes
static uint16_t n_audio; // number of files under the leaf nodes
static Node *m_found_node; // node search result
static std::string m_path; // file path search result
public:
uint16_t key; // a key assigned to each node
uint16_t n_files; // number of audio files
NodeMeta_t meta; // information for album list
std::string name; // folder name or file name
std::vector<Node*> children; // a set of child nodes
Node(const char * name) {
++n_nodes;
this->meta = {0,};
this->n_files = 0;
this->name = name;
}
~Node() {
for (auto &n : this->children) {
delete n;
}
this->children.clear();
this->name.clear();
}
const uint32_t get_n_nodes(void) {
return n_nodes;
}
const uint32_t get_n_leafs(void) {
return n_leafs;
}
const uint32_t get_n_depth(void) {
return n_depth;
}
const uint32_t get_n_audio(void) {
return n_audio;
}
const char * get_path(void) {
return m_path.c_str();
}
Node * get_node(void) {
return m_found_node;
}
// creates a new node and adds it to the set of children
Node* append(const char * name) {
Node *node = new Node(name);
assert(node);
try {
this->children.push_back(node);
} catch (const std::exception &e) {
assert(false); // e.what()
}
return node;
}
private:
// check the file extension
bool check_ext(const char *path) {
const char* const ext[] = MP3_FILE_EXT;
for (int i = 0; i < sizeof(ext) / sizeof(ext[0]); i++) {
if (strcmp(&path[strlen(path) - strlen(ext[i])], ext[i]) == 0) {
return true;
}
}
return false;
}
uint16_t count_files(File &dir) {
uint16_t n = 1;
for (File fd = dir.openNextFile(); fd; fd = dir.openNextFile()) {
#ifdef USE_SDFAT
char buf[BUF_SIZE];
fd.getName(buf, sizeof(buf));
const char *name = buf;
#else
const char *name = fd.name();
#endif
check_ext(name) && ++n;
fd.close();
}
return n;
}
// traversing the file system
void scan_node(File &dir, Node *node, bool scan_file) {
for (File fd = dir.openNextFile(); fd; fd = dir.openNextFile()) {
#ifdef USE_SDFAT
char buf[BUF_SIZE];
fd.getName(buf, sizeof(buf));
const char *name = buf;
#else
const char *name = fd.name();
#endif
if (MP3_IS_VALID(name)) {
if (fd.isDirectory()) {
scan_node(fd, node->append(name), scan_file);
}
else if (check_ext(name)) {
if (!scan_file) {
n_audio += node->n_files = count_files(dir);
} else {
node->append(name);
n_audio += node->n_files++;
}
}
}
fd.close();
}
// Sort child nodes in ascending order
std::sort(node->children.begin(), node->children.end(), [](Node *a, Node *b) {
return a->name.compare(b->name) < 0 ? true : false;
});
}
// traverse and assign keys similar to post-order
// e.g. root
// ┌───────┼─────────┐
// 1 3 8
// ┌─┴─┐ ┌─┴─┐ ┌───┴───┐
// 0 1 2 3 5 8
// ┌─┴─┐ ┌──┼──┐
// 4 5 6 7 8
uint32_t traverse_node(Node *node, uint16_t depth = 1) {
for (auto &n : node->children) {
if (n->meta.depth == 0) {
n->meta.depth = depth;
n->meta.hidden = true;
n->meta.checked = true;
}
if (n->children.size()) {
uint16_t d = traverse_node(n, depth + 1);
n_depth = max(n_depth, d);
n->meta.type = TYPE_NODE;
} else {
n->key = n_leafs++;
n->meta.type = TYPE_LEAF;
}
}
// set the key of the leaf node into the parent
node->key = n_leafs - 1;
return depth;
}
// traverse and assign keys by pre-order
// e.g. root
// ┌───────┼─────────┐
// 0 3 6
// ┌─┴─┐ ┌─┴─┐ ┌───┴───┐
// 1 2 4 5 7 10
// ┌─┴─┐ ┌──┼──┐
// 8 9 11 12 13
void traverse_preorder(Node *node, uint16_t depth) {
for (auto &n : node->children) {
n->key = n_nodes++;
traverse_preorder(n, depth + 1);
}
}
public:
uint32_t traverse_node(void) {
n_leafs = n_depth = 0;
traverse_node(this);
return n_nodes;
}
uint32_t traverse_preorder(void) {
n_nodes = 0;
traverse_preorder(this, 1);
return n_nodes;
}
// create a file tree
void scan_file(File &dir) {
n_nodes = n_leafs = n_depth = n_audio = 0;
scan_node(dir, this, true);
traverse_node(this);
this->key = n_leafs;
this->meta = { 0, TYPE_NODE, false, false, false }; // depth, type, hidden, checked, spare
}
// create a directory tree
void scan_dir(File &dir) {
n_nodes = n_leafs = n_depth = n_audio = 0;
scan_node(dir, this, false);
traverse_node(this);
this->key = n_leafs;
this->meta = { 0, TYPE_NODE, false, false, false }; // depth, type, hidden, checked, spare
}
private:
// find the leaf node with the specified key
bool find_node(Node * node, int key) {
for (auto &n : node->children) {
// within the range ?
if (n->key >= key) {
// are there any subtrees?
if (n->children.size()) {
m_path.append(n->name).append("/");
if (find_node(n, key)) {
return m_found;
}
}
// found the leaf node
else {
DBG_ASSERT(n->key == key);
m_path.append(n->name);
m_found = true;
m_found_node = n;
return m_found;
}
}
}
return m_found;
}
// find the node/leaf with the specified key
Node *find_preorder(Node * node, int key) {
const int N = node->children.size();
for (int i = N - 1; i >= 0; i--) {
Node *n = node->children[i];
if (n->key == key) {
m_found_node = node; // parent node
return n; // found node
}
else if (n->key < key) {
if (n = find_preorder(n, key)) {
return n;
}
}
}
return NULL;
}
public:
// find the leaf node with the specified key and returns node / path.
Node *find_node(int key) {
m_found = false;
m_found_node = NULL;
m_path = this->name;
find_node(this, key);
return m_found_node;
}
std::string find_path(int key) {
m_found = false;
m_found_node = NULL;
m_path = this->name;
find_node(this, key);
return m_path; // without trailing slash
}
// find the node/leaf with the specified key
Node *find_preorder(int key) {
return find_preorder(this, key);
}
void print_node(Node *node) {
Serial.printf("key:%3d, children:%2d, n_files:%2d, depth:%d, type:%d, hidden:%d, checked:%d %s (%d/%d)\n",
node->key, node->children.size(), node->n_files, node->meta.depth, node->meta.type,
node->meta.hidden, node->meta.checked, node->name.c_str(), node->name.size(), node->name.capacity());
}
void print_nodes(Node * node, int indent = 0) {
++indent;
for (auto &n : node->children) {
for (int j = 0; j < indent; j++) { Serial.print(" "); }
print_node(n);
if (n->children.size()) {
print_nodes(n, indent);
}
}
}
// traverse the tree and print node information
void dump_tree(void) {
print_node(this);
print_nodes(this);
Serial.printf("n_nodes: %d, n_leaf: %d, n_depth: %d\n", n_nodes, n_leafs, n_depth);
}
void dump_path(void) {
for (int i = 0; i < n_leafs; i++) {
if (find_node(i)) {
Serial.printf("key: %d, path: %s\n", i, m_path.c_str());
}
}
}
void dump_preorder(bool all = false) {
traverse_preorder();
//dump_tree();
//print_node(this);
for (int i = 0; i < n_nodes; i++) {
Node *n = find_preorder(i);
Node *p = m_found_node; // parent node
DBG_ASSERT(n);
if (all || p->meta.checked == NODE_UNFOLDED) {
print_node(n);
}
}
}
};
#endif // _TREE_HPP_