Merged revisions 195034,195219,195245,195357,195374,195428,195599,195673,195809 via...
[official-gcc.git] / main / libstdc++-v3 / include / tr1 / array
blob99be41dd862f0bc4c1f0ccf62028667d86caa63e
1 // class template array -*- C++ -*-
3 // Copyright (C) 2004-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file tr1/array
26  *  This is a TR1 C++ Library header. 
27  */
29 #ifndef _GLIBCXX_TR1_ARRAY
30 #define _GLIBCXX_TR1_ARRAY 1
32 #pragma GCC system_header
34 #include <bits/stl_algobase.h>
36 namespace std _GLIBCXX_VISIBILITY(default)
38 namespace tr1
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42   /**
43    *  @brief A standard container for storing a fixed size sequence of elements.
44    *
45    *  @ingroup sequences
46    *
47    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
48    *  <a href="tables.html#66">reversible container</a>, and a
49    *  <a href="tables.html#67">sequence</a>.
50    *
51    *  Sets support random access iterators.
52    *
53    *  @param  Tp  Type of element. Required to be a complete type.
54    *  @param  N  Number of elements.
55   */
56   template<typename _Tp, std::size_t _Nm>
57     struct array
58     {
59       typedef _Tp                                     value_type;
60       typedef value_type&                             reference;
61       typedef const value_type&                       const_reference;
62       typedef value_type*                             iterator;
63       typedef const value_type*                       const_iterator;
64       typedef std::size_t                             size_type;
65       typedef std::ptrdiff_t                          difference_type;
66       typedef std::reverse_iterator<iterator>         reverse_iterator;
67       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
69       // Support for zero-sized arrays mandatory.
70       value_type _M_instance[_Nm ? _Nm : 1];
72       // No explicit construct/copy/destroy for aggregate type.
74       void
75       assign(const value_type& __u)
76       { std::fill_n(begin(), size(), __u); }
78       void
79       swap(array& __other)
80       { std::swap_ranges(begin(), end(), __other.begin()); }
82       // Iterators.
83       iterator
84       begin()
85       { return iterator(std::__addressof(_M_instance[0])); }
87       const_iterator
88       begin() const 
89       { return const_iterator(std::__addressof(_M_instance[0])); }
91       iterator
92       end()
93       { return iterator(std::__addressof(_M_instance[_Nm])); }
95       const_iterator
96       end() const
97       { return const_iterator(std::__addressof(_M_instance[_Nm])); }
99       reverse_iterator 
100       rbegin()
101       { return reverse_iterator(end()); }
103       const_reverse_iterator 
104       rbegin() const
105       { return const_reverse_iterator(end()); }
107       reverse_iterator 
108       rend()
109       { return reverse_iterator(begin()); }
111       const_reverse_iterator 
112       rend() const
113       { return const_reverse_iterator(begin()); }
115       // Capacity.
116       size_type 
117       size() const { return _Nm; }
119       size_type 
120       max_size() const { return _Nm; }
122       bool 
123       empty() const { return size() == 0; }
125       // Element access.
126       reference
127       operator[](size_type __n)
128       { return _M_instance[__n]; }
130       const_reference
131       operator[](size_type __n) const
132       { return _M_instance[__n]; }
134       reference
135       at(size_type __n)
136       {
137         if (__n >= _Nm)
138           std::__throw_out_of_range(__N("array::at"));
139         return _M_instance[__n];
140       }
142       const_reference
143       at(size_type __n) const
144       {
145         if (__n >= _Nm)
146           std::__throw_out_of_range(__N("array::at"));
147         return _M_instance[__n];
148       }
150       reference 
151       front()
152       { return *begin(); }
154       const_reference 
155       front() const
156       { return *begin(); }
158       reference 
159       back()
160       { return _Nm ? *(end() - 1) : *end(); }
162       const_reference 
163       back() const
164       { return _Nm ? *(end() - 1) : *end(); }
166       _Tp*
167       data()
168       { return std::__addressof(_M_instance[0]); }
170       const _Tp*
171       data() const
172       { return std::__addressof(_M_instance[0]); }
173     };
175   // Array comparisons.
176   template<typename _Tp, std::size_t _Nm>
177     inline bool 
178     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
179     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
181   template<typename _Tp, std::size_t _Nm>
182     inline bool
183     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
184     { return !(__one == __two); }
186   template<typename _Tp, std::size_t _Nm>
187     inline bool
188     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
189     { 
190       return std::lexicographical_compare(__a.begin(), __a.end(),
191                                           __b.begin(), __b.end()); 
192     }
194   template<typename _Tp, std::size_t _Nm>
195     inline bool
196     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
197     { return __two < __one; }
199   template<typename _Tp, std::size_t _Nm>
200     inline bool
201     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
202     { return !(__one > __two); }
204   template<typename _Tp, std::size_t _Nm>
205     inline bool
206     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
207     { return !(__one < __two); }
209   // Specialized algorithms [6.2.2.2].
210   template<typename _Tp, std::size_t _Nm>
211     inline void
212     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
213     { __one.swap(__two); }
215   // Tuple interface to class template array [6.2.2.5].
217   /// tuple_size
218   template<typename _Tp> 
219     class tuple_size;
221   /// tuple_element
222   template<int _Int, typename _Tp>
223     class tuple_element;
225   template<typename _Tp, std::size_t _Nm>
226     struct tuple_size<array<_Tp, _Nm> >
227     { static const int value = _Nm; };
229   template<typename _Tp, std::size_t _Nm>
230     const int
231     tuple_size<array<_Tp, _Nm> >::value;  
233   template<int _Int, typename _Tp, std::size_t _Nm>
234     struct tuple_element<_Int, array<_Tp, _Nm> >
235     { typedef _Tp type; };
237   template<int _Int, typename _Tp, std::size_t _Nm>
238     inline _Tp&
239     get(array<_Tp, _Nm>& __arr)
240     { return __arr[_Int]; }
242   template<int _Int, typename _Tp, std::size_t _Nm>
243     inline const _Tp&
244     get(const array<_Tp, _Nm>& __arr)
245     { return __arr[_Int]; }
247 _GLIBCXX_END_NAMESPACE_VERSION
251 #endif // _GLIBCXX_TR1_ARRAY