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 #ifdef __EXCEPTIONS
00168       constexpr const_reference
00169       at(size_type __n) const
00170       {
00171     return __n < _Nm ? 
00172            _M_instance[__n] : throw out_of_range(__N("array::at"));
00173       }
00174 #else
00175       const_reference
00176       at(size_type __n) const
00177       {
00178     if (__n >= _Nm)
00179       std::__throw_out_of_range(__N("array::at"));
00180     return _M_instance[__n];
00181       }
00182 #endif
00183 
00184       reference 
00185       front()
00186       { return *begin(); }
00187 
00188       const_reference 
00189       front() const
00190       { return *begin(); }
00191 
00192       reference 
00193       back()
00194       { return _Nm ? *(end() - 1) : *end(); }
00195 
00196       const_reference 
00197       back() const
00198       { return _Nm ? *(end() - 1) : *end(); }
00199 
00200       pointer
00201       data() noexcept
00202       { return std::__addressof(_M_instance[0]); }
00203 
00204       const_pointer
00205       data() const noexcept
00206       { return std::__addressof(_M_instance[0]); }
00207     };
00208 
00209   // Array comparisons.
00210   template<typename _Tp, std::size_t _Nm>
00211     inline bool 
00212     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00213     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
00214 
00215   template<typename _Tp, std::size_t _Nm>
00216     inline bool
00217     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00218     { return !(__one == __two); }
00219 
00220   template<typename _Tp, std::size_t _Nm>
00221     inline bool
00222     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
00223     { 
00224       return std::lexicographical_compare(__a.begin(), __a.end(),
00225                       __b.begin(), __b.end()); 
00226     }
00227 
00228   template<typename _Tp, std::size_t _Nm>
00229     inline bool
00230     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00231     { return __two < __one; }
00232 
00233   template<typename _Tp, std::size_t _Nm>
00234     inline bool
00235     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00236     { return !(__one > __two); }
00237 
00238   template<typename _Tp, std::size_t _Nm>
00239     inline bool
00240     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00241     { return !(__one < __two); }
00242 
00243   // Specialized algorithms.
00244   template<typename _Tp, std::size_t _Nm>
00245     inline void
00246     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
00247     noexcept(noexcept(__one.swap(__two)))
00248     { __one.swap(__two); }
00249 
00250   // Tuple interface to class template array.
00251 
00252   /// tuple_size
00253   template<typename _Tp> 
00254     class tuple_size;
00255 
00256   template<typename _Tp, std::size_t _Nm>
00257     struct tuple_size<array<_Tp, _Nm>>
00258     : public integral_constant<std::size_t, _Nm> { };
00259 
00260   /// tuple_element
00261   template<std::size_t _Int, typename _Tp>
00262     class tuple_element;
00263 
00264   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00265     struct tuple_element<_Int, array<_Tp, _Nm> >
00266     { typedef _Tp type; };
00267 
00268   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00269     constexpr _Tp&
00270     get(array<_Tp, _Nm>& __arr) noexcept
00271     { return __arr._M_instance[_Int]; }
00272 
00273   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00274     constexpr _Tp&&
00275     get(array<_Tp, _Nm>&& __arr) noexcept
00276     { return std::move(get<_Int>(__arr)); }
00277 
00278   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
00279     constexpr const _Tp&
00280     get(const array<_Tp, _Nm>& __arr) noexcept
00281     { return __arr._M_instance[_Int]; }
00282 
00283 _GLIBCXX_END_NAMESPACE_VERSION
00284 } // namespace
00285 
00286 #endif // __GXX_EXPERIMENTAL_CXX0X__
00287 
00288 #endif // _GLIBCXX_ARRAY