fixed_array.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 /* The following code declares class array,
00028  * an STL container (as wrapper) for arrays of constant size.
00029  *
00030  * See
00031  *      http://www.josuttis.com/cppcode
00032  * for details and the latest version.
00033  *
00034  * (C) Copyright Nicolai M. Josuttis 2001.
00035  * Permission to copy, use, modify, sell and distribute this software
00036  * is granted provided this copyright notice appears in all copies.
00037  * This software is provided "as is" without express or implied
00038  * warranty, and with no claim as to its suitability for any purpose.
00039  *
00040  * 29 Jun 2005 - remove boost includes and reverse iterators. (Jeremie Allard)
00041  * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
00042  * 05 Aug 2001 - minor update (Nico Josuttis)
00043  * 20 Jan 2001 - STLport fix (Beman Dawes)
00044  * 29 Sep 2000 - Initial Revision (Nico Josuttis)
00045  */
00046 
00047 // See http://www.boost.org/libs/array for Documentation.
00048 
00049 // FF added operator <
00050 // JA added constructors from tuples
00051 #ifndef SOFA_HELPER_FIXED_ARRAY_H
00052 #define SOFA_HELPER_FIXED_ARRAY_H
00053 
00054 #if !defined(__GNUC__) || (__GNUC__ > 3 || (_GNUC__ == 3 && __GNUC_MINOR__ > 3))
00055 #pragma once
00056 #endif
00057 
00058 #include <sofa/helper/static_assert.h>
00059 
00060 #include <cstddef>
00061 #include <stdexcept>
00062 #include <iterator>
00063 #include <algorithm>
00064 #include <math.h>
00065 
00066 namespace sofa
00067 {
00068 
00069 namespace helper
00070 {
00071 
00072 template<class T, std::size_t N>
00073 class fixed_array
00074 {
00075 public:
00076     T elems[N];    // fixed-size array of elements of type T
00077 
00078 public:
00079     // type definitions
00080     typedef T              value_type;
00081     typedef T*             iterator;
00082     typedef const T*       const_iterator;
00083     typedef T&             reference;
00084     typedef const T&       const_reference;
00085     typedef std::size_t    size_type;
00086     typedef std::ptrdiff_t difference_type;
00087 
00088     fixed_array()
00089     {
00090     }
00091     
00092     
00094     explicit fixed_array(value_type r1)
00095     {
00096         BOOST_STATIC_ASSERT(N==1);
00097         this->elems[0]=r1;
00098     }
00099 
00101     fixed_array(value_type r1, value_type r2)
00102     {
00103         BOOST_STATIC_ASSERT(N == 2);
00104         this->elems[0]=r1;
00105         this->elems[1]=r2;
00106     }
00107     
00109     fixed_array(value_type r1, value_type r2, value_type r3)
00110     {
00111         BOOST_STATIC_ASSERT(N == 3);
00112         this->elems[0]=r1;
00113         this->elems[1]=r2;
00114         this->elems[2]=r3;
00115     }
00116 
00118     fixed_array(value_type r1, value_type r2, value_type r3, value_type r4)
00119     {
00120         BOOST_STATIC_ASSERT(N == 4);
00121         this->elems[0]=r1;
00122         this->elems[1]=r2;
00123         this->elems[2]=r3;
00124         this->elems[3]=r4;
00125     }
00126     
00128     fixed_array(value_type r1, value_type r2, value_type r3, value_type r4, value_type r5)
00129     {
00130         BOOST_STATIC_ASSERT(N == 5);
00131         this->elems[0]=r1;
00132         this->elems[1]=r2;
00133         this->elems[2]=r3;
00134         this->elems[3]=r4;
00135         this->elems[4]=r5;
00136     }
00137     
00139     fixed_array(value_type r1, value_type r2, value_type r3, value_type r4, value_type r5, value_type r6)
00140     {
00141         BOOST_STATIC_ASSERT(N == 6);
00142         this->elems[0]=r1;
00143         this->elems[1]=r2;
00144         this->elems[2]=r3;
00145         this->elems[3]=r4;
00146         this->elems[4]=r5;
00147         this->elems[5]=r6;
00148     }
00149     
00151     fixed_array(value_type r1, value_type r2, value_type r3, value_type r4, value_type r5, value_type r6, value_type r7)
00152     {
00153         BOOST_STATIC_ASSERT(N == 7);
00154         this->elems[0]=r1;
00155         this->elems[1]=r2;
00156         this->elems[2]=r3;
00157         this->elems[3]=r4;
00158         this->elems[4]=r5;
00159         this->elems[5]=r6;
00160         this->elems[6]=r7;
00161     }
00162     
00164     fixed_array(value_type r1, value_type r2, value_type r3, value_type r4, value_type r5, value_type r6, value_type r7, value_type r8)
00165     {
00166         BOOST_STATIC_ASSERT(N == 8);
00167         this->elems[0]=r1;
00168         this->elems[1]=r2;
00169         this->elems[2]=r3;
00170         this->elems[3]=r4;
00171         this->elems[4]=r5;
00172         this->elems[5]=r6;
00173         this->elems[6]=r7;
00174         this->elems[7]=r8;
00175     }
00176     
00178     fixed_array(value_type r1, value_type r2, value_type r3, value_type r4, value_type r5, value_type r6, value_type r7, value_type r8, value_type r9)
00179     {
00180         BOOST_STATIC_ASSERT(N == 9);
00181         this->elems[0]=r1;
00182         this->elems[1]=r2;
00183         this->elems[2]=r3;
00184         this->elems[3]=r4;
00185         this->elems[4]=r5;
00186         this->elems[5]=r6;
00187         this->elems[6]=r7;
00188         this->elems[7]=r8;
00189         this->elems[8]=r9;
00190     }
00191     
00193     fixed_array(value_type r1, value_type r2, value_type r3, value_type r4, value_type r5, value_type r6, value_type r7, value_type r8, value_type r9, value_type r10)
00194     {
00195         BOOST_STATIC_ASSERT(N == 10);
00196         this->elems[0]=r1;
00197         this->elems[1]=r2;
00198         this->elems[2]=r3;
00199         this->elems[3]=r4;
00200         this->elems[4]=r5;
00201         this->elems[5]=r6;
00202         this->elems[6]=r7;
00203         this->elems[7]=r8;
00204         this->elems[8]=r9;
00205         this->elems[9]=r10;
00206     }
00207     
00208 
00209     // iterator support
00210     iterator begin()
00211     {
00212         return elems;
00213     }
00214     const_iterator begin() const
00215     {
00216         return elems;
00217     }
00218     iterator end()
00219     {
00220         return elems+N;
00221     }
00222     const_iterator end() const
00223     {
00224         return elems+N;
00225     }
00226 
00227     // operator[]
00228     reference operator[](size_type i)
00229     {
00230         return elems[i];
00231     }
00232     const_reference operator[](size_type i) const
00233     {
00234         return elems[i];
00235     }
00236 
00237     // at() with range check
00238     reference at(size_type i)
00239     {
00240         rangecheck(i);
00241         return elems[i];
00242     }
00243     const_reference at(size_type i) const
00244     {
00245         rangecheck(i);
00246         return elems[i];
00247     }
00248 
00249     // front() and back()
00250     reference front()
00251     {
00252         return elems[0];
00253     }
00254     const_reference front() const
00255     {
00256         return elems[0];
00257     }
00258     reference back()
00259     {
00260         return elems[N-1];
00261     }
00262     const_reference back() const
00263     {
00264         return elems[N-1];
00265     }
00266 
00267     // size is constant
00268     static size_type size()
00269     {
00270         return N;
00271     }
00272     static bool empty()
00273     {
00274         return false;
00275     }
00276     static size_type max_size()
00277     {
00278         return N;
00279     }
00280     enum { static_size = N };
00281 
00282     // swap (note: linear complexity)
00283     void swap (fixed_array<T,N>& y)
00284     {
00285         std::swap_ranges(begin(),end(),y.begin());
00286     }
00287 
00288     // direct access to data
00289     const T* data() const
00290     {
00291         return elems;
00292     }
00293 
00294     // assignment with type conversion
00295     template <typename T2>
00296     fixed_array<T,N>& operator= (const fixed_array<T2,N>& rhs)
00297     {
00298         //std::copy(rhs.begin(),rhs.end(), begin());
00299     for (size_type i=0;i<N;i++)
00300         elems[i] = rhs[i];
00301         return *this;
00302     }
00303 
00304     // assign one value to all elements
00305     void assign (const T& value)
00306     {
00307         //std::fill_n(begin(),size(),value);
00308     for (size_type i=0;i<N;i++)
00309         elems[i] = value;
00310     }
00311 
00312     inline friend std::ostream& operator << (std::ostream& out, const fixed_array<T,N>& a)
00313     {
00314         for( size_type i=0; i<N; i++ )
00315             out<<a.elems[i]<<" ";
00316         return out;
00317     }
00318 
00319     inline friend std::istream& operator >> (std::istream& in, fixed_array<T,N>& a)
00320     {
00321         for( size_type i=0; i<N; i++ )
00322             in>>a.elems[i];
00323         return in;
00324     }
00325     
00326     inline bool operator < (const fixed_array& v ) const {
00327         for( size_type i=0; i<N; i++ ){
00328             if( elems[i]<v[i] )
00329                 return true;  // (*this)<v
00330             else if( elems[i]>v[i] )
00331                 return false; // (*this)>v
00332         }
00333         return false; // (*this)==v
00334     }
00335 
00336 private:
00337 
00338     // check range (may be private because it is static)
00339     static void rangecheck (size_type i)
00340     {
00341         if (i >= size())
00342         {
00343             throw std::range_error("fixed_array");
00344         }
00345     }
00346 
00347 };
00348 
00349 template<class T>
00350 inline fixed_array<T, 2> make_array(const T& v0, const T& v1)
00351 {
00352     fixed_array<T, 2> v;
00353     v[0] = v0;
00354     v[1] = v1;
00355     return v;
00356 }
00357 
00358 template<class T>
00359 inline fixed_array<T, 3> make_array(const T& v0, const T& v1, const T& v2)
00360 {
00361     fixed_array<T, 3> v;
00362     v[0] = v0;
00363     v[1] = v1;
00364     v[2] = v2;
00365     return v;
00366 }
00367 
00368 template<class T>
00369 inline fixed_array<T, 4> make_array(const T& v0, const T& v1, const T& v2, const T& v3)
00370 {
00371     fixed_array<T, 4> v;
00372     v[0] = v0;
00373     v[1] = v1;
00374     v[2] = v2;
00375     v[3] = v3;
00376     return v;
00377 }
00378 
00379 template<class T>
00380 inline fixed_array<T, 5> make_array(const T& v0, const T& v1, const T& v2, const T& v3, const T& v4)
00381 {
00382     fixed_array<T, 5> v;
00383     v[0] = v0;
00384     v[1] = v1;
00385     v[2] = v2;
00386     v[3] = v3;
00387     v[4] = v4;
00388     return v;
00389 }
00390 
00391 template<class T>
00392 inline fixed_array<T, 6> make_array(const T& v0, const T& v1, const T& v2, const T& v3, const T& v4, const T& v5)
00393 {
00394     fixed_array<T, 6> v;
00395     v[0] = v0;
00396     v[1] = v1;
00397     v[2] = v2;
00398     v[3] = v3;
00399     v[4] = v4;
00400     v[5] = v5;
00401     return v;
00402 }
00403 
00404 template<class T>
00405 inline fixed_array<T, 7> make_array(const T& v0, const T& v1, const T& v2, const T& v3, const T& v4, const T& v5, const T& v6)
00406 {
00407     fixed_array<T, 7> v;
00408     v[0] = v0;
00409     v[1] = v1;
00410     v[2] = v2;
00411     v[3] = v3;
00412     v[4] = v4;
00413     v[5] = v5;
00414     v[6] = v6;
00415     return v;
00416 }
00417 
00418 template<class T>
00419 inline fixed_array<T, 8> make_array(const T& v0, const T& v1, const T& v2, const T& v3, const T& v4, const T& v5, const T& v6, const T& v7)
00420 {
00421     fixed_array<T, 8> v;
00422     v[0] = v0;
00423     v[1] = v1;
00424     v[2] = v2;
00425     v[3] = v3;
00426     v[4] = v4;
00427     v[5] = v5;
00428     v[6] = v6;
00429     v[7] = v7;
00430     return v;
00431 }
00432 
00433 template<class T>
00434 inline fixed_array<T, 9> make_array(const T& v0, const T& v1, const T& v2, const T& v3, const T& v4, const T& v5, const T& v6, const T& v7, const T& v8)
00435 {
00436     fixed_array<T, 9> v;
00437     v[0] = v0;
00438     v[1] = v1;
00439     v[2] = v2;
00440     v[3] = v3;
00441     v[4] = v4;
00442     v[5] = v5;
00443     v[6] = v6;
00444     v[7] = v7;
00445     v[8] = v8;
00446     return v;
00447 }
00448 
00449 template<class T>
00450 inline fixed_array<T, 10> make_array(const T& v0, const T& v1, const T& v2, const T& v3, const T& v4, const T& v5, const T& v6, const T& v7, const T& v8, const T& v9)
00451 {
00452     fixed_array<T, 10> v;
00453     v[0] = v0;
00454     v[1] = v1;
00455     v[2] = v2;
00456     v[3] = v3;
00457     v[4] = v4;
00458     v[5] = v5;
00459     v[6] = v6;
00460     v[7] = v7;
00461     v[8] = v8;
00462     v[9] = v9;
00463     return v;
00464 }
00465 
00466 } // namespace helper
00467 
00468 } // namespace sofa
00469 
00470 #endif

Generated on Sun Nov 23 02:29:29 2008 for Sofa by  doxygen 1.5.7.1