Home | History | Annotate | Download | only in include
      1 // <array> -*- C++ -*-
      2 
      3 // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file include/array
     26  *  This is a Standard C++ Library header.
     27  */
     28 
     29 #ifndef _GLIBCXX_ARRAY
     30 #define _GLIBCXX_ARRAY 1
     31 
     32 #pragma GCC system_header
     33 
     34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
     35 # include <bits/c++0x_warning.h>
     36 #else
     37 
     38 #include <bits/stl_algobase.h>
     39 #include <bits/range_access.h>
     40 
     41 namespace std _GLIBCXX_VISIBILITY(default)
     42 {
     43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     44 
     45   /**
     46    *  @brief A standard container for storing a fixed size sequence of elements.
     47    *
     48    *  @ingroup sequences
     49    *
     50    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
     51    *  <a href="tables.html#66">reversible container</a>, and a
     52    *  <a href="tables.html#67">sequence</a>.
     53    *
     54    *  Sets support random access iterators.
     55    *
     56    *  @param  Tp  Type of element. Required to be a complete type.
     57    *  @param  N  Number of elements.
     58   */
     59   template<typename _Tp, std::size_t _Nm>
     60     struct array
     61     {
     62       typedef _Tp 	    			      value_type;
     63       typedef _Tp*                                    pointer;
     64       typedef const _Tp*                              const_pointer;
     65       typedef value_type&                   	      reference;
     66       typedef const value_type&             	      const_reference;
     67       typedef value_type*          		      iterator;
     68       typedef const value_type*			      const_iterator;
     69       typedef std::size_t                    	      size_type;
     70       typedef std::ptrdiff_t                   	      difference_type;
     71       typedef std::reverse_iterator<iterator>	      reverse_iterator;
     72       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
     73 
     74       // Support for zero-sized arrays mandatory.
     75       value_type _M_instance[_Nm ? _Nm : 1];
     76 
     77       // No explicit construct/copy/destroy for aggregate type.
     78 
     79       // DR 776.
     80       void
     81       fill(const value_type& __u)
     82       { std::fill_n(begin(), size(), __u); }
     83 
     84       void
     85       swap(array& __other)
     86       { std::swap_ranges(begin(), end(), __other.begin()); }
     87 
     88       // Iterators.
     89       iterator
     90       begin()
     91       { return iterator(std::__addressof(_M_instance[0])); }
     92 
     93       const_iterator
     94       begin() const 
     95       { return const_iterator(std::__addressof(_M_instance[0])); }
     96 
     97       iterator
     98       end()
     99       { return iterator(std::__addressof(_M_instance[_Nm])); }
    100 
    101       const_iterator
    102       end() const
    103       { return const_iterator(std::__addressof(_M_instance[_Nm])); }
    104 
    105       reverse_iterator 
    106       rbegin()
    107       { return reverse_iterator(end()); }
    108 
    109       const_reverse_iterator 
    110       rbegin() const
    111       { return const_reverse_iterator(end()); }
    112 
    113       reverse_iterator 
    114       rend()
    115       { return reverse_iterator(begin()); }
    116 
    117       const_reverse_iterator 
    118       rend() const
    119       { return const_reverse_iterator(begin()); }
    120 
    121       const_iterator
    122       cbegin() const 
    123       { return const_iterator(std::__addressof(_M_instance[0])); }
    124 
    125       const_iterator
    126       cend() const
    127       { return const_iterator(std::__addressof(_M_instance[_Nm])); }
    128 
    129       const_reverse_iterator 
    130       crbegin() const
    131       { return const_reverse_iterator(end()); }
    132 
    133       const_reverse_iterator 
    134       crend() const
    135       { return const_reverse_iterator(begin()); }
    136 
    137       // Capacity.
    138       constexpr size_type 
    139       size() const { return _Nm; }
    140 
    141       constexpr size_type 
    142       max_size() const { return _Nm; }
    143 
    144       constexpr bool 
    145       empty() const { return size() == 0; }
    146 
    147       // Element access.
    148       reference
    149       operator[](size_type __n)
    150       { return _M_instance[__n]; }
    151 
    152       const_reference
    153       operator[](size_type __n) const
    154       { return _M_instance[__n]; }
    155 
    156       reference
    157       at(size_type __n)
    158       {
    159 	if (__n >= _Nm)
    160 	  std::__throw_out_of_range(__N("array::at"));
    161 	return _M_instance[__n];
    162       }
    163 
    164       const_reference
    165       at(size_type __n) const
    166       {
    167 	if (__n >= _Nm)
    168 	  std::__throw_out_of_range(__N("array::at"));
    169 	return _M_instance[__n];
    170       }
    171 
    172       reference 
    173       front()
    174       { return *begin(); }
    175 
    176       const_reference 
    177       front() const
    178       { return *begin(); }
    179 
    180       reference 
    181       back()
    182       { return _Nm ? *(end() - 1) : *end(); }
    183 
    184       const_reference 
    185       back() const
    186       { return _Nm ? *(end() - 1) : *end(); }
    187 
    188       _Tp*
    189       data()
    190       { return std::__addressof(_M_instance[0]); }
    191 
    192       const _Tp*
    193       data() const
    194       { return std::__addressof(_M_instance[0]); }
    195     };
    196 
    197   // Array comparisons.
    198   template<typename _Tp, std::size_t _Nm>
    199     inline bool 
    200     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    201     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
    202 
    203   template<typename _Tp, std::size_t _Nm>
    204     inline bool
    205     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    206     { return !(__one == __two); }
    207 
    208   template<typename _Tp, std::size_t _Nm>
    209     inline bool
    210     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
    211     { 
    212       return std::lexicographical_compare(__a.begin(), __a.end(),
    213 					  __b.begin(), __b.end()); 
    214     }
    215 
    216   template<typename _Tp, std::size_t _Nm>
    217     inline bool
    218     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    219     { return __two < __one; }
    220 
    221   template<typename _Tp, std::size_t _Nm>
    222     inline bool
    223     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    224     { return !(__one > __two); }
    225 
    226   template<typename _Tp, std::size_t _Nm>
    227     inline bool
    228     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    229     { return !(__one < __two); }
    230 
    231   // Specialized algorithms [6.2.2.2].
    232   template<typename _Tp, std::size_t _Nm>
    233     inline void
    234     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
    235     { __one.swap(__two); }
    236 
    237   // Tuple interface to class template array [6.2.2.5].
    238 
    239   /// tuple_size
    240   template<typename _Tp> 
    241     class tuple_size;
    242 
    243   /// tuple_element
    244   template<std::size_t _Int, typename _Tp>
    245     class tuple_element;
    246 
    247   template<typename _Tp, std::size_t _Nm>
    248     struct tuple_size<array<_Tp, _Nm> >
    249     { static const std::size_t value = _Nm; };
    250 
    251   template<typename _Tp, std::size_t _Nm>
    252     const std::size_t
    253     tuple_size<array<_Tp, _Nm> >::value;  
    254 
    255   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
    256     struct tuple_element<_Int, array<_Tp, _Nm> >
    257     { typedef _Tp type; };
    258 
    259   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
    260     inline _Tp&
    261     get(array<_Tp, _Nm>& __arr)
    262     { return __arr[_Int]; }
    263 
    264   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
    265     inline const _Tp&
    266     get(const array<_Tp, _Nm>& __arr)
    267     { return __arr[_Int]; }
    268 
    269 _GLIBCXX_END_NAMESPACE_VERSION
    270 } // namespace
    271 
    272 #endif // __GXX_EXPERIMENTAL_CXX0X__
    273 
    274 #endif // _GLIBCXX_ARRAY
    275