fix subvariant parsing/insertion into tree

This commit is contained in:
Lily Tsuru 2024-10-21 20:50:52 -04:00
parent 6f41bc9180
commit 0eaca7d0c9
3 changed files with 75 additions and 14 deletions

View file

@ -21,6 +21,25 @@ namespace vxorg {
return res; return res;
} }
template<class Fn>
void walk_parents_in_tree_order(VxHeavenTree::Node* node, Fn&& fn) {
std::string sample_name {};
std::vector<VxHeavenTree::Node*> parent_list {};
vxorg::VxHeavenTree::Node* parent = node->parent_node();
while(parent) {
if(parent->is_root())
break;
parent_list.push_back(parent);
parent = parent->parent_node();
}
for(auto& item : std::views::reverse(parent_list)) {
fn(item);
}
}
std::string get_sample_name(VxHeavenTree::Node* node) { std::string get_sample_name(VxHeavenTree::Node* node) {
if(node == nullptr) if(node == nullptr)
return ""; return "";
@ -29,26 +48,28 @@ namespace vxorg {
return node->data().name; return node->data().name;
std::string sample_name {}; std::string sample_name {};
std::vector<vxorg::VxHeavenItem*> parent_list {};
vxorg::VxHeavenTree::Node* parent = node->parent_node();
while(parent) { walk_parents_in_tree_order(node, [&](auto* node) {
if(parent->data().name.empty()) sample_name += std::format("{}.", node->data().name);
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; sample_name += node->data().name;
return sample_name; return sample_name;
} }
std::filesystem::path get_sample_path(VxHeavenTree::Node* node) {
if(node == nullptr)
return {};
std::filesystem::path path;
walk_parents_in_tree_order(node, [&](auto* node) {
path /= node->data().name;
});
return path;
}
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)) {
@ -106,10 +127,17 @@ namespace vxorg {
for(auto& subvariant : subvariants) { for(auto& subvariant : subvariants) {
if(auto* node = leaf->find_child([&](auto* node) { return node->data().name == subvariant; }); node == nullptr) { if(auto* node = leaf->find_child([&](auto* node) { return node->data().name == subvariant; }); node == nullptr) {
//printf("creating variant %.*s %.*s\n", split[2].length(), split[2].data(), subvariant.length(), subvariant.data());
leaf = leaf->create_leaf({ .name = std::string(subvariant.data(), subvariant.length()), .is_sample = false }); leaf = leaf->create_leaf({ .name = std::string(subvariant.data(), subvariant.length()), .is_sample = false });
} else {
// existing node for a subvariant
//printf("existing variant %.*s %.*s\n", split[2].length(), split[2].data(), subvariant.length(), subvariant.data());
leaf = node;
continue;
} }
} }
// The last node we visit is the sample
leaf->data().is_sample = true; leaf->data().is_sample = true;
} else { } else {
auto subvariant = split[3]; auto subvariant = split[3];

View file

@ -3,6 +3,8 @@
#include "tree.hpp" #include "tree.hpp"
#include <filesystem>
namespace vxorg { namespace vxorg {
struct VxHeavenItem { struct VxHeavenItem {
@ -15,5 +17,7 @@ namespace vxorg {
std::string get_sample_name(VxHeavenTree::Node* node); std::string get_sample_name(VxHeavenTree::Node* node);
std::filesystem::path get_sample_path(VxHeavenTree::Node* node);
void parse_into_tree(VxHeavenTree& tree, std::istream& is); void parse_into_tree(VxHeavenTree& tree, std::istream& is);
} // namespace vxorg } // namespace vxorg

View file

@ -11,11 +11,19 @@ int main() {
vxorg::parse_into_tree(tree, ifs); vxorg::parse_into_tree(tree, ifs);
std::filesystem::path root = std::filesystem::current_path() / "testdata";
std::filesystem::path unorg = root / "unorg";
std::filesystem::path org = root / "org";
if(!std::filesystem::exists(org))
std::filesystem::create_directories(org);
// walk the resulting tree // walk the resulting tree
tree.walk([](auto* node) { tree.walk([&](auto* node) {
auto tabulation_level = node->parent_count(); auto tabulation_level = node->parent_count();
auto& data = node->data(); auto& data = node->data();
#if 0
if(tabulation_level != 0) { if(tabulation_level != 0) {
for(auto i = 0; i < tabulation_level; ++i) { for(auto i = 0; i < tabulation_level; ++i) {
std::printf("\t"); std::printf("\t");
@ -32,6 +40,27 @@ int main() {
std::printf("%s\n", data.name.c_str()); std::printf("%s\n", data.name.c_str());
} }
} }
#endif
#if 1
if(!node->is_root()) {
if(data.is_sample) {
std::string sample_name = vxorg::get_sample_name(node);
// paths
auto path = org / vxorg::get_sample_path(node);
auto source_path = unorg / vxorg::get_sample_name(node);
if(!std::filesystem::exists(source_path)) {
std::printf("WARNING: sample %s/%s in tree (source disk file %s) does not exist\n", path.string().c_str(), sample_name.c_str(), source_path.string().c_str());
}
//std::printf("sample %s/%s in tree (source disk file %s)\n", path.string().c_str(), sample_name.c_str(), source_path.string().c_str());
}
}
#endif
}); });
return 0; return 0;
} }