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 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 
00043 
00044 #ifndef RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
00045 #define RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
00046 
00047 
00048 #include "RTOpPack_Types.hpp"
00049 #include "Teuchos_Assert.hpp"
00050 
00051 
00052 namespace RTOpPack {
00053 
00054 
00134 template<class Scalar>
00135 class SparseSubVectorT {
00136 public:
00138   SparseSubVectorT()
00139     :globalOffset_(0), subDim_(0), subNz_(0),
00140      valuesStride_(0), indicesStride_(0), localOffset_(0),
00141      isSorted_(false)
00142     {}
00144   SparseSubVectorT(
00145     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in, Teuchos_Index subNz_in,
00146     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
00147     const ArrayRCP<const Teuchos_Index> &indices_in, ptrdiff_t indicesStride_in,
00148     ptrdiff_t localOffset_in, bool isSorted_in
00149     )
00150     :globalOffset_(globalOffset_in), subDim_(subDim_in), subNz_(subNz_in),
00151      values_(values_in), valuesStride_(valuesStride_in), indices_(indices_in),
00152      indicesStride_(indicesStride_in), localOffset_(localOffset_in), isSorted_(isSorted_in)
00153     {
00154 #ifdef TEUCHOS_DEBUG
00155       // Call initialize(...) just to check the preconditions
00156       initialize(globalOffset_in, subDim_in, subNz_in, values_in, valuesStride_in,
00157         indices_in, indicesStride_in, localOffset_in, isSorted_in);
00158 #endif
00159     }
00161   SparseSubVectorT(
00162     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in,
00163     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
00164     )
00165     :globalOffset_(globalOffset_in), subDim_(subDim_in), subNz_(subDim_in),
00166      values_(values_in), valuesStride_(valuesStride_in),
00167      indicesStride_(0), localOffset_(0), isSorted_(true)
00168     {
00169 #ifdef TEUCHOS_DEBUG
00170       // Call initialize(...) just to check the preconditions
00171       initialize(globalOffset_in, subDim_in, values_in, valuesStride_in);
00172 #endif
00173     }
00175   SparseSubVectorT( const ConstSubVectorView<Scalar>& sv )
00176     :globalOffset_(sv.globalOffset()), subDim_(sv.subDim()), subNz_(sv.subDim()),
00177      values_(sv.values()),  valuesStride_(sv.stride()), indicesStride_(0),
00178      localOffset_(0), isSorted_(true)
00179     {}
00181   void initialize(
00182     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in, Teuchos_Index subNz_in,
00183     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
00184     const ArrayRCP<const Teuchos_Index> &indices_in, ptrdiff_t indicesStride_in,
00185     ptrdiff_t localOffset_in, bool isSorted_in
00186     )
00187     {
00188 #ifdef TEUCHOS_DEBUG
00189       TEUCHOS_ASSERT(globalOffset_in >= 0);
00190       TEUCHOS_ASSERT(subDim_in > 0);
00191       TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(subNz_in, 0, subDim_in+1);
00192       TEUCHOS_ASSERT_EQUALITY(values_in.lowerOffset(), 0);
00193       TEUCHOS_ASSERT(valuesStride_in != 0);
00194       TEUCHOS_ASSERT_EQUALITY(values_in.size(), subNz_in*std::abs(valuesStride_in));
00195       if (!is_null(indices_in)) {
00196         TEUCHOS_ASSERT(indicesStride_in != 0);
00197         TEUCHOS_ASSERT_EQUALITY(indices_in.size(), subNz_in*std::abs(indicesStride_in));
00198         // Note: localOffset can be +, -, or 0 so there is nothing to assert!
00199         if (isSorted_in) {
00200           for (int k = 0; k < subNz_in-1; ++k) {
00201             const Teuchos_Index idx_k = indices_in[k*indicesStride_in];
00202             const Teuchos_Index idx_kp1 = indices_in[(k+1)*indicesStride_in];
00203             TEST_FOR_EXCEPTION( !(idx_k < idx_kp1), std::out_of_range,
00204               "Error indices["<<k<<"]="<<idx_k<<" >= indices["<<k+1<<"]="<<idx_kp1
00205               <<"!" );
00206           }
00207         }
00208       }
00209 #endif
00210       globalOffset_ = globalOffset_in;
00211       subDim_ = subDim_in;
00212       subNz_ = subNz_in;
00213       values_ = values_in;
00214       valuesStride_ = valuesStride_in;
00215       indices_ = indices_in;
00216       indicesStride_ = indicesStride_in;
00217       localOffset_ = localOffset_in;
00218       isSorted_ = isSorted_in;
00219     }
00221   void initialize(
00222     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in,
00223     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
00224     )
00225     {
00226       initialize(globalOffset_in, subDim_in, subDim_in, values_in, valuesStride_in,
00227         Teuchos::null, 0, 0, true);
00228     }
00230   void uninitialize()
00231     {
00232       globalOffset_ = 0; subDim_ = 0; subNz_ = 0;
00233       values_ = Teuchos::null; valuesStride_ = 0; indices_ = Teuchos::null;
00234       indicesStride_ = 0; localOffset_ = 0; isSorted_ = false;
00235     }
00237   void setGlobalOffset(Teuchos_Index globalOffset_in) { globalOffset_ = globalOffset_in; } 
00239   Teuchos_Index globalOffset() const { return globalOffset_; } 
00241   Teuchos_Index subDim() const { return subDim_; }
00244   Teuchos_Index subNz() const { return subNz_; }
00247   const ArrayRCP<const Scalar> values() const { return values_; }
00249   ptrdiff_t valuesStride() const { return valuesStride_; }
00254   const ArrayRCP<const Teuchos_Index> indices() const { return indices_; }
00256   ptrdiff_t indicesStride() const { return indicesStride_; }
00259   ptrdiff_t localOffset() const { return localOffset_; }
00262   bool isSorted() const { return isSorted_; }
00263 private:
00264   Teuchos_Index globalOffset_;
00265   Teuchos_Index subDim_;
00266   Teuchos_Index subNz_;
00267   ArrayRCP<const Scalar> values_;
00268   ptrdiff_t valuesStride_;
00269   ArrayRCP<const Teuchos_Index> indices_;
00270   ptrdiff_t indicesStride_;
00271   ptrdiff_t localOffset_;
00272   bool isSorted_;
00273 public:
00275   RTOP_DEPRECATED SparseSubVectorT(
00276     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in, Teuchos_Index subNz_in,
00277     const Scalar values_in[], ptrdiff_t valuesStride_in,
00278     const Teuchos_Index indices_in[], ptrdiff_t indicesStride_in,
00279     ptrdiff_t localOffset_in, int isSorted_in
00280     )
00281     {
00282       initialize(globalOffset, subDim, subNz, values, valuesStride,
00283         indices, indicesStride, localOffset, isSorted);
00284     }
00286   RTOP_DEPRECATED SparseSubVectorT(
00287     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in,
00288     const Scalar values_in[], ptrdiff_t valuesStride_in
00289     )
00290     {
00291       initialize(globalOffset_in, subDim_in, values_in, valuesStride_in);
00292     }
00294   RTOP_DEPRECATED void initialize(
00295     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in, Teuchos_Index subNz_in,
00296     const Scalar values_in[], ptrdiff_t valuesStride_in,
00297     const Teuchos_Index indices_in[], ptrdiff_t indicesStride_in,
00298     ptrdiff_t localOffset_in, int isSorted_in
00299     )
00300     {
00301       initialize(globalOffset_in, subDim_in, subNz_in,
00302         Teuchos::arcp(values_in, 0, subNz_in*std::abs(valuesStride_in), false), valuesStride_in,
00303         Teuchos::arcp(indices_in, 0, subNz_in*std::abs(indicesStride_in), false), indicesStride_in,
00304         localOffset_in, isSorted_in);
00305     }
00307   RTOP_DEPRECATED void initialize(
00308     Teuchos_Index globalOffset_in, Teuchos_Index subDim_in,
00309     const Scalar values_in[], ptrdiff_t valuesStride_in
00310     )
00311     {
00312       initialize(globalOffset_in, subDim_in,
00313         Teuchos::arcp(values_in, 0, subDim_in*std::abs(valuesStride_in), false),
00314         valuesStride_in);
00315     }
00317   RTOP_DEPRECATED void set_uninitialized()
00318     { uninitialize(); }
00319 };
00320 
00321 
00322 } // namespace RTOpPack
00323 
00324 
00325 #endif // RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines