Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_TwoDArray.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_TWODARRAY_HPP
43 #define TEUCHOS_TWODARRAY_HPP
44 
45 
51 #include "Teuchos_Array.hpp"
52 
53 
54 namespace Teuchos{
55 
67 template<class T>
68 class TwoDArray{
69 public:
73  typedef Ordinal size_type;
74 
77 
86  TwoDArray(size_type numRows, size_type numCols, T value=T()):
87  _numRows(numRows),
88  _numCols(numCols),
89  _data(Array<T>(numCols*numRows, value)),
90  _symmetrical(false)
91  {}
96  _numRows(0),_numCols(0),_data(Array<T>()),_symmetrical(false){}
97 
99  virtual ~TwoDArray(){}
100 
102 
105 
108 
110  inline const ArrayView<T> operator[](size_type i) const;
111 
113  inline size_type getNumRows() const{
114  return _numRows;
115  }
116 
118  inline size_type getNumCols() const{
119  return _numCols;
120  }
121 
123  inline const Array<T>& getDataArray() const{
124  return _data;
125  }
126 
128  inline T& operator()(size_type i, size_type j){
129  return _data[(i*_numCols)+j];
130  }
131 
133  inline const T& operator()(size_type i, size_type j) const{
134  return _data[(i*_numCols)+j];
135  }
136 
138  inline void clear(){
139  _data.clear();
140  _numRows =0;
141  _numCols =0;
142  }
143 
144  inline bool isEmpty(){
145  return _numRows == 0 &&
146  _numCols == 0 &&
147  _data.size() == 0;
148  }
149 
163  inline bool isSymmetrical() const{
164  return _symmetrical;
165  }
166 
182  inline void setSymmetrical(bool symmetrical){
183  _symmetrical = symmetrical;
184  }
185 
187 
190 
202  void resizeRows(size_type numberOfRows);
203 
218  void resizeCols(size_type numberOfCols);
219 
221 
222 
225 
229  static const std::string& getMetaSeperator(){
230  static const std::string metaSeperator = ":";
231  return metaSeperator;
232  }
233 
237  static const std::string& getDimensionsDelimiter(){
238  static const std::string dimensionsDelimiter = "x";
239  return dimensionsDelimiter;
240  }
241 
243  static std::string toString(const TwoDArray<T> array);
244 
246  static TwoDArray<T> fromString(const std::string& string);
247 
249 
250 private:
251  size_type _numRows,_numCols;
252  Array<T> _data;
253  TwoDArray(size_type numRows, size_type numCols, Array<T> data):
254  _numRows(numRows),_numCols(numCols),_data(data){}
255  bool _symmetrical;
256 };
257 
258 template<class T> inline
260  return _data.view(_numCols*i, _numCols);
261 }
262 
263 template<class T> inline
265  return _data.view(_numCols*i, _numCols);
266 }
267 
268 template<class T>
270  _data.resize(_numCols*numberOfRows);
271  _numRows = numberOfRows;
272 }
273 
274 
275 template<class T>
277  Array<T> newData(numberOfCols*_numRows);
278  size_type colLimit = (numberOfCols < _numCols ? numberOfCols : _numCols);
279  for(size_type i = 0; i<_numRows; i++){
280  for(size_type j = 0; j<colLimit; j++){
281  newData[i*numberOfCols+j] = _data[i*_numCols+j];
282  }
283  }
284  _data = newData;
285  _numCols=numberOfCols;
286 }
287 
288 
289 template<class T>
290 std::string TwoDArray<T>::toString(const TwoDArray<T> array){
291  std::stringstream numColsStream;
292  std::stringstream numRowsStream;
293  numColsStream << array.getNumCols();
294  numRowsStream << array.getNumRows();
295  std::string metaSeperator = TwoDArray<T>::getMetaSeperator();
296  return
297  numRowsStream.str() +
299  numColsStream.str() +
300  metaSeperator +
301  (array.isSymmetrical() ? "sym"+metaSeperator : "") +
302  array.getDataArray().toString();
303 }
304 
305 template<class T>
306 TwoDArray<T> TwoDArray<T>::fromString(const std::string& string_in){
307  std::string curString = string_in;
308  std::string metaSeperator = TwoDArray<T>::getMetaSeperator();
309  size_t curPos = curString.find(metaSeperator);
310  std::string dimString = curString.substr(0, curPos);
311  curString = curString.substr(curPos+1);
312 
313  //process dimensions
314  size_t dimCharPos =
315  dimString.find(TwoDArray<T>::getDimensionsDelimiter());
316  std::istringstream numRowsStream(dimString.substr(0,dimCharPos));
317  std::istringstream numColsStream(dimString.substr(dimCharPos+1));
318  size_t numRows, numCols;
319  numRowsStream >> numRows;
320  numColsStream >> numCols;
321 
322  //determine symetry state
323  bool symmetrical = false;
324  curPos = curString.find(metaSeperator);
325  if(curPos != std::string::npos){
326  symmetrical = true;
327  curString = curString.substr(curPos+1);
328  }
329 
330  //Get actual data
331  Array<T> array = fromStringToArray<T>(curString);
332 
333  TEUCHOS_TEST_FOR_EXCEPTION(array.size() != (typename Array<T>::size_type)(numRows*numCols),
335  "Error: You've specified an TwoDArray as having the dimensions of "
336  << numRows << "x" << numCols << ". This means you should have " <<
337  (numRows*numCols) << " entries specified in your array. However you "
338  "only specified " << array.size() << " entries."
339  )
340 
341  //Construct object to return
342  TwoDArray<T> toReturn(numRows, numCols, array);
343  toReturn.setSymmetrical(symmetrical);
344  return toReturn;
345 }
346 
347 /* \brief .
348  * \relates TwoDArray
349  */
350 template<class T>
351 std::istringstream& operator>> (std::istringstream& in, TwoDArray<T>& array){
352  array = TwoDArray<T>::fromString(in.str());
353  return in;
354 }
355 
356 /* \brief .
357  * \relates TwoDArray
358  */
359 template<class T> inline
360 std::ostream& operator<<(std::ostream& os, const TwoDArray<T>& array){
361  return os << TwoDArray<T>::toString(array);
362 }
363 
364 
365 namespace TwoDDetails {
366 
375 template<typename T>
376 bool symmetricCompare(const TwoDArray<T> &a1, const TwoDArray<T> &a2 ){
377  if(a1.getNumRows() != a2.getNumRows() ||
378  a1.getNumRows() != a2.getNumRows())
379  {
380  return false;
381  }
382  else{
383  typedef typename TwoDArray<T>::size_type ST;
384  for(ST i=0;i<a1.getNumRows(); ++i){
385  for(ST j=0;j<a1.getNumCols()-a1.getNumRows()+i; ++j){
386  if(a1(i,j) != a2(i,j)){
387  return false;
388  }
389  }
390  }
391  return true;
392  }
393 }
394 
395 
396 }
397 
398 /* \brief Returns true of the two TwoDArrays have the same contents and
399  * their dimensions are the same.
400  *
401  * \note If the arrays are symmetrical, only the values in the upper half
402  * of the array are compared. For example: in a 4x4 array, only the values
403  * indicated with x's in the figure below would be compared.
404  *
405  * o o o o
406  * x o o o
407  * x x o o
408  * x x x o
409  *
410  * \relates TwoDArray
411  */
412 template<typename T>
413 bool operator==( const TwoDArray<T> &a1, const TwoDArray<T> &a2 ){
414  if(a1.isSymmetrical() != a2.isSymmetrical()){
415  return false;
416  }
417  if(a1.isSymmetrical()){
418  return TwoDDetails::symmetricCompare(a1,a2);
419  }
420  else{
421  return a1.getDataArray() == a2.getDataArray() &&
422  a1.getNumRows() == a2.getNumRows() &&
423  a1.getNumCols() == a2.getNumCols();
424  }
425 }
426 
436 inline
438  return "TwoDArray(*)";
439 }
440 
442 template<typename T>
443 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<TwoDArray<T> > {
444 public:
445  static std::string name(){
446  std::string formatString = getTwoDArrayTypeNameTraitsFormat();
447  size_t starPos = formatString.find("*");
448  std::string prefix = formatString.substr(0,starPos);
449  std::string postFix = formatString.substr(starPos+1);
450  return prefix+TypeNameTraits<T>::name()+postFix;
451  }
452  static std::string concreteName(const TwoDArray<T>&)
453  { return name(); }
454 };
455 
456 } //namespace Teuchos
457 
458 
459 #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 interpurted as a 2D Array.
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.
Nonowning array view.
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.
size_type size() const
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 colums with each entry being populated with ...
static TwoDArray< T > fromString(const std::string &string)
Converts a valid string to it&#39;s corresponding TwoDArray.
bool isSymmetrical() const
A simple flag indicating whether or not this TwoDArray should be interpurted as symmetrical.
T & operator()(size_type i, size_type j)
Returns the element located at i,j.
Memory-safe templated array class that encapsulates std::vector.