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