/usr/include/trilinos/Teuchos_TwoDArray.hpp is in libtrilinos-teuchos-dev 12.12.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | // @HEADER
// ***********************************************************************
//
// Teuchos: Common Tools Package
// Copyright (2004) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef TEUCHOS_TWODARRAY_HPP
#define TEUCHOS_TWODARRAY_HPP
/*! \file Teuchos_TwoDArray.hpp
\brief A thin wrapper around the Teuchos Array class that allows for
2 dimensional arrays.
*/
#include "Teuchos_Array.hpp"
namespace Teuchos{
/**
* \brief A thin wrapper around the Array class which causes it to be interpreted
* as a 2D Array.
*
* 2D Array's can also be "symetric". This means that anyone viewing the
* Array should only consider the lower half of the array as valid. The
*
* \warning The TwoDArray will not enforce symetry. However, when two
* symmetrical TwoDArrays are compared, only the the lower half of the
* TwoDArray's will be compared.
*/
template<class T>
class TwoDArray{
public:
/**
* \brief .
*/
typedef Ordinal size_type;
/** \name Constructors and Destructors */
//@{
/**
* \brief Constructs a TwoDArray with the given number of rows and colums with each
* entry being populated with the specified value.
*
* @param numCols The number of columns in the TwoDArray.
* @param numRows The number of rows in the TwoDArray.
* @param value The value with which to populate the TwoDArray.
*/
TwoDArray(size_type numRows, size_type numCols, T value=T()):
_numRows(numRows),
_numCols(numCols),
_data(Array<T>(numCols*numRows, value)),
_symmetrical(false)
{}
/**
* \brief Constructs an empty TwoDArray.
*/
TwoDArray():
_numRows(0),_numCols(0),_data(Array<T>()),_symmetrical(false){}
/** \brief . */
virtual ~TwoDArray(){}
//@}
/** \name Getters and Setters */
//@{
/** \brief Returns an ArrayView containing the contents of row i */
inline ArrayView<T> operator[](size_type i);
/** \brief Returns a const ArrayView containing the contents of row i */
inline ArrayView<const T> operator[](size_type i) const;
/** \brief returns the number of rows in the TwoDArray. */
inline size_type getNumRows() const{
return _numRows;
}
/** \brief returns the number of columns in the TwoDArray. */
inline size_type getNumCols() const{
return _numCols;
}
/** \brief Returns the 1D array that is backing this TwoDArray. */
inline const Array<T>& getDataArray() const{
return _data;
}
/** \brief Returns the element located at i,j */
inline T& operator()(size_type i, size_type j){
return _data[(i*_numCols)+j];
}
/** \brief Returns the element located at i,j */
inline const T& operator()(size_type i, size_type j) const{
return _data[(i*_numCols)+j];
}
/** \brief delets all the entries from the TwoDArray */
inline void clear(){
_data.clear();
_numRows =0;
_numCols =0;
}
inline bool isEmpty(){
return _numRows == 0 &&
_numCols == 0 &&
_data.size() == 0;
}
/** \brief A simple flag indicating whether or not
* this TwoDArray should be interpurted as symmetrical.
*
* \note A symmetrical TwoDArray is defined as an TwoDArray where
* entry i,j is the same as entry j,i.
*
* \note This does not change any of the TwoDArrays behavior.
* It merely serves as an indicator to any one using a TwoDArray
* that the TwoDArray can be read as if it were symmetrical. In other words,
* the TwoDArray class does not enforce the symetry.
*
* @return True if the array is "symmetrical", false otherwise.
*/
inline bool isSymmetrical() const{
return _symmetrical;
}
/**
* \brief Sets whether or not the the TwoDArray should be
* interpurted as symetric.
*
* \note A symmetrical TwoDArray is defined as an TwoDArray where
* entry i,j is the same as entry j,i.
*
* \note This does not change any of the TwoDArrays behavior.
* It merely serves as an indicator to any one using a TwoDArray
* that the TwoDArray can be read as if it were symmetrical. In other words,
* the TwoDArray class does not enforce the symetry.
*
* @param symmetrical Whether or not the matrix should be interpurted
* as symetric.
*/
inline void setSymmetrical(bool symmetrical){
_symmetrical = symmetrical;
}
//@}
/** @name Resizing Functions */
//@{
/** \brief Changes the number of rows in the matrix.
*
* If the new number of rows is less than the current number, the
* last rows in the array will be deleted (i.e. if an array has 10 rows and
* it is resized to have only 5 rows, rows 5-9 are deleted). If the new number
* of rows is greater than the current number of rows, the rows are appended on
* to the end of the array and the new entries are initialized to T's
* default value.
*
* @param numberOfRows The new number of rows the TwoDArray should have.
*/
void resizeRows(size_type numberOfRows);
/** \brief Changes the number of rows in the matrix.
*
* If the new number of columns is less than the current number, the
* last columns in the array will be deleted (i.e. if an array has 10 columns and
* it is resized to have only 5 columns, columns 5-9 are deleted). If the new number
* of columns is greater than the current number of columns, the columns are appended on
* to the end of the array and the new entries are initialized to T's default value.
*
* \warning This operation has the potential to be very expensive
* as it essentially creates an entirely new 2DArray.
* Please take this into account when using this function.
*
* @param numberOfCols The new number of rows the TwoDArray should have.
*/
void resizeCols(size_type numberOfCols);
//@}
/** \name String conversion functions */
//@{
/** \brief returns the string used to seperate meta information from
* actual data information when converting a TwoDArray to a string.
*/
static const std::string& getMetaSeperator(){
static const std::string metaSeperator = ":";
return metaSeperator;
}
/** \brief returns the string used as the dimension dilimeter when convering
* the TwoDArray to a string.
*/
static const std::string& getDimensionsDelimiter(){
static const std::string dimensionsDelimiter = "x";
return dimensionsDelimiter;
}
/** \brief Converts a given TwoDArray to a valid string representation. */
static std::string toString(const TwoDArray<T> array);
/** \brief Converts a valid string to it's corresponding TwoDArray. */
static TwoDArray<T> fromString(const std::string& string);
//@}
private:
size_type _numRows,_numCols;
Array<T> _data;
TwoDArray(size_type numRows, size_type numCols, Array<T> data):
_numRows(numRows),
_numCols(numCols),
_data(data),
_symmetrical(false)
{}
bool _symmetrical;
};
template<class T> inline
ArrayView<T> TwoDArray<T>::operator[](size_type i){
return _data.view(_numCols*i, _numCols);
}
template<class T> inline
ArrayView<const T> TwoDArray<T>::operator[](size_type i) const{
return _data.view(_numCols*i, _numCols);
}
template<class T>
void TwoDArray<T>::resizeRows(size_type numberOfRows){
_data.resize(_numCols*numberOfRows);
_numRows = numberOfRows;
}
template<class T>
void TwoDArray<T>::resizeCols(size_type numberOfCols){
Array<T> newData(numberOfCols*_numRows);
size_type colLimit = (numberOfCols < _numCols ? numberOfCols : _numCols);
for(size_type i = 0; i<_numRows; i++){
for(size_type j = 0; j<colLimit; j++){
newData[i*numberOfCols+j] = _data[i*_numCols+j];
}
}
_data = newData;
_numCols=numberOfCols;
}
template<class T>
std::string TwoDArray<T>::toString(const TwoDArray<T> array){
std::stringstream numColsStream;
std::stringstream numRowsStream;
numColsStream << array.getNumCols();
numRowsStream << array.getNumRows();
std::string metaSeperator = TwoDArray<T>::getMetaSeperator();
return
numRowsStream.str() +
TwoDArray<T>::getDimensionsDelimiter() +
numColsStream.str() +
metaSeperator +
(array.isSymmetrical() ? "sym"+metaSeperator : "") +
array.getDataArray().toString();
}
template<class T>
TwoDArray<T> TwoDArray<T>::fromString(const std::string& string_in){
std::string curString = string_in;
std::string metaSeperator = TwoDArray<T>::getMetaSeperator();
size_t curPos = curString.find(metaSeperator);
std::string dimString = curString.substr(0, curPos);
curString = curString.substr(curPos+1);
//process dimensions
size_t dimCharPos =
dimString.find(TwoDArray<T>::getDimensionsDelimiter());
std::istringstream numRowsStream(dimString.substr(0,dimCharPos));
std::istringstream numColsStream(dimString.substr(dimCharPos+1));
size_t numRows, numCols;
numRowsStream >> numRows;
numColsStream >> numCols;
//determine symetry state
bool symmetrical = false;
curPos = curString.find(metaSeperator);
if(curPos != std::string::npos){
symmetrical = true;
curString = curString.substr(curPos+1);
}
//Get actual data
Array<T> array = fromStringToArray<T>(curString);
TEUCHOS_TEST_FOR_EXCEPTION(array.size() != (typename Array<T>::size_type)(numRows*numCols),
InvalidArrayStringRepresentation,
"Error: You've specified an TwoDArray as having the dimensions of "
<< numRows << "x" << numCols << ". This means you should have " <<
(numRows*numCols) << " entries specified in your array. However you "
"only specified " << array.size() << " entries."
)
//Construct object to return
TwoDArray<T> toReturn(numRows, numCols, array);
toReturn.setSymmetrical(symmetrical);
return toReturn;
}
/* \brief .
* \relates TwoDArray
*/
template<class T>
std::istringstream& operator>> (std::istringstream& in, TwoDArray<T>& array){
array = TwoDArray<T>::fromString(in.str());
return in;
}
/* \brief .
* \relates TwoDArray
*/
template<class T> inline
std::ostream& operator<<(std::ostream& os, const TwoDArray<T>& array){
return os << TwoDArray<T>::toString(array);
}
namespace TwoDDetails {
/**
* \brief A function for comparing symmetrical arrarys.
*
* @param a1 The first array to compare.
* @param a2 The second array to compare.
* @return True if the two TwoDArrays are symmetricaly the same, false
* otherwise.
*/
template<typename T>
bool symmetricCompare(const TwoDArray<T> &a1, const TwoDArray<T> &a2 ){
if(a1.getNumRows() != a2.getNumRows() ||
a1.getNumRows() != a2.getNumRows())
{
return false;
}
else{
typedef typename TwoDArray<T>::size_type ST;
for(ST i=0;i<a1.getNumRows(); ++i){
for(ST j=0;j<a1.getNumCols()-a1.getNumRows()+i; ++j){
if(a1(i,j) != a2(i,j)){
return false;
}
}
}
return true;
}
}
}
/* \brief Returns true of the two TwoDArrays have the same contents and
* their dimensions are the same.
*
* \note If the arrays are symmetrical, only the values in the upper half
* of the array are compared. For example: in a 4x4 array, only the values
* indicated with x's in the figure below would be compared.
*
* o o o o
* x o o o
* x x o o
* x x x o
*
* \relates TwoDArray
*/
template<typename T>
bool operator==( const TwoDArray<T> &a1, const TwoDArray<T> &a2 ){
if(a1.isSymmetrical() != a2.isSymmetrical()){
return false;
}
if(a1.isSymmetrical()){
return TwoDDetails::symmetricCompare(a1,a2);
}
else{
return a1.getDataArray() == a2.getDataArray() &&
a1.getNumRows() == a2.getNumRows() &&
a1.getNumCols() == a2.getNumCols();
}
}
/**
* \brief Get the format that is used for the specialization of the TypeName
* traits class for TwoDArray.
*
* The string returned will contain only one
* "*" character. The "*" character should then be replaced with the actual
* template type of the array.
* \relates TwoDArray.
*/
inline
std::string getTwoDArrayTypeNameTraitsFormat(){
return "TwoDArray(*)";
}
/** \brief TypeNameTraits specialization for Array. */
template<typename T>
class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits<TwoDArray<T> > {
public:
static std::string name(){
std::string formatString = getTwoDArrayTypeNameTraitsFormat();
size_t starPos = formatString.find("*");
std::string prefix = formatString.substr(0,starPos);
std::string postFix = formatString.substr(starPos+1);
return prefix+TypeNameTraits<T>::name()+postFix;
}
static std::string concreteName(const TwoDArray<T>&)
{ return name(); }
};
} //namespace Teuchos
#endif // TEUCHOS_TWODARRAY_H
|