array

Go to the documentation of this file.
00001 // class template array -*- C++ -*-
00002 
00003 // Copyright (C) 2004, 2005, 2006 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 
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 <cstddef>
00041 #include <bits/functexcept.h>
00042 
00043 //namespace std::tr1
00044 namespace std
00045 {
00046 namespace tr1
00047 {
00048   /// @brief  struct array [6.2.2].
00049   /// NB: Requires complete type _Tp.
00050   template<typename _Tp, std::size_t _Nm = 1>
00051     struct array
00052     {
00053       typedef _Tp                         value_type;
00054       typedef value_type&                             reference;
00055       typedef const value_type&                       const_reference;
00056       typedef value_type*                     iterator;
00057       typedef const value_type*               const_iterator;
00058       typedef std::size_t                             size_type;
00059       typedef std::ptrdiff_t                          difference_type;
00060       typedef std::reverse_iterator<iterator>         reverse_iterator;
00061       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
00062 
00063       // Compile time constant without other dependencies.
00064       enum { _S_index = _Nm };
00065 
00066       // Support for zero-sized arrays mandatory.
00067       value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__));
00068 
00069       // No explicit construct/copy/destroy for aggregate type.
00070 
00071       void 
00072       assign(const value_type& __u)
00073       { std::fill_n(begin(), size(), __u); }
00074 
00075       void 
00076       swap(array& __other)
00077       { std::swap_ranges(begin(), end(), __other.begin()); }
00078 
00079       // Iterators.
00080       iterator
00081       begin()
00082       { return iterator(&_M_instance[0]); }
00083 
00084       const_iterator
00085       begin() const 
00086       { return const_iterator(&_M_instance[0]); }
00087 
00088       iterator
00089       end() 
00090       { return iterator(&_M_instance[_Nm]); }
00091 
00092       const_iterator
00093       end() const
00094       { return const_iterator(&_M_instance[_Nm]); }
00095 
00096       reverse_iterator 
00097       rbegin()
00098       { return reverse_iterator(end()); }
00099 
00100       const_reverse_iterator 
00101       rbegin() const
00102       { return const_reverse_iterator(end()); }
00103 
00104       reverse_iterator 
00105       rend()
00106       { return reverse_iterator(begin()); }
00107 
00108       const_reverse_iterator 
00109       rend() const
00110       { return const_reverse_iterator(begin()); }
00111 
00112       // Capacity.
00113       size_type 
00114       size() const { return _Nm; }
00115 
00116       size_type 
00117       max_size() const { return _Nm; }
00118 
00119       bool 
00120       empty() const { return size() == 0; }
00121 
00122       // Element access.
00123       reference 
00124       operator[](size_type __n)
00125       { return _M_instance[__n]; }
00126 
00127       const_reference 
00128       operator[](size_type __n) const
00129       { return _M_instance[__n]; }
00130 
00131       const_reference 
00132       at(size_type __n) const
00133       { 
00134     if (__builtin_expect(__n > _Nm, false))
00135       std::__throw_out_of_range("array::at");
00136     return _M_instance[__n]; 
00137       }
00138 
00139       reference 
00140       at(size_type __n)
00141       { 
00142     if (__builtin_expect(__n > _Nm, false))
00143       std::__throw_out_of_range("array::at");
00144     return _M_instance[__n]; 
00145       }
00146 
00147       reference 
00148       front()
00149       { return *begin(); }
00150 
00151       const_reference 
00152       front() const
00153       { return *begin(); }
00154 
00155       reference 
00156       back()
00157       { return *(end() - 1); }
00158 
00159       const_reference 
00160       back() const
00161       { return *(end() - 1); }
00162 
00163       _Tp* 
00164       data()
00165       { return &_M_instance[0]; }
00166 
00167       const _Tp* 
00168       data() const
00169       { return &_M_instance[0]; }
00170     };
00171 
00172   // Array comparisons.
00173   template<typename _Tp, std::size_t _Nm>
00174     inline bool 
00175     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00176     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
00177 
00178   template<typename _Tp, std::size_t _Nm>
00179     inline bool
00180     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00181     { return !(__one == __two); }
00182 
00183   template<typename _Tp, std::size_t _Nm>
00184     inline bool
00185     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
00186     { 
00187       return std::lexicographical_compare(__a.begin(), __a.end(),
00188                       __b.begin(), __b.end()); 
00189     }
00190 
00191   template<typename _Tp, std::size_t _Nm>
00192     inline bool
00193     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00194     { return __two < __one; }
00195 
00196   template<typename _Tp, std::size_t _Nm>
00197     inline bool
00198     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00199     { return !(__one > __two); }
00200 
00201   template<typename _Tp, std::size_t _Nm>
00202     inline bool
00203     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
00204     { return !(__one < __two); }
00205 
00206   // Specialized algorithms [6.2.2.2].
00207   template<typename _Tp, std::size_t _Nm>
00208     inline void
00209     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
00210     { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
00211 
00212   // Tuple interface to class template array [6.2.2.5].
00213   template<typename _Tp> class tuple_size;
00214   template<int _Int, typename _Tp> class tuple_element;
00215   
00216   template<typename _Tp, std::size_t _Nm>
00217     struct tuple_size<array<_Tp, _Nm> >
00218     { static const int value = _Nm; };
00219  
00220   template<int _Int, typename _Tp, std::size_t _Nm>
00221     struct tuple_element<_Int, array<_Tp, _Nm> >
00222     { typedef _Tp type; };
00223 
00224   template<int _Int, typename _Tp, std::size_t _Nm>
00225     inline _Tp&
00226     get(array<_Tp, _Nm>& __arr)
00227     { return __arr[_Int]; }
00228 
00229   template<int _Int, typename _Tp, std::size_t _Nm>
00230     inline const _Tp&
00231     get(const array<_Tp, _Nm>& __arr)
00232     { return __arr[_Int]; }
00233 } // namespace std::tr1
00234 }
00235 
00236 #endif

Generated on Sun Oct 8 18:16:38 2006 for libstdc++ by  doxygen 1.4.7