1/* This file is part of the Vc library. {{{
2Copyright © 2015 Matthias Kretz <kretz@kde.org>
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the names of contributing organizations nor the
12 names of its contributors may be used to endorse or promote products
13 derived from this software without specific prior written permission.
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27//===---------------------------- array -----------------------------------===//
29// The LLVM Compiler Infrastructure
31// This file is dual licensed under the MIT and the University of Illinois Open
32// Source Licenses. See LICENSE.TXT for details.
34//===----------------------------------------------------------------------===//
36#ifndef VC_INCLUDE_VC_ARRAY_
37#define VC_INCLUDE_VC_ARRAY_
45#include "common/subscript.h"
47namespace Vc_VERSIONED_NAMESPACE
51 * This is `std::array` with additional subscript operators supporting gather and scatter operations.
53 * The [std::array](https://en.cppreference.com/w/cpp/container/array) documentation applies.
55 * Gathers from structured data (AoS: arrays of struct) are possible via a special
59 * Vc::array<float, 100> data;
60 * std::iota(data.begin(), data.end(), 0.f); // fill with values 0, 1, 2, ...
61 * auto indexes = float_v::IndexType::IndexesFromZero();
62 * float_v gathered = data[indexes]; // gathered == [0, 1, 2, ...]
65 * This also works for gathers into arrays of structures:
67 * struct Point { float x, y, z; };
68 * Vc::array<Point, 100> points;
70 * auto indexes = float_v::IndexType::IndexesFromZero();
71 * float_v xs = data[indexes][&Point::x]; // [points[0].x, points[1].x, points[2].x, ...]
72 * float_v ys = data[indexes][&Point::y]; // [points[0].y, points[1].y, points[2].y, ...]
73 * float_v zs = data[indexes][&Point::z]; // [points[0].z, points[1].z, points[2].z, ...]
76 * Arrays may also be nested:
78 * Vc::array<Vc::array<float, 3>, 100> points;
80 * auto indexes = float_v::IndexType::IndexesFromZero();
81 * float_v xs = data[indexes][0]; // [points[0][0], points[1][0], points[2][0], ...]
82 * float_v ys = data[indexes][1]; // [points[0][1], points[1][1], points[2][1], ...]
83 * float_v zs = data[indexes][2]; // [points[0][2], points[1][2], points[2][2], ...]
86template <class T, size_t Size> struct array {
90 typedef value_type& reference;
91 typedef const value_type& const_reference;
92 typedef value_type* iterator;
93 typedef const value_type* const_iterator;
94 typedef value_type* pointer;
95 typedef const value_type* const_pointer;
96 typedef size_t size_type;
97 typedef ptrdiff_t difference_type;
98 typedef std::reverse_iterator<iterator> reverse_iterator;
99 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
101 value_type elems_[Size > 0 ? Size : 1];
103 // No explicit construct/copy/destroy for aggregate type
104 void fill(const value_type& u_) { std::fill_n(elems_, Size, u_); }
105 void swap(array& a_) noexcept(std::swap(std::declval<T &>(), std::declval<T &>()))
107 std::swap_ranges(elems_, elems_ + Size, a_.elems_);
111 iterator begin() noexcept { return iterator(elems_); }
112 const_iterator begin() const noexcept { return const_iterator(elems_); }
113 iterator end() noexcept { return iterator(elems_ + Size); }
114 const_iterator end() const noexcept { return const_iterator(elems_ + Size); }
115 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
116 const_reverse_iterator rbegin() const noexcept
118 return const_reverse_iterator(end());
120 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
121 const_reverse_iterator rend() const noexcept
123 return const_reverse_iterator(begin());
126 const_iterator cbegin() const noexcept { return begin(); }
127 const_iterator cend() const noexcept { return end(); }
128 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
129 const_reverse_iterator crend() const noexcept { return rend(); }
131 constexpr size_type size() const noexcept { return Size; }
132 constexpr size_type max_size() const noexcept { return Size; }
133 constexpr bool empty() const noexcept { return Size == 0; }
135 reference operator[](size_type n_) { return elems_[n_]; }
136 constexpr const_reference operator[](size_type n_) const { return elems_[n_]; }
139 * \name Data-Parallel Subscripting for Gather & Scatter
142 template <typename I>
143 Vc_ALWAYS_INLINE auto operator[](I&& arg_)
144 -> decltype(subscript_operator(*this, std::forward<I>(arg_)))
146 return subscript_operator(*this, std::forward<I>(arg_));
149 template <typename I>
150 Vc_ALWAYS_INLINE auto operator[](I&& arg_) const
151 -> decltype(subscript_operator(*this, std::forward<I>(arg_)))
153 return subscript_operator(*this, std::forward<I>(arg_));
157 reference at(size_type n_);
158 constexpr const_reference at(size_type n_) const;
160 reference front() { return elems_[0]; }
161 constexpr const_reference front() const { return elems_[0]; }
162 reference back() { return elems_[Size > 0 ? Size - 1 : 0]; }
163 constexpr const_reference back() const { return elems_[Size > 0 ? Size - 1 : 0]; }
164 value_type* data() noexcept { return elems_; }
165 const value_type* data() const noexcept { return elems_; }
168template <class T, size_t Size>
169typename array<T, Size>::reference array<T, Size>::at(size_type n_)
172 throw std::out_of_range("array::at");
177template <class T, size_t Size>
178constexpr typename array<T, Size>::const_reference array<T, Size>::at(size_type n_) const
180 return n_ >= Size ? (throw std::out_of_range("array::at"), elems_[0]) : elems_[n_];
183template <class T, size_t Size>
184inline bool operator==(const array<T, Size>& x_, const array<T, Size>& y_)
186 return std::equal(x_.elems_, x_.elems_ + Size, y_.elems_);
189template <class T, size_t Size>
190inline bool operator!=(const array<T, Size>& x_, const array<T, Size>& y_)
195template <class T, size_t Size>
196inline bool operator<(const array<T, Size>& x_, const array<T, Size>& y_)
198 return std::lexicographical_compare(x_.elems_, x_.elems_ + Size, y_.elems_,
202template <class T, size_t Size>
203inline bool operator>(const array<T, Size>& x_, const array<T, Size>& y_)
208template <class T, size_t Size>
209inline bool operator<=(const array<T, Size>& x_, const array<T, Size>& y_)
214template <class T, size_t Size>
215inline bool operator>=(const array<T, Size>& x_, const array<T, Size>& y_)
220/**\name non-member begin & end
221 * Implement the non-member begin & end functions in the %Vc namespace so that ADL works
222 * and `begin(some_vc_array)` always works.
225template <typename T, std::size_t N>
226inline auto begin(array<T, N>& arr) -> decltype(arr.begin())
230template <typename T, std::size_t N>
231inline auto begin(const array<T, N>& arr) -> decltype(arr.begin())
235template <typename T, std::size_t N>
236inline auto end(array<T, N>& arr) -> decltype(arr.end())
240template <typename T, std::size_t N>
241inline auto end(const array<T, N>& arr) -> decltype(arr.end())
249template <typename T, std::size_t N>
250struct has_no_allocated_data_impl<Vc::array<T, N>> : public std::true_type
253template <typename T, std::size_t N>
254struct has_contiguous_storage_impl<Vc::array<T, N>> : public std::true_type
262template <class T, size_t Size>
265 // MSVC fails to do SFINAE correctly and gets totally confused:
266 // error C2433: 'type': 'inline' not permitted on data declarations
267 // error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
268 // error C2061: syntax error: identifier 'swap'
271 typename enable_if<is_same<void, decltype(swap(declval<T&>(), declval<T&>()))>::value,
274 swap(const Vc::array<T, Size>& x_,
275 const Vc::array<T, Size>& y_) noexcept(swap(declval<T&>(), declval<T&>()))
280template <class T, size_t Size>
281class tuple_size<Vc::array<T, Size>> : public integral_constant<size_t, Size>
285template <size_t I, class T, size_t Size> class tuple_element<I, Vc::array<T, Size>>
291template <size_t I, class T, size_t Size>
292inline constexpr typename std::enable_if<(I < Size), T&>::type get(
293 Vc::array<T, Size>& a_) noexcept
298template <size_t I, class T, size_t Size>
299inline constexpr typename std::enable_if<(I < Size), const T&>::type get(
300 const Vc::array<T, Size>& a_) noexcept
305template <size_t I, class T, size_t Size>
306inline constexpr typename std::enable_if<(I < Size), T&&>::type get(
307 Vc::array<T, Size>&& a_) noexcept
309 return std::move(a_.elems_[I]);
313#endif // VC_INCLUDE_VC_ARRAY_
315// vim: ft=cpp foldmethod=marker