2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Declares gmx::ConstArrayRef.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_ARRAYREF_H
44 #define GMX_UTILITY_ARRAYREF_H
53 #include "gmxassert.h"
59 * STL container non-mutable interface for a C array (or part of a std::vector).
61 * \tparam T Value type of elements.
63 * This class provides an interface similar to \c std::vector<T>, with the
64 * following main differences:
65 * - This class does not have its own storage. Instead, it references an
66 * existing array of values (either a C-style array or part of an existing
68 * - Only const methods are provided to access the stored values.
69 * It is not possible to alter the referenced array.
70 * - Copying objects of this type is cheap, and the copies behave identically
71 * to the original object: the copy references the same set of values.
74 * \ingroup module_utility
80 //! Type of values stored in the container.
82 //! Type for representing size of the container.
83 typedef size_t size_type;
84 //! Type for representing difference between two container indices.
85 typedef ptrdiff_t difference_type;
86 //! Const reference to a container element.
87 typedef const T &const_reference;
88 //! Const pointer to a container element.
89 typedef const T *const_pointer;
90 //! Const iterator type for the container.
91 typedef const T *const_iterator;
92 //! Equal to \a const_reference since changes are not allowed.
93 typedef const_reference reference;
94 //! Equal to \a const_pointer since changes are not allowed.
95 typedef const_pointer pointer;
96 //! Equal to \a const_iterator since changes are not allowed.
97 typedef const_iterator iterator;
98 //! Standard reverse iterator.
99 typedef std::reverse_iterator<iterator> reverse_iterator;
100 //! Standard reverse iterator.
101 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
104 * Constructs an empty reference.
106 ConstArrayRef() : begin_(NULL), end_(NULL) {}
108 * Constructs a reference to a particular range.
110 * \param[in] begin Pointer to the beginning of a range.
111 * \param[in] end Pointer to the end of a range.
113 * Passed pointers must remain valid for the lifetime of this object.
115 ConstArrayRef(const_pointer begin, const_pointer end)
116 : begin_(begin), end_(end)
118 GMX_ASSERT(end >= begin, "Invalid range");
121 * Constructs a reference to a particular rangein a std::vector.
123 * \param[in] begin Pointer to the beginning of a range.
124 * \param[in] end Pointer to the end of a range.
126 * The referenced vector must remain valid and not be reallocated for
127 * the lifetime of this object.
129 ConstArrayRef(typename std::vector<T>::const_iterator begin,
130 typename std::vector<T>::const_iterator end)
131 : begin_((begin != end) ? &*begin : NULL),
132 end_(begin_+(end-begin))
134 GMX_ASSERT(end >= begin, "Invalid range");
137 * Constructs a reference to an array.
139 * \param[in] begin Pointer to the beginning of the array.
140 * May be NULL if \p size is zero.
141 * \param[in] size Number of elements in the array.
143 * Passed pointer must remain valid for the lifetime of this object.
145 ConstArrayRef(const_pointer begin, size_type size)
146 : begin_(begin), end_(begin + size)
150 //! Returns an interator to the beginning of the container.
151 const_iterator begin() const { return begin_; }
152 //! Returns an interator to the end of the container.
153 const_iterator end() const { return end_; }
154 //! Returns an interator to the reverse beginning of the container.
155 const_iterator rbegin() const { return reverse_iterator(end()); }
156 //! Returns an interator to the reverse end of the container.
157 const_iterator rend() const { return reverse_iterator(begin()); }
159 //! Returns the size of the container.
160 size_type size() const { return end_ - begin_; }
161 //! Identical to size().
162 size_type capacity() const { return end_ - begin_; }
163 //! Whether the container is empty.
164 bool empty() const { return begin_ == end_; }
166 //! Access container element.
167 const_reference operator[](size_type n) const { return begin_[n]; }
168 //! Access container element (throws on out-of-range error).
169 const_reference at(size_type n) const
173 throw std::out_of_range("Vector index out of range");
177 //! Returns the first element in the container.
178 const_reference front() const { return *begin_; }
179 //! Returns the last element in the container.
180 const_reference back() const { return *(end_ - 1); }
182 //! Returns a raw pointer to the contents of the array.
183 const_pointer data() const { return begin_; }
186 * Swaps referenced memory with the other object.
188 * The actual memory areas are not modified, only the references are
191 void swap(ConstArrayRef<T> &other)
193 std::swap(begin_, other.begin_);
194 std::swap(end_, other.end_);
198 const_pointer begin_;
203 * Simple swap method for ConstArrayRef objects.
205 * \see ConstArrayRef::swap()
207 template <typename T>
208 void swap(ConstArrayRef<T> &a, ConstArrayRef<T> &b)