-
Notifications
You must be signed in to change notification settings - Fork 43
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
Conversation
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.
There was a problem hiding this 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>>>; |
There was a problem hiding this comment.
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>
.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supplement all constructors.
cpp/modmesh/toggle/RadixTree.hpp
Outdated
oss << getData(); | ||
} | ||
return oss.str(); | ||
return m_prev; | ||
} |
There was a problem hiding this comment.
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; } |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
cpp/modmesh/toggle/RadixTree.hpp
Outdated
return (it != m_children.end()) ? it->get() : nullptr; | ||
} | ||
|
||
// Get prev node | ||
RadixTreeNode<T> * getPrev() const | ||
RadixTreeNode<T> * get_prev() const |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.