Created
August 6, 2013 06:06
-
-
Save Kaminate/6162413 to your computer and use it in GitHub Desktop.
2D dynamic array wrapper.
Allocates memory in the constructor,
deallocates memory in the destructor. Everything is inline, so it can be used as a header-only (library?).
This file contains 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
//////////////////// | |
// Nathan Park // | |
// August 3, 2013 // | |
//////////////////// | |
#ifndef __ARRAY2D_H_ | |
#define __ARRAY2D_H_ | |
#include <iostream> | |
#include <iomanip> | |
/*! | |
@brief | |
Array2D is a simple dynamic 2d array wrapper. | |
It has overloaded << operator for Array2D<int> | |
*/ | |
template<typename T> | |
class Array2D | |
{ | |
T ** mData; | |
public: | |
const unsigned mRows; | |
const unsigned mCols; | |
Array2D(unsigned rows, unsigned cols); | |
~Array2D(); | |
T * operator [](unsigned row); | |
const T * operator [](unsigned row) const; | |
T & operator ()(unsigned row, unsigned col); | |
const T& operator ()(unsigned row, unsigned col) const; | |
}; | |
// output operator | |
inline std::ostream & operator <<(std::ostream & os, const Array2D<int>& rhs); | |
inline std::ostream & operator <<(std::ostream & os, const Array2D<bool>& rhs); | |
// member function definitions | |
template <typename T> | |
inline Array2D<T>::Array2D(unsigned rows, unsigned cols) | |
: mRows(rows), mCols(cols) | |
{ | |
mData = new T* [rows]; | |
while(rows) | |
mData[--rows] = new T[cols]; | |
} | |
template <typename T> | |
inline Array2D<T>::~Array2D() | |
{ | |
for (unsigned row = 0; row < mRows; ++row) | |
delete [] mData[row]; | |
delete [] mData; | |
} | |
template <typename T> | |
inline T * Array2D<T>::operator [](unsigned row) | |
{ | |
return mData[row]; | |
} | |
template <typename T> | |
inline const T * Array2D<T>::operator [](unsigned row) const | |
{ | |
return mData[row]; | |
} | |
template <typename T> | |
inline T & Array2D<T>::operator ()(unsigned row, unsigned col) | |
{ | |
return mData[row][col]; | |
} | |
template <typename T> | |
inline const T& Array2D<T>::operator ()(unsigned row, unsigned col) const | |
{ | |
return mData[row][col]; | |
} | |
// output operator Array2D<int> | |
inline std::ostream & operator << (std::ostream & os, const Array2D<int>& rhs) | |
{ | |
for (unsigned r = 0; r < rhs.mRows; ++r) | |
{ | |
for (unsigned c = 0; c < rhs.mCols; ++c) | |
{ | |
os << std::setw(3) << rhs[r][c] << " "; | |
} | |
os << std::endl; | |
} | |
return os; | |
} | |
// output operator Array2D<bool> | |
inline std::ostream & operator << (std::ostream & os, const Array2D<bool>& rhs) | |
{ | |
for (unsigned r = 0; r < rhs.mRows; ++r) | |
{ | |
for (unsigned c = 0; c < rhs.mCols; ++c) | |
{ | |
os << rhs[r][c] ? "1" : "0"; | |
} | |
os << std::endl; | |
} | |
return os; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment