Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2025 The plumed team 3 : (see the PEOPLE file at the root of the distribution for a list of names) 4 : 5 : See http://www.plumed.org for more information. 6 : 7 : This file is part of plumed, version 2. 8 : 9 : plumed is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : plumed 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 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with plumed. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #ifndef __PLUMED_tools_View2D_h 23 : #define __PLUMED_tools_View2D_h 24 : #include <limits> 25 : #include <type_traits> 26 : 27 : #include "View.h" 28 : 29 : namespace PLMD { 30 : 31 : /**A not-owning view for accessing array witha 2D interface 32 : 33 : The main idea is to have something that works like the mdspan from c++23. 34 : 35 : Views are CHEAP to copy (pointer and an integer), so it is better to pass 36 : them as values 37 : 38 : 39 : 40 : @todo ctors from std::array and from iterators to parallel the span implementatio 41 : */ 42 : template <typename T, std::size_t N= helpers::dynamic_extent, std::size_t M= helpers::dynamic_extent> 43 : class View2D { 44 : public: 45 : using value_type = T; 46 : using element_type = View<value_type,M>; 47 : using pointer = value_type*; 48 : using iterator = pointer; 49 : using const_iterator = const pointer; 50 : using reference = value_type&; 51 : using const_reference = const value_type&; 52 : private: 53 : pointer ptr_; 54 : std::size_t sizeN_{N}; 55 : std::size_t sizeM_{M}; 56 : public: 57 : 58 : ///constructor for fixed size View2D 59 : template <size_t N_ = N, size_t M_ = M, 60 : typename = std::enable_if_t<N_ != helpers::dynamic_extent && M_ != helpers::dynamic_extent>> 61 : explicit View2D(pointer p) noexcept : ptr_(p) {} 62 : 63 : ///constructor for a View2D with known second dimension at compile time 64 : template <size_t N_ = N, size_t M_ = M, 65 : typename = std::enable_if_t<N_ == helpers::dynamic_extent && M_ != helpers::dynamic_extent>> 66 4500403 : View2D(pointer p, size_t NN) noexcept: ptr_(p), sizeN_(NN) {} 67 : 68 : ///constructor for a View2D with all dimension known at run time 69 : template <size_t N_ = N, size_t M_ = M, 70 : typename = std::enable_if_t<N_ == helpers::dynamic_extent && M_ == helpers::dynamic_extent>> 71 94436940 : View2D(pointer p, size_t NN, size_t MM) noexcept : ptr_(p), sizeN_(NN), sizeM_(MM) {} 72 : 73 : View2D(const View2D&) noexcept =default; 74 : View2D(View2D&&) noexcept =default; 75 : View2D&operator =(const View2D&) noexcept =default; 76 : View2D&operator =(View2D&&) noexcept =default; 77 : 78 : ///returns the size of the first dimension 79 : constexpr size_t size() const noexcept { 80 2191521 : return sizeN_; 81 : } 82 : 83 : ///returns the View to the i-th row 84 : constexpr element_type operator[](size_t i) noexcept { 85 887659911 : return element_type{ptr_ + i * sizeM_,sizeM_}; 86 : } 87 : 88 : ///returns the reference i-th element 89 : constexpr const element_type operator[](size_t i) const noexcept { 90 928112229 : return element_type{ptr_ + i * sizeM_, sizeM_}; 91 : } 92 : 93 : ///return the pointer to the data 94 : constexpr pointer data() const noexcept { 95 : return ptr_; 96 : } 97 : }; 98 : 99 : } // namespace PLMD 100 : #endif // __PLUMED_tools_View2D_h