25#ifndef JAU_BASIC_COLLECTIONS_HPP_
26#define JAU_BASIC_COLLECTIONS_HPP_
33#include <unordered_map>
34#include <unordered_set>
53 return std::hash<std::string_view>{}(txt);
55 [[nodiscard]]
size_t operator()(std::string_view txt)
const {
56 return std::hash<std::string_view>{}(txt);
58 [[nodiscard]]
size_t operator()(
const std::string &txt)
const {
59 return std::hash<std::string>{}(txt);
66 template<
typename K,
typename V>
73 using StringHashMap = std::unordered_map<std::string, T, string_hash, std::equal_to<>>;
78 using StringHashSet = std::unordered_set<std::string, string_hash, std::equal_to<>>;
86 using StringViewHashMap = std::unordered_map<std::string_view, T, string_hash, std::equal_to<>>;
93 using StringViewHashSet = std::unordered_set<std::string_view, string_hash, std::equal_to<>>;
115 template<
typename Key_type,
typename Value_type,
116 typename Novalue_type, Novalue_type No_value,
117 typename Hash_functor = std::hash<Key_type>,
118 typename Query_type = Key_type,
119 typename KeyEqual = std::equal_to<>,
120 typename Allocator = std::allocator<std::pair<const Key_type, Value_type>> >
121 requires std::constructible_from<Value_type, Novalue_type> &&
122 std::constructible_from<Key_type, Query_type>
135 typedef std::unordered_map<key_type, value_type, hash_functor, KeyEqual, Allocator>
HashMapType;
159 auto it = m_map.find(key);
160 if( it != m_map.end() ) {
167 auto it = m_map.find(key);
168 if( it != m_map.end() ) {
176 auto it = m_map.find(key);
177 if( it != m_map.end() ) {
185 return m_map.contains(key);
190 for (
const std::pair<const key_type, value_type>& n : m_map) {
191 if( n.second ==
value ) {
192 return std::optional<const key_type&>{n.first};
203 return m_map.insert( {key,
obj} ).second;
211 requires (!std::same_as<key_type, query_type>) &&
212 std::same_as<Q, query_type>
214 return m_map.insert( {
key_type(key),
obj} ).second;
222 auto it = m_map.find(key);
223 if( it != m_map.end() ) {
227 m_map.insert( {key,
obj} );
236 requires (!std::same_as<key_type, query_type>) &&
237 std::same_as<Q, query_type>
239 auto it = m_map.find(key);
240 if( it != m_map.end() ) {
257 auto it = m_map.find(key);
258 if( it != m_map.end() ) {
262 auto [ it2, res ] = m_map.insert( {key,
obj} );
263 return res ? it2->second :
novalue();
275 requires (!std::same_as<key_type, query_type>) &&
276 std::same_as<Q, query_type>
278 auto it = m_map.find(key);
279 if( it != m_map.end() ) {
283 auto [ it2, res ] = m_map.insert( {
key_type(key),
obj} );
284 return res ? it2->second :
novalue();
295 auto it = m_map.find(key);
296 if (it != m_map.end()) {
301 m_map.insert({ key,
obj });
313 requires (!std::same_as<key_type, query_type>) &&
314 std::same_as<Q, query_type>
316 auto it = m_map.find(key);
317 if( it != m_map.end() ) {
331 auto it = m_map.find(key);
332 if( it != m_map.end() ) {
344 requires (!std::same_as<key_type, query_type>) &&
345 std::same_as<Q, query_type>
347 auto it = m_map.find(key);
348 if( it != m_map.end() ) {
357 auto it = m_map.find(key);
358 if( it != m_map.end() ) {
367 requires (!std::same_as<key_type, query_type>) &&
368 std::same_as<Q, query_type>
370 auto it = m_map.find(key);
371 if( it != m_map.end() ) {
386 auto it = m_map.find(key);
387 if( it != m_map.end() ) {
403 requires (!std::same_as<key_type, query_type>) &&
404 std::same_as<Q, query_type>
406 auto it = m_map.find(key);
407 if( it != m_map.end() ) {
421 template<
typename Value_type,
typename Novalue_type, Novalue_type no_value>
431 template<
typename Value_type,
typename Novalue_type, Novalue_type no_value>
HashMapWrap, generic std::unordered_map exposing a more higher level API.
bool insert(const Q &key, const_reference obj)
Adds a new mapping of the value for the given key, does nothing if a mapping exists.
value_type put3(const Q &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
Hash_functor hash_functor
value_type remove2(const query_type &key)
Removes value if mapped and returns it, otherwise returns no_value.
bool replace(const Q &key, const_reference obj)
Replaces the already mapped value for the given key, does nothing if no mapping exists.
bool containsKey(const query_type &key) const
Returns true if the given key maps to a value or no_value.
const_reference get(const query_type &key) const
Returns the immutable mapped value reference for the given key or novalue()
reference put2(const key_type &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
constexpr reference novalue() noexcept
Returns the no-value mutable refererence, no_value (should not be writable or written to)
bool put(const Q &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
HashMapType & map() noexcept
converts novalue_type No_value to lvalue value_type to return its reference
bool put(const key_type &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
bool remove(const Q &key)
Removes value if mapped and returns true, otherwise returns false.
Novalue_type novalue_type
std::unordered_map< key_type, value_type, hash_functor, KeyEqual, Allocator > HashMapType
void clear()
Clears the hash map.
const value_type & const_reference
value_type remove2(const Q &key)
Removes value if mapped and returns it, otherwise returns no_value.
constexpr const_reference novalue() const noexcept
Returns the no-value immutable refererence no_value
bool replace(const key_type &key, const_reference obj)
Replaces the already mapped value for the given key, does nothing if no mapping exists.
std::optional< const key_type & > containsValue(const_reference value) const
Returns the key reference of the first value, otherwise std::nullopt.
size_type size() const noexcept
bool insert(const key_type &key, const_reference obj)
Adds a new mapping of the value for the given key, does nothing if a mapping exists.
reference put2(const Q &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
value_type put3(const key_type &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
const pair_type * find(const query_type &key) const
Returns the immutable pair_type pointer for the given key or nullptr.
HashMapType::size_type size_type
const HashMapType & map() const noexcept
bool remove(const query_type &key)
Removes value if mapped and returns true, otherwise returns false.
HashMapType::value_type pair_type
reference get(const query_type &key)
Returns the mutable mapped value reference for the given key or novalue()
std::unordered_map< std::string_view, T, string_hash, std::equal_to<> > StringViewHashMap
std::unordered_map using std::string_view key and heterogenous jau::string_hash functor,...
HashMapWrap< std::string, Value_type, Novalue_type, no_value, jau::string_hash, std::string_view > StringHashMapWrap
StringHashMapWrap, generic std::unordered_map exposing a more higher level API.
std::unordered_map< K, V, jau::string_hash, std::equal_to<> > StringlikeHashMap
std::unordered_map using K key and heterogenous jau::string_hash functor
HashMapWrap< std::string_view, Value_type, Novalue_type, no_value, jau::string_hash > StringViewHashMapWrap
StringHashViewMapWrap, generic std::unordered_map exposing a more higher level API,...
std::unordered_set< std::string, string_hash, std::equal_to<> > StringHashSet
std::unordered_set using std::string key and heterogenous jau::string_hash functor
std::unordered_map< std::string, T, string_hash, std::equal_to<> > StringHashMap
std::unordered_map using std::string key and heterogenous jau::string_hash functor
std::unordered_set< std::string_view, string_hash, std::equal_to<> > StringViewHashSet
std::unordered_set using std::string_view key and heterogenous jau::string_hash functor,...
constexpr bool value(const Bool rhs) noexcept
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
C++20: Heterogeneous Lookup in (Un)ordered Containers.
size_t operator()(const std::string &txt) const
size_t operator()(const char *txt) const
size_t operator()(std::string_view txt) const