Skip to content
Snippets Groups Projects
array.hh 6.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
    // vi: set et ts=4 sw=2 sts=2:
    
    Oliver Sander's avatar
    Oliver Sander committed
    #ifndef DUNE_ARRAY_HH
    #define DUNE_ARRAY_HH
    
    Oliver Sander's avatar
    Oliver Sander committed
        \brief Fallback implementation of the std::array class (a static array)
    
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    
    // Include system implementation of array class if present
    #ifdef HAVE_ARRAY
    #include <array>
    
    #else // #ifdef HAVE_ARRAY
    
    #include <algorithm>
    
    #endif // #else // #ifdef HAVE_ARRAY
    
    Christian Engwer's avatar
    Christian Engwer committed
    #include "deprecated.hh"
    
    
    #else // #ifdef HAVE_ARRAY
    
    Oliver Sander's avatar
    Oliver Sander committed
      /** \brief Simple fixed size array class.  This replaces std::array,
       * if that is not available.
    
      template< class T, std::size_t N >
      struct array
      {
    
        //! Remember the storage type
        typedef T value_type;
    
        /** \brief Reference to an object */
    
        typedef value_type                                &reference;
    
        typedef const value_type                          &const_reference;
    
        typedef value_type                                *iterator;
    
        typedef const value_type                          *const_iterator;
    
    
        /** \brief Type used for array indices */
        typedef std::size_t size_type;
    
        /** \brief Difference type */
        typedef std::ptrdiff_t difference_type;
    
        /** \brief Reverse iterator type */
    
        typedef std::reverse_iterator<iterator>           reverse_iterator;
    
    
        /** \brief Const reverse iterator type */
        typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
    
        /** \brief Return array size */
    
        size_type size () const { return N; }
    
        /** \brief Determine, whether the container is empty */
        bool empty () const { return (N == 0); }
    
    
        //! Assign value to all entries
        array<T,N>& operator= (const T& t)
        {
    
    Christian Engwer's avatar
    Christian Engwer committed
          fill(t);
    
        //! \brief Assign value to all entries (according to C++0x the fill method is to be prefered)
    
        void fill ( const T &t )
    
          for( size_type i = 0; i < size(); ++i )
            a[ i ] = t;
    
        //! Component access
        reference operator[] (size_type i)
        {
          return a[i];
        }
    
        //! Const component access
        const_reference operator[] (size_type i) const
        {
          return a[i];
        }
    
    
        iterator begin ()
        {
          return a;
        }
    
        const_iterator begin () const
        {
          return a;
        }
    
        iterator end ()
        {
          return a + N;
        }
    
        const_iterator end () const
        {
          return a + N;
        }
    
    
    
    
    
      // Comparison Operators (see [lib.container.requirements])
      // -------------------------------------------------------
    
    
      template< class T, std::size_t N >
    
      inline bool operator< ( const array< T, N > &a, const array< T, N > &b )
      {
        return std::lexicographical_compare( a.begin(), a.end(), b.begin(), b.end() );
      }
    
    
      template< class T, std::size_t N >
    
      inline bool operator> ( const array< T, N > &a, const array< T, N > &b )
      {
        return b < a;
      }
    
    
      template< class T, std::size_t N >
    
      inline bool operator<= ( const array< T, N > &a, const array< T, N > &b )
      {
        return !(a > b);
      }
    
    
      template< class T, std::size_t N >
    
      inline bool operator>= ( const array< T, N > &a, const array< T, N > &b )
      {
        return !(a < b);
      }
    
    #endif // #else // #ifdef HAVE_ARRAY
    
      /** \brief output operator for array
       *
       *  \note This operator is Dune-specific and not part of any C++-standard.
       */
      template< class T, std::size_t N >
      inline std::ostream &operator<< ( std::ostream &s, const array< T, N > &e )
    
        typedef typename array< T, N >::size_type size_type;
    
        if( !e.empty() )
    
    Christoph Grüninger's avatar
    Christoph Grüninger committed
        {
    
          s << "[" << e[ size_type( 0 ) ];
          for( size_type i = 1; i < e.size(); ++i )
            s << "," << e[ i ];
          s << "]";
    
    Christoph Grüninger's avatar
    Christoph Grüninger committed
        }
    
    Christoph Grüninger's avatar
    Christoph Grüninger committed
    
    
    #ifndef DOXYGEN
      template<class T>
      array<T, 1> make_array(const T &t0) {
        array<T, 1> result = { t0 };
        return result;
      }
    
      template<class T>
      array<T, 2> make_array(const T &t0, const T &t1) {
        array<T, 2> result = { t0, t1 };
        return result;
      }
    
      template<class T>
      array<T, 3> make_array(const T &t0, const T &t1, const T &t2) {
        array<T, 3> result = { t0, t1, t2 };
        return result;
      }
    
      template<class T>
      array<T, 4> make_array(const T &t0, const T &t1, const T &t2, const T &t3) {
        array<T, 4> result = { t0, t1, t2, t3 };
        return result;
      }
    
      template<class T>
      array<T, 5> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
                             const T &t4)
      {
        array<T, 5> result = { t0, t1, t2, t3, t4 };
        return result;
      }
    
      template<class T>
      array<T, 6> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
                             const T &t4, const T &t5)
      {
        array<T, 6> result = { t0, t1, t2, t3, t4, t5 };
        return result;
      }
    
      template<class T>
      array<T, 7> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
                             const T &t4, const T &t5, const T &t6)
      {
        array<T, 7> result = { t0, t1, t2, t3, t4, t5, t6 };
        return result;
      }
    
      template<class T>
      array<T, 8> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
                             const T &t4, const T &t5, const T &t6, const T &t7)
      {
        array<T, 8> result = { t0, t1, t2, t3, t4, t5, t6, t7 };
        return result;
      }
    
      template<class T>
      array<T, 9> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
                             const T &t4, const T &t5, const T &t6, const T &t7,
                             const T &t8)
      {
        array<T, 9> result = { t0, t1, t2, t3, t4, t5, t6, t7, t8 };
        return result;
      }
    
    #endif // #ifndef DOXYGEN
    
      /** \brief create and initialize an array
       *  \note There are overloads for this method which take fewer arguments
       *        (minimum 1).  The current maximum of 10 arguments is arbitrary and
       *        can be raised on demand.
       *  \note This function is Dune-specific and not part of any C++-standard.
    
       */
      template<class T>
      array<T, 10> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
                              const T &t4, const T &t5, const T &t6, const T &t7,
                              const T &t8, const T &t9)
      {
        array<T, 10> result = { t0, t1, t2, t3, t4, t5, t6, t7, t8, t9 };
        return result;
      }
    
    
    } // namespace Dune
    
    #endif // #ifndef DUNE_ARRAY_HH