summaryrefslogtreecommitdiff
path: root/include/array
blob: 2c3d89cde9d59aa224ed8c7a7804ac7ed7f83610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// -*- C++ -*-
//===---------------------------- array -----------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_ARRAY
#define _LIBCPP_ARRAY

/*
    array synopsis

namespace std
{
template <class T, size_t N >
struct array
{
    // types:
    typedef T & reference;
    typedef const T & const_reference;
    typedef implementation defined iterator;
    typedef implementation defined const_iterator;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    typedef T value_type;
    typedef T* pointer;
    typedef const T* const_pointer;
    typedef std::reverse_iterator<iterator> reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

    // No explicit construct/copy/destroy for aggregate type
    void fill(const T& u);
    void swap(array& a);

    // iterators:
    iterator begin();
    const_iterator begin() const;
    iterator end();
    const_iterator end() const;

    reverse_iterator rbegin();
    const_reverse_iterator rbegin() const;
    reverse_iterator rend();
    const_reverse_iterator rend() const;

    const_iterator cbegin() const;
    const_iterator cend() const;
    const_reverse_iterator crbegin() const;
    const_reverse_iterator crend() const;

    // capacity:
    constexpr size_type size() const;
    constexpr size_type max_size() const;
    bool empty() const;

    // element access:
    reference operator[](size_type n);
    const_reference operator[](size_type n) const;
    const_reference at(size_type n) const;
    reference at(size_type n);

    reference front();
    const_reference front() const;
    reference back();
    const_reference back() const;

    T* data();
    const T* data() const;
};

template <class T, size_t N>
  bool operator==(const array<T,N>& x, const array<T,N>& y);
template <class T, size_t N>
  bool operator!=(const array<T,N>& x, const array<T,N>& y);
template <class T, size_t N>
  bool operator<(const array<T,N>& x, const array<T,N>& y);
template <class T, size_t N>
  bool operator>(const array<T,N>& x, const array<T,N>& y);
template <class T, size_t N>
  bool operator<=(const array<T,N>& x, const array<T,N>& y);
template <class T, size_t N>
  bool operator>=(const array<T,N>& x, const array<T,N>& y);

template <class T, size_t N >
  void swap(array<T,N>& x, array<T,N>& y);

template <class T> class tuple_size;
template <int I, class T> class tuple_element;
template <class T, size_t N> struct tuple_size<array<T, N>>;
template <int I, class T, size_t N> struct tuple_element<I, array<T, N>>;
template <int I, class T, size_t N> T& get(array<T, N>&);
template <int I, class T, size_t N> const T& get(const array<T, N>&);

}  // std

*/

#include <__config>
#include <__tuple>
#include <type_traits>
#include <utility>
#include <iterator>
#include <algorithm>
#include <stdexcept>
#if defined(_LIBCPP_NO_EXCEPTIONS)
    #include <cassert>
#endif

#pragma GCC system_header

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Tp, size_t _Size>
struct _LIBCPP_VISIBLE array
{
    // types:
    typedef array __self;
    typedef _Tp                                   value_type;
    typedef value_type&                           reference;
    typedef const value_type&                     const_reference;
    typedef value_type*                           iterator;
    typedef const value_type*                     const_iterator;
    typedef value_type*                           pointer;
    typedef const value_type*                     const_pointer;
    typedef size_t                                size_type;
    typedef ptrdiff_t                             difference_type;
    typedef std::reverse_iterator<iterator>       reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

    value_type __elems_[_Size > 0 ? _Size : 1];

    // No explicit construct/copy/destroy for aggregate type
    _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u)
        {_STD::fill_n(__elems_, _Size, __u);}
    _LIBCPP_INLINE_VISIBILITY void swap(array& __a)
        {_STD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);}

    // iterators:
    _LIBCPP_INLINE_VISIBILITY iterator begin()             {return iterator(__elems_);}
    _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(__elems_);}
    _LIBCPP_INLINE_VISIBILITY iterator end()               {return iterator(__elems_ + _Size);}
    _LIBCPP_INLINE_VISIBILITY const_iterator end() const   {return const_iterator(__elems_ + _Size);}

    _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin()             {return reverse_iterator(end());}
    _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
    _LIBCPP_INLINE_VISIBILITY reverse_iterator rend()               {return reverse_iterator(begin());}
    _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const   {return const_reverse_iterator(begin());}

    _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const          {return begin();}
    _LIBCPP_INLINE_VISIBILITY const_iterator cend() const            {return end();}
    _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
    _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const   {return rend();}

    // capacity:
    _LIBCPP_INLINE_VISIBILITY /*constexpr*/ size_type size() const      {return _Size;}
    _LIBCPP_INLINE_VISIBILITY /*constexpr*/ size_type max_size() const  {return _Size;}
    _LIBCPP_INLINE_VISIBILITY bool empty() const                    {return _Size == 0;}

    // element access:
    _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n)             {return __elems_[__n];}
    _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __elems_[__n];}
    reference at(size_type __n);
    const_reference at(size_type __n) const;

    _LIBCPP_INLINE_VISIBILITY reference front()             {return __elems_[0];}
    _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __elems_[0];}
    _LIBCPP_INLINE_VISIBILITY reference back()              {return __elems_[_Size > 0 ? _Size-1 : 0];}
    _LIBCPP_INLINE_VISIBILITY const_reference back() const  {return __elems_[_Size > 0 ? _Size-1 : 0];}

    _LIBCPP_INLINE_VISIBILITY value_type* data()             {return __elems_;}
    _LIBCPP_INLINE_VISIBILITY const value_type* data() const {return __elems_;}
};

template <class _Tp, size_t _Size>
typename array<_Tp, _Size>::reference
array<_Tp, _Size>::at(size_type __n)
{
    if (__n >= _Size)
#ifndef _LIBCPP_NO_EXCEPTIONS
        throw out_of_range("array::at");
#else
        assert(!"array::at out_of_range");
#endif
    return __elems_[__n];
}

template <class _Tp, size_t _Size>
typename array<_Tp, _Size>::const_reference
array<_Tp, _Size>::at(size_type __n) const
{
    if (__n >= _Size)
#ifndef _LIBCPP_NO_EXCEPTIONS
        throw out_of_range("array::at");
#else
        assert(!"array::at out_of_range");
#endif
    return __elems_[__n];
}

template <class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
bool
operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
{
    return _STD::equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_);
}

template <class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
bool
operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
{
    return !(__x == __y);
}

template <class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
bool
operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
{
    return _STD::lexicographical_compare(__x.__elems_, __x.__elems_ + _Size, __y.__elems_, __y.__elems_ + _Size);
}

template <class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
bool
operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
{
    return __y < __x;
}

template <class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
bool
operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
{
    return !(__y < __x);
}

template <class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
bool
operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
{
    return !(__x < __y);
}

template <class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
void
swap(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
{
    __x.swap(__y);
}

template <class _Tp, size_t _Size>
class _LIBCPP_VISIBLE tuple_size<array<_Tp, _Size> >
    : public integral_constant<size_t, _Size> {};

template <class _Tp, size_t _Size>
class _LIBCPP_VISIBLE tuple_size<const array<_Tp, _Size> >
    : public integral_constant<size_t, _Size> {};

template <size_t _Ip, class _Tp, size_t _Size>
class _LIBCPP_VISIBLE tuple_element<_Ip, array<_Tp, _Size> >
{
public:
    typedef _Tp type;
};

template <size_t _Ip, class _Tp, size_t _Size>
class _LIBCPP_VISIBLE tuple_element<_Ip, const array<_Tp, _Size> >
{
public:
    typedef const _Tp type;
};

template <size_t _Ip, class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
_Tp&
get(array<_Tp, _Size>& __a)
{
    return __a[_Ip];
}

template <size_t _Ip, class _Tp, size_t _Size>
_LIBCPP_INLINE_VISIBILITY inline
const _Tp&
get(const array<_Tp, _Size>& __a)
{
    return __a[_Ip];
}

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP_ARRAY