RTOp Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, Teuchos_Ordinal subNz_in,
00146     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
00147     const ArrayRCP<const Teuchos_Ordinal> &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_Ordinal globalOffset_in, Teuchos_Ordinal 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_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, Teuchos_Ordinal subNz_in,
00183     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
00184     const ArrayRCP<const Teuchos_Ordinal> &indices_in, ptrdiff_t indicesStride_in,
00185     ptrdiff_t localOffset_in, bool isSorted_in
00186     )
00187     {
00188       using Teuchos::as;
00189 #ifdef TEUCHOS_DEBUG
00190       TEUCHOS_ASSERT(globalOffset_in >= 0);
00191       TEUCHOS_ASSERT(subDim_in > 0);
00192       TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(subNz_in, 0, subDim_in+1);
00193       TEUCHOS_ASSERT_EQUALITY(values_in.lowerOffset(), 0);
00194       TEUCHOS_ASSERT(valuesStride_in != 0);
00195       TEUCHOS_ASSERT_EQUALITY(values_in.size(),
00196         subNz_in*as<Teuchos_Ordinal>(std::abs(as<int>(valuesStride_in))));
00197       if (!is_null(indices_in)) {
00198         TEUCHOS_ASSERT(indicesStride_in != 0);
00199         TEUCHOS_ASSERT_EQUALITY(indices_in.size(),
00200           subNz_in*as<Teuchos_Ordinal>(std::abs(as<int>(indicesStride_in))));
00201         // Note: localOffset can be +, -, or 0 so there is nothing to assert!
00202         if (isSorted_in) {
00203           for (int k = 0; k < subNz_in-1; ++k) {
00204             const Teuchos_Ordinal idx_k = indices_in[k*indicesStride_in];
00205             const Teuchos_Ordinal idx_kp1 = indices_in[(k+1)*indicesStride_in];
00206             TEUCHOS_TEST_FOR_EXCEPTION( !(idx_k < idx_kp1), std::out_of_range,
00207               "Error indices["<<k<<"]="<<idx_k<<" >= indices["<<k+1<<"]="<<idx_kp1
00208               <<"!" );
00209           }
00210         }
00211       }
00212 #endif
00213       globalOffset_ = globalOffset_in;
00214       subDim_ = subDim_in;
00215       subNz_ = subNz_in;
00216       values_ = values_in;
00217       valuesStride_ = valuesStride_in;
00218       indices_ = indices_in;
00219       indicesStride_ = indicesStride_in;
00220       localOffset_ = localOffset_in;
00221       isSorted_ = isSorted_in;
00222     }
00224   void initialize(
00225     Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in,
00226     const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
00227     )
00228     {
00229       initialize(globalOffset_in, subDim_in, subDim_in, values_in, valuesStride_in,
00230         Teuchos::null, 0, 0, true);
00231     }
00233   void uninitialize()
00234     {
00235       globalOffset_ = 0; subDim_ = 0; subNz_ = 0;
00236       values_ = Teuchos::null; valuesStride_ = 0; indices_ = Teuchos::null;
00237       indicesStride_ = 0; localOffset_ = 0; isSorted_ = false;
00238     }
00240   void setGlobalOffset(Teuchos_Ordinal globalOffset_in) { globalOffset_ = globalOffset_in; } 
00242   Teuchos_Ordinal globalOffset() const { return globalOffset_; } 
00244   Teuchos_Ordinal subDim() const { return subDim_; }
00247   Teuchos_Ordinal subNz() const { return subNz_; }
00250   const ArrayRCP<const Scalar> values() const { return values_; }
00252   ptrdiff_t valuesStride() const { return valuesStride_; }
00257   const ArrayRCP<const Teuchos_Ordinal> indices() const { return indices_; }
00259   ptrdiff_t indicesStride() const { return indicesStride_; }
00262   ptrdiff_t localOffset() const { return localOffset_; }
00265   bool isSorted() const { return isSorted_; }
00266 private:
00267   Teuchos_Ordinal globalOffset_;
00268   Teuchos_Ordinal subDim_;
00269   Teuchos_Ordinal subNz_;
00270   ArrayRCP<const Scalar> values_;
00271   ptrdiff_t valuesStride_;
00272   ArrayRCP<const Teuchos_Ordinal> indices_;
00273   ptrdiff_t indicesStride_;
00274   ptrdiff_t localOffset_;
00275   bool isSorted_;
00276 public:
00277 };
00278 
00279 
00280 } // namespace RTOpPack
00281 
00282 
00283 #endif // RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines