10 #ifndef TEUCHOS_TWODARRAY_HPP
11 #define TEUCHOS_TWODARRAY_HPP
57 _data(
Array<T>(numCols*numRows, value)),
64 _numRows(0),_numCols(0),_data(
Array<T>()),_symmetrical(false){}
97 return _data[(i*_numCols)+j];
102 return _data[(i*_numCols)+j];
112 inline bool isEmpty(){
113 return _numRows == 0 &&
151 _symmetrical = symmetrical;
198 static const std::string metaSeperator =
":";
199 return metaSeperator;
206 static const std::string dimensionsDelimiter =
"x";
207 return dimensionsDelimiter;
231 template<
class T>
inline
233 return _data.view(_numCols*i, _numCols);
236 template<
class T>
inline
238 return _data.view(_numCols*i, _numCols);
243 _data.resize(_numCols*numberOfRows);
244 _numRows = numberOfRows;
250 Array<T> newData(numberOfCols*_numRows);
251 size_type colLimit = (numberOfCols < _numCols ? numberOfCols : _numCols);
254 newData[i*numberOfCols+j] = _data[i*_numCols+j];
258 _numCols=numberOfCols;
264 std::stringstream numColsStream;
265 std::stringstream numRowsStream;
270 numRowsStream.str() +
272 numColsStream.str() +
280 std::string curString = string_in;
282 size_t curPos = curString.find(metaSeperator);
283 std::string dimString = curString.substr(0, curPos);
284 curString = curString.substr(curPos+1);
289 std::istringstream numRowsStream(dimString.substr(0,dimCharPos));
290 std::istringstream numColsStream(dimString.substr(dimCharPos+1));
291 size_t numRows, numCols;
292 numRowsStream >> numRows;
293 numColsStream >> numCols;
296 bool symmetrical =
false;
297 curPos = curString.find(metaSeperator);
298 if(curPos != std::string::npos){
300 curString = curString.substr(curPos+1);
304 Array<T> array = fromStringToArray<T>(curString);
308 "Error: You've specified an TwoDArray as having the dimensions of "
309 << numRows <<
"x" << numCols <<
". This means you should have " <<
310 (numRows*numCols) <<
" entries specified in your array. However you "
311 "only specified " << array.
size() <<
" entries."
316 toReturn.setSymmetrical(symmetrical);
324 std::istringstream& operator>> (std::istringstream& in,
TwoDArray<T>& array){
332 template<
class T>
inline
333 std::ostream& operator<<(std::ostream& os, const TwoDArray<T>& array){
334 return os << TwoDArray<T>::toString(array);
338 namespace TwoDDetails {
349 bool symmetricCompare(
const TwoDArray<T> &a1,
const TwoDArray<T> &a2 ){
350 if(a1.getNumRows() != a2.getNumRows() ||
351 a1.getNumRows() != a2.getNumRows())
356 typedef typename TwoDArray<T>::size_type ST;
357 for(ST i=0;i<a1.getNumRows(); ++i){
358 for(ST j=0;j<a1.getNumCols()-a1.getNumRows()+i; ++j){
359 if(a1(i,j) != a2(i,j)){
386 bool operator==(
const TwoDArray<T> &a1,
const TwoDArray<T> &a2 ){
387 if(a1.isSymmetrical() != a2.isSymmetrical()){
390 if(a1.isSymmetrical()){
391 return TwoDDetails::symmetricCompare(a1,a2);
394 return a1.getDataArray() == a2.getDataArray() &&
395 a1.getNumRows() == a2.getNumRows() &&
396 a1.getNumCols() == a2.getNumCols();
411 return "TwoDArray(*)";
418 static std::string name(){
419 std::string formatString = getTwoDArrayTypeNameTraitsFormat();
420 size_t starPos = formatString.find(
"*");
421 std::string prefix = formatString.substr(0,starPos);
422 std::string postFix = formatString.substr(starPos+1);
432 #endif // TEUCHOS_TWODARRAY_H
std::string getTwoDArrayTypeNameTraitsFormat()
Get the format that is used for the specialization of the TypeName traits class for TwoDArray...
const Array< T > & getDataArray() const
Returns the 1D array that is backing this TwoDArray.
void setSymmetrical(bool symmetrical)
Sets whether or not the the TwoDArray should be interpurted as symetric.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void clear()
delets all the entries from the TwoDArray
static std::string toString(const TwoDArray< T > array)
Converts a given TwoDArray to a valid string representation.
A thin wrapper around the Array class which causes it to be interpreted as a 2D Array.
Ordinal size_type
The type of Array sizes and capacities.
size_type getNumCols() const
returns the number of columns in the TwoDArray.
void resizeCols(size_type numberOfCols)
Changes the number of rows in the matrix.
void resizeRows(size_type numberOfRows)
Changes the number of rows in the matrix.
size_type getNumRows() const
returns the number of rows in the TwoDArray.
Templated array class derived from the STL std::vector.
Default traits class that just returns typeid(T).name().
static const std::string & getMetaSeperator()
returns the string used to seperate meta information from actual data information when converting a T...
static const std::string & getDimensionsDelimiter()
returns the string used as the dimension dilimeter when convering the TwoDArray to a string...
ArrayView< T > operator[](size_type i)
Returns an ArrayView containing the contents of row i.
TwoDArray()
Constructs an empty TwoDArray.
const T & operator()(size_type i, size_type j) const
Returns the element located at i,j.
TwoDArray(size_type numRows, size_type numCols, T value=T())
Constructs a TwoDArray with the given number of rows and columns with each entry being populated with...
Partial specialization of ArrayView for const T.
static TwoDArray< T > fromString(const std::string &string)
Converts a valid string to it's corresponding TwoDArray.
bool isSymmetrical() const
A simple flag indicating whether or not this TwoDArray should be interpurted as symmetrical.
static std::string name()
T & operator()(size_type i, size_type j)
Returns the element located at i,j.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...