* \inpublicapi
* \ingroup module_utility
*/
-template <typename T>
+template<typename T>
class ArrayRef
{
- public:
- //! Type of values stored in the reference.
- typedef T value_type;
- //! Type for representing size of the reference.
- typedef size_t size_type;
- //! Type for representing difference between two indices.
- typedef ptrdiff_t difference_type;
- //! Const reference to an element.
- typedef const T &const_reference;
- //! Const pointer to an element.
- typedef const T *const_pointer;
- //! Const iterator type to an element.
- typedef const T *const_iterator;
- //! Reference to an element.
- typedef T &reference;
- //! Pointer to an element.
- typedef T *pointer;
- //! Iterator type to an element.
- typedef T *iterator;
- //! Standard reverse iterator.
- typedef std::reverse_iterator<iterator> reverse_iterator;
- //! Standard reverse iterator.
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
+public:
+ //! Type of values stored in the reference.
+ typedef T value_type;
+ //! Type for representing size of the reference.
+ typedef size_t size_type;
+ //! Type for representing difference between two indices.
+ typedef ptrdiff_t difference_type;
+ //! Const reference to an element.
+ typedef const T& const_reference;
+ //! Const pointer to an element.
+ typedef const T* const_pointer;
+ //! Const iterator type to an element.
+ typedef const T* const_iterator;
+ //! Reference to an element.
+ typedef T& reference;
+ //! Pointer to an element.
+ typedef T* pointer;
+ //! Iterator type to an element.
+ typedef T* iterator;
+ //! Standard reverse iterator.
+ typedef std::reverse_iterator<iterator> reverse_iterator;
+ //! Standard reverse iterator.
+ typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
- /*! \brief
- * Constructs an empty reference.
- */
- ArrayRef() : begin_(nullptr), end_(nullptr) {}
- /*! \brief
- * Constructs a reference to a container or reference
- *
- * \param[in] o container to reference.
- *
- * Can be used to create a reference to a whole vector, std::array or
- * an ArrayRef. The destination has to have a convertible pointer type
- * (identical besides const or base class).
- *
- * Passed container must remain valid and not be reallocated for the
- * lifetime of this object.
- *
- * This constructor is not explicit to allow directly passing
- * a container to a method that takes ArrayRef.
- */
- template<typename U,
- typename = std::enable_if_t<
- std::is_convertible<typename std::remove_reference_t<U>::pointer,
- pointer>::value> >
- ArrayRef(U &&o) : begin_(o.data()), end_(o.data()+o.size()) {}
- /*! \brief
- * Constructs a reference to a particular range.
- *
- * \param[in] begin Pointer to the beginning of a range.
- * \param[in] end Pointer to the end of a range.
- *
- * Passed pointers must remain valid for the lifetime of this object.
- */
- ArrayRef(pointer begin, pointer end)
- : begin_(begin), end_(end)
- {
- GMX_ASSERT(end >= begin, "Invalid range");
- }
- //! \cond
- // Doxygen 1.8.5 doesn't parse the declaration correctly...
- /*! \brief
- * Constructs a reference to a C array.
- *
- * \param[in] array C array to reference.
- * \tparam count Deduced number of elements in \p array.
- *
- * This constructor can only be used with a real array (not with a
- * pointer). It constructs a reference to the whole array, without
- * a need to pass the number of elements explicitly. The compiler
- * must be able to deduce the array size.
- *
- * Passed array must remain valid for the lifetime of this object.
- *
- * This constructor is not explicit to allow directly passing
- * a C array to a function that takes an ArrayRef parameter.
- */
- template <size_t count>
- ArrayRef(value_type (&array)[count])
- : begin_(array), end_(array + count)
- {
- }
- //! \endcond
+ /*! \brief
+ * Constructs an empty reference.
+ */
+ ArrayRef() : begin_(nullptr), end_(nullptr) {}
+ /*! \brief
+ * Constructs a reference to a container or reference
+ *
+ * \param[in] o container to reference.
+ *
+ * Can be used to create a reference to a whole vector, std::array or
+ * an ArrayRef. The destination has to have a convertible pointer type
+ * (identical besides const or base class).
+ *
+ * Passed container must remain valid and not be reallocated for the
+ * lifetime of this object.
+ *
+ * This constructor is not explicit to allow directly passing
+ * a container to a method that takes ArrayRef.
+ */
+ template<typename U, typename = std::enable_if_t<std::is_convertible<typename std::remove_reference_t<U>::pointer, pointer>::value>>
+ ArrayRef(U&& o) : begin_(o.data()), end_(o.data() + o.size())
+ {
+ }
+ /*! \brief
+ * Constructs a reference to a particular range.
+ *
+ * \param[in] begin Pointer to the beginning of a range.
+ * \param[in] end Pointer to the end of a range.
+ *
+ * Passed pointers must remain valid for the lifetime of this object.
+ */
+ ArrayRef(pointer begin, pointer end) : begin_(begin), end_(end)
+ {
+ GMX_ASSERT(end >= begin, "Invalid range");
+ }
+ //! \cond
+ // Doxygen 1.8.5 doesn't parse the declaration correctly...
+ /*! \brief
+ * Constructs a reference to a C array.
+ *
+ * \param[in] array C array to reference.
+ * \tparam count Deduced number of elements in \p array.
+ *
+ * This constructor can only be used with a real array (not with a
+ * pointer). It constructs a reference to the whole array, without
+ * a need to pass the number of elements explicitly. The compiler
+ * must be able to deduce the array size.
+ *
+ * Passed array must remain valid for the lifetime of this object.
+ *
+ * This constructor is not explicit to allow directly passing
+ * a C array to a function that takes an ArrayRef parameter.
+ */
+ template<size_t count>
+ ArrayRef(value_type (&array)[count]) : begin_(array), end_(array + count)
+ {
+ }
+ //! \endcond
- //! Returns a reference to part of the memory.
- ArrayRef subArray(size_type start, size_type count) const
- {
- return {begin_+start, begin_+start+count};
- }
- //! Returns an iterator to the beginning of the reference.
- iterator begin() const { return begin_; }
- //! Returns an iterator to the end of the reference.
- iterator end() const { return end_; }
- //! Returns an iterator to the reverse beginning of the reference.
- reverse_iterator rbegin() const { return reverse_iterator(end()); }
- //! Returns an iterator to the reverse end of the reference.
- reverse_iterator rend() const { return reverse_iterator(begin()); }
+ //! Returns a reference to part of the memory.
+ ArrayRef subArray(size_type start, size_type count) const
+ {
+ return { begin_ + start, begin_ + start + count };
+ }
+ //! Returns an iterator to the beginning of the reference.
+ iterator begin() const { return begin_; }
+ //! Returns an iterator to the end of the reference.
+ iterator end() const { return end_; }
+ //! Returns an iterator to the reverse beginning of the reference.
+ reverse_iterator rbegin() const { return reverse_iterator(end()); }
+ //! Returns an iterator to the reverse end of the reference.
+ reverse_iterator rend() const { return reverse_iterator(begin()); }
- /*! \brief Returns the size of the reference.
- *
- * \note Use ssize for any expression involving arithmetic operations
- (including loop indices).
- */
- size_type size() const { return end_ - begin_; }
- //! Returns the signed size of the reference.
- index ssize() const { return size(); }
- //! Identical to size().
- size_type capacity() const { return end_ - begin_; }
- //! Whether the reference refers to no memory.
- bool empty() const { return begin_ == end_; }
+ /*! \brief Returns the size of the reference.
+ *
+ * \note Use ssize for any expression involving arithmetic operations
+ (including loop indices).
+ */
+ size_type size() const { return end_ - begin_; }
+ //! Returns the signed size of the reference.
+ index ssize() const { return size(); }
+ //! Identical to size().
+ size_type capacity() const { return end_ - begin_; }
+ //! Whether the reference refers to no memory.
+ bool empty() const { return begin_ == end_; }
- //! Access an element.
- reference operator[](size_type n) const { return begin_[n]; }
- //! Access an element (throws on out-of-range error).
- reference at(size_type n) const
+ //! Access an element.
+ reference operator[](size_type n) const { return begin_[n]; }
+ //! Access an element (throws on out-of-range error).
+ reference at(size_type n) const
+ {
+ if (n >= size())
{
- if (n >= size())
- {
- throw std::out_of_range("Vector index out of range");
- }
- return begin_[n];
+ throw std::out_of_range("Vector index out of range");
}
- //! Returns the first element.
- reference front() const { return *begin_; }
- //! Returns the first element.
- reference back() const { return *(end_ - 1); }
+ return begin_[n];
+ }
+ //! Returns the first element.
+ reference front() const { return *begin_; }
+ //! Returns the first element.
+ reference back() const { return *(end_ - 1); }
- //! Returns a raw pointer to the contents of the array.
- pointer data() const { return begin_; }
+ //! Returns a raw pointer to the contents of the array.
+ pointer data() const { return begin_; }
- /*! \brief
- * Swaps referenced memory with the other object.
- *
- * The actual memory areas are not modified, only the references are
- * swapped.
- */
- void swap(ArrayRef<T> &other)
- {
- std::swap(begin_, other.begin_);
- std::swap(end_, other.end_);
- }
+ /*! \brief
+ * Swaps referenced memory with the other object.
+ *
+ * The actual memory areas are not modified, only the references are
+ * swapped.
+ */
+ void swap(ArrayRef<T>& other)
+ {
+ std::swap(begin_, other.begin_);
+ std::swap(end_, other.end_);
+ }
- private:
- pointer begin_;
- pointer end_;
+private:
+ pointer begin_;
+ pointer end_;
};
//! \copydoc ArrayRef::fromArray()
//! \related ArrayRef
-template <typename T>
-ArrayRef<T> arrayRefFromArray(T *begin, size_t size)
+template<typename T>
+ArrayRef<T> arrayRefFromArray(T* begin, size_t size)
{
- return ArrayRef<T>(begin, begin+size);
+ return ArrayRef<T>(begin, begin + size);
}
//! \copydoc ArrayRef::fromArray()
//! \related ArrayRef
-template <typename T>
-ArrayRef<const T> constArrayRefFromArray(const T *begin, size_t size)
+template<typename T>
+ArrayRef<const T> constArrayRefFromArray(const T* begin, size_t size)
{
- return ArrayRef<const T>(begin, begin+size);
+ return ArrayRef<const T>(begin, begin + size);
}
/*! \brief
*
* \see ArrayRef
*/
-template <typename T>
-ArrayRef < std::conditional_t < std::is_const<T>::value,
-const typename T::value_type,
-typename T::value_type>>
-makeArrayRef(T &c)
+template<typename T>
+ArrayRef<std::conditional_t<std::is_const<T>::value, const typename T::value_type, typename T::value_type>>
+makeArrayRef(T& c)
{
return c;
}
*
* \see ArrayRef
*/
-template <typename T>
-ArrayRef<const typename T::value_type> makeConstArrayRef(const T &c)
+template<typename T>
+ArrayRef<const typename T::value_type> makeConstArrayRef(const T& c)
{
return c;
}
*
* \ingroup module_utility
*/
-template <typename T>
-void swap(ArrayRef<T> &a, ArrayRef<T> &b)
+template<typename T>
+void swap(ArrayRef<T>& a, ArrayRef<T>& b)
{
a.swap(b);
}
*
* \ingroup module_utility
*/
-template <typename T>
-std::vector<T> copyOf(const ArrayRef<const T> &arrayRef)
+template<typename T>
+std::vector<T> copyOf(const ArrayRef<const T>& arrayRef)
{
return std::vector<T>(arrayRef.begin(), arrayRef.end());
}