From 371e23985996e3e548adfa81ec0faf96c172dcbf Mon Sep 17 00:00:00 2001 From: Asher Glick Date: Fri, 29 Sep 2023 23:59:39 -0500 Subject: [PATCH] Adding string hierarchy helper functions, and fixing pointer and reference styling rules --- xml_converter/.clang-format | 3 +- xml_converter/src/string_hierarchy.cpp | 52 ++++++++++++++++++++++++++ xml_converter/src/string_hierarchy.hpp | 9 +++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/xml_converter/.clang-format b/xml_converter/.clang-format index 71dc1ff2..bf384993 100644 --- a/xml_converter/.clang-format +++ b/xml_converter/.clang-format @@ -57,7 +57,7 @@ ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true DeriveLineEnding: true -DerivePointerAlignment: true +DerivePointerAlignment: false DisableFormat: false ExperimentalAutoDetectBinPacking: false FixNamespaceComments: true @@ -106,6 +106,7 @@ PenaltyBreakTemplateDeclaration: 10 PenaltyExcessCharacter: 1 # 1000000 PenaltyReturnTypeOnItsOwnLine: 200 PointerAlignment: Left +ReferenceAlignment: Right RawStringFormats: - Language: Cpp Delimiters: diff --git a/xml_converter/src/string_hierarchy.cpp b/xml_converter/src/string_hierarchy.cpp index 0c907c39..03371f00 100644 --- a/xml_converter/src/string_hierarchy.cpp +++ b/xml_converter/src/string_hierarchy.cpp @@ -2,6 +2,24 @@ #include +//////////////////////////////////////////////////////////////////////////////// +// in_hierarchy +// +// Returns if a particular node exists at the top level of the hirearchy. +//////////////////////////////////////////////////////////////////////////////// +bool StringHierarchy::in_hierarchy( + const std::string &node) const { + if (this->all_children_included) { + return true; + } + + auto iterator = this->children.find(node); + if (iterator == this->children.end()) { + return false; + } + return true; +} + //////////////////////////////////////////////////////////////////////////////// // in_hierarchy // @@ -39,6 +57,40 @@ bool StringHierarchy::_in_hierarchy( return iterator->second._in_hierarchy(path, continue_index + 1); } +//////////////////////////////////////////////////////////////////////////////// +// sub_hierarchy +// +// A helper function to grab a sub hierarchy one level down from the top. +//////////////////////////////////////////////////////////////////////////////// +const StringHierarchy* StringHierarchy::sub_hierarchy( + const std::string &node) const { + if (this->all_children_included) { + return this; + } + + auto iterator = this->children.find(node); + if (iterator == this->children.end()) { + return nullptr; + } + return &(iterator->second); +} + +//////////////////////////////////////////////////////////////////////////////// +// sub_hierarchy +// +// Get a subtree of the StringHierarchy in order to avoid needing to query the +// entire hierarchy in the case where the use case is traversing down a tree +// anyways and does not want to keep track of the parent's values. +//////////////////////////////////////////////////////////////////////////////// +const StringHierarchy* StringHierarchy::sub_hierarchy( + const std::vector &path) const { + const StringHierarchy* sub_hierarchy = this; + for (size_t i = 0; i < path.size(); i++) { + sub_hierarchy = this->sub_hierarchy(path[i]); + } + return sub_hierarchy; +} + //////////////////////////////////////////////////////////////////////////////// // add_path // diff --git a/xml_converter/src/string_hierarchy.hpp b/xml_converter/src/string_hierarchy.hpp index 9a5587db..47218a97 100644 --- a/xml_converter/src/string_hierarchy.hpp +++ b/xml_converter/src/string_hierarchy.hpp @@ -7,6 +7,15 @@ class StringHierarchy { public: bool in_hierarchy( + const std::string &node) const; + + bool in_hierarchy( + const std::vector &path) const; + + const StringHierarchy* sub_hierarchy( + const std::string &node) const; + + const StringHierarchy* sub_hierarchy( const std::vector &path) const; void add_path(