ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
mask.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <concepts>
23#include <cstddef>
24#include <limits>
25#include <type_traits>
26#include <utility>
27
28namespace Utils {
29namespace detail {
30template <class T, std::size_t... I>
31auto mask_impl(std::unsigned_integral auto mask, T const &t,
32 std::index_sequence<I...>) {
33 return T{((mask & (1u << I)) ? get<I>(t) : std::tuple_element_t<I, T>{})...};
34}
35} // namespace detail
36
37/**
38 * @brief Pick elements of a tuple-like by a bit mask.
39 *
40 * E.g. every element of the input for which the corresponding
41 * bit is set in the mask is set is copied to the output unmodified,
42 * the elements that are not set are set to zero (default constructed
43 * instance of the type).
44 *
45 * Example:
46 * <tt>mask(0b1011u, {1, 2, 3, 4}) => {1, 2, 0, 4}</tt>
47 *
48 * @tparam T implements the tuple interface(get, tuple_size, ...)
49 * @param mask bit mask, if the i-th bit is set, the i-th element
50 * in @p t is copied to the output, otherwise it is set to zero.
51 * @param t input elements
52 * @return t partially zeroed out according to mask
53 */
54template <class T> T mask(std::unsigned_integral auto mask, T const &t) {
55 auto constexpr size_in_bits = std::numeric_limits<decltype(mask)>::digits;
56 static_assert(size_in_bits >= std::tuple_size_v<T>);
57 return detail::mask_impl(mask, t,
58 std::make_index_sequence<std::tuple_size_v<T>>{});
59}
60} // namespace Utils
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
T mask(std::unsigned_integral auto mask, T const &t)
Pick elements of a tuple-like by a bit mask.
Definition mask.hpp:54
STL namespace.