aboutsummaryrefslogtreecommitdiff
path: root/system/include/libcxx/dynarray
blob: b0d04f91544ef4ac9c8f43bae01245d9eb78302e (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
301
302
303
304
305
306
307
308
309
310
311
// -*- C++ -*-
//===-------------------------- dynarray ----------------------------------===//
//
//                     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_DYNARRAY
#define _LIBCPP_DYNARRAY

#include <__config>
#if _LIBCPP_STD_VER > 11

/*
    dynarray synopsis

namespace std {

template< typename T >
class dynarray
{
    // types:
    typedef       T                               value_type;
    typedef       T&                              reference;
    typedef const T&                              const_reference;
    typedef       T*                              pointer;
    typedef const T*                              const_pointer;
    typedef       implementation-defined          iterator;
    typedef       implementation-defined          const_iterator;
    typedef reverse_iterator<iterator>            reverse_iterator;
    typedef reverse_iterator<const_iterator>      const_reverse_iterator;
    typedef size_t                                size_type;
    typedef ptrdiff_t                             difference_type;

public:
    // construct/copy/destroy:
    explicit dynarray(size_type c);
    template <typename Alloc>
      dynarray(size_type c, const Alloc& alloc);
    dynarray(size_type c, const T& v);
    template <typename Alloc>
      dynarray(size_type c, const T& v, const Alloc& alloc);
    dynarray(const dynarray& d);
    template <typename Alloc>
      dynarray(const dynarray& d, const Alloc& alloc);
    dynarray(initializer_list<T>);
    template <typename Alloc>
      dynarray(initializer_list<T>, const Alloc& alloc);

    dynarray& operator=(const dynarray&) = delete;
    ~dynarray();

    // iterators:
    iterator       begin()        noexcept;
    const_iterator begin()  const noexcept;
    const_iterator cbegin() const noexcept;
    iterator       end()          noexcept;
    const_iterator end()    const noexcept;
    const_iterator cend()   const noexcept;

    reverse_iterator       rbegin()        noexcept;
    const_reverse_iterator rbegin()  const noexcept;
    const_reverse_iterator crbegin() const noexcept;
    reverse_iterator       rend()          noexcept;
    const_reverse_iterator rend()    const noexcept;
    const_reverse_iterator crend()   const noexcept;

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

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

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

    const_reference at(size_type n) const;
    reference       at(size_type n);

    // data access:
    T*       data()       noexcept;
    const T* data() const noexcept;

    // mutating member functions:
    void fill(const T& v);
};

}  // std

*/

#include <__functional_base>
#include <iterator>
#include <stdexcept>
#include <initializer_list>
#include <new>
#include <algorithm>

#if defined(_LIBCPP_NO_EXCEPTIONS)
    #include <cassert>
#endif

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Tp>
struct _LIBCPP_TYPE_VIS_ONLY dynarray
{
public:
    // types:
    typedef dynarray __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;

private:
    size_t                  __size_;
    value_type *            __base_;
    _LIBCPP_ALWAYS_INLINE dynarray () noexcept : __base_(nullptr), __size_(0) {}
    
    static inline _LIBCPP_INLINE_VISIBILITY value_type* __allocate ( size_t count )
    {
        if ( numeric_limits<size_t>::max() / sizeof (value_type) <= count )
        {
#ifndef _LIBCPP_NO_EXCEPTIONS
            throw bad_array_length();
#else
            assert(!"dynarray::allocation");
#endif
        }
        return static_cast<value_type *> (::operator new (sizeof(value_type) * count));
    }

    static inline _LIBCPP_INLINE_VISIBILITY void __deallocate ( value_type* __ptr ) noexcept
    {
        ::operator delete (static_cast<void *> (__ptr));
    }

public:

    explicit dynarray(size_type __c);
    dynarray(size_type __c, const value_type& __v);
    dynarray(const dynarray& __d);
    dynarray(initializer_list<value_type>);

//  We're not implementing these right now.
//  Waiting for the resolution of LWG issue #2235
//     template <typename _Alloc>
//       dynarray(size_type __c, const _Alloc& __alloc);
//     template <typename _Alloc>
//       dynarray(size_type __c, const value_type& __v, const _Alloc& __alloc);
//     template <typename _Alloc>
//       dynarray(const dynarray& __d, const _Alloc& __alloc);
//     template <typename _Alloc>
//       dynarray(initializer_list<value_type>, const _Alloc& __alloc);

    dynarray& operator=(const dynarray&) = delete;
    ~dynarray();

    // iterators:
    inline _LIBCPP_INLINE_VISIBILITY iterator       begin()        noexcept { return iterator(data()); }
    inline _LIBCPP_INLINE_VISIBILITY const_iterator begin()  const noexcept { return const_iterator(data()); }
    inline _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const noexcept { return const_iterator(data()); }
    inline _LIBCPP_INLINE_VISIBILITY iterator       end()          noexcept { return iterator(data() + __size_); }
    inline _LIBCPP_INLINE_VISIBILITY const_iterator end()    const noexcept { return const_iterator(data() + __size_); }
    inline _LIBCPP_INLINE_VISIBILITY const_iterator cend()   const noexcept { return const_iterator(data() + __size_); }

    inline _LIBCPP_INLINE_VISIBILITY reverse_iterator       rbegin()        noexcept { return reverse_iterator(end()); }
    inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin()  const noexcept { return const_reverse_iterator(end()); }
    inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
    inline _LIBCPP_INLINE_VISIBILITY reverse_iterator       rend()          noexcept { return reverse_iterator(begin()); }
    inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend()    const noexcept { return const_reverse_iterator(begin()); }
    inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend()   const noexcept { return const_reverse_iterator(begin()); }

    // capacity:
    inline _LIBCPP_INLINE_VISIBILITY size_type size()     const noexcept { return __size_; }
    inline _LIBCPP_INLINE_VISIBILITY size_type max_size() const noexcept { return __size_; }
    inline _LIBCPP_INLINE_VISIBILITY bool      empty()    const noexcept { return __size_ == 0; }

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

    inline _LIBCPP_INLINE_VISIBILITY reference       front()       { return data()[0]; }
    inline _LIBCPP_INLINE_VISIBILITY const_reference front() const { return data()[0]; }
    inline _LIBCPP_INLINE_VISIBILITY reference       back()        { return data()[__size_-1]; }
    inline _LIBCPP_INLINE_VISIBILITY const_reference back()  const { return data()[__size_-1]; }

    inline _LIBCPP_INLINE_VISIBILITY const_reference at(size_type __n) const;
    inline _LIBCPP_INLINE_VISIBILITY reference       at(size_type __n);

    // data access:
    inline _LIBCPP_INLINE_VISIBILITY _Tp*       data()       noexcept { return __base_; }
    inline _LIBCPP_INLINE_VISIBILITY const _Tp* data() const noexcept { return __base_; }

    // mutating member functions:
    inline _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __v) { fill_n(begin(), __size_, __v); }
};

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
dynarray<_Tp>::dynarray(size_type __c) : dynarray ()
{
    __base_ = __allocate (__c);
    value_type *__data = data ();
    for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
        ::new (__data) value_type;
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
dynarray<_Tp>::dynarray(size_type __c, const value_type& __v) : dynarray ()
{
    __base_ = __allocate (__c);
    value_type *__data = data ();
    for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
        ::new (__data) value_type (__v);
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
dynarray<_Tp>::dynarray(initializer_list<value_type> __il) : dynarray ()
{
    size_t sz = __il.size();
    __base_ = __allocate (sz);
    value_type *__data = data ();
    auto src = __il.begin();
    for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
        ::new (__data) value_type (*src);
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
dynarray<_Tp>::dynarray(const dynarray& __d) : dynarray ()
{
    size_t sz = __d.size();
    __base_ = __allocate (sz);
    value_type *__data = data ();
    auto src = __d.begin();
    for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
        ::new (__data) value_type (*src);
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
dynarray<_Tp>::~dynarray()
{ 
    value_type *__data = data () + __size_;
    for ( size_t i = 0; i < __size_; ++i )
        (--__data)->value_type::~value_type();
    __deallocate ( __base_ );
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
typename dynarray<_Tp>::reference
dynarray<_Tp>::at(size_type __n)
{
    if (__n >= __size_)
    {
#ifndef _LIBCPP_NO_EXCEPTIONS
        throw out_of_range("dynarray::at");
#else
        assert(!"dynarray::at out_of_range");
#endif
    }
    return data()[__n];
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
typename dynarray<_Tp>::const_reference
dynarray<_Tp>::at(size_type __n) const
{
    if (__n >= __size_)
    {
#ifndef _LIBCPP_NO_EXCEPTIONS
        throw out_of_range("dynarray::at");
#else
        assert(!"dynarray::at out_of_range");
#endif
    }
    return data()[__n];
}

template <class _Tp, class _Alloc>
struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<dynarray<_Tp>, _Alloc> : true_type {};

_LIBCPP_END_NAMESPACE_STD

#endif  // if _LIBCPP_STD_VER > 11 
#endif  // _LIBCPP_DYNARRAY