This commit is contained in:
Lily Tsuru 2024-10-21 12:27:40 -04:00
parent af49206a16
commit f8285f9e4c
5 changed files with 111 additions and 114 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
__pycache__/
*.o
/vxorg
/tree_test
/testdata

View file

@ -4,7 +4,6 @@
/// Port of tree.py to C++ /// Port of tree.py to C++
template <class T> template <class T>
struct Tree { struct Tree {
// FIXME make T not require default constructability // FIXME make T not require default constructability
struct Node { struct Node {
protected: protected:
@ -12,27 +11,20 @@ struct Tree {
Node* parent = nullptr; Node* parent = nullptr;
std::vector<Node*> children {}; std::vector<Node*> children {};
T item {}; T item {};
public: public:
~Node() { ~Node() {
for(auto& child : children) for(auto& child : children)
delete child; delete child;
} }
T& data() { T& data() { return item; }
return item;
}
const T& data() const { const T& data() const { return item; }
return item;
}
bool is_leaf() const { bool is_leaf() const { return children.size() == 0; }
return children.size() == 0;
}
Node* parent_node() { Node* parent_node() { return parent; }
return parent;
}
Node* create_leaf(const T& item) { Node* create_leaf(const T& item) {
auto* node = new Node; auto* node = new Node;
@ -72,16 +64,11 @@ struct Tree {
} }
return parent_count; return parent_count;
} }
}; };
Tree() { Tree() { root = new Node; }
root = new Node;
}
~Tree() { ~Tree() { delete root; }
delete root;
}
// Trees are not copyable but they can move // Trees are not copyable but they can move
Tree(const Tree&) = delete; Tree(const Tree&) = delete;
@ -92,14 +79,9 @@ struct Tree {
root->walk(fn); root->walk(fn);
} }
Node* create_leaf(const T& item) { return root->create_leaf(item); }
Node* create_leaf(const T& item) { Node* root_node() { return root; }
return root->create_leaf(item);
}
Node* root_node() {
return root;
}
private: private:
Node* root; Node* root;

View file

@ -21,6 +21,34 @@ namespace vxorg {
return res; return res;
} }
std::string get_sample_name(VxHeavenTree::Node* node) {
if(node == nullptr)
return "";
if(!node->data().is_sample)
return node->data().name;
std::string sample_name {};
std::vector<vxorg::VxHeavenItem*> parent_list {};
vxorg::VxHeavenTree::Node* parent = node->parent_node();
while(parent) {
if(parent->data().name.empty())
break;
parent_list.push_back(&parent->data());
parent = parent->parent_node();
}
for(auto& item : std::views::reverse(parent_list)) {
sample_name += std::format("{}.", item->name);
}
sample_name += node->data().name;
return sample_name;
}
void parse_into_tree(VxHeavenTree& tree, std::istream& is) { void parse_into_tree(VxHeavenTree& tree, std::istream& is) {
std::string line {}; std::string line {};
while(std::getline(is, line)) { while(std::getline(is, line)) {

View file

@ -1,7 +1,8 @@
#pragma once #pragma once
#include "tree.hpp"
#include <string> #include <string>
#include "tree.hpp"
namespace vxorg { namespace vxorg {
struct VxHeavenItem { struct VxHeavenItem {
@ -12,5 +13,7 @@ namespace vxorg {
using VxHeavenTree = Tree<VxHeavenItem>; using VxHeavenTree = Tree<VxHeavenItem>;
std::string get_sample_name(VxHeavenTree::Node* node);
void parse_into_tree(VxHeavenTree& tree, std::istream& is); void parse_into_tree(VxHeavenTree& tree, std::istream& is);
} } // namespace vxorg

View file

@ -1,15 +1,12 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <ranges>
#include <string> #include <string>
#include <string_view>
#include <vector>
#include "tree.hpp" #include "tree.hpp"
#include "vxheaven_parse.hpp" #include "vxheaven_parse.hpp"
int main() { int main() {
std::ifstream ifs("./samples.sort"); std::ifstream ifs("./testdata/samples.sort");
vxorg::VxHeavenTree tree; vxorg::VxHeavenTree tree;
vxorg::parse_into_tree(tree, ifs); vxorg::parse_into_tree(tree, ifs);
@ -26,26 +23,8 @@ int main() {
std::printf("(root)\n"); std::printf("(root)\n");
} else { } else {
if(data.is_sample) { if(data.is_sample) {
std::string sample_name {}; std::string sample_name = vxorg::get_sample_name(node);
std::vector<vxorg::VxHeavenItem*> parent_list {};
vxorg::VxHeavenTree::Node* parent = node->parent_node();
while(parent) {
if(parent->data().name.empty())
break;
parent_list.push_back(&parent->data());
parent = parent->parent_node();
}
for(auto& item : std::views::reverse(parent_list)) {
sample_name += std::format("{}.", item->name);
}
sample_name += data.name;
std::printf("%s (sample %s)\n", data.name.c_str(), sample_name.c_str()); std::printf("%s (sample %s)\n", data.name.c_str(), sample_name.c_str());
} else { } else {
std::printf("%s\n", data.name.c_str()); std::printf("%s\n", data.name.c_str());
} }