1212#include < string>
1313#include < unordered_set>
1414
15+ #include < sharg/platform.hpp>
16+
1517// !\cond
1618
1719namespace sharg ::detail
@@ -22,40 +24,65 @@ namespace sharg::detail
2224 */
2325struct id_pair
2426{
25- std::string short_id{}; // !< The short identifier for the option.
26- std::string long_id{}; // !< The long identifier for the option.
27+ char short_id{}; // !< The short identifier for the option.
28+ std::string long_id{}; // !< The long identifier for the option.
2729
2830 id_pair () = default ;
2931 id_pair (id_pair const &) = default ;
30- id_pair (id_pair &&) = default ;
3132 id_pair & operator =(id_pair const &) = default ;
33+ id_pair (id_pair &&) = default ;
3234 id_pair & operator =(id_pair &&) = default ;
3335 ~id_pair () = default ;
3436
35- id_pair (std::string id_short, std::string id_long ) : short_id{std::move (id_short)}, long_id{ std::move (id_long) }
37+ id_pair (char const short_id ) : short_id{short_id }
3638 {}
3739
38- id_pair (char const id_short, std::string id_long) :
39- short_id{std::string (id_short != ' \0 ' , id_short)},
40- long_id{std::move (id_long)}
40+ id_pair (std::string long_id) : long_id{std::move (long_id)}
4141 {}
4242
43- id_pair (char const id_short) : short_id{std::string (id_short != ' \0 ' , id_short)}
44- {}
45-
46- id_pair (std::string id_long) : long_id{std::move (id_long)}
43+ id_pair (char const short_id, std::string long_id) : short_id{short_id}, long_id{std::move (long_id)}
4744 {}
4845
4946 // !\brief Compares two id_pairs for equality.
50- friend auto operator <=>(id_pair const &, id_pair const &) = default ;
47+ friend bool operator ==(id_pair const & lhs, id_pair const & rhs)
48+ {
49+ return (!lhs.empty_short_id () && lhs.short_id == rhs.short_id )
50+ || (!lhs.empty_long_id () && lhs.long_id == rhs.long_id );
51+ }
52+
53+ friend bool operator ==(id_pair const & lhs, char const & rhs)
54+ {
55+ return !lhs.empty_short_id () && lhs.short_id == rhs;
56+ }
57+
58+ friend bool operator ==(id_pair const & lhs, std::string const & rhs)
59+ {
60+ return !lhs.empty_long_id () && lhs.long_id == rhs;
61+ }
62+
63+ bool empty_short_id () const
64+ {
65+ return empty (short_id);
66+ }
67+
68+ bool empty_long_id () const
69+ {
70+ return empty (long_id);
71+ }
5172
52- static auto find (std::unordered_set<id_pair> const & used_ids, char const short_id);
73+ bool empty () const
74+ {
75+ return empty_short_id () && empty_long_id ();
76+ }
5377
54- static auto find (std::unordered_set<id_pair> const & used_ids, std::string const & long_id);
78+ template <typename id_type>
79+ static bool empty (id_type const & id);
5580
56- static bool contains (std::unordered_set<id_pair> const & used_ids, char const short_id);
81+ template <typename id_type>
82+ static auto find (std::unordered_set<id_pair> const & used_ids, id_type const & id);
5783
58- static bool contains (std::unordered_set<id_pair> const & used_ids, std::string const & long_id);
84+ template <typename id_type>
85+ static bool contains (std::unordered_set<id_pair> const & used_ids, id_type const & id);
5986};
6087
6188} // namespace sharg::detail
@@ -68,8 +95,8 @@ struct hash<sharg::detail::id_pair>
6895{
6996 size_t operator ()(sharg::detail::id_pair const & value) const noexcept
7097 {
71- size_t h1 = std::hash<std::string >{}(value.short_id );
72- size_t h2 = std::hash<std::string>{}(value.long_id );
98+ size_t const h1 = std::hash<char >{}(value.short_id );
99+ size_t const h2 = std::hash<std::string>{}(value.long_id );
73100 return h1 ^ (h2 << 1 );
74101 }
75102};
@@ -79,35 +106,34 @@ struct hash<sharg::detail::id_pair>
79106namespace sharg ::detail
80107{
81108
82- inline auto id_pair::find (std::unordered_set<id_pair> const & used_ids, char const short_id)
83- {
84- std::string const id_str (1 , short_id);
85- auto it = std::ranges::find_if (used_ids,
86- [&id_str](id_pair const & id)
87- {
88- return id.short_id == id_str;
89- });
90- return it;
91- }
92-
93- inline auto id_pair::find (std::unordered_set<id_pair> const & used_ids, std::string const & long_id)
109+ template <typename id_type>
110+ inline bool id_pair::empty (id_type const & id)
94111{
95- auto it = std::ranges::find_if (used_ids,
96- [&long_id](id_pair const & id)
97- {
98- return id. long_id == long_id ;
99- });
100- return it ;
112+ if constexpr ( std::same_as<id_type, id_pair>)
113+ return id. empty ();
114+ else if constexpr (std::same_as<id_type, char >)
115+ return id == ' \0 ' ;
116+ else
117+ return id. empty () ;
101118}
102119
103- inline bool id_pair::contains (std::unordered_set<id_pair> const & used_ids, char const short_id)
120+ template <typename id_type>
121+ inline auto id_pair::find (std::unordered_set<id_pair> const & used_ids, id_type const & id)
104122{
105- return id_pair::find (used_ids, short_id) != used_ids.end ();
123+ if (empty (id))
124+ return used_ids.end ();
125+
126+ return std::ranges::find_if (used_ids,
127+ [&id](id_pair const & pair)
128+ {
129+ return pair == id;
130+ });
106131}
107132
108- inline bool id_pair::contains (std::unordered_set<id_pair> const & used_ids, std::string const & long_id)
133+ template <typename id_type>
134+ inline bool id_pair::contains (std::unordered_set<id_pair> const & used_ids, id_type const & id)
109135{
110- return id_pair:: find (used_ids, long_id ) != used_ids.end ();
136+ return find (used_ids, id ) != used_ids.end ();
111137}
112138
113139} // namespace sharg::detail
0 commit comments