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

View file

@ -21,6 +21,34 @@ namespace vxorg {
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) {
std::string line {};
while(std::getline(is, line)) {

View file

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

View file

@ -1,15 +1,12 @@
#include <filesystem>
#include <fstream>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
#include "tree.hpp"
#include "vxheaven_parse.hpp"
int main() {
std::ifstream ifs("./samples.sort");
std::ifstream ifs("./testdata/samples.sort");
vxorg::VxHeavenTree tree;
vxorg::parse_into_tree(tree, ifs);
@ -26,26 +23,8 @@ int main() {
std::printf("(root)\n");
} else {
if(data.is_sample) {
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 += data.name;
std::string sample_name = vxorg::get_sample_name(node);
std::printf("%s (sample %s)\n", data.name.c_str(), sample_name.c_str());
} else {
std::printf("%s\n", data.name.c_str());
}