-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3265: Bit Mask utility function r=RudolfWeeber a=fweik Description of changes: - tuple interface for Utils::{Array, Vector} - Extract bit masking utility function to utils - Some small code modernization Co-authored-by: Florian Weik <[email protected]>
- Loading branch information
Showing
11 changed files
with
207 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef ESPRESSO_MASK_HPP | ||
#define ESPRESSO_MASK_HPP | ||
|
||
#include <utils/Array.hpp> | ||
#include <utils/get.hpp> | ||
#include <utils/type_traits.hpp> | ||
|
||
#include <utility> | ||
|
||
namespace Utils { | ||
namespace detail { | ||
template <class T, class Integral, size_t... I> | ||
auto mask_impl(Integral mask, T t, std::index_sequence<I...>) { | ||
return T{((mask & (1u << I)) ? get<I>(t) : tuple_element_t<I, T>{})...}; | ||
} | ||
} // namespace detail | ||
|
||
/** | ||
* @brief Pick elements of a tuple-like by a bit mask. | ||
* | ||
* E.g. every element of the input for which the corresponding | ||
* bit is set in the mask is set is copied to the output unmodified, | ||
* the elements that are not set are set to zero (default constructed | ||
* instance of the type). | ||
* | ||
* Example: | ||
* mask(0b1011, {1, 2, 3, 4}) => {1, 0, 3, 4} | ||
* | ||
* @tparam T implements the tuple interface(get, tuple_size, ...) | ||
* @tparam Integral An unsigned integral type | ||
* @param mask bit mask, if the i-th bit is set, the i-th element | ||
* in @param t is copied to the output, otherwise it is set to zero. | ||
* @param t input elements | ||
* @return t partially zeroed out according to mask | ||
*/ | ||
template <class T, class Integral> | ||
auto mask(Integral mask, T t) | ||
-> std::enable_if_t<std::is_unsigned<Integral>::value && | ||
(size_in_bits<Integral>::value >= | ||
tuple_size<T>::value), | ||
T> { | ||
return detail::mask_impl(mask, t, | ||
std::make_index_sequence<tuple_size<T>::value>{}); | ||
} | ||
} // namespace Utils | ||
|
||
#endif // ESPRESSO_MASK_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2019 The ESPResSo project | ||
* | ||
* This file is part of ESPResSo. | ||
* | ||
* ESPResSo is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* ESPResSo is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#define BOOST_TEST_MODULE mask test | ||
#define BOOST_TEST_DYN_LINK | ||
#include <boost/test/unit_test.hpp> | ||
|
||
#include <utils/mask.hpp> | ||
|
||
#include <array> | ||
#include <cstdint> | ||
#include <tuple> | ||
|
||
BOOST_AUTO_TEST_CASE(mask_) { | ||
using Utils::get; | ||
using Utils::tuple_element_t; | ||
|
||
const uint8_t mask = 1u | 4u; | ||
|
||
auto const a = std::make_tuple(std::string("a"), 3, 4.5); | ||
using input_type = decltype(a); | ||
|
||
auto const result = Utils::mask(mask, a); | ||
using result_type = decltype(result); | ||
|
||
static_assert(std::is_same<input_type, result_type>::value, ""); | ||
|
||
BOOST_CHECK_EQUAL(get<0>(result), get<0>(a)); | ||
BOOST_CHECK_EQUAL(get<1>(result), 0); | ||
BOOST_CHECK_EQUAL(get<2>(result), get<2>(a)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2018-2019 The ESPResSo project | ||
* | ||
* This file is part of ESPResSo. | ||
* | ||
* ESPResSo is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* ESPResSo is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** \file | ||
* Unit test for Utils Type Traits. | ||
* | ||
*/ | ||
|
||
#define BOOST_TEST_MODULE type traits tests | ||
#define BOOST_TEST_DYN_LINK | ||
#include <boost/test/unit_test.hpp> | ||
|
||
#include <utils/type_traits.hpp> | ||
|
||
#include <climits> | ||
|
||
BOOST_AUTO_TEST_CASE(size_in_bits) { | ||
static_assert(CHAR_BIT == Utils::size_in_bits<char>::value, ""); | ||
static_assert(CHAR_BIT * sizeof(int) == Utils::size_in_bits<int>::value, ""); | ||
} |