repertory/support/include/utils/collection.hpp
Scott E. Graves 035f830b71
Some checks are pending
BlockStorage/repertory/pipeline/head Build queued...
updated build system
2024-08-03 12:15:08 -05:00

134 lines
4.7 KiB
C++

/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef REPERTORY_INCLUDE_UTILS_COLLECTION_HPP_
#define REPERTORY_INCLUDE_UTILS_COLLECTION_HPP_
#include "utils/config.hpp"
namespace repertory::utils::collection {
template <typename col_t>
[[nodiscard]] inline auto
excludes(const col_t &collection,
const typename col_t::value_type &val) -> bool;
template <typename col_t>
[[nodiscard]] inline auto
includes(const col_t &collection,
const typename col_t::value_type &val) -> bool;
template <typename val_t>
[[nodiscard]] inline auto from_hex_string(std::string_view str,
val_t &val) -> bool;
template <typename val_t>
[[nodiscard]] inline auto from_hex_string(std::wstring_view str,
val_t &val) -> bool;
template <typename col_t>
inline auto remove_element(col_t &collection,
const typename col_t::value_type &value) -> col_t &;
template <typename col_t>
[[nodiscard]] inline auto to_hex_string(const col_t &collection) -> std::string;
template <typename col_t>
[[nodiscard]] inline auto
to_hex_wstring(const col_t &collection) -> std::wstring;
template <typename col_t>
inline auto excludes(const col_t &collection,
const typename col_t::value_type &val) -> bool {
return std::find(collection.begin(), collection.end(), val) ==
collection.end();
}
template <typename col_t>
inline auto includes(const col_t &collection,
const typename col_t::value_type &val) -> bool {
return std::find(collection.begin(), collection.end(), val) !=
collection.end();
}
template <typename val_t, typename string_t>
[[nodiscard]] inline auto
from_hex_string_t(std::basic_string_view<typename string_t::value_type> str,
val_t &val) -> bool {
static constexpr const auto base16{16};
val.clear();
if (not(str.length() % 2U)) {
for (std::size_t i = 0U; i < str.length(); i += 2U) {
val.emplace_back(static_cast<typename val_t::value_type>(
std::strtol(string_t{str.substr(i, 2U)}.c_str(), nullptr, base16)));
}
return true;
}
return false;
}
template <typename col_t>
inline auto remove_element(col_t &collection,
const typename col_t::value_type &value) -> col_t & {
collection.erase(std::remove(collection.begin(), collection.end(), value),
collection.end());
return collection;
}
template <typename col_t, typename string_t>
[[nodiscard]] inline auto to_hex_string_t(const col_t &collection) -> string_t {
static_assert(sizeof(typename col_t::value_type) == 1U,
"value_type must be 1 byte in size");
static constexpr const auto mask = 0xFF;
std::basic_stringstream<typename string_t::value_type> stream{};
for (auto &&val : collection) {
stream << std::setfill('0') << std::setw(2) << std::hex
<< (static_cast<std::uint32_t>(val) & mask);
}
return stream.str();
}
template <typename val_t>
inline auto from_hex_string(std::string_view str, val_t &val) -> bool {
return from_hex_string_t<val_t, std::string>(str, val);
}
template <typename val_t>
inline auto from_hex_string(std::wstring_view str, val_t &val) -> bool {
return from_hex_string_t<val_t, std::wstring>(str, val);
}
template <typename col_t>
inline auto to_hex_string(const col_t &collection) -> std::string {
return to_hex_string_t<col_t, std::string>(collection);
}
template <typename col_t>
inline auto to_hex_wstring(const col_t &collection) -> std::wstring {
return to_hex_string_t<col_t, std::wstring>(collection);
}
} // namespace repertory::utils::collection
#endif // REPERTORY_INCLUDE_UTILS_COLLECTION_HPP_