Home | History | Annotate | Line # | Download | only in tr1
      1 // class template array -*- C++ -*-
      2 
      3 // Copyright (C) 2004-2024 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 tr1/array
     26  *  This is a TR1 C++ Library header. 
     27  */
     28 
     29 #ifndef _GLIBCXX_TR1_ARRAY
     30 #define _GLIBCXX_TR1_ARRAY 1
     31 
     32 #pragma GCC system_header
     33 
     34 #include <bits/requires_hosted.h> // TR1
     35 
     36 #include <bits/stl_algobase.h>
     37 
     38 namespace std _GLIBCXX_VISIBILITY(default)
     39 {
     40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     41 
     42 namespace tr1
     43 {
     44   /**
     45    *  @brief A standard container for storing a fixed size sequence of elements.
     46    *
     47    *  @ingroup sequences
     48    *
     49    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
     50    *  <a href="tables.html#66">reversible container</a>, and a
     51    *  <a href="tables.html#67">sequence</a>.
     52    *
     53    *  Sets support random access iterators.
     54    *
     55    *  @param  Tp  Type of element. Required to be a complete type.
     56    *  @param  N  Number of elements.
     57   */
     58   template<typename _Tp, std::size_t _Nm>
     59     struct array
     60     {
     61       typedef _Tp 	    			      value_type;
     62       typedef value_type&                   	      reference;
     63       typedef const value_type&             	      const_reference;
     64       typedef value_type*          		      iterator;
     65       typedef const value_type*			      const_iterator;
     66       typedef std::size_t                    	      size_type;
     67       typedef std::ptrdiff_t                   	      difference_type;
     68       typedef std::reverse_iterator<iterator>	      reverse_iterator;
     69       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
     70 
     71       // Support for zero-sized arrays mandatory.
     72       value_type _M_instance[_Nm ? _Nm : 1];
     73 
     74       // No explicit construct/copy/destroy for aggregate type.
     75 
     76       void
     77       assign(const value_type& __u)
     78       { std::fill_n(begin(), size(), __u); }
     79 
     80       void
     81       swap(array& __other)
     82       { std::swap_ranges(begin(), end(), __other.begin()); }
     83 
     84       // Iterators.
     85       iterator
     86       begin()
     87       { return iterator(std::__addressof(_M_instance[0])); }
     88 
     89       const_iterator
     90       begin() const 
     91       { return const_iterator(std::__addressof(_M_instance[0])); }
     92 
     93       iterator
     94       end()
     95       { return iterator(std::__addressof(_M_instance[_Nm])); }
     96 
     97       const_iterator
     98       end() const
     99       { return const_iterator(std::__addressof(_M_instance[_Nm])); }
    100 
    101       reverse_iterator 
    102       rbegin()
    103       { return reverse_iterator(end()); }
    104 
    105       const_reverse_iterator 
    106       rbegin() const
    107       { return const_reverse_iterator(end()); }
    108 
    109       reverse_iterator 
    110       rend()
    111       { return reverse_iterator(begin()); }
    112 
    113       const_reverse_iterator 
    114       rend() const
    115       { return const_reverse_iterator(begin()); }
    116 
    117       // Capacity.
    118       size_type 
    119       size() const { return _Nm; }
    120 
    121       size_type 
    122       max_size() const { return _Nm; }
    123 
    124       _GLIBCXX_NODISCARD bool 
    125       empty() const { return size() == 0; }
    126 
    127       // Element access.
    128       reference
    129       operator[](size_type __n)
    130       { return _M_instance[__n]; }
    131 
    132       const_reference
    133       operator[](size_type __n) const
    134       { return _M_instance[__n]; }
    135 
    136       reference
    137       at(size_type __n)
    138       {
    139 	if (__n >= _Nm)
    140 	  std::__throw_out_of_range(__N("array::at"));
    141 	return _M_instance[__n];
    142       }
    143 
    144       const_reference
    145       at(size_type __n) const
    146       {
    147 	if (__n >= _Nm)
    148 	  std::__throw_out_of_range(__N("array::at"));
    149 	return _M_instance[__n];
    150       }
    151 
    152       reference 
    153       front()
    154       { return *begin(); }
    155 
    156       const_reference 
    157       front() const
    158       { return *begin(); }
    159 
    160       reference 
    161       back()
    162       { return _Nm ? *(end() - 1) : *end(); }
    163 
    164       const_reference 
    165       back() const
    166       { return _Nm ? *(end() - 1) : *end(); }
    167 
    168       _Tp*
    169       data()
    170       { return std::__addressof(_M_instance[0]); }
    171 
    172       const _Tp*
    173       data() const
    174       { return std::__addressof(_M_instance[0]); }
    175     };
    176 
    177   // Array comparisons.
    178   template<typename _Tp, std::size_t _Nm>
    179     inline bool 
    180     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    181     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
    182 
    183   template<typename _Tp, std::size_t _Nm>
    184     inline bool
    185     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    186     { return !(__one == __two); }
    187 
    188   template<typename _Tp, std::size_t _Nm>
    189     inline bool
    190     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
    191     { 
    192       return std::lexicographical_compare(__a.begin(), __a.end(),
    193 					  __b.begin(), __b.end()); 
    194     }
    195 
    196   template<typename _Tp, std::size_t _Nm>
    197     inline bool
    198     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    199     { return __two < __one; }
    200 
    201   template<typename _Tp, std::size_t _Nm>
    202     inline bool
    203     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    204     { return !(__one > __two); }
    205 
    206   template<typename _Tp, std::size_t _Nm>
    207     inline bool
    208     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    209     { return !(__one < __two); }
    210 
    211   // Specialized algorithms [6.2.2.2].
    212   template<typename _Tp, std::size_t _Nm>
    213     inline void
    214     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
    215     { __one.swap(__two); }
    216 
    217   // Tuple interface to class template array [6.2.2.5].
    218 
    219   /// tuple_size
    220   template<typename _Tp> 
    221     class tuple_size;
    222 
    223   /// tuple_element
    224   template<int _Int, typename _Tp>
    225     class tuple_element;
    226 
    227   template<typename _Tp, std::size_t _Nm>
    228     struct tuple_size<array<_Tp, _Nm> >
    229     { static const int value = _Nm; };
    230 
    231   template<typename _Tp, std::size_t _Nm>
    232     const int
    233     tuple_size<array<_Tp, _Nm> >::value;  
    234 
    235   template<int _Int, typename _Tp, std::size_t _Nm>
    236     struct tuple_element<_Int, array<_Tp, _Nm> >
    237     { typedef _Tp type; };
    238 
    239   template<int _Int, typename _Tp, std::size_t _Nm>
    240     inline _Tp&
    241     get(array<_Tp, _Nm>& __arr)
    242     { return __arr[_Int]; }
    243 
    244   template<int _Int, typename _Tp, std::size_t _Nm>
    245     inline const _Tp&
    246     get(const array<_Tp, _Nm>& __arr)
    247     { return __arr[_Int]; }
    248 }
    249 
    250 _GLIBCXX_END_NAMESPACE_VERSION
    251 }
    252 
    253 #endif // _GLIBCXX_TR1_ARRAY
    254