forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 194
Fix IntArray class and a few math classes with memory management and code style improvements #7112
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
Open
mohanchen
wants to merge
12
commits into
deepmodeling:develop
Choose a base branch
from
mohanchen:fix_intarray
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c60e91
Fix IntArray class with memory management and code style improvements
ed5b30a
Add memory allocation failure test and fix move operations
a567f82
Rename basic data types class design file to basic_types_class.md and…
9a02576
Merge branch 'develop' into fix_intarray
ErjieWu 313befc
update docs
14a96ff
Merge remote-tracking branch 'origin/fix_intarray' into fix_intarray
4e323a5
update vector3.h
ecf9bfa
update vector3
a7e9ad1
update docs
1de1ebb
update matrix.cpp
c4c7d58
update doc
2f691bb
update complexarray and intarray_test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| # Basic Tool Classes Design Guide for ABACUS | ||
|
|
||
| ## Overview | ||
|
|
||
| This document provides guidelines for designing and implementing basic tool classes in the ABACUS codebase, focusing on best practices for memory management, code style, and testing. These guidelines apply to all basic mathematical and utility classes, including but not limited to: | ||
|
|
||
| - vector3.h | ||
| - matrix.h | ||
| - timer.h | ||
| - ndarray.h | ||
| - realarray.h | ||
| - complexarray.h | ||
| - complexmatrix.h | ||
| - matrix3.h | ||
| - intarray.h | ||
| - formatter.h | ||
| - math_chebyshev.h | ||
|
|
||
| While this guide uses `IntArray` as an example for illustration purposes, the principles and practices described here are applicable to all basic tool classes in ABACUS. | ||
|
|
||
| ## Memory Management | ||
|
|
||
| ### 1. Exception Handling for Memory Allocation | ||
|
|
||
| Always use try-catch blocks when allocating memory to handle `std::bad_alloc` exceptions gracefully: | ||
|
|
||
| ### 2. Two-Stage Memory Allocation | ||
|
|
||
| When reallocating memory (e.g., in `create` methods), use a two-stage approach to ensure that the original object remains valid if memory allocation fails. | ||
|
|
||
| ### 3. Null Pointer Checks | ||
|
|
||
| Always check for null pointers before accessing memory, especially in methods that might be called on objects with failed memory allocation. | ||
|
|
||
| ## Class Design | ||
|
|
||
| ### 1. Copy Constructor | ||
|
|
||
| Implement a copy constructor to avoid shallow copy issues. | ||
|
|
||
| ### 2. Move Semantics | ||
|
|
||
| Implement move constructor and move assignment operator to improve performance. | ||
|
|
||
| ### 3. Boundary Checks | ||
|
|
||
| Add boundary checks to prevent out-of-bounds access. | ||
|
|
||
| ## Code Style | ||
|
|
||
| ### 1. Brace Style | ||
|
|
||
| Use separate lines for braces, and always use braces for "if" and "for" statements, even if they contain one line of code | ||
|
|
||
| ### 2. Indentation | ||
|
|
||
| Use spaces instead of tabs for indentation (4 spaces per indent level). | ||
|
|
||
| ### 3. Comments | ||
|
|
||
| Use English for comments and document important functionality. Follow Doxygen-style documentation for classes and methods. | ||
|
|
||
| ## Code Quality | ||
|
|
||
| ### 1. Named Constants | ||
|
|
||
| Avoid using magic numbers. Instead, define named constants for numerical values: | ||
|
|
||
| ### 2. Header Includes | ||
|
|
||
| Ensure all necessary header files are included, especially for functions like `assert`: | ||
|
|
||
| ```cpp | ||
| #include <cassert> | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| ### 1. Unit Tests | ||
|
|
||
| Write comprehensive unit tests for all classes, including: | ||
| - Constructor tests | ||
| - Method tests | ||
| - Exception handling tests | ||
| - Edge case tests | ||
|
|
||
| ### 2. Test Class Initialization | ||
|
|
||
| Use constructor initialization lists for test classes to improve compatibility: | ||
|
|
||
| ```cpp | ||
| class IntArrayTest : public testing::Test | ||
| { | ||
| protected: | ||
| ModuleBase::IntArray a2, a3, a4, a5, a6; | ||
| int aa; | ||
| int bb; | ||
| int count0; | ||
| int count1; | ||
| const int zero; | ||
|
|
||
| IntArrayTest() : aa(11), bb(1), zero(0) | ||
| { | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## Best Practices | ||
|
|
||
| 1. **Single Responsibility Principle**: Each class should have a single, well-defined responsibility. | ||
| 2. **Encapsulation**: Hide implementation details and expose only necessary interfaces. | ||
| 3. **Error Handling**: Handle errors gracefully, especially memory allocation failures. | ||
| 4. **Performance**: Use move semantics and other performance optimizations where appropriate. | ||
| 5. **Testing**: Write comprehensive tests for all functionality. | ||
| 6. **Code Style**: Follow consistent code style guidelines, including: | ||
| - Always use braces for if and for statements | ||
| - Use separate lines for braces | ||
| - Use spaces instead of tabs for indentation | ||
| - Use English for comments | ||
| 7. **Code Quality**: Maintain high code quality by: | ||
| - Using named constants instead of magic numbers | ||
| - Ensuring all necessary header files are included | ||
| - Adding boundary checks to prevent out-of-bounds access | ||
| 8. **Documentation**: Document classes and methods to improve maintainability. | ||
| 9. **Compatibility**: Ensure code is compatible with C++11 standard. | ||
| 10. **Portability**: Write code that works across different platforms. | ||
| 11. **Reusability**: Design classes to be reusable in different contexts. | ||
|
|
||
| ## Application to Other Basic Tool Classes | ||
|
|
||
| While this guide uses `IntArray` as an example, these principles apply to all basic tool classes in ABACUS. For example: | ||
|
|
||
| - **vector3.h**: Apply the same memory management and error handling principles, with additional focus on vector operations and operator overloading. | ||
| - **matrix.h**: Extend the memory management practices to 2D arrays, with additional considerations for matrix operations. | ||
| - **timer.h**: Focus on static member management and time measurement accuracy. | ||
| - **ndarray.h**: Apply the same principles to multi-dimensional arrays, with additional considerations for shape manipulation. | ||
| - **formatter.h**: Focus on string manipulation and formatting, with attention to performance and usability. | ||
| - **math_chebyshev.h**: Apply the principles to template classes, with additional focus on mathematical algorithm implementation. | ||
|
|
||
| By following these guidelines, you can ensure that all basic tool classes in ABACUS are well-designed, robust, and maintainable. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| .. _developers_guide: | ||
|
|
||
| Developers Guide | ||
| ================ | ||
|
|
||
| This section provides guidelines and resources for developers working on the ABACUS codebase. | ||
|
|
||
| .. toctree:: | ||
| :maxdepth: 2 | ||
| :caption: Developer Resources | ||
|
|
||
| basic_types_class.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 name of this document is "basic_types_class.md", but the content is entirely focused on IntArray. I noticed that "index.rst" is also created for developers to read. Should the file name be adjusted, or should other contents be added to this document later?
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 have updated the document, the document is intended for tool classes