summaryrefslogtreecommitdiffstats
path: root/src/include/array
blob: 76b1371c8b0a9dac8dedd4bfaa466bae074e5a69 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/include/array $                                           */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2017                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#ifndef stl_array
#define stl_array

/**
 * @file array
 * @brief simple stl array template class declaration.
 */

#include <stddef.h>

#if !defined( __STDC_LIMIT_MACROS)
#define __STDC_LIMIT_MACROS
#endif
#include <stdint.h>
#include <algorithm>

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

/*  Not supporting for now
        typedef reverse_iterator<iterator>       reverse_iterator;
        typedef reverse_iterator<const_iterator> const_reverse_iterator;
*/

        T elems[N ? N : 1]; // exposition only

        // no explicit construct/copy/destroy for aggregate type

        /**
         * Assigns the given value to all elements in the container.
         * @param[in] the value to assign to the elements
         * @pre None.
         * @post None.
         */
        void fill(const T& value)
        {
            fill_n(begin(), N, value);
        }

        /**
         * Exchanges the contents of the container with those of other.
         * @param[in] container to exchange the contents with
         * @pre Does not cause iterators and references to associate with the
         *      other container.
         * @post None.
         */
        void swap(array<T, N>& other)
        {
            std::swap(elems, other.elems);
        }

        // iterators:

        /**
         * Returns an iterator to the first element of the container.
         * @return iterator to the first element
         * @pre If the container is empty, the returned iterator will be equal
         *      to end().
         * @post None.
         */
        iterator begin()
        {
            return iterator(&elems[0]);
        }

        /**
         * Returns a const_iterator to the first element of the container.
         * @return const_iterator to the first element
         * @pre If the container is empty, the returned iterator will be equal
         *      to end().
         * @post None.
         */
        constexpr const_iterator begin() const
        {
            return const_iterator(&elems[0]);
        }

        /**
         * Returns a const_iterator to the first element of the container.
         * @return const_iterator to the first element
         * @pre If the container is empty, the returned iterator will be equal
         *      to end().
         * @post None.
         */
        constexpr const_iterator cbegin() const
        {
            return const_iterator(&elems[0]);
        }

        /**
         * Returns an iterator to the element following the last element of the
         * container.
         * @return iterator to the element following the last element.
         * @pre This element acts as a placeholder; attempting to access it
         *      results in undefined behavior.
         * @post None.
         */
        iterator end()
        {
            return iterator(&elems[N]);
        }

        /**
         * Returns an const_iterator to the element following the last element
         * of the container.
         * @return const_iterator to the element following the last element.
         * @pre This element acts as a placeholder; attempting to access it
         *      results in undefined behavior.
         * @post None.
         */
        constexpr const_iterator end() const
        {
            return const_iterator(&elems[N]);
        }

        /**
         * Returns an const_iterator to the element following the last element
         * of the container.
         * @return const_iterator to the element following the last element.
         * @pre This element acts as a placeholder; attempting to access it
         *      results in undefined behavior.
         * @post None.
         */
        constexpr const_iterator cend() const
        {
            return const_iterator(&elems[N]);
        }

/*  Not supporting for now
        reverse_iterator        rbegin();
        const_reverse_iterator  rbegin() const;
        reverse_iterator        rend();
        const_reverse_iterator  rend() const;
        const_reverse_iterator  crbegin() const;
        const_reverse_iterator  crend() const;
*/

        // capacity:

        /**
         * Returns the number of elements in the container
         * @return The number of elements in the container.
         * @pre None.
         * @post None.
         */
        constexpr size_type size()
        {
            return N;
        }

        /**
         * Returns the maximum number of elements the container is able to hold
         * due to system or library implementation limitations,
         * @return Maximum number of elements.
         * @pre Because each std::array<T, N> is a fixed-size container, the
         *      value returned by max_size equals N (which is also the value
         *      returned by size)
         * @post None.
         */
        constexpr size_type max_size()
        {
            return size();
        }

        /**
         * Checks if the container has no elements
         * @return true if the container is empty, false otherwise
         * @pre None.
         * @post None.
         */
        constexpr bool empty()
        {
            return (N == 0);
        }

        // element access:

        /**
         * Returns a reference to the element at specified location pos.
         * No bounds checking is performed.
         * @param[in] position of the element to return
         * @return Reference to the requested element.
         * @pre Unlike std::map::operator[], this operator never inserts a new
         *      element into the container.
         * @post None.
         */
        reference operator[](size_type n)
        {
            return elems[n];
        }

        /**
         * Returns a const_reference to the element at specified location pos.
         * No bounds checking is performed.
         * @param[in] position of the element to return
         * @return const_reference to the requested element.
         * @pre None.
         * @post None.
         */
        const_reference operator[](size_type n) const
        {
            return elems[n];
        }

        /**
         * Returns a reference to the element at specified location pos, with
         * bounds checking. Will assert if pos is not within the range of the
         * container
         * @param[in] position of the element to return
         * @return reference to the requested element.
         * @pre None.
         * @post None.
         */
        reference at(size_type n)
        {
            assert(n < size());
            return elems[n];
        }

        /**
         * Returns a const_reference to the element at specified location pos,
         * with bounds checking. Will assert if pos is not within the range of
         * the container
         * @param[in] position of the element to return
         * @return const_reference to the requested element.
         * @pre None.
         * @post None.
         */
        const_reference at(size_type n) const
        {
            assert(n < size());
            return elems[n];
        }

        /**
         * Returns a reference to the first element in the container. Calling
         * front on an empty container is undefined.
         * @return reference to the first element
         * @pre For a container c, the expression c.front() is equivalent to
         *      *c.begin().
         * @post None.
         */
        reference front()
        {
            return *begin();
        }

        /**
         * Returns a const_reference to the first element in the container.
         * Calling front on an empty container is undefined.
         * @return const_reference to the first element
         * @pre For a container c, the expression c.front() is equivalent to
         *      *c.begin().
         * @post None.
         */
        constexpr const_reference front() const
        {
            return *begin();
        }

        /**
         * Returns reference to the last element in the container.
         * Calling back on an empty container is undefined.
         * @return Reference to the last element.
         * @pre For a container c, the expression return c.back(); is equivalent
         *      to { auto tmp = c.end(); --tmp; return *tmp; }
         * @post None.
         */
        reference back()
        {
            return N ? *(end() - 1) : *end();
        }

        /**
         * Returns const_reference to the last element in the container.
         * Calling back on an empty container is undefined.
         * @return const_reference to the last element.
         * @pre For a container c, the expression return c.back(); is equivalent
         *      to { auto tmp = c.end(); --tmp; return *tmp; }
         * @post None.
         */
        constexpr const_reference back() const
        {
            return N ? *(end() - 1) : *end();
        }

        /**
         * Returns pointer to the underlying array serving as element storage.
         * Calling back on an empty container is undefined.
         * @return Pointer to the underlying element storage.
         *         For non-empty containers, returns &front()
         * @pre The pointer is such that range [data(); data() + size()) is
         *      always a valid range, even if the container is empty (data() is
         *      not dereferenceable in that case).
         * @post None.
         */
        T * data()
        {
            return &front();
        }

        /**
         * Returns const pointer to the underlying array serving as element
         * storage. Calling back on an empty container is undefined.
         * @return const_pointer to the underlying element storage.
         *         For non-empty containers, returns &front()
         * @pre The pointer is such that range [data(); data() + size()) is
         *      always a valid range, even if the container is empty (data() is
         *      not dereferenceable in that case).
         * @post None.
         */
        constexpr const T * data() const
        {
            return &front();
        }
    };

    /**
     * Compare the contents of two containers - overload operator==
     * @return true if the contents of the containers are equal, false otherwise
     * @pre None.
     * @post None.
     */
    template <class T, size_t N, size_t M=N>
    inline bool operator==(const array<T,N>& lhs, const array<T,M>& rhs)
    {
        static_assert(N==M, "std::arrays must be of the same size when using "
                            "overloaded compare operators");
        return std::equal(lhs.begin(), lhs.end(), rhs.begin());
    }

    /**
     * Compare the contents of two containers - overload operator!=
     * @return true if the contents of the containers are not equal, false
     *         otherwise
     * @pre None.
     * @post None.
     */
    template <class T, size_t N, size_t M=N>
    inline bool operator!=(const array<T,N>& lhs, const array<T,M>& rhs)
    {
        static_assert(N==M, "std::arrays must be of the same size when using "
                            "overloaded compare operators");
        return !(lhs == rhs);
    }

    /**
     * Compare the contents of two containers - overload operator<
     * @return true if the contents of the lhs are lexicographically less than
     *         the contents of rhs, false otherwise
     * @pre None.
     * @post None.
     */
    template <class T, size_t N, size_t M=N>
    inline bool operator<(const array<T,N>& lhs, const array<T,M>& rhs)
    {
        static_assert(N==M, "std::arrays must be of the same size when using overloaded compare operators");
        return std::lexicographical_compare(lhs.begin(), lhs.end(),
                                            rhs.begin(), rhs.end());
    }

    /**
     * Compare the contents of two containers - overload operator<=
     * @return true if the contents of the lhs are lexicographically less than
     *         or equal the contents of rhs, false otherwise
     * @pre None.
     * @post None.
     */
    template <class T, size_t N, size_t M=N>
    inline bool operator<=(const array<T,N>& lhs, const array<T,M>& rhs)
    {
        static_assert(N==M, "std::arrays must be of the same size when using overloaded compare operators");
        return (lhs < rhs) || (lhs == rhs);
    }

    /**
     * Compare the contents of two containers - overload operator>
     * @return true if the contents of the lhs are lexicographically greater
     *         than the contents of rhs, false otherwise
     * @pre None.
     * @post None.
     */
    template <class T, size_t N, size_t M=N>
    inline bool operator>(const array<T,N>& lhs, const array<T,M>& rhs)
    {
        static_assert(N==M, "std::arrays must be of the same size when using overloaded compare operators");
        return !(lhs < rhs) && !(lhs == rhs);
    }

    /**
     * Compare the contents of two containers - overload operator>=
     * @return true if the contents of the lhs are lexicographically greater
     *         than or equal the contents of rhs, false otherwise
     * @pre None.
     * @post None.
     */
    template <class T, size_t N, size_t M=N>
    inline bool operator>=(const array<T,N>& lhs, const array<T,M>& rhs)
    {
        static_assert(N==M, "std::arrays must be of the same size when using overloaded compare operators");
        return !(lhs < rhs);
    }

    /**
     * Specializes the std::swap algorithm for std::array. Swaps the contents of
     * lhs and rhs. Calls lhs.swap(rhs)
     * @param[in] container whose contents to swap
     * @param[in] container whose contents to swap
     * @pre None.
     * @post None.
     */
    template <class T, size_t N>
    inline void swap(array<T,N>& lhs, array<T,N>& rhs)
    {
        lhs.swap(rhs);
    }

    /**
     * Extracts the Ith element element from the array.
     * @param[in] array whose contents to extract
     * @return A reference to the Ith element of a.
     * @pre I must be an integer value in range [0, N). This is enforced at
     *      compile time as opposed to at() or operator[].
     * @post None.
     */
    template <size_t I, class T, size_t N>
    inline constexpr T& get(array<T, N>& a)
    {
        static_assert( ( I>=0 && I<N ), "std::get trying to access element out of range");
        return a[I];
    }

    /**
     * Extracts the Ith element element from the array.
     * @param[in] array whose contents to extract
     * @return A const reference to the Ith element of a.
     * @pre I must be an integer value in range [0, N). This is enforced at
     *      compile time as opposed to at() or operator[].
     * @post None.
     */
    template <size_t I, class T, size_t N>
    inline constexpr const T& get(const array<T, N>& a)
    {
        static_assert( ( I>=0 && I<N ), "std::get trying to access element out of range");
        return a[I];
    }

    /**
     * Extracts the Ith element element from the array.
     * @param[in] array whose contents to extract
     * @return An rvalue reference to the Ith element of a.
     * @pre I must be an integer value in range [0, N). This is enforced at
     *      compile time as opposed to at() or operator[].
     * @post None.
     */
    template <size_t I, class T, size_t N>
    inline constexpr T&& get(array<T, N>&& a)
    {
        static_assert( ( I>=0 && I<N ), "std::get trying to access element out of range");
        return a[I];
    }

/*  Not supporting for now
    template <class T> class tuple_size;
    template <size_t I, class T> class tuple_element;
    template <class T, size_t N> struct tuple_size<array<T, N> >;
    template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N> >;
*/

}

#endif
OpenPOWER on IntegriCloud