Skip to content

Commit

Permalink
simplified matrix_alloc.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
jewettaij committed Feb 15, 2020
1 parent 59f54e5 commit 1942fa3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 43 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
*.so *.o
test_jacobi

# coveralls files
*.gcno *.gcda
# output files generated by various tests and profilers
massif.out.*
*.gcda
*.gcno
.coverage
coverage.info

Expand All @@ -12,3 +14,4 @@ venv

# text-editor temporary files:
*~

4 changes: 4 additions & 0 deletions include/jacobi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ SetSize(int n) {
Alloc(n);
}

// memory management:

template<typename Scalar,typename Vector,typename Matrix,typename ConstMatrix>
void Jacobi<Scalar, Vector, Matrix, ConstMatrix>::
Alloc(int n) {
Expand All @@ -498,6 +500,8 @@ Dealloc() {
Init();
}

// memory management: copy and move constructor, swap, and assignment operator:

template<typename Scalar,typename Vector,typename Matrix,typename ConstMatrix>
Jacobi<Scalar, Vector, Matrix, ConstMatrix>::
Jacobi(const Jacobi<Scalar, Vector, Matrix, ConstMatrix>& source)
Expand Down
60 changes: 19 additions & 41 deletions include/matrix_alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,39 @@

namespace matrix_alloc {

/// @brief
/// Allocate a 2-dimensional table row-major order.
/// In this version, the the size of the array specified by 2 integer arguments.
/// @brief Allocate a 2-dimensional table row-major order.
template<typename Entry>
void Alloc2D(int M, //!< size of the array (number of rows)
int N, //!< size of the array (number of columns)
Entry ***paaX); //!< pointer to 2-D multidimensional array
void Alloc2D(size_t nrows, //!< size of the array (number of rows)
size_t ncols, //!< size of the array (number of columns)
Entry ***paaX //!< pointer to a 2-D C-style array
);


/// @brief Allocate a 2-dimensional table. After allocation, the entries in
/// the table (*paaX)[i][j] exist for all 0<=i<size[0] and 0<=j<size[1].
template<typename Entry, typename Integer>
void Alloc2D(Integer const size[2], //!< size of the array in x,y directions
Entry ***paaX); //!< pointer to 2-D multidimensional array

/// @brief
/// This function is the corresponding way to deallocate arrays
/// that were created using Alloc2D()
/// @brief This function is the corresponding way to deallocate
/// arrays that were created using Alloc2D().
template<typename Entry>
void Dealloc2D(Entry ***paaX); //!< pointer to 2-D multidimensional array
void Dealloc2D(Entry ***paaX //!< pointer to 2-D multidimensional array
);


// --- IMPLEMENTATION ---

/// @brief
/// Allocate a 2-dimensional table row-major order.
/// In this version, the the size of the array specified by 2 integer arguments.
template<typename Entry, typename Integer>
void Alloc2D(Integer const size[2], //!< size of the array in x,y directions
Entry ***paaX) //!< pointer to 2-D multidimensional array
template<typename Entry>
void Alloc2D(size_t nrows, // size of the array (number of rows)
size_t ncols, // size of the array (number of columns)
Entry ***paaX) // pointer to a 2-D C-style array
{
assert(paaX);
// Allocate a conventional 2-dimensional
// pointer-to-a-pointer data structure.
*paaX = new Entry* [size[0]];
(*paaX)[0] = new Entry [size[0] * size[1]];
for(Integer iy=0; iy<size[0]; iy++)
(*paaX)[iy] = (*paaX)[0] + iy*size[1];
// The caller can access the contents of *paX using (*paaX)[i][j] notation.
}


template<typename Entry>
void Alloc2D(int nrows, //!< size of the array (outer)
int ncolumns, //!< size of the array (inner)
Entry ***paaX) //!< pointer to 2-D multidimensional array
{
int size[2];
size[0] = nrows;
size[1] = ncolumns;
Alloc2D<Entry, int>(size, paaX);
*paaX = new Entry* [nrows];
(*paaX)[0] = new Entry [nrows * ncols];
for(size_t iy=0; iy<nrows; iy++)
(*paaX)[iy] = (*paaX)[0] + iy*ncols;
// The caller can access the contents using (*paaX)[i][j] notation.
}

template<typename Entry>
void Dealloc2D(Entry ***paaX) //!< pointer to 2-D multidimensional array
void Dealloc2D(Entry ***paaX) // pointer to 2-D multidimensional array
{
if (paaX && *paaX) {
delete [] (*paaX)[0];
Expand Down

0 comments on commit 1942fa3

Please sign in to comment.