libstdc++
array
Go to the documentation of this file.
00001 // <array> -*- C++ -*-
00002 
00003 // Copyright (C) 2007-2016 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/array
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_ARRAY
00030 #define _GLIBCXX_ARRAY 1
00031 
00032 #pragma GCC system_header
00033 
00034 #if __cplusplus < 201103L
00035 # include <bits/c++0x_warning.h>
00036 #else
00037 
00038 #include <utility>
00039 #include <stdexcept>
00040 #include <bits/stl_algobase.h>
00041 #include <bits/range_access.h>
00042 
00043 namespace std _GLIBCXX_VISIBILITY(default)
00044 {
00045 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
00046 
00047   template<typename _Tp, std::size_t _Nm>
00048     struct __array_traits
00049     {
00050       typedef _Tp _Type[_Nm];
00051 
00052       static constexpr _Tp&
00053       _S_ref(const _Type& __t, std::size_t __n) noexcept
00054       { return const_cast<_Tp&>(__t[__n]); }
00055 
00056       static constexpr _Tp*
00057       _S_ptr(const _Type& __t) noexcept
00058       { return const_cast<_Tp*>(__t); }
00059     };
00060 
00061  template<typename _Tp>
00062    struct __array_traits<_Tp, 0>
00063    {
00064      struct _Type { };
00065 
00066      static constexpr _Tp&
00067      _S_ref(const _Type&, std::size_t) noexcept
00068      { return *static_cast<_Tp*>(nullptr); }
00069 
00070      static constexpr _Tp*
00071      _S_ptr(const _Type&) noexcept
00072      { return nullptr; }
00073    };
00074 
00075   /**
00076    *  @brief A standard container for storing a fixed size sequence of elements.
00077    *
00078    *  @ingroup sequences
00079    *
00080    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00081    *  <a href="tables.html#66">reversible container</a>, and a
00082    *  <a href="tables.html#67">sequence</a>.
00083    *
00084    *  Sets support random access iterators.
00085    *
00086    *  @tparam  Tp  Type of element. Required to be a complete type.
00087    *  @tparam  N  Number of elements.
00088   */
00089   template<typename _Tp, std::size_t _Nm>
00090     struct array
00091     {
00092       typedef _Tp                                     value_type;
00093       typedef value_type*                             pointer;
00094       typedef const value_type*                       const_pointer;
00095       typedef value_type&                             reference;
00096       typedef const value_type&                       const_reference;
00097       typedef value_type*                             iterator;
00098       typedef const value_type*                       const_iterator;
00099       typedef std::size_t                             size_type;
00100       typedef std::ptrdiff_t                          difference_type;
00101       typedef std::reverse_iterator<iterator>         reverse_iterator;
00102       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
00103 
00104       // Support for zero-sized arrays mandatory.
00105       typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
00106       typename _AT_Type::_Type                         _M_elems;
00107 
00108       // No explicit construct/copy/destroy for aggregate type.
00109 
00110       // DR 776.
00111       void
00112       fill(const value_type& __u)
00113       { std::fill_n(begin(), size(), __u); }
00114 
00115       void
00116       swap(array& __other)
00117       noexcept(__is_nothrow_swappable<_Tp>::value)
00118       { std::swap_ranges(begin(), end(), __other.begin()); }
00119 
00120       // Iterators.
00121       iterator
00122       begin() noexcept
00123       { return iterator(data()); }
00124 
00125       const_iterator
00126       begin() const noexcept
00127       { return const_iterator(data()); }
00128 
00129       iterator
00130       end() noexcept
00131       { return iterator(data() + _Nm); }
00132 
00133       const_iterator
00134       end() const noexcept
00135       { return const_iterator(data() + _Nm); }
00136 
00137       reverse_iterator 
00138       rbegin() noexcept
00139       { return reverse_iterator(end()); }
00140 
00141       const_reverse_iterator 
00142       rbegin() const noexcept
00143       { return const_reverse_iterator(end()); }
00144 
00145       reverse_iterator 
00146       rend() noexcept
00147       { return reverse_iterator(begin()); }
00148 
00149       const_reverse_iterator 
00150       rend() const noexcept
00151       { return const_reverse_iterator(begin()); }
00152 
00153       const_iterator
00154       cbegin() const noexcept
00155       { return const_iterator(data()); }
00156 
00157       const_iterator
00158       cend() const noexcept
00159       { return const_iterator(data() + _Nm); }
00160 
00161       const_reverse_iterator 
00162       crbegin() const noexcept
00163       { return const_reverse_iterator(end()); }
00164 
00165       const_reverse_iterator 
00166       crend() const noexcept
00167       { return const_reverse_iterator(begin()); }
00168 
00169       // Capacity.
00170       constexpr size_type 
00171       size() const noexcept { return _Nm; }
00172 
00173       constexpr size_type 
00174       max_size() const noexcept { return _Nm; }
00175 
00176       constexpr bool 
00177       empty() const noexcept { return size() == 0; }
00178 
00179       // Element access.
00180       reference
00181       operator[](size_type __n) noexcept
00182       { return _AT_Type::_S_ref(_M_elems, __n); }
00183 
00184       constexpr const_reference
00185       operator[](size_type __n) const noexcept
00186       { return _AT_Type::_S_ref(_M_elems, __n); }
00187 
00188       reference
00189       at(size_type __n)
00190       {
00191         if (__n >= _Nm)
00192           std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
00193                                             ">= _Nm (which is %zu)"),
00194                                         __n, _Nm);
00195         return _AT_Type::_S_ref(_M_elems, __n);
00196       }
00197 
00198       constexpr const_reference
00199       at(size_type __n) const
00200       {
00201         // Result of conditional expression must be an lvalue so use
00202         // boolean ? lvalue : (throw-expr, lvalue)
00203         return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
00204           : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
00205                                                ">= _Nm (which is %zu)"),
00206                                            __n, _Nm),
00207              _AT_Type::_S_ref(_M_elems, 0));
00208       }
00209 
00210       reference 
00211       front() noexcept
00212       { return *begin(); }
00213 
00214       constexpr const_reference 
00215       front() const noexcept
00216       { return _AT_Type::_S_ref(_M_elems, 0); }
00217 
00218       reference 
00219       back() noexcept
00220       { return _Nm ? *(end() - 1) : *end(); }
00221 
00222       constexpr const_reference 
00223       back() const noexcept
00224       { 
00225         return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1) 
00226                    : _AT_Type::_S_ref(_M_elems, 0);
00227       }
00228 
00229       pointer
00230       data() noexcept
00231       { return _AT_Type::_S_ptr(_M_elems); }
00232 
00233       const_pointer
00234       data() const noexcept
00235       { return _AT_Type::_S_ptr(_M_elems); }
00236     };
00237 
00238   // Array comparisons.
00239   template<typename _Tp, std::size_t _Nm>
00240     inline bool 
00241     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00242     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
00243 
00244   template<typename _Tp, std::size_t _Nm>
00245     inline bool
00246     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00247     { return !(__one == __two); }
00248 
00249   template<typename _Tp, std::size_t _Nm>
00250     inline bool
00251     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
00252     { 
00253       return std::lexicographical_compare(__a.begin(), __a.end(),
00254                                           __b.begin(), __b.end()); 
00255     }
00256 
00257   template<typename _Tp, std::size_t _Nm>
00258     inline bool
00259     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00260     { return __two < __one; }
00261 
00262   template<typename _Tp, std::size_t _Nm>
00263     inline bool
00264     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00265     { return !(__one > __two); }
00266 
00267   template<typename _Tp, std::size_t _Nm>
00268     inline bool
00269     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00270     { return !(__one < __two); }
00271 
00272   // Specialized algorithms.
00273   template<typename _Tp, std::size_t _Nm>
00274     inline void
00275     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
00276     noexcept(noexcept(__one.swap(__two)))
00277     { __one.swap(__two); }
00278 
00279   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00280     constexpr _Tp&
00281     get(array<_Tp, _Nm>& __arr) noexcept
00282     {
00283       static_assert(_Int < _Nm, "index is out of bounds");
00284       return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
00285         _S_ref(__arr._M_elems, _Int);
00286     }
00287 
00288   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00289     constexpr _Tp&&
00290     get(array<_Tp, _Nm>&& __arr) noexcept
00291     {
00292       static_assert(_Int < _Nm, "index is out of bounds");
00293       return std::move(_GLIBCXX_STD_C::get<_Int>(__arr));
00294     }
00295 
00296   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00297     constexpr const _Tp&
00298     get(const array<_Tp, _Nm>& __arr) noexcept
00299     {
00300       static_assert(_Int < _Nm, "index is out of bounds");
00301       return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
00302         _S_ref(__arr._M_elems, _Int);
00303     }
00304 
00305 _GLIBCXX_END_NAMESPACE_CONTAINER
00306 } // namespace std
00307 
00308 namespace std _GLIBCXX_VISIBILITY(default)
00309 {
00310 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00311 
00312   // Tuple interface to class template array.
00313 
00314   /// tuple_size
00315   template<typename _Tp> 
00316     class tuple_size;
00317 
00318   /// Partial specialization for std::array
00319   template<typename _Tp, std::size_t _Nm>
00320     struct tuple_size<_GLIBCXX_STD_C::array<_Tp, _Nm>>
00321     : public integral_constant<std::size_t, _Nm> { };
00322 
00323   /// tuple_element
00324   template<std::size_t _Int, typename _Tp>
00325     class tuple_element;
00326 
00327   /// Partial specialization for std::array
00328   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00329     struct tuple_element<_Int, _GLIBCXX_STD_C::array<_Tp, _Nm>>
00330     {
00331       static_assert(_Int < _Nm, "index is out of bounds");
00332       typedef _Tp type;
00333     };
00334 
00335   template<typename _Tp, std::size_t _Nm>
00336     struct __is_tuple_like_impl<_GLIBCXX_STD_C::array<_Tp, _Nm>> : true_type
00337     { };
00338 
00339 _GLIBCXX_END_NAMESPACE_VERSION
00340 } // namespace std
00341 
00342 #ifdef _GLIBCXX_DEBUG
00343 # include <debug/array>
00344 #endif
00345 
00346 #ifdef _GLIBCXX_PROFILE
00347 # include <profile/array>
00348 #endif
00349 
00350 #endif // C++11
00351 
00352 #endif // _GLIBCXX_ARRAY