# tr1_impl/array

Go to the documentation of this file.

    00001 // class template array -*- C++ -*-
    00002
    00003 // Copyright (C) 2007, 2008, 2009, 2010 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 tr1_impl/array
    00026  *  This is an internal header file, included by other library headers.
    00027  *  You should not attempt to use it directly.
    00028  */
    00029
    00030 namespace std
    00031 {
    00032 _GLIBCXX_BEGIN_NAMESPACE_TR1
    00033
    00034   /**
    00035    *  @brief A standard container for storing a fixed size sequence of elements.
    00036    *
    00037    *  @ingroup sequences
    00038    *
    00039    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
    00040    *  <a href="tables.html#66">reversible container</a>, and a
    00041    *  <a href="tables.html#67">sequence</a>.
    00042    *
    00043    *  Sets support random access iterators.
    00044    *
    00045    *  @param  Tp  Type of element. Required to be a complete type.
    00046    *  @param  N  Number of elements.
    00047   */
    00048   template<typename _Tp, std::size_t _Nm>
    00049     struct array
    00050     {
    00051       typedef _Tp                         value_type;
    00052 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00053       typedef _Tp*                                    pointer;
    00054       typedef const _Tp*                              const_pointer;
    00055 #endif
    00056       typedef value_type&                             reference;
    00057       typedef const value_type&                       const_reference;
    00058       typedef value_type*                     iterator;
    00059       typedef const value_type*               const_iterator;
    00060       typedef std::size_t                             size_type;
    00061       typedef std::ptrdiff_t                          difference_type;
    00062       typedef std::reverse_iterator<iterator>         reverse_iterator;
    00063       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
    00064
    00065       // Support for zero-sized arrays mandatory.
    00066       value_type _M_instance[_Nm ? _Nm : 1];
    00067
    00068       // No explicit construct/copy/destroy for aggregate type.
    00069
    00070       void
    00071 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00072       // DR 776.
    00073       fill(const value_type& __u)
    00074 #else
    00075       assign(const value_type& __u)
    00076 #endif
    00077       { std::fill_n(begin(), size(), __u); }
    00078
    00079       void
    00080       swap(array& __other)
    00081       { std::swap_ranges(begin(), end(), __other.begin()); }
    00082
    00083       // Iterators.
    00084       iterator
    00085       begin()
    00086       { return iterator(&_M_instance[0]); }
    00087
    00088       const_iterator
    00089       begin() const
    00090       { return const_iterator(&_M_instance[0]); }
    00091
    00092       iterator
    00093       end()
    00094       { return iterator(&_M_instance[_Nm]); }
    00095
    00096       const_iterator
    00097       end() const
    00098       { return const_iterator(&_M_instance[_Nm]); }
    00099
    00100       reverse_iterator
    00101       rbegin()
    00102       { return reverse_iterator(end()); }
    00103
    00104       const_reverse_iterator
    00105       rbegin() const
    00106       { return const_reverse_iterator(end()); }
    00107
    00108       reverse_iterator
    00109       rend()
    00110       { return reverse_iterator(begin()); }
    00111
    00112       const_reverse_iterator
    00113       rend() const
    00114       { return const_reverse_iterator(begin()); }
    00115
    00116 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00117       const_iterator
    00118       cbegin() const
    00119       { return const_iterator(&_M_instance[0]); }
    00120
    00121       const_iterator
    00122       cend() const
    00123       { return const_iterator(&_M_instance[_Nm]); }
    00124
    00125       const_reverse_iterator
    00126       crbegin() const
    00127       { return const_reverse_iterator(end()); }
    00128
    00129       const_reverse_iterator
    00130       crend() const
    00131       { return const_reverse_iterator(begin()); }
    00132 #endif
    00133
    00134       // Capacity.
    00135       size_type
    00136       size() const { return _Nm; }
    00137
    00138       size_type
    00139       max_size() const { return _Nm; }
    00140
    00141       bool
    00142       empty() const { return size() == 0; }
    00143
    00144       // Element access.
    00145       reference
    00146       operator[](size_type __n)
    00147       { return _M_instance[__n]; }
    00148
    00149       const_reference
    00150       operator[](size_type __n) const
    00151       { return _M_instance[__n]; }
    00152
    00153       reference
    00154       at(size_type __n)
    00155       {
    00156     if (__n >= _Nm)
    00157       std::__throw_out_of_range(__N("array::at"));
    00158     return _M_instance[__n];
    00159       }
    00160
    00161       const_reference
    00162       at(size_type __n) const
    00163       {
    00164     if (__n >= _Nm)
    00165       std::__throw_out_of_range(__N("array::at"));
    00166     return _M_instance[__n];
    00167       }
    00168
    00169       reference
    00170       front()
    00171       { return *begin(); }
    00172
    00173       const_reference
    00174       front() const
    00175       { return *begin(); }
    00176
    00177       reference
    00178       back()
    00179       { return _Nm ? *(end() - 1) : *end(); }
    00180
    00181       const_reference
    00182       back() const
    00183       { return _Nm ? *(end() - 1) : *end(); }
    00184
    00185       _Tp*
    00186       data()
    00187       { return &_M_instance[0]; }
    00188
    00189       const _Tp*
    00190       data() const
    00191       { return &_M_instance[0]; }
    00192     };
    00193
    00194   // Array comparisons.
    00195   template<typename _Tp, std::size_t _Nm>
    00196     inline bool
    00197     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00198     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
    00199
    00200   template<typename _Tp, std::size_t _Nm>
    00201     inline bool
    00202     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00203     { return !(__one == __two); }
    00204
    00205   template<typename _Tp, std::size_t _Nm>
    00206     inline bool
    00207     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
    00208     {
    00209       return std::lexicographical_compare(__a.begin(), __a.end(),
    00210                       __b.begin(), __b.end());
    00211     }
    00212
    00213   template<typename _Tp, std::size_t _Nm>
    00214     inline bool
    00215     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00216     { return __two < __one; }
    00217
    00218   template<typename _Tp, std::size_t _Nm>
    00219     inline bool
    00220     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00221     { return !(__one > __two); }
    00222
    00223   template<typename _Tp, std::size_t _Nm>
    00224     inline bool
    00225     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00226     { return !(__one < __two); }
    00227
    00228   // Specialized algorithms [6.2.2.2].
    00229   template<typename _Tp, std::size_t _Nm>
    00230     inline void
    00231     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
    00232     { __one.swap(__two); }
    00233
    00234   // Tuple interface to class template array [6.2.2.5].
    00235
    00236   /// tuple_size
    00237   template<typename _Tp>
    00238     class tuple_size;
    00239
    00240   /// tuple_element
    00241 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00242   template<std::size_t _Int, typename _Tp>
    00243 #else
    00244   template<int _Int, typename _Tp>
    00245 #endif
    00246     class tuple_element;
    00247
    00248   template<typename _Tp, std::size_t _Nm>
    00249     struct tuple_size<array<_Tp, _Nm> >
    00250 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00251     { static const std::size_t value = _Nm; };
    00252 #else
    00253     { static const int value = _Nm; };
    00254 #endif
    00255
    00256   template<typename _Tp, std::size_t _Nm>
    00257 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00258     const std::size_t
    00259 #else
    00260     const int
    00261 #endif
    00262     tuple_size<array<_Tp, _Nm> >::value;
    00263
    00264 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00265   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
    00266 #else
    00267   template<int _Int, typename _Tp, std::size_t _Nm>
    00268 #endif
    00269     struct tuple_element<_Int, array<_Tp, _Nm> >
    00270     { typedef _Tp type; };
    00271
    00272 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00273   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
    00274 #else
    00275   template<int _Int, typename _Tp, std::size_t _Nm>
    00276 #endif
    00277     inline _Tp&
    00278     get(array<_Tp, _Nm>& __arr)
    00279     { return __arr[_Int]; }
    00280
    00281 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00282   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
    00283 #else
    00284   template<int _Int, typename _Tp, std::size_t _Nm>
    00285 #endif
    00286     inline const _Tp&
    00287     get(const array<_Tp, _Nm>& __arr)
    00288     { return __arr[_Int]; }
    00289
    00290 _GLIBCXX_END_NAMESPACE_TR1
    00291 }