Skip to content

Commit

Permalink
Adding string hierarchy helper functions, and fixing pointer and refe…
Browse files Browse the repository at this point in the history
…rence styling rules
  • Loading branch information
AsherGlick committed Sep 30, 2023
1 parent 32ccac8 commit 371e239
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion xml_converter/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
Expand Down Expand Up @@ -106,6 +106,7 @@ PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1 # 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReferenceAlignment: Right
RawStringFormats:
- Language: Cpp
Delimiters:
Expand Down
52 changes: 52 additions & 0 deletions xml_converter/src/string_hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

#include <iostream>

////////////////////////////////////////////////////////////////////////////////
// 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
//
Expand Down Expand Up @@ -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<std::string> &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
//
Expand Down
9 changes: 9 additions & 0 deletions xml_converter/src/string_hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
class StringHierarchy {
public:
bool in_hierarchy(
const std::string &node) const;

bool in_hierarchy(
const std::vector<std::string> &path) const;

const StringHierarchy* sub_hierarchy(
const std::string &node) const;

const StringHierarchy* sub_hierarchy(
const std::vector<std::string> &path) const;

void add_path(
Expand Down

0 comments on commit 371e239

Please sign in to comment.