Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the experimental RadixTree code #252

Merged
merged 2 commits into from
Nov 15, 2023

Conversation

yungyuc
Copy link
Member

@yungyuc yungyuc commented Nov 13, 2023

  • Fix the function and variable naming style to use snake_case from the wrong camelCase.
  • Use single-name accessor naming style when possible.
  • Add proper type aliases and static assertion.
  • Remove dead code.
  • Move RadixTreeNode::info() to testing code because the function is only used in testing.
  • Use namespace (alias mm = modmesh) in C++ testing code, which is considered API reference.

* Fix the function and variable naming style to use snake_case from the wrong camelCase.
* Use single-name accessor naming style when possible.
* Add proper type aliases and static assertion.
* Remove dead code.
* Move RadixTreeNode::info() to testing code because the function is only used in testing.
* Use namespace (alias mm = modmesh) in C++ testing code, which is considered API reference.
@yungyuc yungyuc added the refactor Change code without changing tests label Nov 13, 2023
Copy link
Member Author

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@q40603 , please take a look at the refactor. Let me know what you think by adding global and inline comments.

RadixTreeNode(std::string name, int key)
: m_name(std::move(name))

using child_list_type = std::list<std::unique_ptr<RadixTreeNode<T>>>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use snake_case for internal type names. Also add back the explicit <T> in RadixTreeNode<T>.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for helping adding back explicit T, which I fully forgot.
But will there be any negative scenarios for aliasing without passing template parameter in this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It reads strange. Semantically it's the same with or without the template argument, IIRC. But one needs to consult the standard to be sure which is right. It hurts productivity. When in doubt, choose the least surprising way to write it.

: m_name(std::move(name))

using child_list_type = std::list<std::unique_ptr<RadixTreeNode<T>>>;
using key_type = int32_t;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add key_type so that in the future we can widen the index key type.

using child_list_type = std::list<std::unique_ptr<RadixTreeNode<T>>>;
using key_type = int32_t;

static_assert(std::is_integral_v<key_type> && std::is_signed_v<key_type>, "signed integral required");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key type needs to be a signed integer.

T & getData() { return m_data; }
const T & getData() const { return m_data; }
const ChildList & getChildren() const { return m_children; }
RadixTreeNode() = default;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supplement all constructors.

oss << getData();
}
return oss.str();
return m_prev;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move Info() to the test file because it is only used for testing.

RadixTreeNode & operator=(RadixTreeNode &&) = default;
~RadixTreeNode() = default;

key_type key() const { return m_key; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use single-name accessor style.


// Add a child node
RadixTreeNode<T> * addChild(std::string childName, int childKey)
RadixTreeNode<T> * add_child(std::string const & name, key_type key)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Member functions use snake_case naming style, as other code in modmesh does.

@@ -22,7 +22,7 @@ add_executable(
test_nopython_buffer.cpp
test_nopython_modmesh.cpp
test_nopython_inout.cpp
test_nonpython_radixtree.cpp
test_nopython_radixtree.cpp
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the file name.

#endif

template <typename T>
std::string get_info(modmesh::RadixTreeNode<T> & r)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper for printing the contents.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if the tests should work like example, how can the user know what class members to be printed.
Also, when developing, should debug printing function also be added into unit tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind that the RadixTree is still a prototype. It does not work for the profiling and stack printing yet. It does not have a Python wrapper yet. The usability cannot be defined yet. It is too early to evaluate how users will use the information. Because we do not have the information or experience for this feature, we do not add fragile code.

Adding fragile code in the tests is less intrusive, so we are doing it.


TEST(RadixTree, construction)
{
namespace mm = modmesh;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests also work as examples for C++ API. We do not want users to use the library by using the namespace. Thus, use mm = modmesh alias here.

return (it != m_children.end()) ? it->get() : nullptr;
}

// Get prev node
RadixTreeNode<T> * getPrev() const
RadixTreeNode<T> * get_prev() const
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is single-name accessor style also applicable to this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't decided, so leave it as a getter. get_prev and get_child look like a group so that I keep them in the same style. And I am not yet sure about whether a pointer or a reference should be returned. It makes more sense to return a reference with the single-name style.

Let's keep it as is for a while.

@yungyuc yungyuc merged commit a9cee5b into solvcon:master Nov 15, 2023
11 checks passed
@yungyuc yungyuc deleted the refactor/radixtree branch November 15, 2023 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Change code without changing tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants