# tr1_impl/array

Go to the documentation of this file.

    00001 // class template array -*- C++ -*-
    00002
    00003 // Copyright (C) 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /** @file tr1_impl/array
    00031  *  This is an internal header file, included by other library headers.
    00032  *  You should not attempt to use it directly.
    00033  */
    00034
    00035 namespace std
    00036 {
    00037 _GLIBCXX_BEGIN_NAMESPACE_TR1
    00038
    00039   /// array.
    00040   /// NB: Requires complete type _Tp.
    00041   template<typename _Tp, std::size_t _Nm>
    00042     struct array
    00043     {
    00044       typedef _Tp                         value_type;
    00045       typedef value_type&                             reference;
    00046       typedef const value_type&                       const_reference;
    00047       typedef value_type*                     iterator;
    00048       typedef const value_type*               const_iterator;
    00049       typedef std::size_t                             size_type;
    00050       typedef std::ptrdiff_t                          difference_type;
    00051       typedef std::reverse_iterator<iterator>         reverse_iterator;
    00052       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
    00053
    00054       // Support for zero-sized arrays mandatory.
    00055       value_type _M_instance[_Nm ? _Nm : 1];
    00056
    00057       // No explicit construct/copy/destroy for aggregate type.
    00058
    00059       void
    00060       assign(const value_type& __u)
    00061       { std::fill_n(begin(), size(), __u); }
    00062
    00063       void
    00064       swap(array& __other)
    00065       { std::swap_ranges(begin(), end(), __other.begin()); }
    00066
    00067       // Iterators.
    00068       iterator
    00069       begin()
    00070       { return iterator(&_M_instance[0]); }
    00071
    00072       const_iterator
    00073       begin() const
    00074       { return const_iterator(&_M_instance[0]); }
    00075
    00076       iterator
    00077       end()
    00078       { return iterator(&_M_instance[_Nm]); }
    00079
    00080       const_iterator
    00081       end() const
    00082       { return const_iterator(&_M_instance[_Nm]); }
    00083
    00084       reverse_iterator
    00085       rbegin()
    00086       { return reverse_iterator(end()); }
    00087
    00088       const_reverse_iterator
    00089       rbegin() const
    00090       { return const_reverse_iterator(end()); }
    00091
    00092       reverse_iterator
    00093       rend()
    00094       { return reverse_iterator(begin()); }
    00095
    00096       const_reverse_iterator
    00097       rend() const
    00098       { return const_reverse_iterator(begin()); }
    00099
    00100 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
    00101       const_iterator
    00102       cbegin() const
    00103       { return const_iterator(&_M_instance[0]); }
    00104
    00105       const_iterator
    00106       cend() const
    00107       { return const_iterator(&_M_instance[_Nm]); }
    00108
    00109       const_reverse_iterator
    00110       crbegin() const
    00111       { return const_reverse_iterator(end()); }
    00112
    00113       const_reverse_iterator
    00114       crend() const
    00115       { return const_reverse_iterator(begin()); }
    00116 #endif
    00117
    00118       // Capacity.
    00119       size_type
    00120       size() const { return _Nm; }
    00121
    00122       size_type
    00123       max_size() const { return _Nm; }
    00124
    00125       bool
    00126       empty() const { return size() == 0; }
    00127
    00128       // Element access.
    00129       reference
    00130       operator[](size_type __n)
    00131       { return _M_instance[__n]; }
    00132
    00133       const_reference
    00134       operator[](size_type __n) const
    00135       { return _M_instance[__n]; }
    00136
    00137       reference
    00138       at(size_type __n)
    00139       {
    00140     if (__builtin_expect(__n >= _Nm, false))
    00141       std::__throw_out_of_range(__N("array::at"));
    00142     return _M_instance[__n];
    00143       }
    00144
    00145       const_reference
    00146       at(size_type __n) const
    00147       {
    00148     if (__builtin_expect(__n >= _Nm, false))
    00149       std::__throw_out_of_range(__N("array::at"));
    00150     return _M_instance[__n];
    00151       }
    00152
    00153       reference
    00154       front()
    00155       { return *begin(); }
    00156
    00157       const_reference
    00158       front() const
    00159       { return *begin(); }
    00160
    00161       reference
    00162       back()
    00163       { return _Nm ? *(end() - 1) : *end(); }
    00164
    00165       const_reference
    00166       back() const
    00167       { return _Nm ? *(end() - 1) : *end(); }
    00168
    00169       _Tp*
    00170       data()
    00171       { return &_M_instance[0]; }
    00172
    00173       const _Tp*
    00174       data() const
    00175       { return &_M_instance[0]; }
    00176     };
    00177
    00178   // Array comparisons.
    00179   template<typename _Tp, std::size_t _Nm>
    00180     inline bool
    00181     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00182     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
    00183
    00184   template<typename _Tp, std::size_t _Nm>
    00185     inline bool
    00186     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00187     { return !(__one == __two); }
    00188
    00189   template<typename _Tp, std::size_t _Nm>
    00190     inline bool
    00191     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
    00192     {
    00193       return std::lexicographical_compare(__a.begin(), __a.end(),
    00194                       __b.begin(), __b.end());
    00195     }
    00196
    00197   template<typename _Tp, std::size_t _Nm>
    00198     inline bool
    00199     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00200     { return __two < __one; }
    00201
    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 !(__one > __two); }
    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   // Specialized algorithms [6.2.2.2].
    00213   template<typename _Tp, std::size_t _Nm>
    00214     inline void
    00215     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
    00216     { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
    00217
    00218   // Tuple interface to class template array [6.2.2.5].
    00219
    00220   /// tuple_size
    00221   template<typename _Tp>
    00222     class tuple_size;
    00223
    00224   /// tuple_element
    00225   template<int _Int, typename _Tp>
    00226     class tuple_element;
    00227
    00228   template<typename _Tp, std::size_t _Nm>
    00229     struct tuple_size<array<_Tp, _Nm> >
    00230     { static const int value = _Nm; };
    00231
    00232   template<typename _Tp, std::size_t _Nm>
    00233     const int tuple_size<array<_Tp, _Nm> >::value;
    00234
    00235   template<int _Int, typename _Tp, std::size_t _Nm>
    00236     struct tuple_element<_Int, array<_Tp, _Nm> >
    00237     { typedef _Tp type; };
    00238
    00239   template<int _Int, typename _Tp, std::size_t _Nm>
    00240     inline _Tp&
    00241     get(array<_Tp, _Nm>& __arr)
    00242     { return __arr[_Int]; }
    00243
    00244   template<int _Int, typename _Tp, std::size_t _Nm>
    00245     inline const _Tp&
    00246     get(const array<_Tp, _Nm>& __arr)
    00247     { return __arr[_Int]; }
    00248
    00249 _GLIBCXX_END_NAMESPACE_TR1
    00250 }