diff --git a/docs/developers_guide/basic_types_class.md b/docs/developers_guide/basic_types_class.md new file mode 100644 index 0000000000..c582665e96 --- /dev/null +++ b/docs/developers_guide/basic_types_class.md @@ -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 +``` + +## 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. diff --git a/docs/developers_guide/index.rst b/docs/developers_guide/index.rst new file mode 100644 index 0000000000..089deee90a --- /dev/null +++ b/docs/developers_guide/index.rst @@ -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 diff --git a/docs/index.rst b/docs/index.rst index ef0071197f..ae96c40663 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -56,6 +56,7 @@ the developments and applications of novel machine-learning-assisted DFT methods :caption: Developing Team DevelopingTeam + developers_guide/index .. toctree:: :maxdepth: 2 diff --git a/source/source_base/complexarray.cpp b/source/source_base/complexarray.cpp index 17bfa7f9ee..3652746b38 100644 --- a/source/source_base/complexarray.cpp +++ b/source/source_base/complexarray.cpp @@ -6,260 +6,414 @@ #include #include "complexarray.h" -#include "global_function.h" namespace ModuleBase { -void complexArrayxAlloc(){ModuleBase::WARNING_QUIT("ComplexArray","Allocation error for complexArray");} +void complexArrayxAlloc() +{ + std::cerr << "Allocation error for complexArray" << std::endl; + std::exit(EXIT_FAILURE); +} -ComplexArray::ComplexArray(const int bnd1, const int bnd2, const int bnd3, const int bnd4){ - bound1 = bnd1; - bound2 = bnd2; - bound3 = bnd3; - bound4 = bnd4; - init(this->getSize()); +ComplexArray::ComplexArray(const int bnd1, const int bnd2, const int bnd3, const int bnd4) +{ + bound1 = bnd1; + bound2 = bnd2; + bound3 = bnd3; + bound4 = bnd4; + init(this->getSize()); } -ComplexArray::~ComplexArray(){ - freemem(); -} -void ComplexArray::init(const int size){ - assert(size>=0); - if(size>0){ - ptr = new std::complex [size]; - assert(ptr != 0);} - else - {ptr = nullptr;} -} -void ComplexArray::freemem(){ - delete [] ptr; - ptr = nullptr; - bound1 = 0; - bound2 = 0; - bound3 = 0; - bound4 = 0; -} -void ComplexArray::create(const int bnd1, const int bnd2, const int bnd3, const int bnd4){ - delete [] ptr; - bound1 = bnd1; - bound2 = bnd2; - bound3 = bnd3; - bound4 = bnd4; - const int size = this->getSize(); - this->init(size); - this->zero_out(); -} -ComplexArray::ComplexArray(const ComplexArray &cd){ - this->freemem(); - const int size = cd.getSize(); - this->init(size); - for (int i = 0; i < size; i++) - ptr[i] = cd.ptr[i]; - this->bound1 = cd.bound1; - this->bound2 = cd.bound2; - this->bound3 = cd.bound3; - this->bound4 = cd.bound4; -} -ComplexArray::ComplexArray(ComplexArray &&cd){ - delete [] this->ptr; - this->ptr =cd.ptr; cd.ptr =nullptr; - this->bound1=cd.bound1; cd.bound1=0; - this->bound2=cd.bound2; cd.bound2=0; - this->bound3=cd.bound3; cd.bound3=0; - this->bound4=cd.bound4; cd.bound4=0; -} -ComplexArray& ComplexArray::operator=(ComplexArray &&cd){ - delete [] this->ptr; - this->ptr =cd.ptr; cd.ptr =nullptr; - this->bound1=cd.bound1; cd.bound1=0; - this->bound2=cd.bound2; cd.bound2=0; - this->bound3=cd.bound3; cd.bound3=0; - this->bound4=cd.bound4; cd.bound4=0; - return *this;} -ComplexArray &ComplexArray::operator=(const ComplexArray & cd){ - const int size = this->getSize(); - assert(size==cd.getSize()); - for (int i = 0; i < size; i++) - ptr[i] = cd.ptr[i]; - return *this;} -void ComplexArray::operator=(const std::complex < double> c){ - const int size = this->getSize(); - for (int i = 0; i < size; i++) - ptr[i] = c;} -ComplexArray ComplexArray::operator+(const ComplexArray &cd) const{ - const int size = this->getSize(); - assert(size==cd.getSize()); - ComplexArray cd2(*this); - for (int i = 0; i < size; i++) - cd2.ptr[i] += cd.ptr[i]; - return cd2;} -void ComplexArray::operator+=(const ComplexArray & cd){ - const int size = this->getSize(); - assert(size==cd.getSize()); - for (int i = 0; i < size; i++) - ptr[i] += cd.ptr[i]; -} -ComplexArray ComplexArray::operator-(const ComplexArray &cd) const{ - const int size = this->getSize(); - assert(size==cd.getSize()); - ComplexArray cd2(*this); - for (int i = 0; i < size; i++) - cd2.ptr[i] -= cd.ptr[i]; - return cd2;} -void ComplexArray::operator-=(const ComplexArray & cd){ - const int size = this->getSize(); - assert(size==cd.getSize()); - for (int i = 0; i < size; i++) - ptr[i] -= cd.ptr[i]; -} -void ComplexArray::operator*=(const ComplexArray & cd){ - const int size = this->getSize(); - assert(size==cd.getSize()); - for (int i = 0; i < size; i++) - ptr[i] *= cd.ptr[i]; -} -ComplexArray operator*(const double r, const ComplexArray &cd){ - ComplexArray cd2(cd); - const int size = cd.getSize(); - for (int i = 0; i < size; i++) - cd2.ptr[i] *= r; - return cd2;} -ComplexArray ComplexArray::operator*(const double r) const{ - ComplexArray cd2(*this); - const int size = this->getSize(); - for (int i = 0; i < size; i++) - cd2.ptr[i] *= r; - return cd2;} -ComplexArray operator*(const std::complex < double> c, const ComplexArray &cd){ - const int size = cd.getSize(); - ComplexArray cd2(cd.getSize()); - for (int i = 0; i < size; i++) - cd2.ptr[i] = c * cd.ptr[i]; - return cd2;} -ComplexArray ComplexArray::operator*(const std::complex < double> c) const{ - const int size = this->getSize(); - ComplexArray cd(size); - for (int i = 0; i < size; i++) - cd.ptr[i] = ptr[i] * c; - return cd;} -void ComplexArray::operator*=(const std::complex c){ - const int size = this->getSize(); - for (int i = 0; i < size; i++) - ptr[i] *= c; -} -void ComplexArray::operator*=(const double r){ - const int size = this->getSize(); - for (int i = 0; i < size; i++) - ptr[i] *= r; -} -bool ComplexArray::operator==(const ComplexArray &cd2)const{ - const int size1 = this->getSize(); - const int size2 = cd2.getSize(); - const int b11 = this->getBound1(); - const int b12 = this->getBound2(); - const int b13 = this->getBound3(); - const int b14 = this->getBound4(); - const int b21 = cd2.getBound1(); - const int b22 = cd2.getBound2(); - const int b23 = cd2.getBound3(); - const int b24 = cd2.getBound4(); - if (size1 != size2) {return false;} - if (b11 != b21) {return false;} - if (b12 != b22) {return false;} - if (b13 != b23) {return false;} - if (b14 != b24) {return false;} - for ( int i = 0;i ptr[i] != cd2.ptr[i]) {return false;} } - return true;} -bool ComplexArray::operator!=(const ComplexArray &cd2)const{ - const int size1 = this->getSize(); - const int size2 = cd2.getSize(); - const int b11 = this->getBound1(); - const int b12 = this->getBound2(); - const int b13 = this->getBound3(); - const int b14 = this->getBound4(); - const int b21 = cd2.getBound1(); - const int b22 = cd2.getBound2(); - const int b23 = cd2.getBound3(); - const int b24 = cd2.getBound4(); - if (size1 != size2) {return true;} - if (b11 != b21) {return true;} - if (b12 != b22) {return true;} - if (b13 != b23) {return true;} - if (b14 != b24) {return true;} - for ( int i = 0;i ptr[i] != cd2.ptr[i]) {return true;} } - return false;} -void ComplexArray::zero_out(void){ - const int size = this->getSize(); - for (int i = 0;i < size; i++) - ptr[i] = std::complex < double> (0.0, 0.0); -} -void ComplexArray::negate(void){ - const int size = this->getSize(); - for (int i = 0;i < size; i++){ - ptr[i] = -ptr[i];} -} -void ComplexArray::randomize(void){ - const int size = this->getSize(); - for (int i = 0;i < size; i++){ - ptr[i] = std::complex < double> (rand() / (RAND_MAX + 1.) - .5, - rand() / (RAND_MAX + 1.) - .5);} -} -double abs2(const ComplexArray &cd){ - double cdcd= 0.0; - const int size = cd.getSize(); - for (int i = 0; i < size; i++){ - const std::complex < double> c = cd.ptr[i]; - cdcd += c.real() * c.real() + c.imag() * c.imag();} - return cdcd;} +ComplexArray::~ComplexArray() +{ + freemem(); +} +void ComplexArray::init(const int size) +{ + assert(size>=0); + if(size>0) + { + ptr = new std::complex [size]; + assert(ptr != 0); + } + else + { + ptr = nullptr; + } +} +void ComplexArray::freemem() +{ + delete [] ptr; + ptr = nullptr; + bound1 = 0; + bound2 = 0; + bound3 = 0; + bound4 = 0; +} +void ComplexArray::create(const int bnd1, const int bnd2, const int bnd3, const int bnd4) +{ + delete [] ptr; + bound1 = bnd1; + bound2 = bnd2; + bound3 = bnd3; + bound4 = bnd4; + const int size = this->getSize(); + this->init(size); + this->zero_out(); +} +ComplexArray::ComplexArray(const ComplexArray &cd) +{ + this->freemem(); + const int size = cd.getSize(); + this->init(size); + for (int i = 0; i < size; i++) + { + ptr[i] = cd.ptr[i]; + } + this->bound1 = cd.bound1; + this->bound2 = cd.bound2; + this->bound3 = cd.bound3; + this->bound4 = cd.bound4; +} +ComplexArray::ComplexArray(ComplexArray &&cd) +{ + delete [] this->ptr; + this->ptr = cd.ptr; + cd.ptr = nullptr; + this->bound1 = cd.bound1; + cd.bound1 = 0; + this->bound2 = cd.bound2; + cd.bound2 = 0; + this->bound3 = cd.bound3; + cd.bound3 = 0; + this->bound4 = cd.bound4; + cd.bound4 = 0; +} +ComplexArray& ComplexArray::operator=(ComplexArray &&cd) +{ + delete [] this->ptr; + this->ptr = cd.ptr; + cd.ptr = nullptr; + this->bound1 = cd.bound1; + cd.bound1 = 0; + this->bound2 = cd.bound2; + cd.bound2 = 0; + this->bound3 = cd.bound3; + cd.bound3 = 0; + this->bound4 = cd.bound4; + cd.bound4 = 0; + return *this; +} +ComplexArray &ComplexArray::operator=(const ComplexArray & cd) +{ + const int size = this->getSize(); + assert(size==cd.getSize()); + for (int i = 0; i < size; i++) + { + ptr[i] = cd.ptr[i]; + } + return *this; +} +void ComplexArray::operator=(const std::complex < double> c) +{ + const int size = this->getSize(); + for (int i = 0; i < size; i++) + { + ptr[i] = c; + } +} +ComplexArray ComplexArray::operator+(const ComplexArray &cd) const +{ + const int size = this->getSize(); + assert(size==cd.getSize()); + ComplexArray cd2(*this); + for (int i = 0; i < size; i++) + { + cd2.ptr[i] += cd.ptr[i]; + } + return cd2; +} +void ComplexArray::operator+=(const ComplexArray & cd) +{ + const int size = this->getSize(); + assert(size==cd.getSize()); + for (int i = 0; i < size; i++) + { + ptr[i] += cd.ptr[i]; + } +} +ComplexArray ComplexArray::operator-(const ComplexArray &cd) const +{ + const int size = this->getSize(); + assert(size==cd.getSize()); + ComplexArray cd2(*this); + for (int i = 0; i < size; i++) + { + cd2.ptr[i] -= cd.ptr[i]; + } + return cd2; +} +void ComplexArray::operator-=(const ComplexArray & cd) +{ + const int size = this->getSize(); + assert(size==cd.getSize()); + for (int i = 0; i < size; i++) + { + ptr[i] -= cd.ptr[i]; + } +} +void ComplexArray::operator*=(const ComplexArray & cd) +{ + const int size = this->getSize(); + assert(size==cd.getSize()); + for (int i = 0; i < size; i++) + { + ptr[i] *= cd.ptr[i]; + } +} +ComplexArray operator*(const double r, const ComplexArray &cd) +{ + ComplexArray cd2(cd); + const int size = cd.getSize(); + for (int i = 0; i < size; i++) + { + cd2.ptr[i] *= r; + } + return cd2; +} +ComplexArray ComplexArray::operator*(const double r) const +{ + ComplexArray cd2(*this); + const int size = this->getSize(); + for (int i = 0; i < size; i++) + { + cd2.ptr[i] *= r; + } + return cd2; +} +ComplexArray operator*(const std::complex < double> c, const ComplexArray &cd) +{ + const int size = cd.getSize(); + ComplexArray cd2(cd.getSize()); + for (int i = 0; i < size; i++) + { + cd2.ptr[i] = c * cd.ptr[i]; + } + return cd2; +} +ComplexArray ComplexArray::operator*(const std::complex < double> c) const +{ + const int size = this->getSize(); + ComplexArray cd(size); + for (int i = 0; i < size; i++) + { + cd.ptr[i] = ptr[i] * c; + } + return cd; +} +void ComplexArray::operator*=(const std::complex c) +{ + const int size = this->getSize(); + for (int i = 0; i < size; i++) + { + ptr[i] *= c; + } +} +void ComplexArray::operator*=(const double r) +{ + const int size = this->getSize(); + for (int i = 0; i < size; i++) + { + ptr[i] *= r; + } +} +bool ComplexArray::operator==(const ComplexArray &cd2)const +{ + const int size1 = this->getSize(); + const int size2 = cd2.getSize(); + const int b11 = this->getBound1(); + const int b12 = this->getBound2(); + const int b13 = this->getBound3(); + const int b14 = this->getBound4(); + const int b21 = cd2.getBound1(); + const int b22 = cd2.getBound2(); + const int b23 = cd2.getBound3(); + const int b24 = cd2.getBound4(); + if (size1 != size2) + { + return false; + } + if (b11 != b21) + { + return false; + } + if (b12 != b22) + { + return false; + } + if (b13 != b23) + { + return false; + } + if (b14 != b24) + { + return false; + } + for ( int i = 0;i ptr[i] != cd2.ptr[i]) + { + return false; + } + } + return true; +} +bool ComplexArray::operator!=(const ComplexArray &cd2)const +{ + const int size1 = this->getSize(); + const int size2 = cd2.getSize(); + const int b11 = this->getBound1(); + const int b12 = this->getBound2(); + const int b13 = this->getBound3(); + const int b14 = this->getBound4(); + const int b21 = cd2.getBound1(); + const int b22 = cd2.getBound2(); + const int b23 = cd2.getBound3(); + const int b24 = cd2.getBound4(); + if (size1 != size2) + { + return true; + } + if (b11 != b21) + { + return true; + } + if (b12 != b22) + { + return true; + } + if (b13 != b23) + { + return true; + } + if (b14 != b24) + { + return true; + } + for ( int i = 0;i ptr[i] != cd2.ptr[i]) + { + return true; + } + } + return false; +} +void ComplexArray::zero_out(void) +{ + const int size = this->getSize(); + for (int i = 0;i < size; i++) + { + ptr[i] = std::complex < double> (0.0, 0.0); + } +} +void ComplexArray::negate(void) +{ + const int size = this->getSize(); + for (int i = 0;i < size; i++) + { + ptr[i] = -ptr[i]; + } +} +void ComplexArray::randomize(void) +{ + const int size = this->getSize(); + for (int i = 0;i < size; i++) + { + ptr[i] = std::complex < double> (rand() / (RAND_MAX + 1.) - .5, + rand() / (RAND_MAX + 1.) - .5); + } +} +double abs2(const ComplexArray &cd) +{ + double cdcd= 0.0; + const int size = cd.getSize(); + for (int i = 0; i < size; i++) + { + const std::complex < double> c = cd.ptr[i]; + cdcd += c.real() * c.real() + c.imag() * c.imag(); + } + return cdcd; +} // void add_scale_abs2(const std::complex < double> &c, const ComplexArray & in, ComplexArray &out){ -// assert(in.getSize() == out.getSize()); -// const int size = in.getSize(); -// for (int i = 0; i < size; i++) -// out.ptr[i] += std::complex < double> (c.real() * 22, c.imag() * 22);} -std::complex dot(const ComplexArray &cd1, const ComplexArray &cd2){ - assert(cd1.getSize()==cd2.getSize()); - const int size = cd1.getSize(); - std::complex < double> dot12(0.0,0.0); - for (int i = 0; i < size; i++){ - dot12 += std::complex < double> - (cd1.ptr[i].real() * cd2.ptr[i].real() + - cd1.ptr[i].imag() * cd2.ptr[i].imag(), - cd1.ptr[i].real() * cd2.ptr[i].imag() - - cd1.ptr[i].imag() * cd2.ptr[i].real());} - return dot12;} -void scale_accumulate(double r, const ComplexArray &cd1, ComplexArray &cd2){ - assert(cd1.getSize()==cd2.getSize()); - const int size = cd1.getSize(); - for (int i = 0; i < size; i++) - cd2.ptr[i] += r * cd1.ptr[i]; -} -void scale_accumulate(const std::complex c, const ComplexArray &cd1, ComplexArray &cd2){ - assert(cd1.getSize()==cd2.getSize()); - const int size = cd1.getSize(); - for (int i = 0; i < size; i++) - cd2.ptr[i] += c * cd1.ptr[i]; -} -void scaled_sum(double r1, const ComplexArray &cd1,double r2, const ComplexArray &cd2,ComplexArray &cd3){ - assert(cd1.getSize()==cd2.getSize()); - assert(cd1.getSize()==cd3.getSize()); - const int size = cd1.getSize(); - for (int i = 0; i < size; i++) - cd3.ptr[i] = r1 * cd1.ptr[i] + r2 * cd2.ptr[i]; -} -void scaled_sum(std::complex < double> c1, const ComplexArray &cd1,std::complex < double> c2, const ComplexArray &cd2,ComplexArray &cd3){ - assert(cd1.getSize()==cd2.getSize()); - assert(cd1.getSize()==cd3.getSize()); - const int size = cd1.getSize(); - for (int i = 0; i < size; i++) - cd3.ptr[i] = c1 * cd1.ptr[i] + c2 * cd2.ptr[i];} -void point_mult(ComplexArray &in1, ComplexArray &in2, ComplexArray &out){ - assert(in1.getSize()==in2.getSize()); - assert(in1.getSize()==out.getSize()); - const int size = in1.getSize(); - for (int i = 0; i < size; i++){ - out.ptr[i] = std::complex < double> - (in1.ptr[i].real() * in2.ptr[i].real() - - in1.ptr[i].imag() * in2.ptr[i].imag(), - in1.ptr[i].real() * in2.ptr[i].imag() + - in1.ptr[i].imag() * in2.ptr[i].real());} +// assert(in.getSize() == out.getSize()); +// const int size = in.getSize(); +// for (int i = 0; i < size; i++) +// out.ptr[i] += std::complex < double> (c.real() * 22, c.imag() * 22);} +std::complex dot(const ComplexArray &cd1, const ComplexArray &cd2) +{ + assert(cd1.getSize()==cd2.getSize()); + const int size = cd1.getSize(); + std::complex < double> dot12(0.0,0.0); + for (int i = 0; i < size; i++) + { + dot12 += std::complex < double> + (cd1.ptr[i].real() * cd2.ptr[i].real() + + cd1.ptr[i].imag() * cd2.ptr[i].imag(), + cd1.ptr[i].real() * cd2.ptr[i].imag() - + cd1.ptr[i].imag() * cd2.ptr[i].real()); + } + return dot12; +} +void scale_accumulate(double r, const ComplexArray &cd1, ComplexArray &cd2) +{ + assert(cd1.getSize()==cd2.getSize()); + const int size = cd1.getSize(); + for (int i = 0; i < size; i++) + { + cd2.ptr[i] += r * cd1.ptr[i]; + } +} +void scale_accumulate(const std::complex c, const ComplexArray &cd1, ComplexArray &cd2) +{ + assert(cd1.getSize()==cd2.getSize()); + const int size = cd1.getSize(); + for (int i = 0; i < size; i++) + { + cd2.ptr[i] += c * cd1.ptr[i]; + } +} +void scaled_sum(double r1, const ComplexArray &cd1,double r2, const ComplexArray &cd2,ComplexArray &cd3) +{ + assert(cd1.getSize()==cd2.getSize()); + assert(cd1.getSize()==cd3.getSize()); + const int size = cd1.getSize(); + for (int i = 0; i < size; i++) + { + cd3.ptr[i] = r1 * cd1.ptr[i] + r2 * cd2.ptr[i]; + } +} +void scaled_sum(std::complex < double> c1, const ComplexArray &cd1,std::complex < double> c2, const ComplexArray &cd2,ComplexArray &cd3) +{ + assert(cd1.getSize()==cd2.getSize()); + assert(cd1.getSize()==cd3.getSize()); + const int size = cd1.getSize(); + for (int i = 0; i < size; i++) + { + cd3.ptr[i] = c1 * cd1.ptr[i] + c2 * cd2.ptr[i]; + } +} +void point_mult(ComplexArray &in1, ComplexArray &in2, ComplexArray &out) +{ + assert(in1.getSize()==in2.getSize()); + assert(in1.getSize()==out.getSize()); + const int size = in1.getSize(); + for (int i = 0; i < size; i++) + { + out.ptr[i] = std::complex < double> + (in1.ptr[i].real() * in2.ptr[i].real() - + in1.ptr[i].imag() * in2.ptr[i].imag(), + in1.ptr[i].real() * in2.ptr[i].imag() + + in1.ptr[i].imag() * in2.ptr[i].real()); + } } } diff --git a/source/source_base/intarray.cpp b/source/source_base/intarray.cpp index 10c3b7f39d..7c9cc0e71d 100644 --- a/source/source_base/intarray.cpp +++ b/source/source_base/intarray.cpp @@ -1,6 +1,3 @@ -/******************************************* - * ESCP:Electro-Structure Calculate Package. - ********************************************/ #include #include "intarray.h" @@ -8,77 +5,179 @@ namespace ModuleBase { void IntArrayAlloc() { - std::cout << "\n Allocation error for IntArray " << std::endl; - exit(0); + std::cout << "\n Allocation error for IntArray " << std::endl; + exit(0); } IntArray::IntArray(const int d1,const int d2) { - dim = 2; - bound1 = (d1 <= 0) ? 1 : d1; - bound2 = (d2 <= 0) ? 1 : d2; - bound3 = bound4 = bound5 = bound6 = 0; - size = bound1 * bound2; - ptr = new int[size];zero_out(); - assert( ptr != nullptr); + dim = 2; + bound1 = (d1 <= 0) ? 1 : d1; + bound2 = (d2 <= 0) ? 1 : d2; + bound3 = bound4 = bound5 = bound6 = 0; + size = bound1 * bound2; + try + { + ptr = new int[size]; + zero_out(); + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error for IntArray: " << e.what() << std::endl; + ptr = nullptr; + size = 0; + throw; + } + assert( ptr != nullptr); } IntArray::IntArray(const int d1,const int d2,const int d3) { - dim = 3; - bound1 = (d1 <= 0) ? 1 : d1; - bound2 = (d2 <= 0) ? 1 : d2; - bound3 = (d3 <= 0) ? 1 : d3; - bound4 = bound5 = bound6 = 0; - //set_new_handler(IntArrayAlloc); - size = bound1 * bound2 * bound3 ; //* sizeof(float); - ptr = new int[size];zero_out(); - assert(ptr != nullptr); + dim = 3; + bound1 = (d1 <= 0) ? 1 : d1; + bound2 = (d2 <= 0) ? 1 : d2; + bound3 = (d3 <= 0) ? 1 : d3; + bound4 = bound5 = bound6 = 0; + //set_new_handler(IntArrayAlloc); + size = bound1 * bound2 * bound3 ; //* sizeof(float); + try + { + ptr = new int[size]; + zero_out(); + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error for IntArray: " << e.what() << std::endl; + ptr = nullptr; + size = 0; + throw; + } + assert(ptr != nullptr); } IntArray::IntArray(const int d1,const int d2,const int d3,const int d4) { - dim = 4; - bound1 = (d1 <= 0) ? 1 : d1; - bound2 = (d2 <= 0) ? 1 : d2; - bound3 = (d3 <= 0) ? 1 : d3; - bound4 = (d4 <= 0) ? 1 : d4; - bound5 = bound6 = 0; - //set_new_handler(IntArrayAlloc); - size = bound1 * bound2 * bound3 * bound4 ; //* sizeof(float); - ptr = new int[size];zero_out(); - assert(ptr != nullptr); + dim = 4; + bound1 = (d1 <= 0) ? 1 : d1; + bound2 = (d2 <= 0) ? 1 : d2; + bound3 = (d3 <= 0) ? 1 : d3; + bound4 = (d4 <= 0) ? 1 : d4; + bound5 = bound6 = 0; + //set_new_handler(IntArrayAlloc); + size = bound1 * bound2 * bound3 * bound4 ; //* sizeof(float); + try + { + ptr = new int[size]; + zero_out(); + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error for IntArray: " << e.what() << std::endl; + ptr = nullptr; + size = 0; + throw; + } + assert(ptr != nullptr); } IntArray::IntArray(const int d1,const int d2,const int d3, - const int d4,const int d5) + const int d4,const int d5) { - dim = 5; - bound1 = (d1 <= 0) ? 1 : d1; - bound2 = (d2 <= 0) ? 1 : d2; - bound3 = (d3 <= 0) ? 1 : d3; - bound4 = (d4 <= 0) ? 1 : d4; - bound5 = (d5 <= 0) ? 1 : d5; - //set_new_handler(IntArrayAlloc); - size = bound1 * bound2 * bound3 * bound4 * bound5; - ptr = new int[size];zero_out(); - assert(ptr != nullptr); + dim = 5; + bound1 = (d1 <= 0) ? 1 : d1; + bound2 = (d2 <= 0) ? 1 : d2; + bound3 = (d3 <= 0) ? 1 : d3; + bound4 = (d4 <= 0) ? 1 : d4; + bound5 = (d5 <= 0) ? 1 : d5; + //set_new_handler(IntArrayAlloc); + size = bound1 * bound2 * bound3 * bound4 * bound5; + try + { + ptr = new int[size]; + zero_out(); + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error for IntArray: " << e.what() << std::endl; + ptr = nullptr; + size = 0; + throw; + } + assert(ptr != nullptr); } IntArray::IntArray(const int d1,const int d2,const int d3, - const int d4,const int d5,const int d6) + const int d4,const int d5,const int d6) { - dim = 6; - bound1 = (d1 <= 0) ? 1 : d1; + dim = 6; + bound1 = (d1 <= 0) ? 1 : d1; bound2 = (d2 <= 0) ? 1 : d2; bound3 = (d3 <= 0) ? 1 : d3; bound4 = (d4 <= 0) ? 1 : d4; bound5 = (d5 <= 0) ? 1 : d5; - bound6 = (d6 <= 0) ? 1 : d6; + bound6 = (d6 <= 0) ? 1 : d6; //set_new_handler(IntArrayAlloc); size = bound1 * bound2 * bound3 * bound4 * bound5 * bound6; - ptr = new int[size];zero_out(); - assert(ptr != nullptr); + try + { + ptr = new int[size]; + zero_out(); + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error for IntArray: " << e.what() << std::endl; + ptr = nullptr; + size = 0; + throw; + } + assert(ptr != nullptr); +} + +// Copy constructor +IntArray::IntArray(const IntArray& other) +{ + size = other.size; + dim = other.dim; + bound1 = other.bound1; + bound2 = other.bound2; + bound3 = other.bound3; + bound4 = other.bound4; + bound5 = other.bound5; + bound6 = other.bound6; + try + { + ptr = new int[size]; + for (int i = 0; i < size; i++) + { + ptr[i] = other.ptr[i]; + } + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error in IntArray copy constructor: " << e.what() << std::endl; + ptr = nullptr; + size = 0; + throw; + } + assert(ptr != nullptr); +} + +// Move constructor +IntArray::IntArray(IntArray&& other) noexcept + : size(other.size), + dim(other.dim), + bound1(other.bound1), + bound2(other.bound2), + bound3(other.bound3), + bound4(other.bound4), + bound5(other.bound5), + bound6(other.bound6), + ptr(other.ptr) +{ + other.ptr = nullptr; + other.size = 0; + other.dim = 0; + other.bound1 = other.bound2 = other.bound3 = other.bound4 = other.bound5 = other.bound6 = 0; } //******************************** @@ -91,56 +190,139 @@ IntArray ::~IntArray() void IntArray::freemem() { - if(ptr!= nullptr) - { - delete [] ptr; - ptr = nullptr; - } + if (ptr != nullptr) + { + delete[] ptr; + ptr = nullptr; + } +} + +// Move assignment operator +IntArray& IntArray::operator=(IntArray&& other) noexcept +{ + if (this != &other) + { + freemem(); + size = other.size; + dim = other.dim; + bound1 = other.bound1; + bound2 = other.bound2; + bound3 = other.bound3; + bound4 = other.bound4; + bound5 = other.bound5; + bound6 = other.bound6; + ptr = other.ptr; + other.ptr = nullptr; + other.size = 0; + other.dim = 0; + other.bound1 = other.bound2 = other.bound3 = other.bound4 = other.bound5 = other.bound6 = 0; + } + return *this; } void IntArray::create(const int d1,const int d2,const int d3,const int d4,const int d5,const int d6) { - size = d1 * d2 * d3 * d4 * d5 * d6;assert(size>0); - dim = 6; - bound1 = d1;bound2 = d2;bound3 = d3;bound4 = d4;bound5 = d5;bound6 = d6; - delete[] ptr; ptr = new int[size]; - assert(ptr != nullptr);zero_out(); + size = d1 * d2 * d3 * d4 * d5 * d6;assert(size>0); + dim = 6; + bound1 = d1;bound2 = d2;bound3 = d3;bound4 = d4;bound5 = d5;bound6 = d6; + int* new_ptr = nullptr; + try + { + new_ptr = new int[size]; + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error in IntArray::create: " << e.what() << std::endl; + assert(new_ptr != nullptr); + return; + } + delete[] ptr; + ptr = new_ptr; + zero_out(); } void IntArray::create(const int d1,const int d2,const int d3,const int d4,const int d5) { - size = d1 * d2 * d3 * d4 * d5;assert(size>0); - dim = 5; - bound1 = d1;bound2 = d2;bound3 = d3;bound4 = d4;bound5 = d5; - delete[] ptr; ptr = new int[size]; - assert(ptr != nullptr);zero_out(); + size = d1 * d2 * d3 * d4 * d5;assert(size>0); + dim = 5; + bound1 = d1;bound2 = d2;bound3 = d3;bound4 = d4;bound5 = d5; + int* new_ptr = nullptr; + try + { + new_ptr = new int[size]; + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error in IntArray::create: " << e.what() << std::endl; + assert(new_ptr != nullptr); + return; + } + delete[] ptr; + ptr = new_ptr; + zero_out(); } void IntArray::create(const int d1,const int d2,const int d3,const int d4) { - size = d1 * d2 * d3 * d4;assert(size>0); - dim = 4; - bound1 = d1;bound2 = d2;bound3 = d3;bound4 = d4; - delete[] ptr; ptr = new int[size]; - assert(ptr != nullptr);zero_out(); + size = d1 * d2 * d3 * d4;assert(size>0); + dim = 4; + bound1 = d1;bound2 = d2;bound3 = d3;bound4 = d4; + int* new_ptr = nullptr; + try + { + new_ptr = new int[size]; + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error in IntArray::create: " << e.what() << std::endl; + assert(new_ptr != nullptr); + return; + } + delete[] ptr; + ptr = new_ptr; + zero_out(); } void IntArray::create(const int d1,const int d2,const int d3) { - size = d1 * d2 * d3;assert(size>0); - dim = 3; - bound1 = d1;bound2 = d2;bound3 = d3;bound4 = 1; - delete [] ptr;ptr = new int[size]; - assert(ptr != nullptr);zero_out(); + size = d1 * d2 * d3;assert(size>0); + dim = 3; + bound1 = d1;bound2 = d2;bound3 = d3;bound4 = 1; + int* new_ptr = nullptr; + try + { + new_ptr = new int[size]; + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error in IntArray::create: " << e.what() << std::endl; + assert(new_ptr != nullptr); + return; + } + delete [] ptr; + ptr = new_ptr; + zero_out(); } void IntArray::create(const int d1, const int d2) { - size = d1 * d2;assert(size>0); - dim = 2; - bound1 = d1;bound2 = d2;bound3 = bound4 = 1; - delete[] ptr;ptr = new int[size]; - assert(ptr != nullptr );zero_out(); + size = d1 * d2;assert(size>0); + dim = 2; + bound1 = d1;bound2 = d2;bound3 = bound4 = 1; + int* new_ptr = nullptr; + try + { + new_ptr = new int[size]; + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error in IntArray::create: " << e.what() << std::endl; + assert(new_ptr != nullptr); + return; + } + delete[] ptr; + ptr = new_ptr; + zero_out(); } //**************************** @@ -148,15 +330,15 @@ void IntArray::create(const int d1, const int d2) //**************************** void IntArray::zero_out() { - if (size <= 0) - { - return; - } - for (int i = 0;i < size; i++) - { - ptr[i] = 0; - } - return; + if (size <= 0 || ptr == nullptr) + { + return; + } + for (int i = 0;i < size; i++) + { + ptr[i] = 0; + } + return; } -} \ No newline at end of file +} diff --git a/source/source_base/intarray.h b/source/source_base/intarray.h index a35f8aa887..c518ad3e67 100644 --- a/source/source_base/intarray.h +++ b/source/source_base/intarray.h @@ -1,7 +1,3 @@ -/******************************************* - * ESCP:Electro-Structure Calculate Package. - ********************************************/ - #ifndef INTARRAY_H #define INTARRAY_H @@ -9,6 +5,7 @@ #include #include #include +#include namespace ModuleBase { @@ -32,6 +29,10 @@ class IntArray IntArray(const int d1, const int d2, const int d3, const int d4); IntArray(const int d1, const int d2, const int d3, const int d4, const int d5); IntArray(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6); + // Copy constructor + IntArray(const IntArray& other); + // Move constructor + IntArray(IntArray&& other) noexcept; ~IntArray(); @@ -66,12 +67,28 @@ class IntArray bound4 = other.bound4; bound5 = other.bound5; bound6 = other.bound6; - ptr = new int[size]; - for (int i = 0;i < size;i++) - { ptr[i] = other.ptr[i]; } + try + { + ptr = new int[size]; + for (int i = 0;i < size;i++) + { + ptr[i] = other.ptr[i]; + } + } + catch (const std::bad_alloc& e) + { + std::cerr << "Allocation error in IntArray copy assignment: " << e.what() << std::endl; + ptr = nullptr; + size = 0; + throw; + } } return *this; } + + // Move assignment operator + IntArray& operator=(IntArray&& other) noexcept; + /** * @brief Equal all elements of an IntArray to an @@ -82,7 +99,12 @@ class IntArray */ const IntArray &operator=(const int &right) { - for (int i = 0;i < size;i++) ptr[i] = right; + if (ptr != nullptr && size > 0) { + for (int i = 0;i < size;i++) + { + ptr[i] = right; + } + } return *this;// enables x = y = z; } @@ -95,42 +117,42 @@ class IntArray */ int &operator()(const int d1, const int d2) { - assert( d1 < bound1 ); - assert( d2 < bound2 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); return ptr[ d1 * bound2 + d2 ]; } int &operator()(const int d1, const int d2, const int d3) { - assert( d1 < bound1 ); - assert( d2 < bound2 ); - assert( d3 < bound3 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); + assert( d3 >= 0 && d3 < bound3 ); return ptr[ (d1 * bound2 + d2) * bound3 + d3 ]; } int &operator()(const int d1, const int d2, const int d3, const int d4) { - assert( d1 < bound1 ); - assert( d2 < bound2 ); - assert( d3 < bound3 ); - assert( d4 < bound4 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); + assert( d3 >= 0 && d3 < bound3 ); + assert( d4 >= 0 && d4 < bound4 ); return ptr[ ((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4 ]; } int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5) { - assert( d1 < bound1 ); - assert( d2 < bound2 ); - assert( d3 < bound3 ); - assert( d4 < bound4 ); - assert( d5 < bound5 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); + assert( d3 >= 0 && d3 < bound3 ); + assert( d4 >= 0 && d4 < bound4 ); + assert( d5 >= 0 && d5 < bound5 ); return ptr[ (((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5 ]; } int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6) { - assert( d1 < bound1 ); - assert( d2 < bound2 ); - assert( d3 < bound3 ); - assert( d4 < bound4 ); - assert( d5 < bound5 ); - assert( d6 < bound6 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); + assert( d3 >= 0 && d3 < bound3 ); + assert( d4 >= 0 && d4 < bound4 ); + assert( d5 >= 0 && d5 < bound5 ); + assert( d6 >= 0 && d6 < bound6 ); return ptr[ ((((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5) * bound6 + d6 ]; } @@ -144,42 +166,42 @@ class IntArray */ const int &operator()(const int d1, const int d2) const { - assert( d1 < bound1 ); - assert( d2 < bound2 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); return ptr[ d1 * bound2 + d2 ]; } const int &operator()(const int d1, const int d2, const int d3) const { - assert( d1 < bound1 ); - assert( d2 < bound2 ); - assert( d3 < bound3 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); + assert( d3 >= 0 && d3 < bound3 ); return ptr[ (d1 * bound2 + d2) * bound3 + d3 ]; } const int &operator()(const int d1, const int d2, const int d3, const int d4) const { - assert( d1 < bound1 ); - assert( d2 < bound2 ); - assert( d3 < bound3 ); - assert( d4 < bound4 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); + assert( d3 >= 0 && d3 < bound3 ); + assert( d4 >= 0 && d4 < bound4 ); return ptr[ ((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4 ]; } const int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5) const { - assert( d1 < bound1 ); - assert( d2 < bound2 ); - assert( d3 < bound3 ); - assert( d4 < bound4 ); - assert( d5 < bound5 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); + assert( d3 >= 0 && d3 < bound3 ); + assert( d4 >= 0 && d4 < bound4 ); + assert( d5 >= 0 && d5 < bound5 ); return ptr[ (((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5 ]; } const int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6) const { - assert( d1 < bound1 ); - assert( d2 < bound2 ); - assert( d3 < bound3 ); - assert( d4 < bound4 ); - assert( d5 < bound5 ); - assert( d6 < bound6 ); + assert( d1 >= 0 && d1 < bound1 ); + assert( d2 >= 0 && d2 < bound2 ); + assert( d3 >= 0 && d3 < bound3 ); + assert( d4 >= 0 && d4 < bound4 ); + assert( d5 >= 0 && d5 < bound5 ); + assert( d6 >= 0 && d6 < bound6 ); return ptr[ ((((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5) * bound6 + d6 ]; } diff --git a/source/source_base/matrix.cpp b/source/source_base/matrix.cpp index f047f48462..eaa7c30406 100644 --- a/source/source_base/matrix.cpp +++ b/source/source_base/matrix.cpp @@ -10,7 +10,6 @@ #include #include "matrix.h" -#include "global_function.h" #ifdef __NORMAL #else @@ -27,7 +26,11 @@ namespace ModuleBase { -void matrixAlloc(){ModuleBase::WARNING_QUIT("matrix","Allocation error for Matrix");} +void matrixAlloc() +{ + std::cerr << "Allocation error for Matrix" << std::endl; + std::exit(EXIT_FAILURE); +} /*create a matrix with nrows*ncols size */ matrix::matrix( const int nrows, const int ncols, const bool flag_zero ) @@ -163,7 +166,9 @@ matrix operator+(const matrix &m1, const matrix &m2) matrix tm(m1); const int size = m1.nr*m1.nc; for (int i = 0; i < size; i++) + { tm.c[i] += m2.c[i]; + } return tm; } @@ -176,7 +181,9 @@ matrix operator-(const matrix &m1, const matrix &m2) matrix tm(m1); const int size = m1.nr*m1.nc; for(int i = 0; i < size; i++) + { tm.c[i] -= m2.c[i]; + } return tm; } @@ -224,7 +231,9 @@ matrix operator*(const double &s, const matrix &m) matrix sm(m); const int size=m.nr*m.nc; for (int i = 0; i < size; i++) + { sm.c[i] *= s; + } return sm; } @@ -234,7 +243,9 @@ matrix operator*(const matrix &m,const double &s) matrix sm(m); const int size=m.nr*m.nc; for (int i = 0; i < size; i++) + { sm.c[i] *= s; + } return sm; } @@ -258,7 +269,9 @@ void matrix::operator+=(const matrix & m) const int size=nc*nr; const double * const c_in = m.c; for( int i = 0; i < size; ++i ) + { c[i] += c_in[i]; + } } @@ -274,7 +287,9 @@ void matrix::operator-=(const matrix & m) const int size=nc*nr; const double * const c_in = m.c; for( int i = 0; i < size; ++i ) + { c[i] -= c_in[i]; + } } /* zero out the matrix */ diff --git a/source/source_base/test/intarray_test.cpp b/source/source_base/test/intarray_test.cpp index 7372b4e115..bc9bb7b6fa 100644 --- a/source/source_base/test/intarray_test.cpp +++ b/source/source_base/test/intarray_test.cpp @@ -31,6 +31,12 @@ * - without changing its elements * -IntArrayAlloc * - Warning of integer array allocation error + * - CopyConstructor + * - test copy constructor + * - MoveConstructor + * - test move constructor + * - MoveAssignment + * - test move assignment operator */ namespace ModuleBase @@ -41,12 +47,16 @@ void IntArrayAlloc(); class IntArrayTest : public testing::Test { protected: - ModuleBase::IntArray a2, a3, a4, a5, a6; - int aa = 11; - int bb = 1; - int count0; - int count1; - const int zero = 0; + 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) + { + } }; TEST_F(IntArrayTest,Construct) @@ -312,3 +322,63 @@ TEST_F(IntArrayTest,Alloc) output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output,testing::HasSubstr("Allocation error for IntArray")); } + +TEST_F(IntArrayTest,CopyConstructor) +{ + a2.create(2,3); + a2 = aa; + ModuleBase::IntArray x2(a2); + EXPECT_EQ(x2.getSize(),a2.getSize()); + EXPECT_EQ(x2.getDim(),a2.getDim()); + EXPECT_EQ(x2.getBound1(),a2.getBound1()); + EXPECT_EQ(x2.getBound2(),a2.getBound2()); + for (int i=0;i +#include #include #include #include @@ -12,6 +13,8 @@ namespace ModuleBase { + // Small epsilon value for numerical comparisons + constexpr double epsilon = 1e-10; /** * @brief 3 elements vector @@ -26,20 +29,30 @@ template class Vector3 T z; /** - * @brief Construct a new Vector 3 object + * @brief Default constructor * * @param x1 * @param y1 * @param z1 */ - Vector3(const T &x1 = 0, const T &y1 = 0, const T &z1 = 0) : x(x1), y(y1), z(z1){}; - Vector3(const Vector3 &v) : x(v.x), y(v.y), z(v.z){}; // Peize Lin add 2018-07-16 - explicit Vector3(const std::array &v) :x(v[0]), y(v[1]), z(v[2]){} + Vector3(const T &x1 = 0, const T &y1 = 0, const T &z1 = 0) + : x(x1), y(y1), z(z1) + {} + Vector3(const Vector3 &v) + : x(v.x), y(v.y), z(v.z) + {} + explicit Vector3(const std::array &v) + : x(v[0]), y(v[1]), z(v[2]) + {} template - explicit Vector3(const Vector3& other) : x(static_cast(other.x)), y(static_cast(other.y)), z(static_cast(other.z)) {} + explicit Vector3(const Vector3& other) + : x(static_cast(other.x)), y(static_cast(other.y)), z(static_cast(other.z)) + {} - Vector3(Vector3 &&v) noexcept : x(v.x), y(v.y), z(v.z) {} + Vector3(Vector3 &&v) noexcept + : x(v.x), y(v.y), z(v.z) + {} /** * @brief set a 3d vector @@ -77,6 +90,20 @@ template class Vector3 return *this; } + /** + * @brief Move assignment operator + * + * @param u + * @return Vector3& + */ + Vector3 &operator=(Vector3 &&u) noexcept + { + x = u.x; + y = u.y; + z = u.z; + return *this; + } + /** * @brief Overload operator "+=" for Vector3 * @@ -120,13 +147,14 @@ template class Vector3 } /** - * @brief Overload operator "/=" for (Vector3)/scalar + * @brief Overload operator "/=" for Vector3 * * @param s * @return Vector3& */ Vector3 &operator/=(const T &s) { + assert(s != 0); // Avoid division by zero x /= s; y /= s; z /= s; @@ -141,7 +169,7 @@ template class Vector3 Vector3 operator-() const { return Vector3(-x, -y, -z); - } // Peize Lin add 2017-01-10 + } /** * @brief Over load "[]" for accessing elements with pointers @@ -151,6 +179,7 @@ template class Vector3 */ T operator[](int index) const { + assert(index >= 0 && index < 3); //return (&x)[index]; // this is undefind behavior and breaks with icpx T const* ptr[3] = {&x, &y, &z}; return *ptr[index]; @@ -164,6 +193,7 @@ template class Vector3 */ T &operator[](int index) { + assert(index >= 0 && index < 3); //return (&x)[index]; // this is undefind behavior and breaks with icpx T* ptr[3] = {&x, &y, &z}; return *ptr[index]; @@ -197,9 +227,12 @@ template class Vector3 Vector3 &normalize(void) { const T m = norm(); - x /= m; - y /= m; - z /= m; + if (m > epsilon) // Avoid division by zero + { + x /= m; + y /= m; + z /= m; + } return *this; } // Peize Lin update return 2019-09-08 @@ -221,7 +254,12 @@ template class Vector3 * with formats * */ - void print(void) const; // mohan add 2009-11-29 + /** + * @brief Print a Vector3 on standard output with formats + * + * @param precision The number of decimal places to display (must be positive, default: 5) + */ + void print(const int precision = 5) const; }; /** @@ -369,15 +407,25 @@ template inline Vector3 cross(const Vector3 &u, const Vector3 template bool operator<(const Vector3 &u, const Vector3 &v) { if (u.x < v.x) + { return true; + } if (u.x > v.x) + { return false; + } if (u.y < v.y) + { return true; + } if (u.y > v.y) + { return false; + } if (u.z < v.z) + { return true; + } return false; } @@ -390,17 +438,22 @@ template inline bool operator!=(const Vector3 &u, const Vector3 template inline bool operator==(const Vector3 &u, const Vector3 &v) { if (u.x == v.x && u.y == v.y && u.z == v.z) + { return true; + } return false; } /** * @brief Print a Vector3 on standard output with formats * + * @param precision The number of decimal places to display (must be positive, default: 5) */ -template void Vector3::print(void) const +template void Vector3::print(const int precision) const { - std::cout.precision(5); + // Ensure precision is non-negative + int valid_precision = precision > 0 ? precision : 5; + std::cout.precision(valid_precision); std::cout << "(" << std::setw(10) << x << "," << std::setw(10) << y << "," << std::setw(10) << z << ")" << std::endl; return;