2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Declares gmx::ArrayRef
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \author Mark Abraham <mark.j.abraham@gmail.com>
42 * \author Roland Schulz <roland.schulz@intel.com>
43 * \author Berk Hess <hess@kth.se>
45 * \ingroup module_utility
47 #ifndef GMX_UTILITY_ARRAYREF_H
48 #define GMX_UTILITY_ARRAYREF_H
58 #if __has_include(<boost/stl_interfaces/iterator_interface.hpp>)
59 # include <boost/stl_interfaces/iterator_interface.hpp>
60 #else // fallback for installed headers
61 # include <gromacs/external/boost/stl_interfaces/iterator_interface.hpp>
64 #include "gromacs/utility/gmxassert.h"
71 boost::stl_interfaces::iterator_interface<ArrayRefIter<T>, std::random_access_iterator_tag, T>
73 // This default constructor does not initialize it_
74 constexpr ArrayRefIter() noexcept {}
75 constexpr explicit ArrayRefIter(T* it) noexcept : it_(it) {}
76 // TODO: Use std::is_const_v when CUDA 11 is a requirement.
77 template<class T2 = T, class = std::enable_if_t<std::is_const<T2>::value>>
78 constexpr ArrayRefIter(ArrayRefIter<std::remove_const_t<T2>> it) noexcept : it_(&*it)
81 constexpr T* data() const noexcept { return it_; }
82 constexpr T& operator*() const noexcept { return *it_; }
83 constexpr ArrayRefIter& operator+=(std::ptrdiff_t i) noexcept
88 constexpr auto operator-(ArrayRefIter other) const noexcept { return it_ - other.it_; }
94 /*! \brief STL-like interface to a C array of T (or part
95 * of a std container of T).
97 * \tparam T Value type of elements.
99 * This class provides an interface similar to \c std::vector<T, A>, with the
100 * following main differences:
101 * - This class does not have its own storage. Instead, it references an
102 * existing array of values (either a C-style array or part of an existing
103 * std::vector<T, A> or std::array<T>).
104 * - It is only possible to modify the values themselves through ArrayRef;
105 * it is not possible to add or remove values.
106 * - Copying objects of this type is cheap, and the copies behave identically
107 * to the original object: the copy references the same set of values.
109 * This class is useful for writing wrappers that expose a view of the
110 * internal data stored as a single vector/array, which can be a whole
111 * or part of the underlying storage.
113 * Methods in this class do not throw, except where indicated.
115 * Note that due to a Doxygen limitation, the constructor that takes a C array
116 * whose size is known at compile time does not appear in the documentation.
118 * To refer to const data of type T, ArrayRef<const T> is used. For both const
119 * and non-const std::vector and std::array an ArrayRef view can be created.
120 * Attempting to create a non-const ArrayRef of a const vector/array will result
121 * in a compiler error in the respective constructor.
123 * For SIMD types there is template specialization available
124 * (e.g. ArrayRef<SimdReal>) in gromacs/simd/simd_memory.h which should have
125 * the same functionality as much as possible.
128 * This class is not complete. There are likely also methods missing (not
129 * required for current usage).
132 * \ingroup module_utility
138 //! Type of values stored in the reference.
139 typedef T value_type;
140 //! Type for representing size of the reference.
141 typedef size_t size_type;
142 //! Type for representing difference between two indices.
143 typedef ptrdiff_t difference_type;
144 //! Const reference to an element.
145 typedef const T& const_reference;
146 //! Const pointer to an element.
147 typedef const T* const_pointer;
148 //! Const iterator type to an element.
149 typedef ArrayRefIter<const T> const_iterator;
150 //! Reference to an element.
151 typedef T& reference;
152 //! Pointer to an element.
154 //! Iterator type to an element.
155 typedef ArrayRefIter<T> iterator;
156 //! Standard reverse iterator.
157 typedef std::reverse_iterator<iterator> reverse_iterator;
158 //! Standard reverse iterator.
159 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
162 * Constructs an empty reference.
164 ArrayRef() : begin_(nullptr), end_(nullptr) {}
166 * Constructs a reference to a container or reference
168 * \param[in] o container to reference.
170 * Can be used to create a reference to a whole vector, std::array or
171 * an ArrayRef. The destination has to have a convertible pointer type
172 * (identical besides const or base class).
174 * Passed container must remain valid and not be reallocated for the
175 * lifetime of this object.
177 * This constructor is not explicit to allow directly passing
178 * a container to a method that takes ArrayRef.
180 * \todo Use std::is_convertible_v when CUDA 11 is a requirement.
182 template<typename U, typename = std::enable_if_t<std::is_convertible<typename std::remove_reference_t<U>::pointer, pointer>::value>>
183 ArrayRef(U&& o) : begin_(o.data()), end_(o.data() + o.size())
187 * Constructs a reference to a particular range.
189 * \param[in] begin Pointer to the beginning of a range.
190 * \param[in] end Pointer to the end of a range.
192 * Passed pointers must remain valid for the lifetime of this object.
194 ArrayRef(pointer begin, pointer end) : begin_(begin), end_(end)
196 GMX_ASSERT(end >= begin, "Invalid range");
199 * Constructs a reference to a particular range.
201 * \param[in] begin Iterator to the beginning of a range.
202 * \param[in] end iterator to the end of a range.
204 * Passed iterators must remain valid for the lifetime of this object.
206 ArrayRef(iterator begin, iterator end) : begin_(begin), end_(end)
208 GMX_ASSERT(end >= begin, "Invalid range");
211 // Doxygen 1.8.5 doesn't parse the declaration correctly...
213 * Constructs a reference to a C array.
215 * \param[in] array C array to reference.
216 * \tparam count Deduced number of elements in \p array.
218 * This constructor can only be used with a real array (not with a
219 * pointer). It constructs a reference to the whole array, without
220 * a need to pass the number of elements explicitly. The compiler
221 * must be able to deduce the array size.
223 * Passed array must remain valid for the lifetime of this object.
225 * This constructor is not explicit to allow directly passing
226 * a C array to a function that takes an ArrayRef parameter.
228 template<size_t count>
229 ArrayRef(value_type (&array)[count]) : begin_(array), end_(array + count)
234 //! Returns a reference to part of the memory.
235 ArrayRef subArray(size_type start, size_type count) const
237 return { begin_ + start, begin_ + start + count };
239 //! Returns an iterator to the beginning of the reference.
240 iterator begin() const { return iterator(begin_); }
241 //! Returns an iterator to the end of the reference.
242 iterator end() const { return iterator(end_); }
243 //! Returns an iterator to the reverse beginning of the reference.
244 reverse_iterator rbegin() const { return reverse_iterator(end()); }
245 //! Returns an iterator to the reverse end of the reference.
246 reverse_iterator rend() const { return reverse_iterator(begin()); }
248 /*! \brief Returns the size of the reference.
250 * \note Use ssize for any expression involving arithmetic operations
251 (including loop indices).
253 size_type size() const { return end_ - begin_; }
254 //! Returns the signed size of the reference.
255 index ssize() const { return size(); }
256 //! Identical to size().
257 size_type capacity() const { return end_ - begin_; }
258 //! Whether the reference refers to no memory.
259 bool empty() const { return begin_ == end_; }
261 //! Access an element.
262 reference operator[](size_type n) const { return begin_[n]; }
263 //! Access an element (throws on out-of-range error).
264 reference at(size_type n) const
268 throw std::out_of_range("Vector index out of range");
272 //! Returns the first element.
273 reference front() const { return *(begin_); }
274 //! Returns the first element.
275 reference back() const { return *(end_ - 1); }
277 //! Returns a raw pointer to the contents of the array.
278 pointer data() const { return begin_.data(); }
281 * Swaps referenced memory with the other object.
283 * The actual memory areas are not modified, only the references are
286 void swap(ArrayRef<T>& other)
288 std::swap(begin_, other.begin_);
289 std::swap(end_, other.end_);
297 //! \copydoc ArrayRef::fromArray()
298 //! \related ArrayRef
300 ArrayRef<T> arrayRefFromArray(T* begin, size_t size)
302 return ArrayRef<T>(begin, begin + size);
305 //! \copydoc ArrayRef::fromArray()
306 //! \related ArrayRef
308 ArrayRef<const T> constArrayRefFromArray(const T* begin, size_t size)
310 return ArrayRef<const T>(begin, begin + size);
314 * Create ArrayRef from container with type deduction
318 * \todo Use std::is_const_v when CUDA 11 is a requirement.
321 ArrayRef<std::conditional_t<std::is_const<T>::value, const typename T::value_type, typename T::value_type>>
328 * Create ArrayRef to const T from container with type deduction
333 ArrayRef<const typename T::value_type> makeConstArrayRef(const T& c)
339 * Simple swap method for ArrayRef objects.
341 * \see ArrayRef::swap()
343 * \ingroup module_utility
346 void swap(ArrayRef<T>& a, ArrayRef<T>& b)
351 /*! \brief Return a vector that is a copy of an ArrayRef.
353 * This makes it convenient, clear, and performant (the compiler will
354 * either do RVO to elide the temporary, or invoke the move constructor
355 * taking the unnamed temporary) to write a declaration like
357 * auto v = copyOf(arrayRef);
359 * \ingroup module_utility
362 std::vector<T> copyOf(const ArrayRef<const T>& arrayRef)
364 return std::vector<T>(arrayRef.begin(), arrayRef.end());