1// class template array -*- C++ -*-
2
3// Copyright (C) 2004-2016 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.
10
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.
15
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.
19
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/>.
24
25/** @file tr1/array
26 *  This is a TR1 C++ Library header.
27 */
28
29#ifndef _GLIBCXX_TR1_ARRAY
30#define _GLIBCXX_TR1_ARRAY 1
31
32#pragma GCC system_header
33
34#include <bits/stl_algobase.h>
35
36namespace std _GLIBCXX_VISIBILITY(default)
37{
38namespace tr1
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41
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;
68
69      // Support for zero-sized arrays mandatory.
70      value_type _M_instance[_Nm ? _Nm : 1];
71
72      // No explicit construct/copy/destroy for aggregate type.
73
74      void
75      assign(const value_type& __u)
76      { std::fill_n(begin(), size(), __u); }
77
78      void
79      swap(array& __other)
80      { std::swap_ranges(begin(), end(), __other.begin()); }
81
82      // Iterators.
83      iterator
84      begin()
85      { return iterator(std::__addressof(_M_instance[0])); }
86
87      const_iterator
88      begin() const
89      { return const_iterator(std::__addressof(_M_instance[0])); }
90
91      iterator
92      end()
93      { return iterator(std::__addressof(_M_instance[_Nm])); }
94
95      const_iterator
96      end() const
97      { return const_iterator(std::__addressof(_M_instance[_Nm])); }
98
99      reverse_iterator
100      rbegin()
101      { return reverse_iterator(end()); }
102
103      const_reverse_iterator
104      rbegin() const
105      { return const_reverse_iterator(end()); }
106
107      reverse_iterator
108      rend()
109      { return reverse_iterator(begin()); }
110
111      const_reverse_iterator
112      rend() const
113      { return const_reverse_iterator(begin()); }
114
115      // Capacity.
116      size_type
117      size() const { return _Nm; }
118
119      size_type
120      max_size() const { return _Nm; }
121
122      bool
123      empty() const { return size() == 0; }
124
125      // Element access.
126      reference
127      operator[](size_type __n)
128      { return _M_instance[__n]; }
129
130      const_reference
131      operator[](size_type __n) const
132      { return _M_instance[__n]; }
133
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      }
141
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      }
149
150      reference
151      front()
152      { return *begin(); }
153
154      const_reference
155      front() const
156      { return *begin(); }
157
158      reference
159      back()
160      { return _Nm ? *(end() - 1) : *end(); }
161
162      const_reference
163      back() const
164      { return _Nm ? *(end() - 1) : *end(); }
165
166      _Tp*
167      data()
168      { return std::__addressof(_M_instance[0]); }
169
170      const _Tp*
171      data() const
172      { return std::__addressof(_M_instance[0]); }
173    };
174
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()); }
180
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); }
185
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    }
193
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; }
198
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); }
203
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); }
208
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); }
214
215  // Tuple interface to class template array [6.2.2.5].
216
217  /// tuple_size
218  template<typename _Tp>
219    class tuple_size;
220
221  /// tuple_element
222  template<int _Int, typename _Tp>
223    class tuple_element;
224
225  template<typename _Tp, std::size_t _Nm>
226    struct tuple_size<array<_Tp, _Nm> >
227    { static const int value = _Nm; };
228
229  template<typename _Tp, std::size_t _Nm>
230    const int
231    tuple_size<array<_Tp, _Nm> >::value;
232
233  template<int _Int, typename _Tp, std::size_t _Nm>
234    struct tuple_element<_Int, array<_Tp, _Nm> >
235    { typedef _Tp type; };
236
237  template<int _Int, typename _Tp, std::size_t _Nm>
238    inline _Tp&
239    get(array<_Tp, _Nm>& __arr)
240    { return __arr[_Int]; }
241
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]; }
246
247_GLIBCXX_END_NAMESPACE_VERSION
248}
249}
250
251#endif // _GLIBCXX_TR1_ARRAY
252