Add nblib backend: Part 1 of 2
[alexxy/gromacs.git] / api / nblib / topology.h
index 17774c2f77e2173faff0fc4608c07650db8c453e..380daf79ac57e144475491779cfb36a48fcae729 100644 (file)
@@ -175,6 +175,26 @@ private:
     ParticleTypesInteractions particleTypesInteractions_;
 };
 
+//! utility function to extract Particle quantities and expand them to the full
+//! array of length numParticles()
+template<class F>
+inline auto expandQuantity(const Topology& topology, F&& particleTypeExtractor)
+{
+    using ValueType = decltype((std::declval<ParticleType>().*std::declval<F>())());
+
+    std::vector<ValueType> ret;
+    ret.reserve(topology.numParticles());
+
+    const std::vector<ParticleType>& particleTypes = topology.getParticleTypes();
+
+    for (size_t id : topology.getParticleTypeIdOfAllParticles())
+    {
+        ret.push_back((particleTypes[id].*particleTypeExtractor)());
+    }
+
+    return ret;
+}
+
 } // namespace nblib
 
 #endif // NBLIB_TOPOLOGY_H