|
| 1 | +// SPDX-FileCopyrightText: 2006-2024, Knut Reinert & Freie Universität Berlin |
| 2 | +// SPDX-FileCopyrightText: 2016-2024, Knut Reinert & MPI für molekulare Genetik |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +/*!\file |
| 6 | + * \author Enrico Seiler <enrico.seiler AT fu-berlin.de> |
| 7 | + * \brief Provides sharg::detail::id_pair. |
| 8 | + */ |
| 9 | + |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <string> |
| 14 | +#include <unordered_set> |
| 15 | + |
| 16 | +#include <sharg/platform.hpp> |
| 17 | + |
| 18 | +namespace sharg::detail |
| 19 | +{ |
| 20 | + |
| 21 | +/*!\brief A simple struct to store a short and a long identifier for an option. |
| 22 | + * \ingroup parser |
| 23 | + */ |
| 24 | +struct id_pair |
| 25 | +{ |
| 26 | + char short_id{}; //!< The short identifier for the option. |
| 27 | + std::string long_id{}; //!< The long identifier for the option. |
| 28 | + |
| 29 | + id_pair() = default; //!< Defaulted. |
| 30 | + id_pair(id_pair const &) = default; //!< Defaulted. |
| 31 | + id_pair & operator=(id_pair const &) = default; //!< Defaulted. |
| 32 | + id_pair(id_pair &&) = default; //!< Defaulted. |
| 33 | + id_pair & operator=(id_pair &&) = default; //!< Defaulted. |
| 34 | + ~id_pair() = default; //!< Defaulted. |
| 35 | + |
| 36 | + //!\brief Construct an id_pair from a short ID. |
| 37 | + id_pair(char const short_id) : short_id{short_id} |
| 38 | + {} |
| 39 | + |
| 40 | + //!\brief Construct an id_pair from a long ID. |
| 41 | + id_pair(std::string long_id) : long_id{std::move(long_id)} |
| 42 | + {} |
| 43 | + |
| 44 | + //!\brief Construct an id_pair from a short and long ID. |
| 45 | + id_pair(char const short_id, std::string long_id) : short_id{short_id}, long_id{std::move(long_id)} |
| 46 | + {} |
| 47 | + |
| 48 | + /*!\brief Two id_pairs are equal if their short **or** long ID is equal. |
| 49 | + * Empty IDs are not considered for equality. If both IDs are empty, the id_pairs are considered **not** equal. |
| 50 | + */ |
| 51 | + friend bool operator==(id_pair const & lhs, id_pair const & rhs) |
| 52 | + { |
| 53 | + return (!lhs.empty_short_id() && lhs.short_id == rhs.short_id) |
| 54 | + || (!lhs.empty_long_id() && lhs.long_id == rhs.long_id); |
| 55 | + } |
| 56 | + |
| 57 | + /*!\brief Compares the given short ID with the short ID of the id_pair. |
| 58 | + * Returns false if the id_pair's short ID is empty. |
| 59 | + */ |
| 60 | + friend bool operator==(id_pair const & lhs, char const & rhs) |
| 61 | + { |
| 62 | + return !lhs.empty_short_id() && lhs.short_id == rhs; |
| 63 | + } |
| 64 | + |
| 65 | + /*!\brief Compares the given long ID with the long ID of the id_pair. |
| 66 | + * Returns false if the id_pair's long ID is empty. |
| 67 | + */ |
| 68 | + friend bool operator==(id_pair const & lhs, std::string const & rhs) |
| 69 | + { |
| 70 | + return !lhs.empty_long_id() && lhs.long_id == rhs; |
| 71 | + } |
| 72 | + |
| 73 | + //!\brief Returns true if the short ID is empty. |
| 74 | + bool empty_short_id() const noexcept |
| 75 | + { |
| 76 | + return empty(short_id); |
| 77 | + } |
| 78 | + |
| 79 | + //!\brief Returns true if the long ID is empty. |
| 80 | + bool empty_long_id() const noexcept |
| 81 | + { |
| 82 | + return empty(long_id); |
| 83 | + } |
| 84 | + |
| 85 | + //!\brief Returns true if both IDs are empty. |
| 86 | + bool empty() const noexcept |
| 87 | + { |
| 88 | + return empty_short_id() && empty_long_id(); |
| 89 | + } |
| 90 | + |
| 91 | + //!\brief Checks whether id is empty. |
| 92 | + template <typename id_type> |
| 93 | + static bool empty(id_type const & id) noexcept; |
| 94 | + |
| 95 | + // Note: The following two functions are declared, but not defined. |
| 96 | + // We first need to specialise std::hash<id_pair> in the std namespace. |
| 97 | + // After that, we can define the functions. |
| 98 | + // Defining them here would generate std::hash<id_pair> before the specialisation. |
| 99 | + // 1.) Now there are two specialisations for std::hash<id_pair> (error) |
| 100 | + // 2.) The default-generated std::hash<id_pair> does actually not work |
| 101 | + |
| 102 | + //!\brief Finds an id_pair in a set of used ids. |
| 103 | + template <typename id_type> |
| 104 | + static auto find(std::unordered_set<id_pair> const & used_ids, id_type const & id); |
| 105 | + |
| 106 | + //!\brief Checks whether an id is already contained in a set of used ids. |
| 107 | + template <typename id_type> |
| 108 | + static bool contains(std::unordered_set<id_pair> const & used_ids, id_type const & id); |
| 109 | +}; |
| 110 | + |
| 111 | +} // namespace sharg::detail |
| 112 | + |
| 113 | +namespace std |
| 114 | +{ |
| 115 | + |
| 116 | +/*!\brief Hash specialization for sharg::detail::id_pair. |
| 117 | + * \ingroup parser |
| 118 | + */ |
| 119 | +template <> |
| 120 | +struct hash<sharg::detail::id_pair> |
| 121 | +{ |
| 122 | + //!\brief Computes the hash value for a given id_pair. |
| 123 | + size_t operator()(sharg::detail::id_pair const & value) const noexcept |
| 124 | + { |
| 125 | + size_t const h1 = std::hash<char>{}(value.short_id); |
| 126 | + size_t const h2 = std::hash<std::string>{}(value.long_id); |
| 127 | + return h1 ^ (h2 << 1); |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace std |
| 132 | + |
| 133 | +namespace sharg::detail |
| 134 | +{ |
| 135 | + |
| 136 | +template <typename id_type> |
| 137 | +inline bool id_pair::empty(id_type const & id) noexcept |
| 138 | +{ |
| 139 | + if constexpr (std::same_as<id_type, id_pair>) |
| 140 | + return id.empty(); |
| 141 | + else if constexpr (std::same_as<id_type, char>) |
| 142 | + return id == '\0'; |
| 143 | + else |
| 144 | + return id.empty(); |
| 145 | +} |
| 146 | + |
| 147 | +template <typename id_type> |
| 148 | +inline auto id_pair::find(std::unordered_set<id_pair> const & used_ids, id_type const & id) |
| 149 | +{ |
| 150 | + if (empty(id)) |
| 151 | + return used_ids.end(); |
| 152 | + |
| 153 | + return std::ranges::find_if(used_ids, |
| 154 | + [&id](id_pair const & pair) |
| 155 | + { |
| 156 | + return pair == id; |
| 157 | + }); |
| 158 | +} |
| 159 | + |
| 160 | +template <typename id_type> |
| 161 | +inline bool id_pair::contains(std::unordered_set<id_pair> const & used_ids, id_type const & id) |
| 162 | +{ |
| 163 | + return find(used_ids, id) != used_ids.end(); |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace sharg::detail |
0 commit comments