Skip to content

Commit 16cd841

Browse files
mohanchenabacus_fixerErjieWu
authored
Fix IntArray class and a few math classes with memory management and code style improvements (#7112)
* Fix IntArray class with memory management and code style improvements Changes: 1. Fix memory management issues: - Add exception handling for memory allocation failures - Use two-stage memory allocation in create() methods to avoid memory leaks - Add null pointer checks in zero_out() and operator=(int) 2. Improve class design: - Implement copy constructor to avoid shallow copy issues - Implement move constructor and move assignment operator for better performance - Add comprehensive boundary checks for negative indices 3. Code style improvements: - Change braces to be on separate lines - Replace tabs with spaces - Change Chinese comments to English 4. Ensure code robustness: - Add error messages for memory allocation failures - Maintain object validity even when memory allocation fails This fix ensures the IntArray class is more robust, secure, and efficient, while following consistent code style guidelines. * Add memory allocation failure test and fix move operations Changes: 1. Add memory allocation failure test using custom operator new override 2. Fix move constructor and move assignment operator to properly reset all member variables 3. Improve test class initialization by moving member initialization to constructor 4. Ensure all tests pass with the new functionality This commit enhances the robustness of the IntArray class by ensuring it properly handles memory allocation failures and correctly implements move semantics. * Rename basic data types class design file to basic_types_class.md and update index * update docs * update vector3.h * update vector3 * update docs * update matrix.cpp * update doc * update complexarray and intarray_test * fix matrix and complexarray tests --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn> Co-authored-by: Erjie Wu <110683255+ErjieWu@users.noreply.github.com>
1 parent 6eec740 commit 16cd841

11 files changed

Lines changed: 1057 additions & 425 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Basic Tool Classes Design Guide for ABACUS
2+
3+
## Overview
4+
5+
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:
6+
7+
- vector3.h
8+
- matrix.h
9+
- timer.h
10+
- ndarray.h
11+
- realarray.h
12+
- complexarray.h
13+
- complexmatrix.h
14+
- matrix3.h
15+
- intarray.h
16+
- formatter.h
17+
- math_chebyshev.h
18+
19+
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.
20+
21+
## Memory Management
22+
23+
### 1. Exception Handling for Memory Allocation
24+
25+
Always use try-catch blocks when allocating memory to handle `std::bad_alloc` exceptions gracefully:
26+
27+
### 2. Two-Stage Memory Allocation
28+
29+
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.
30+
31+
### 3. Null Pointer Checks
32+
33+
Always check for null pointers before accessing memory, especially in methods that might be called on objects with failed memory allocation.
34+
35+
## Class Design
36+
37+
### 1. Copy Constructor
38+
39+
Implement a copy constructor to avoid shallow copy issues.
40+
41+
### 2. Move Semantics
42+
43+
Implement move constructor and move assignment operator to improve performance.
44+
45+
### 3. Boundary Checks
46+
47+
Add boundary checks to prevent out-of-bounds access.
48+
49+
## Code Style
50+
51+
### 1. Brace Style
52+
53+
Use separate lines for braces, and always use braces for "if" and "for" statements, even if they contain one line of code
54+
55+
### 2. Indentation
56+
57+
Use spaces instead of tabs for indentation (4 spaces per indent level).
58+
59+
### 3. Comments
60+
61+
Use English for comments and document important functionality. Follow Doxygen-style documentation for classes and methods.
62+
63+
## Code Quality
64+
65+
### 1. Named Constants
66+
67+
Avoid using magic numbers. Instead, define named constants for numerical values:
68+
69+
### 2. Header Includes
70+
71+
Ensure all necessary header files are included, especially for functions like `assert`:
72+
73+
```cpp
74+
#include <cassert>
75+
```
76+
77+
## Testing
78+
79+
### 1. Unit Tests
80+
81+
Write comprehensive unit tests for all classes, including:
82+
- Constructor tests
83+
- Method tests
84+
- Exception handling tests
85+
- Edge case tests
86+
87+
### 2. Test Class Initialization
88+
89+
Use constructor initialization lists for test classes to improve compatibility:
90+
91+
```cpp
92+
class IntArrayTest : public testing::Test
93+
{
94+
protected:
95+
ModuleBase::IntArray a2, a3, a4, a5, a6;
96+
int aa;
97+
int bb;
98+
int count0;
99+
int count1;
100+
const int zero;
101+
102+
IntArrayTest() : aa(11), bb(1), zero(0)
103+
{
104+
}
105+
};
106+
```
107+
108+
## Best Practices
109+
110+
1. **Single Responsibility Principle**: Each class should have a single, well-defined responsibility.
111+
2. **Encapsulation**: Hide implementation details and expose only necessary interfaces.
112+
3. **Error Handling**: Handle errors gracefully, especially memory allocation failures.
113+
4. **Performance**: Use move semantics and other performance optimizations where appropriate.
114+
5. **Testing**: Write comprehensive tests for all functionality.
115+
6. **Code Style**: Follow consistent code style guidelines, including:
116+
- Always use braces for if and for statements
117+
- Use separate lines for braces
118+
- Use spaces instead of tabs for indentation
119+
- Use English for comments
120+
7. **Code Quality**: Maintain high code quality by:
121+
- Using named constants instead of magic numbers
122+
- Ensuring all necessary header files are included
123+
- Adding boundary checks to prevent out-of-bounds access
124+
8. **Documentation**: Document classes and methods to improve maintainability.
125+
9. **Compatibility**: Ensure code is compatible with C++11 standard.
126+
10. **Portability**: Write code that works across different platforms.
127+
11. **Reusability**: Design classes to be reusable in different contexts.
128+
129+
## Application to Other Basic Tool Classes
130+
131+
While this guide uses `IntArray` as an example, these principles apply to all basic tool classes in ABACUS. For example:
132+
133+
- **vector3.h**: Apply the same memory management and error handling principles, with additional focus on vector operations and operator overloading.
134+
- **matrix.h**: Extend the memory management practices to 2D arrays, with additional considerations for matrix operations.
135+
- **timer.h**: Focus on static member management and time measurement accuracy.
136+
- **ndarray.h**: Apply the same principles to multi-dimensional arrays, with additional considerations for shape manipulation.
137+
- **formatter.h**: Focus on string manipulation and formatting, with attention to performance and usability.
138+
- **math_chebyshev.h**: Apply the principles to template classes, with additional focus on mathematical algorithm implementation.
139+
140+
By following these guidelines, you can ensure that all basic tool classes in ABACUS are well-designed, robust, and maintainable.

docs/developers_guide/index.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. _developers_guide:
2+
3+
Developers Guide
4+
================
5+
6+
This section provides guidelines and resources for developers working on the ABACUS codebase.
7+
8+
.. toctree::
9+
:maxdepth: 2
10+
:caption: Developer Resources
11+
12+
basic_types_class.md

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ the developments and applications of novel machine-learning-assisted DFT methods
5656
:caption: Developing Team
5757

5858
DevelopingTeam
59+
developers_guide/index
5960

6061
.. toctree::
6162
:maxdepth: 2

0 commit comments

Comments
 (0)