Skip to content

Commit

Permalink
clang format src/
Browse files Browse the repository at this point in the history
Co-authored by: Aleksandar Zeljic
  • Loading branch information
wu-haoze committed Feb 13, 2024
1 parent 3b3611b commit b030584
Show file tree
Hide file tree
Showing 379 changed files with 11,020 additions and 10,210 deletions.
59 changes: 59 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
AccessModifierOffset: '-4'
AlignOperands: 'AlignAfterOperator'
BreakBeforeBinaryOperators: None
AlignTrailingComments: 'true'
AllowAllArgumentsOnNextLine: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'Empty'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortEnumsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: 'false'
AllowShortIfStatementsOnASingleLine: 'Never'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterReturnType: 'None'
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: 'true'
AfterClass: 'true'
AfterControlStatement: 'true'
AfterEnum: 'false'
AfterFunction: 'true'
AfterStruct: 'true'
BeforeCatch: 'true'
BeforeElse: 'true'
BeforeWhile: 'true'
BreakInheritanceList: 'BeforeComma'
ColumnLimit: '100'
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
ConstructorInitializerIndentWidth: '4'
EmptyLineBeforeAccessModifier: 'Always'
FixNamespaceComments: 'true'
IncludeBlocks: Regroup
IndentCaseLabels: 'false'
IndentWidth: '4'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
Language: Cpp
PointerAlignment: Right
ReflowComments: 'true'
PenaltyBreakComment: '20'
SortIncludes: 'true'
SortUsingDeclarations: 'true'
SpaceAfterLogicalNot: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'true'
SpacesInSquareBrackets: 'false'
SpacesInCStyleCastParentheses: 'false'
Standard: Cpp11
TabWidth: '4'
UseTab: Never
Cpp11BracedListStyle: 'false'
MaxEmptyLinesToKeep: 2
14 changes: 7 additions & 7 deletions src/basis_factorization/BasisFactorizationError.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class BasisFactorizationError : public Error
{
public:
enum Code {
enum Code {
ALLOCATION_FAILED = 0,
CANT_INVERT_BASIS_BECAUSE_OF_ETAS = 1,
UNKNOWN_BASIS_FACTORIZATION_TYPE = 2,
Expand All @@ -32,13 +32,13 @@ class BasisFactorizationError : public Error
FEATURE_NOT_YET_SUPPORTED = 6,
};

BasisFactorizationError( BasisFactorizationError::Code code ) :
Error( "BasisFactorizationError", (int)code )
{
}
BasisFactorizationError( BasisFactorizationError::Code code )
: Error( "BasisFactorizationError", (int)code )
{
}

BasisFactorizationError( BasisFactorizationError::Code code, const char *userMessage ) :
Error( "BasisFactorizationError", (int)code, userMessage )
BasisFactorizationError( BasisFactorizationError::Code code, const char *userMessage )
: Error( "BasisFactorizationError", (int)code, userMessage )
{
}
};
Expand Down
10 changes: 6 additions & 4 deletions src/basis_factorization/BasisFactorizationFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
**/

#include "BasisFactorizationError.h"
#include "BasisFactorizationFactory.h"

#include "BasisFactorizationError.h"
#include "ForrestTomlinFactorization.h"
#include "GlobalConfiguration.h"
#include "LUFactorization.h"
#include "SparseFTFactorization.h"
#include "SparseLUFactorization.h"

IBasisFactorization *BasisFactorizationFactory::createBasisFactorization( unsigned basisSize, const IBasisFactorization::BasisColumnOracle &basisColumnOracle )
IBasisFactorization *BasisFactorizationFactory::createBasisFactorization(
unsigned basisSize,
const IBasisFactorization::BasisColumnOracle &basisColumnOracle )
{
// LU
if ( GlobalConfiguration::BASIS_FACTORIZATION_TYPE ==
GlobalConfiguration::LU_FACTORIZATION )
if ( GlobalConfiguration::BASIS_FACTORIZATION_TYPE == GlobalConfiguration::LU_FACTORIZATION )
return new LUFactorization( basisSize, basisColumnOracle );

// Sparse LU
Expand Down
4 changes: 3 additions & 1 deletion src/basis_factorization/BasisFactorizationFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
class BasisFactorizationFactory
{
public:
static IBasisFactorization *createBasisFactorization( unsigned basisSize, const IBasisFactorization::BasisColumnOracle &basisColumnOracle );
static IBasisFactorization *
createBasisFactorization( unsigned basisSize,
const IBasisFactorization::BasisColumnOracle &basisColumnOracle );
};

#endif // __BasisFactorizationFactory_h__
Expand Down
60 changes: 33 additions & 27 deletions src/basis_factorization/CSRMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
**/

#include "BasisFactorizationError.h"
#include "CSRMatrix.h"

#include "BasisFactorizationError.h"
#include "Debug.h"
#include "FloatUtils.h"
#include "MString.h"
Expand Down Expand Up @@ -61,13 +62,13 @@ void CSRMatrix::initialize( const double *M, unsigned m, unsigned n )
for ( unsigned j = 0; j < _n; ++j )
{
// Ignore zero entries
if ( FloatUtils::isZero( M[i*_n + j] ) )
if ( FloatUtils::isZero( M[i * _n + j] ) )
continue;

if ( _nnz >= _estimatedNnz )
increaseCapacity();

_A[_nnz] = M[i*_n + j];
_A[_nnz] = M[i * _n + j];
++_IA[i + 1];
_JA[_nnz] = j;

Expand All @@ -92,11 +93,13 @@ void CSRMatrix::initializeToEmpty( unsigned m, unsigned n )

_IA = new unsigned[_m + 1];
if ( !_IA )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "CSRMatrix::IA" );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"CSRMatrix::IA" );

_JA = new unsigned[_estimatedNnz];
if ( !_JA )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "CSRMatrix::JA" );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"CSRMatrix::JA" );

std::fill_n( _IA, _m + 1, 0.0 );
_nnz = 0;
Expand All @@ -109,14 +112,16 @@ void CSRMatrix::increaseCapacity()

double *newA = new double[newEstimatedNnz];
if ( !newA )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "CSRMatrix::newA" );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"CSRMatrix::newA" );

unsigned *newJA = new unsigned[newEstimatedNnz];
if ( !newJA )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "CSRMatrix::newJA" );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"CSRMatrix::newJA" );

memcpy( newA, _A, _estimatedNnz * sizeof(double) );
memcpy( newJA, _JA, _estimatedNnz * sizeof(unsigned) );
memcpy( newA, _A, _estimatedNnz * sizeof( double ) );
memcpy( newJA, _JA, _estimatedNnz * sizeof( unsigned ) );

delete[] _A;
delete[] _JA;
Expand All @@ -138,7 +143,7 @@ void CSRMatrix::addLastRow( const double *row )
{
// Array _IA needs to increase by one
unsigned *newIA = new unsigned[_m + 2];
memcpy( newIA, _IA, sizeof(unsigned) * ( _m + 1 ) );
memcpy( newIA, _IA, sizeof( unsigned ) * ( _m + 1 ) );
delete[] _IA;
_IA = newIA;

Expand Down Expand Up @@ -188,7 +193,7 @@ void CSRMatrix::addLastColumn( const double *column )
continue;

// Ignore all rows greater than i
while ( arrayIndex > _IA[i+1] - 1 )
while ( arrayIndex > _IA[i + 1] - 1 )
{
_A[arrayIndex + offset] = _A[arrayIndex];
_JA[arrayIndex + offset] = _JA[arrayIndex];
Expand All @@ -207,7 +212,7 @@ void CSRMatrix::addLastColumn( const double *column )
{
if ( !FloatUtils::isZero( column[i] ) )
++increase;
_IA[i+1] += increase;
_IA[i + 1] += increase;
}

++_n;
Expand Down Expand Up @@ -248,18 +253,21 @@ void CSRMatrix::storeIntoOther( SparseMatrix *other ) const

otherCsr->_A = new double[_estimatedNnz];
if ( !otherCsr->_A )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "CSRMatrix::otherCsrA" );
memcpy( otherCsr->_A, _A, sizeof(double) * _estimatedNnz );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"CSRMatrix::otherCsrA" );
memcpy( otherCsr->_A, _A, sizeof( double ) * _estimatedNnz );

otherCsr->_IA = new unsigned[_m + 1];
if ( !otherCsr->_IA )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "CSRMatrix::otherCsrIA" );
memcpy( otherCsr->_IA, _IA, sizeof(unsigned) * ( _m + 1 ) );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"CSRMatrix::otherCsrIA" );
memcpy( otherCsr->_IA, _IA, sizeof( unsigned ) * ( _m + 1 ) );

otherCsr->_JA = new unsigned[_estimatedNnz];
if ( !otherCsr->_JA )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "CSRMatrix::otherCsrJA" );
memcpy( otherCsr->_JA, _JA, sizeof(unsigned) * _estimatedNnz );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"CSRMatrix::otherCsrJA" );
memcpy( otherCsr->_JA, _JA, sizeof( unsigned ) * _estimatedNnz );
}

void CSRMatrix::getRow( unsigned row, SparseUnsortedList *result ) const
Expand Down Expand Up @@ -449,7 +457,7 @@ void CSRMatrix::insertElements( const Map<unsigned, Set<CommittedChange>> &inser
Elements of row i are stored in _A and _JA between
indices _IA[i] and _IA[i+1] - 1.
*/
int j = _IA[i+1] - 1;
int j = _IA[i + 1] - 1;
while ( j >= (int)_IA[i] )
{
if ( !rowHasInsertions || nextInsertion == insertions[i].rend() )
Expand Down Expand Up @@ -496,15 +504,14 @@ void CSRMatrix::insertElements( const Map<unsigned, Set<CommittedChange>> &inser
--newArrayIndex;
}
}

}

// Make a final pass to adjust the IA indices
unsigned totalAddedSoFar = 0;
for ( unsigned i = 0; i < _m; ++i )
{
totalAddedSoFar += insertions.exists( i ) ? insertions[i].size() : 0;
_IA[i+1] += totalAddedSoFar;
_IA[i + 1] += totalAddedSoFar;
}

_nnz += totalAddedSoFar;
Expand Down Expand Up @@ -633,7 +640,7 @@ void CSRMatrix::executeChanges()
void CSRMatrix::countElements( unsigned *numRowElements, unsigned *numColumnElements )
{
for ( unsigned i = 0; i < _m; ++i )
numRowElements[i] = _IA[i+1] - _IA[i];
numRowElements[i] = _IA[i + 1] - _IA[i];

std::fill_n( numColumnElements, _n, 0 );
for ( unsigned i = 0; i < _nnz; ++i )
Expand Down Expand Up @@ -706,7 +713,6 @@ void CSRMatrix::dump() const
for ( unsigned i = 0; i < _m + 1; ++i )
printf( "%5u ", _IA[i] );
printf( "\n" );

}

void CSRMatrix::dumpDense() const
Expand All @@ -718,7 +724,7 @@ void CSRMatrix::dumpDense() const
{
for ( unsigned j = 0; j < _n; ++j )
{
printf( "%5.2lf ", work[i*_n + j] );
printf( "%5.2lf ", work[i * _n + j] );
}
printf( "\n" );
}
Expand All @@ -745,11 +751,11 @@ void CSRMatrix::checkInvariants() const
for ( unsigned i = 0; i < _m; ++i )
{
unsigned start = _IA[i];
unsigned end = _IA[i+1];
unsigned end = _IA[i + 1];

for ( unsigned j = start; j + 1 < end; ++j )
{
if ( _JA[j] >= _JA[j+1] )
if ( _JA[j] >= _JA[j + 1] )
{
printf( "CSRMatrix error! _JA elements not increasing. "
"Dumping and terminating\n" );
Expand All @@ -764,7 +770,7 @@ void CSRMatrix::clear()
{
_nnz = 0;
for ( unsigned i = 0; i < _m; ++i )
_IA[i+1] = 0;
_IA[i + 1] = 0;
}

const double *CSRMatrix::getA() const
Expand Down
29 changes: 17 additions & 12 deletions src/basis_factorization/EtaMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
**/

#include "BasisFactorizationError.h"
#include "EtaMatrix.h"

#include "BasisFactorizationError.h"
#include "FloatUtils.h"

#include <cstdio>
Expand All @@ -28,9 +29,10 @@ EtaMatrix::EtaMatrix( unsigned m, unsigned index, const double *column )
{
_column = new double[_m];
if ( !_column )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "EtaMatrix::column" );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"EtaMatrix::column" );

memcpy( _column, column, sizeof(double) * _m );
memcpy( _column, column, sizeof( double ) * _m );
}

EtaMatrix::EtaMatrix( unsigned m, unsigned index )
Expand All @@ -40,7 +42,8 @@ EtaMatrix::EtaMatrix( unsigned m, unsigned index )
{
_column = new double[_m];
if ( !_column )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "EtaMatrix::column" );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"EtaMatrix::column" );

std::fill( _column, _column + _m, 0.0 );

Expand All @@ -54,9 +57,10 @@ EtaMatrix::EtaMatrix( const EtaMatrix &other )
{
_column = new double[_m];
if ( !_column )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "EtaMatrix::column" );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"EtaMatrix::column" );

memcpy( _column, other._column, sizeof(double) * _m );
memcpy( _column, other._column, sizeof( double ) * _m );
}

EtaMatrix &EtaMatrix::operator=( const EtaMatrix &other )
Expand All @@ -72,9 +76,10 @@ EtaMatrix &EtaMatrix::operator=( const EtaMatrix &other )

_column = new double[_m];
if ( !_column )
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED, "EtaMatrix::column" );
throw BasisFactorizationError( BasisFactorizationError::ALLOCATION_FAILED,
"EtaMatrix::column" );

memcpy( _column, other._column, sizeof(double) * _m );
memcpy( _column, other._column, sizeof( double ) * _m );

return *this;
}
Expand All @@ -101,11 +106,11 @@ void EtaMatrix::dump() const
void EtaMatrix::toMatrix( double *A ) const
{
std::fill_n( A, _m * _m, 0.0 );
for ( unsigned i = 0; i < _m; ++i )
for ( unsigned i = 0; i < _m; ++i )
{
A[i * _m + i] = 1.;
A[_columnIndex + i * _m] = _column[i];
}
A[i * _m + i] = 1.;
A[_columnIndex + i * _m] = _column[i];
}
}

bool EtaMatrix::operator==( const EtaMatrix &other ) const
Expand Down
Loading

0 comments on commit b030584

Please sign in to comment.