SundanceArrayOfTuples.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
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 Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
00030 
00031 #ifndef SUNDANCE_ARRAYOFTUPLES_H
00032 #define SUNDANCE_ARRAYOFTUPLES_H
00033 
00034 #include "Teuchos_Array.hpp"
00035 
00036 namespace Sundance
00037 {
00038   using namespace Teuchos;
00039 
00040   /** 
00041    * Class ArrayOfTuples packs an heterogeneous array of tuples into 
00042    * a single 1D array. 
00043    */
00044   template<class T>
00045     class ArrayOfTuples
00046     {
00047     public:
00048       /** Empty ctor */
00049       ArrayOfTuples();
00050 
00051       /** Constructor specifying the size of each tuple, but not the 
00052        * number of tuples */
00053       ArrayOfTuples(int tupleSize);
00054 
00055       /** Constructor specifying both the size and number of the tuples */
00056       ArrayOfTuples(int numTuples, int tupleSize);
00057 
00058       /** Returns the number of tuples */
00059       int length() const {return numTuples_;}
00060 
00061       /** Returns the size of the tuples */
00062       int tupleSize() const {return tupleSize_;}
00063 
00064       /** Change the number of tuples */
00065       void resize(int newSize) {
00066         numTuples_ = newSize;
00067         data_.resize(numTuples_*tupleSize_);
00068       }
00069 
00070       /** Change the number and size of the tuples */
00071       void resize(int newSize, int newTupleSize) 
00072         {
00073         numTuples_ = newSize;
00074         tupleSize_ = newTupleSize;
00075         data_.resize(numTuples_*tupleSize_);
00076         }
00077 
00078       /** Reserve memory for a number of tuples */
00079       void reserve(int newSize) {data_.reserve(newSize*tupleSize_);}
00080 
00081       /** Specify the size of the tuples */
00082       void setTupleSize(int tupleSize) {tupleSize_ = tupleSize;}
00083 
00084       /** Get the j-th entry in the i-th tuple */
00085       const T& value(int i, int j) const {return data_[i*tupleSize_+j];}
00086 
00087       /** Get the j-th entry in the i-th tuple */
00088       T& value(int i, int j) {return data_[i*tupleSize_+j];}
00089 
00090       /** Append a new tuple to the array */
00091       void append(const Array<T>& x);
00092 
00093       /** Append a new tuple to the array */
00094       void append(const T* x, int n);
00095 
00096     private:
00097 
00098       int numTuples_;
00099       int tupleSize_;
00100 
00101       Array<T> data_;
00102 
00103     };
00104 
00105   template<class T> inline ArrayOfTuples<T>::ArrayOfTuples()
00106     : numTuples_(0), tupleSize_(0), data_()
00107     {;}
00108 
00109   template<class T> inline ArrayOfTuples<T>::ArrayOfTuples(int tupleSize)
00110     : numTuples_(0), tupleSize_(tupleSize), data_()
00111     {;}
00112 
00113   template<class T> inline ArrayOfTuples<T>::ArrayOfTuples(int numTuples, int tupleSize)
00114     : numTuples_(numTuples), tupleSize_(tupleSize), data_(numTuples*tupleSize)
00115     {;}
00116 
00117   template<class T> inline void ArrayOfTuples<T>::append(const Array<T>& x)
00118     {
00119       for (int i=0; i<x.length(); i++)
00120         {
00121           data_.append(x[i]);
00122         }
00123       numTuples_++;
00124     }
00125 
00126   template<class T> inline void ArrayOfTuples<T>::append(const T* x, int n)
00127     {
00128       for (int i=0; i<n; i++)
00129         {
00130           data_.append(x[i]);
00131         }
00132       numTuples_++;
00133     }
00134 
00135 
00136 }
00137 
00138 #endif

Site Contact