SundanceFixedArray.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_FIXEDARRAY_H
00032 #define SUNDANCE_FIXEDARRAY_H
00033 
00034 
00035 #include "SundanceDefs.hpp"
00036 #include <iostream>
00037 #include <algorithm>
00038 
00039 namespace Sundance
00040 {
00041 
00042 /**
00043  * A simple fixed-size array 
00044  */
00045 template <int N, class T> class FixedArray
00046 {
00047 public:
00048   /** */
00049   FixedArray(){;}
00050 
00051   /** */
00052   const T& operator[](int i) const 
00053     {
00054       TEUCHOS_TEST_FOR_EXCEPT(i<0);
00055       TEUCHOS_TEST_FOR_EXCEPT(i>=N);
00056       return data_[i];
00057     }
00058 
00059   /** */
00060   T& operator[](int i) 
00061     {
00062       TEUCHOS_TEST_FOR_EXCEPT(i<0);
00063       TEUCHOS_TEST_FOR_EXCEPT(i>=N);
00064       return data_[i];
00065     }
00066 
00067   /** */
00068   int size() const {return N;}
00069 
00070   /** */
00071   const T* begin() const {return &(data_[0]);}
00072 
00073   /** */
00074   const T* end() const {return begin()+N;}
00075 
00076   /** */
00077   T* begin() {return data_;}
00078   
00079   /** */
00080   T* end() {return data_+N;}
00081 
00082   /** */
00083   bool operator<(const FixedArray<N, T>& other) const
00084     {
00085       return std::lexicographical_compare(begin(), end(), 
00086         other.begin(), other.end());
00087     }
00088 
00089 private:
00090   T data_[N];
00091 };
00092 
00093 
00094 
00095 /** \relate FixedArray */
00096 template<class T>
00097 FixedArray<2,T> makeFA(const T& a, const T& b)
00098 {
00099   FixedArray<2,T> rtn;
00100   rtn[0] = a;
00101   rtn[1] = b;
00102   return rtn;
00103 }
00104 
00105 /** \relate FixedArray */
00106 template<class T>
00107 FixedArray<3,T> makeFA(const T& a, const T& b, const T& c)
00108 {
00109   FixedArray<3,T> rtn;
00110   rtn[0] = a;
00111   rtn[1] = b;
00112   rtn[2] = c;
00113   return rtn;
00114 }
00115 
00116 
00117 /** \relate FixedArray */
00118 template<class T>
00119 FixedArray<4,T> makeFA(const T& a, const T& b, const T& c, const T& d)
00120 {
00121   FixedArray<4,T> rtn;
00122   rtn[0] = a;
00123   rtn[1] = b;
00124   rtn[2] = c;
00125   rtn[2] = d;
00126   return rtn;
00127 }
00128 
00129 }
00130 
00131 
00132 namespace std
00133 {
00134 /** \relates FixedArray */
00135 template <int N, class T> 
00136 ostream& operator<<(std::ostream& os, const Sundance::FixedArray<N, T>& f)
00137 {
00138   os << "{";
00139   for (int i=0; i<N; i++) 
00140   {
00141     if (i>0) os << ", ";
00142     os << f[i];
00143   }
00144   os << "}";
00145   return os;
00146 }
00147 
00148 }
00149 
00150 #endif

Site Contact