128-bit AVX2 SIMD for AMD Ryzen
[gromacs.git] / src / gromacs / options / valuestore.h
blobd19652adeccfddca285dad5308d8b6f73b745269
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source 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.
35 /*! \file
36 * \brief
37 * Declares implementations for IOptionValueStore.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
42 #ifndef GMX_OPTIONS_VALUESTORE_H
43 #define GMX_OPTIONS_VALUESTORE_H
45 #include <vector>
47 #include "gromacs/options/ivaluestore.h"
48 #include "gromacs/utility/arrayref.h"
50 namespace gmx
53 template <typename T>
54 class OptionValueStorePlain : public IOptionValueStore<T>
56 public:
57 OptionValueStorePlain(T *store, int *storeCount, int initialCount)
58 : count_(initialCount), store_(store), storeCount_(storeCount)
62 virtual int valueCount() { return count_; }
63 virtual ArrayRef<T> values() { return ArrayRef<T>::fromArray(store_, count_); }
64 virtual void clear()
66 count_ = 0;
67 if (storeCount_ != nullptr)
69 *storeCount_ = count_;
72 virtual void reserve(size_t /*count*/)
75 virtual void append(const T &value)
77 store_[count_] = value;
78 ++count_;
79 if (storeCount_ != nullptr)
81 *storeCount_ = count_;
85 private:
86 int count_;
87 T *store_;
88 int *storeCount_;
91 template <typename T>
92 class OptionValueStoreVector : public IOptionValueStore<T>
94 public:
95 // cppcheck-suppress uninitMemberVar
96 explicit OptionValueStoreVector(std::vector<T> *store) : store_(store) {}
98 virtual int valueCount() { return static_cast<int>(store_->size()); }
99 virtual ArrayRef<T> values() { return *store_; }
100 virtual void clear() { store_->clear(); }
101 virtual void reserve(size_t count)
103 store_->reserve(store_->size() + count);
105 virtual void append(const T &value)
107 store_->push_back(value);
110 private:
111 std::vector<T> *store_;
114 // Specialization that works around std::vector<bool> specialities.
115 template <>
116 // cppcheck-suppress noConstructor
117 class OptionValueStoreVector<bool> : public IOptionValueStore<bool>
119 public:
120 explicit OptionValueStoreVector(std::vector<bool> *store) : store_(store)
124 virtual int valueCount() { return static_cast<int>(store_->size()); }
125 virtual ArrayRef<bool> values()
127 return ArrayRef<bool>::fromArray(reinterpret_cast<bool *>(boolStore_.data()),
128 boolStore_.size());
130 virtual void clear()
132 boolStore_.clear();
133 store_->clear();
135 virtual void reserve(size_t count)
137 boolStore_.reserve(boolStore_.size() + count);
138 store_->reserve(store_->size() + count);
140 virtual void append(const bool &value)
142 boolStore_.push_back({value});
143 store_->push_back(value);
146 private:
147 struct Bool
149 bool value;
152 std::vector<Bool> boolStore_;
153 std::vector<bool> *store_;
156 /*! \internal
157 * \brief
158 * Value storage that does not store anywhere.
160 * This is needed because even though the values are not stored anywhere, the
161 * code still expects to access them later through valueCount() and values().
163 * \ingroup module_options
165 template <typename T>
166 class OptionValueStoreNull : public IOptionValueStore<T>
168 public:
169 OptionValueStoreNull() : store_(&vector_) {}
171 virtual int valueCount() { return store_.valueCount(); }
172 virtual ArrayRef<T> values() { return store_.values(); }
173 virtual void clear() { store_.clear(); }
174 virtual void reserve(size_t count) { store_.reserve(count); }
175 virtual void append(const T &value) { store_.append(value); }
177 private:
178 std::vector<T> vector_;
179 OptionValueStoreVector<T> store_;
182 } // namespace gmx
184 #endif