libstdc++
array
Go to the documentation of this file.
00001 // <array> -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file include/array
00027  *  This is a Standard C++ Library header.
00028  */
00029 
00030 #ifndef _GLIBCXX_ARRAY
00031 #define _GLIBCXX_ARRAY 1
00032 
00033 #pragma GCC system_header
00034 
00035 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00036 # include <bits/c++0x_warning.h>
00037 #else
00038 
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_VERSION
00046 
00047   /**
00048    *  @brief A standard container for storing a fixed size sequence of elements.
00049    *
00050    *  @ingroup sequences
00051    *
00052    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00053    *  <a href="tables.html#66">reversible container</a>, and a
00054    *  <a href="tables.html#67">sequence</a>.
00055    *
00056    *  Sets support random access iterators.
00057    *
00058    *  @param  Tp  Type of element. Required to be a complete type.
00059    *  @param  N  Number of elements.
00060   */
00061   template<typename _Tp, std::size_t _Nm>
00062     struct array
00063     {
00064       typedef _Tp                         value_type;
00065       typedef value_type*                 pointer;
00066       typedef const value_type*                       const_pointer;
00067       typedef value_type&                             reference;
00068       typedef const value_type&                       const_reference;
00069       typedef value_type*                     iterator;
00070       typedef const value_type*               const_iterator;
00071       typedef std::size_t                             size_type;
00072       typedef std::ptrdiff_t                          difference_type;
00073       typedef std::reverse_iterator<iterator>         reverse_iterator;
00074       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
00075 
00076       // Support for zero-sized arrays mandatory.
00077       value_type _M_instance[_Nm ? _Nm : 1];
00078 
00079       // No explicit construct/copy/destroy for aggregate type.
00080 
00081       // DR 776.
00082       void
00083       fill(const value_type& __u)
00084       { std::fill_n(begin(), size(), __u); }
00085 
00086       void
00087       swap(array& __other)
00088       noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
00089       { std::swap_ranges(begin(), end(), __other.begin()); }
00090 
00091       // Iterators.
00092       iterator
00093       begin() noexcept
00094       { return iterator(data()); }
00095 
00096       const_iterator
00097       begin() const noexcept
00098       { return const_iterator(data()); }
00099 
00100       iterator
00101       end() noexcept
00102       { return iterator(data() + _Nm); }
00103 
00104       const_iterator
00105       end() const noexcept
00106       { return const_iterator(data() + _Nm); }
00107 
00108       reverse_iterator 
00109       rbegin() noexcept
00110       { return reverse_iterator(end()); }
00111 
00112       const_reverse_iterator 
00113       rbegin() const noexcept
00114       { return const_reverse_iterator(end()); }
00115 
00116       reverse_iterator 
00117       rend() noexcept
00118       { return reverse_iterator(begin()); }
00119 
00120       const_reverse_iterator 
00121       rend() const noexcept
00122       { return const_reverse_iterator(begin()); }
00123 
00124       const_iterator
00125       cbegin() const noexcept
00126       { return const_iterator(std::__addressof(_M_instance[0])); }
00127 
00128       const_iterator
00129       cend() const noexcept
00130       { return const_iterator(std::__addressof(_M_instance[_Nm])); }
00131 
00132       const_reverse_iterator 
00133       crbegin() const noexcept
00134       { return const_reverse_iterator(end()); }
00135 
00136       const_reverse_iterator 
00137       crend() const noexcept
00138       { return const_reverse_iterator(begin()); }
00139 
00140       // Capacity.
00141       constexpr size_type 
00142       size() const noexcept { return _Nm; }
00143 
00144       constexpr size_type 
00145       max_size() const noexcept { return _Nm; }
00146 
00147       constexpr bool 
00148       empty() const noexcept { return size() == 0; }
00149 
00150       // Element access.
00151       reference
00152       operator[](size_type __n)
00153       { return _M_instance[__n]; }
00154 
00155       constexpr const_reference
00156       operator[](size_type __n) const noexcept
00157       { return _M_instance[__n]; }
00158 
00159       reference
00160       at(size_type __n)
00161       {
00162     if (__n >= _Nm)
00163       std::__throw_out_of_range(__N("array::at"));
00164     return _M_instance[__n];
00165       }
00166 
00167       constexpr const_reference
00168       at(size_type __n) const
00169       {
00170     // Result of conditional expression must be an lvalue so use
00171     // boolean ? lvalue : (throw-expr, lvalue)
00172     return __n < _Nm ? _M_instance[__n]
00173       : (std::__throw_out_of_range(__N("array::at")), _M_instance[0]);
00174       }
00175 
00176       reference 
00177       front()
00178       { return *begin(); }
00179 
00180       const_reference 
00181       front() const
00182       { return *begin(); }
00183 
00184       reference 
00185       back()
00186       { return _Nm ? *(end() - 1) : *end(); }
00187 
00188       const_reference 
00189       back() const
00190       { return _Nm ? *(end() - 1) : *end(); }
00191 
00192       pointer
00193       data() noexcept
00194       { return std::__addressof(_M_instance[0]); }
00195 
00196       const_pointer
00197       data() const noexcept
00198       { return std::__addressof(_M_instance[0]); }
00199     };
00200 
00201   // Array comparisons.
00202   template<typename _Tp, std::size_t _Nm>
00203     inline bool 
00204     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00205     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
00206 
00207   template<typename _Tp, std::size_t _Nm>
00208     inline bool
00209     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00210     { return !(__one == __two); }
00211 
00212   template<typename _Tp, std::size_t _Nm>
00213     inline bool
00214     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
00215     { 
00216       return std::lexicographical_compare(__a.begin(), __a.end(),
00217                       __b.begin(), __b.end()); 
00218     }
00219 
00220   template<typename _Tp, std::size_t _Nm>
00221     inline bool
00222     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00223     { return __two < __one; }
00224 
00225   template<typename _Tp, std::size_t _Nm>
00226     inline bool
00227     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00228     { return !(__one > __two); }
00229 
00230   template<typename _Tp, std::size_t _Nm>
00231     inline bool
00232     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00233     { return !(__one < __two); }
00234 
00235   // Specialized algorithms.
00236   template<typename _Tp, std::size_t _Nm>
00237     inline void
00238     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
00239     noexcept(noexcept(__one.swap(__two)))
00240     { __one.swap(__two); }
00241 
00242   // Tuple interface to class template array.
00243 
00244   /// tuple_size
00245   template<typename _Tp> 
00246     class tuple_size;
00247 
00248   template<typename _Tp, std::size_t _Nm>
00249     struct tuple_size<array<_Tp, _Nm>>
00250     : public integral_constant<std::size_t, _Nm> { };
00251 
00252   /// tuple_element
00253   template<std::size_t _Int, typename _Tp>
00254     class tuple_element;
00255 
00256   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00257     struct tuple_element<_Int, array<_Tp, _Nm> >
00258     { typedef _Tp type; };
00259 
00260   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00261     constexpr _Tp&
00262     get(array<_Tp, _Nm>& __arr) noexcept
00263     { return __arr._M_instance[_Int]; }
00264 
00265   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00266     constexpr _Tp&&
00267     get(array<_Tp, _Nm>&& __arr) noexcept
00268     { return std::move(get<_Int>(__arr)); }
00269 
00270   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00271     constexpr const _Tp&
00272     get(const array<_Tp, _Nm>& __arr) noexcept
00273     { return __arr._M_instance[_Int]; }
00274 
00275 _GLIBCXX_END_NAMESPACE_VERSION
00276 } // namespace
00277 
00278 #endif // __GXX_EXPERIMENTAL_CXX0X__
00279 
00280 #endif // _GLIBCXX_ARRAY