RTOp Package Browser (Single Doxygen Collection) Version of the Day
RTOpPack_SparseSubVectorT.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
00005 //       Operations
00006 //                Copyright (2006) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //  
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 
00030 
00031 #ifndef RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
00032 #define RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
00033 
00034 
00035 #include "RTOpPack_Types.hpp"
00036 #include "Teuchos_Assert.hpp"
00037 
00038 
00039 namespace RTOpPack {
00040 
00041 
00121 template<class Scalar>
00122 class SparseSubVectorT {
00123 public:
00125   SparseSubVectorT()
00126     :globalOffset_(0), subDim_(0), subNz_(0),
00127      valuesStride_(0), indicesStride_(0), localOffset_(0),
00128      isSorted_(false)
00129     {}
00131   SparseSubVectorT(
00132     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in, Teuchos_Index subNz_in,
00133     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
00134     const ArrayRCP<const Teuchos_Index> &indices_in, ptrdiff_t indicesStride_in,
00135     ptrdiff_t localOffset_in, bool isSorted_in
00136     )
00137     :globalOffset_(globalOffset_in), subDim_(subDim_in), subNz_(subNz_in),
00138      values_(values_in), valuesStride_(valuesStride_in), indices_(indices_in),
00139      indicesStride_(indicesStride_in), localOffset_(localOffset_in), isSorted_(isSorted_in)
00140     {
00141 #ifdef TEUCHOS_DEBUG
00142       // Call initialize(...) just to check the preconditions
00143       initialize(globalOffset_in, subDim_in, subNz_in, values_in, valuesStride_in,
00144         indices_in, indicesStride_in, localOffset_in, isSorted_in);
00145 #endif
00146     }
00148   SparseSubVectorT(
00149     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in,
00150     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
00151     )
00152     :globalOffset_(globalOffset_in), subDim_(subDim_in), subNz_(subDim_in),
00153      values_(values_in), valuesStride_(valuesStride_in),
00154      indicesStride_(0), localOffset_(0), isSorted_(true)
00155     {
00156 #ifdef TEUCHOS_DEBUG
00157       // Call initialize(...) just to check the preconditions
00158       initialize(globalOffset_in, subDim_in, values_in, valuesStride_in);
00159 #endif
00160     }
00162   SparseSubVectorT( const ConstSubVectorView<Scalar>& sv )
00163     :globalOffset_(sv.globalOffset()), subDim_(sv.subDim()), subNz_(sv.subDim()),
00164      values_(sv.values()),  valuesStride_(sv.stride()), indicesStride_(0),
00165      localOffset_(0), isSorted_(true)
00166     {}
00168   void initialize(
00169     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in, Teuchos_Index subNz_in,
00170     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
00171     const ArrayRCP<const Teuchos_Index> &indices_in, ptrdiff_t indicesStride_in,
00172     ptrdiff_t localOffset_in, bool isSorted_in
00173     )
00174     {
00175 #ifdef TEUCHOS_DEBUG
00176       TEUCHOS_ASSERT(globalOffset_in >= 0);
00177       TEUCHOS_ASSERT(subDim_in > 0);
00178       TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(subNz_in, 0, subDim_in+1);
00179       TEUCHOS_ASSERT_EQUALITY(values_in.lowerOffset(), 0);
00180       TEUCHOS_ASSERT(valuesStride_in != 0);
00181       TEUCHOS_ASSERT_EQUALITY(values_in.size(), subNz_in*std::abs(valuesStride_in));
00182       if (!is_null(indices_in)) {
00183         TEUCHOS_ASSERT(indicesStride_in != 0);
00184         TEUCHOS_ASSERT_EQUALITY(indices_in.size(), subNz_in*std::abs(indicesStride_in));
00185         // Note: localOffset can be +, -, or 0 so there is nothing to assert!
00186         if (isSorted_in) {
00187           for (int k = 0; k < subNz_in-1; ++k) {
00188             const Teuchos_Index idx_k = indices_in[k*indicesStride_in];
00189             const Teuchos_Index idx_kp1 = indices_in[(k+1)*indicesStride_in];
00190             TEST_FOR_EXCEPTION( !(idx_k < idx_kp1), std::out_of_range,
00191               "Error indices["<<k<<"]="<<idx_k<<" >= indices["<<k+1<<"]="<<idx_kp1
00192               <<"!" );
00193           }
00194         }
00195       }
00196 #endif
00197       globalOffset_ = globalOffset_in;
00198       subDim_ = subDim_in;
00199       subNz_ = subNz_in;
00200       values_ = values_in;
00201       valuesStride_ = valuesStride_in;
00202       indices_ = indices_in;
00203       indicesStride_ = indicesStride_in;
00204       localOffset_ = localOffset_in;
00205       isSorted_ = isSorted_in;
00206     }
00208   void initialize(
00209     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in,
00210     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
00211     )
00212     {
00213       initialize(globalOffset_in, subDim_in, subDim_in, values_in, valuesStride_in,
00214         Teuchos::null, 0, 0, true);
00215     }
00217   void uninitialize()
00218     {
00219       globalOffset_ = 0; subDim_ = 0; subNz_ = 0;
00220       values_ = Teuchos::null; valuesStride_ = 0; indices_ = Teuchos::null;
00221       indicesStride_ = 0; localOffset_ = 0; isSorted_ = false;
00222     }
00224   void setGlobalOffset(Teuchos_Index globalOffset_in) { globalOffset_ = globalOffset_in; } 
00226   Teuchos_Index globalOffset() const { return globalOffset_; } 
00228   Teuchos_Index subDim() const { return subDim_; }
00231   Teuchos_Index subNz() const { return subNz_; }
00234   const ArrayRCP<const Scalar> values() const { return values_; }
00236   ptrdiff_t valuesStride() const { return valuesStride_; }
00241   const ArrayRCP<const Teuchos_Index> indices() const { return indices_; }
00243   ptrdiff_t indicesStride() const { return indicesStride_; }
00246   ptrdiff_t localOffset() const { return localOffset_; }
00249   bool isSorted() const { return isSorted_; }
00250 private:
00251   Teuchos_Index globalOffset_;
00252   Teuchos_Index subDim_;
00253   Teuchos_Index subNz_;
00254   ArrayRCP<const Scalar> values_;
00255   ptrdiff_t valuesStride_;
00256   ArrayRCP<const Teuchos_Index> indices_;
00257   ptrdiff_t indicesStride_;
00258   ptrdiff_t localOffset_;
00259   bool isSorted_;
00260 public:
00262   RTOP_DEPRECATED SparseSubVectorT(
00263     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in, Teuchos_Index subNz_in,
00264     const Scalar values_in[], ptrdiff_t valuesStride_in,
00265     const Teuchos_Index indices_in[], ptrdiff_t indicesStride_in,
00266     ptrdiff_t localOffset_in, int isSorted_in
00267     )
00268     {
00269       initialize(globalOffset, subDim, subNz, values, valuesStride,
00270         indices, indicesStride, localOffset, isSorted);
00271     }
00273   RTOP_DEPRECATED SparseSubVectorT(
00274     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in,
00275     const Scalar values_in[], ptrdiff_t valuesStride_in
00276     )
00277     {
00278       initialize(globalOffset_in, subDim_in, values_in, valuesStride_in);
00279     }
00281   RTOP_DEPRECATED void initialize(
00282     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in, Teuchos_Index subNz_in,
00283     const Scalar values_in[], ptrdiff_t valuesStride_in,
00284     const Teuchos_Index indices_in[], ptrdiff_t indicesStride_in,
00285     ptrdiff_t localOffset_in, int isSorted_in
00286     )
00287     {
00288       initialize(globalOffset_in, subDim_in, subNz_in,
00289         Teuchos::arcp(values_in, 0, subNz_in*std::abs(valuesStride_in), false), valuesStride_in,
00290         Teuchos::arcp(indices_in, 0, subNz_in*std::abs(indicesStride_in), false), indicesStride_in,
00291         localOffset_in, isSorted_in);
00292     }
00294   RTOP_DEPRECATED void initialize(
00295     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in,
00296     const Scalar values_in[], ptrdiff_t valuesStride_in
00297     )
00298     {
00299       initialize(globalOffset_in, subDim_in,
00300         Teuchos::arcp(values_in, 0, subDim_in*std::abs(valuesStride_in), false),
00301         valuesStride_in);
00302     }
00304   RTOP_DEPRECATED void set_uninitialized()
00305     { uninitialize(); }
00306 };
00307 
00308 
00309 } // namespace RTOpPack
00310 
00311 
00312 #endif // RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines