# array

Go to the documentation of this file.

    00001 // class template array -*- C++ -*-
    00002
    00003 // Copyright (C) 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    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
    00031  *  This is a TR1 C++ Library header.
    00032  */
    00033
    00034 #ifndef _ARRAY
    00035 #define _ARRAY 1
    00036
    00037 #include <new>
    00038 #include <iterator>
    00039 #include <algorithm>
    00040 #include <bits/functexcept.h>
    00041
    00042 //namespace std::tr1
    00043 namespace std
    00044 {
    00045 namespace tr1
    00046 {
    00047   /// @brief  struct array [6.2.2].
    00048   /// NB: Requires complete type _Tp.
    00049   template<typename _Tp, size_t _Nm = 1>
    00050     struct array
    00051     {
    00052       typedef _Tp                   value_type;
    00053       typedef value_type&                       reference;
    00054       typedef const value_type&                 const_reference;
    00055       typedef value_type*               iterator;
    00056       typedef const value_type*         const_iterator;
    00057       typedef size_t                        size_type;
    00058       typedef ptrdiff_t                         difference_type;
    00059       typedef std::reverse_iterator<iterator>   reverse_iterator;
    00060       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    00061
    00062       // Compile time constant without other dependencies.
    00063       enum { _S_index = _Nm };
    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       assign(const value_type& u);
    00072
    00073       void
    00074       swap(array&);
    00075
    00076       // Iterators.
    00077       iterator
    00078       begin()
    00079       { return reinterpret_cast<iterator>(&_M_instance[0]); }
    00080
    00081       const_iterator
    00082       begin() const
    00083       { return reinterpret_cast<const_iterator>(&_M_instance[0]); }
    00084
    00085       iterator
    00086       end()
    00087       { return reinterpret_cast<iterator>(&_M_instance[_Nm]); }
    00088
    00089       const_iterator
    00090       end() const
    00091       { return reinterpret_cast<const_iterator>(&_M_instance[_Nm]); }
    00092
    00093       reverse_iterator
    00094       rbegin()
    00095       { return reverse_iterator(this->end()); }
    00096
    00097       const_reverse_iterator
    00098       rbegin() const
    00099       { return const_reverse_iterator(this->end()); }
    00100
    00101       reverse_iterator
    00102       rend()
    00103       { return reverse_iterator(this->begin()); }
    00104
    00105       const_reverse_iterator
    00106       rend() const
    00107       { return const_reverse_iterator(this->begin()); }
    00108
    00109       // Capacity.
    00110       size_type
    00111       size() const { return _Nm; }
    00112
    00113       size_type
    00114       max_size() const { return _Nm; }
    00115
    00116       bool
    00117       empty() const { return size() == 0; }
    00118
    00119       // Element access.
    00120       reference
    00121       operator[](size_type __n)
    00122       { return reinterpret_cast<reference>(_M_instance[__n]); }
    00123
    00124       const_reference
    00125       operator[](size_type __n) const
    00126       { return reinterpret_cast<const_reference>(_M_instance[__n]); }
    00127
    00128       const_reference
    00129       at(size_type __n) const
    00130       {
    00131     if (__builtin_expect(__n > _Nm, false))
    00132       std::__throw_out_of_range("array::at");
    00133     return reinterpret_cast<const_reference>(_M_instance[__n]);
    00134       }
    00135
    00136       reference
    00137       at(size_type __n)
    00138       {
    00139     if (__builtin_expect(__n > _Nm, false))
    00140       std::__throw_out_of_range("array::at");
    00141     return reinterpret_cast<reference>(_M_instance[__n]);
    00142       }
    00143
    00144       reference
    00145       front();
    00146
    00147       const_reference
    00148       front() const;
    00149
    00150       reference
    00151       back();
    00152
    00153       const_reference
    00154       back() const;
    00155
    00156       _Tp*
    00157       data();
    00158
    00159       const _Tp*
    00160       data() const;
    00161     };
    00162
    00163   // Array comparisons.
    00164  template<typename _Tp, size_t _Nm>
    00165    bool
    00166    operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00167    { return std::equal(__one.begin(), __one.end(), __two.begin()); }
    00168
    00169  template<typename _Tp, size_t _Nm>
    00170    bool
    00171    operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00172    { return !(__one == __two); }
    00173
    00174  template<typename _Tp, size_t _Nm>
    00175    bool
    00176    operator<(const array<_Tp, _Nm>& a, const array<_Tp, _Nm>& b)
    00177    {
    00178      return std::lexicographical_compare(a.begin(), a.end(),
    00179                      b.begin(), b.end());
    00180    }
    00181
    00182  template<typename _Tp, size_t _Nm>
    00183    bool
    00184    operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00185    { return __two < __one; }
    00186
    00187  template<typename _Tp, size_t _Nm>
    00188    bool
    00189    operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00190    { return !(__one > __two); }
    00191
    00192  template<typename _Tp, size_t _Nm>
    00193    bool
    00194    operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
    00195    { return !(__one < __two); }
    00196
    00197   // Specialized algorithms [6.2.2.2].
    00198  template<typename _Tp, size_t _Nm>
    00199    void
    00200    swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
    00201    { swap_ranges(__one.begin(), __one.end(), __two.begin()); }
    00202 } // namespace std::tr1
    00203 }
    00204
    00205 #endif