SundanceFixedArray.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #ifndef SUNDANCE_FIXEDARRAY_H
00043 #define SUNDANCE_FIXEDARRAY_H
00044 
00045 
00046 #include "SundanceDefs.hpp"
00047 #include <iostream>
00048 #include <algorithm>
00049 
00050 namespace Sundance
00051 {
00052 
00053 /**
00054  * A simple fixed-size array 
00055  */
00056 template <int N, class T> class FixedArray
00057 {
00058 public:
00059   /** */
00060   FixedArray(){;}
00061 
00062   /** */
00063   const T& operator[](int i) const 
00064     {
00065       TEUCHOS_TEST_FOR_EXCEPT(i<0);
00066       TEUCHOS_TEST_FOR_EXCEPT(i>=N);
00067       return data_[i];
00068     }
00069 
00070   /** */
00071   T& operator[](int i) 
00072     {
00073       TEUCHOS_TEST_FOR_EXCEPT(i<0);
00074       TEUCHOS_TEST_FOR_EXCEPT(i>=N);
00075       return data_[i];
00076     }
00077 
00078   /** */
00079   int size() const {return N;}
00080 
00081   /** */
00082   const T* begin() const {return &(data_[0]);}
00083 
00084   /** */
00085   const T* end() const {return begin()+N;}
00086 
00087   /** */
00088   T* begin() {return data_;}
00089   
00090   /** */
00091   T* end() {return data_+N;}
00092 
00093   /** */
00094   bool operator<(const FixedArray<N, T>& other) const
00095     {
00096       return std::lexicographical_compare(begin(), end(), 
00097         other.begin(), other.end());
00098     }
00099 
00100 private:
00101   T data_[N];
00102 };
00103 
00104 
00105 
00106 /** \relate FixedArray */
00107 template<class T>
00108 FixedArray<2,T> makeFA(const T& a, const T& b)
00109 {
00110   FixedArray<2,T> rtn;
00111   rtn[0] = a;
00112   rtn[1] = b;
00113   return rtn;
00114 }
00115 
00116 /** \relate FixedArray */
00117 template<class T>
00118 FixedArray<3,T> makeFA(const T& a, const T& b, const T& c)
00119 {
00120   FixedArray<3,T> rtn;
00121   rtn[0] = a;
00122   rtn[1] = b;
00123   rtn[2] = c;
00124   return rtn;
00125 }
00126 
00127 
00128 /** \relate FixedArray */
00129 template<class T>
00130 FixedArray<4,T> makeFA(const T& a, const T& b, const T& c, const T& d)
00131 {
00132   FixedArray<4,T> rtn;
00133   rtn[0] = a;
00134   rtn[1] = b;
00135   rtn[2] = c;
00136   rtn[2] = d;
00137   return rtn;
00138 }
00139 
00140 }
00141 
00142 
00143 namespace std
00144 {
00145 /** \relates FixedArray */
00146 template <int N, class T> 
00147 ostream& operator<<(std::ostream& os, const Sundance::FixedArray<N, T>& f)
00148 {
00149   os << "{";
00150   for (int i=0; i<N; i++) 
00151   {
00152     if (i>0) os << ", ";
00153     os << f[i];
00154   }
00155   os << "}";
00156   return os;
00157 }
00158 
00159 }
00160 
00161 #endif

Site Contact