libstdc++
array
Go to the documentation of this file.
00001 // Debugging array implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2012-2015 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 debug/array
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_DEBUG_ARRAY
00030 #define _GLIBCXX_DEBUG_ARRAY 1
00031 
00032 #pragma GCC system_header
00033 
00034 #include <debug/safe_sequence.h>
00035 
00036 namespace std _GLIBCXX_VISIBILITY(default)
00037 {
00038 namespace __debug
00039 {
00040   template<typename _Tp, std::size_t _Nm>
00041     struct array
00042     {
00043       typedef _Tp                                     value_type;
00044       typedef value_type*                             pointer;
00045       typedef const value_type*                       const_pointer;
00046       typedef value_type&                             reference;
00047       typedef const value_type&                       const_reference;
00048       typedef value_type*                             iterator;
00049       typedef const value_type*                       const_iterator;
00050       typedef std::size_t                             size_type;
00051       typedef std::ptrdiff_t                          difference_type;
00052       typedef std::reverse_iterator<iterator>         reverse_iterator;
00053       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
00054 
00055       // Support for zero-sized arrays mandatory.
00056       typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
00057       typename _AT_Type::_Type                         _M_elems;
00058 
00059       template<std::size_t _Size>
00060         struct _Array_check_subscript
00061         {
00062           std::size_t size() { return _Size; }
00063 
00064           _Array_check_subscript(std::size_t __index)
00065           { __glibcxx_check_subscript(__index); }
00066         };
00067 
00068       template<std::size_t _Size>
00069         struct _Array_check_nonempty
00070         {
00071           bool empty() { return _Size == 0; }
00072 
00073           _Array_check_nonempty()
00074           { __glibcxx_check_nonempty(); }
00075         };
00076 
00077       // No explicit construct/copy/destroy for aggregate type.
00078 
00079       // DR 776.
00080       void
00081       fill(const value_type& __u)
00082       { std::fill_n(begin(), size(), __u); }
00083 
00084       void
00085       swap(array& __other)
00086       noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
00087       { std::swap_ranges(begin(), end(), __other.begin()); }
00088 
00089       // Iterators.
00090       iterator
00091       begin() noexcept
00092       { return iterator(data()); }
00093 
00094       const_iterator
00095       begin() const noexcept
00096       { return const_iterator(data()); }
00097 
00098       iterator
00099       end() noexcept
00100       { return iterator(data() + _Nm); }
00101 
00102       const_iterator
00103       end() const noexcept
00104       { return const_iterator(data() + _Nm); }
00105 
00106       reverse_iterator 
00107       rbegin() noexcept
00108       { return reverse_iterator(end()); }
00109 
00110       const_reverse_iterator 
00111       rbegin() const noexcept
00112       { return const_reverse_iterator(end()); }
00113 
00114       reverse_iterator 
00115       rend() noexcept
00116       { return reverse_iterator(begin()); }
00117 
00118       const_reverse_iterator 
00119       rend() const noexcept
00120       { return const_reverse_iterator(begin()); }
00121 
00122       const_iterator
00123       cbegin() const noexcept
00124       { return const_iterator(data()); }
00125 
00126       const_iterator
00127       cend() const noexcept
00128       { return const_iterator(data() + _Nm); }
00129 
00130       const_reverse_iterator 
00131       crbegin() const noexcept
00132       { return const_reverse_iterator(end()); }
00133 
00134       const_reverse_iterator 
00135       crend() const noexcept
00136       { return const_reverse_iterator(begin()); }
00137 
00138       // Capacity.
00139       constexpr size_type 
00140       size() const noexcept { return _Nm; }
00141 
00142       constexpr size_type 
00143       max_size() const noexcept { return _Nm; }
00144 
00145       constexpr bool 
00146       empty() const noexcept { return size() == 0; }
00147 
00148       // Element access.
00149       reference
00150       operator[](size_type __n) noexcept
00151       {
00152         __glibcxx_check_subscript(__n);
00153         return _AT_Type::_S_ref(_M_elems, __n);
00154       }
00155 
00156       constexpr const_reference
00157       operator[](size_type __n) const noexcept
00158       {
00159         return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
00160          : (_GLIBCXX_THROW_OR_ABORT(_Array_check_subscript<_Nm>(__n)),
00161             _AT_Type::_S_ref(_M_elems, 0));
00162       }
00163 
00164       reference
00165       at(size_type __n)
00166       {
00167         if (__n >= _Nm)
00168           std::__throw_out_of_range_fmt(__N("array::at: __n "
00169                                             "(which is %zu) >= _Nm "
00170                                             "(which is %zu)"),
00171                                         __n, _Nm);
00172         return _AT_Type::_S_ref(_M_elems, __n);
00173       }
00174 
00175       constexpr const_reference
00176       at(size_type __n) const
00177       {
00178         // Result of conditional expression must be an lvalue so use
00179         // boolean ? lvalue : (throw-expr, lvalue)
00180         return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
00181           : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
00182                                                ">= _Nm (which is %zu)"),
00183                                            __n, _Nm),
00184              _AT_Type::_S_ref(_M_elems, 0));
00185       }
00186 
00187       reference 
00188       front() noexcept
00189       {
00190         __glibcxx_check_nonempty();
00191         return *begin();
00192       }
00193 
00194       constexpr const_reference 
00195       front() const noexcept
00196       {
00197         return _Nm ? _AT_Type::_S_ref(_M_elems, 0)
00198           : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
00199              _AT_Type::_S_ref(_M_elems, 0));
00200       }
00201 
00202       reference 
00203       back() noexcept
00204       {
00205         __glibcxx_check_nonempty();
00206         return _Nm ? *(end() - 1) : *end();
00207       }
00208 
00209       constexpr const_reference 
00210       back() const noexcept
00211       {
00212         return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
00213           : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
00214              _AT_Type::_S_ref(_M_elems, 0));
00215       }
00216 
00217       pointer
00218       data() noexcept
00219       { return _AT_Type::_S_ptr(_M_elems); }
00220 
00221       const_pointer
00222       data() const noexcept
00223       { return _AT_Type::_S_ptr(_M_elems); }
00224     };
00225 
00226   // Array comparisons.
00227   template<typename _Tp, std::size_t _Nm>
00228     inline bool 
00229     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00230     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
00231 
00232   template<typename _Tp, std::size_t _Nm>
00233     inline bool
00234     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00235     { return !(__one == __two); }
00236 
00237   template<typename _Tp, std::size_t _Nm>
00238     inline bool
00239     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
00240     { 
00241       return std::lexicographical_compare(__a.begin(), __a.end(),
00242                                           __b.begin(), __b.end()); 
00243     }
00244 
00245   template<typename _Tp, std::size_t _Nm>
00246     inline bool
00247     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00248     { return __two < __one; }
00249 
00250   template<typename _Tp, std::size_t _Nm>
00251     inline bool
00252     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00253     { return !(__one > __two); }
00254 
00255   template<typename _Tp, std::size_t _Nm>
00256     inline bool
00257     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00258     { return !(__one < __two); }
00259 
00260   // Specialized algorithms.
00261   template<typename _Tp, std::size_t _Nm>
00262     inline void
00263     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
00264     noexcept(noexcept(__one.swap(__two)))
00265     { __one.swap(__two); }
00266 
00267   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00268     constexpr _Tp&
00269     get(array<_Tp, _Nm>& __arr) noexcept
00270     {
00271       static_assert(_Int < _Nm, "index is out of bounds");
00272       return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
00273         _S_ref(__arr._M_elems, _Int);
00274     }
00275 
00276   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00277     constexpr _Tp&&
00278     get(array<_Tp, _Nm>&& __arr) noexcept
00279     {
00280       static_assert(_Int < _Nm, "index is out of bounds");
00281       return std::move(__debug::get<_Int>(__arr));
00282     }
00283 
00284   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00285     constexpr const _Tp&
00286     get(const array<_Tp, _Nm>& __arr) noexcept
00287     {
00288       static_assert(_Int < _Nm, "index is out of bounds");
00289       return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
00290         _S_ref(__arr._M_elems, _Int);
00291     }
00292 } // namespace __debug
00293 
00294   // Tuple interface to class template array.
00295 
00296   /// tuple_size
00297   template<typename _Tp, std::size_t _Nm>
00298     struct tuple_size<__debug::array<_Tp, _Nm>>
00299     : public integral_constant<std::size_t, _Nm> { };
00300 
00301   /// tuple_element
00302   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00303     struct tuple_element<_Int, __debug::array<_Tp, _Nm>>
00304     {
00305       static_assert(_Int < _Nm, "index is out of bounds");
00306       typedef _Tp type;
00307     };
00308 } // namespace std
00309 
00310 #endif // _GLIBCXX_DEBUG_ARRAY