diff --git a/Utility/StringMap.h b/Utility/StringMap.h index 1c05a07..1b8d397 100644 --- a/Utility/StringMap.h +++ b/Utility/StringMap.h @@ -9,16 +9,29 @@ static_assert( __cplusplus > 2020'99 ); #include #include +#include + #include +#include + namespace Alepha::Hydrogen::Utility ::detail:: StringMap_m { + namespace C + { + const bool debug= false; + const bool debugCompare= false or C::debug; + const bool debugInsertion= false or C::debug; + } + inline namespace exports { template< typename Value > class StringMap; } + using namespace Alepha::Algorithm::exports::string_distance_m; + template< typename Value > class exports::StringMap { @@ -27,8 +40,9 @@ namespace Alepha::Hydrogen::Utility ::detail:: StringMap_m template< typename Self > Value & - at_impl( Self &self, const std::string_view key ) + at_impl( Self &self, const std::string &key ) { + if( C::debugInsertion ) error() << "Map has " << storage.size() << " entries" << std::endl; if( not storage.contains( key ) ) { const auto closest= std::min_element( begin( storage ), end( storage ), @@ -36,26 +50,33 @@ namespace Alepha::Hydrogen::Utility ::detail:: StringMap_m { const auto l= optimalStringDistance( lhs.first, key ); const auto r= optimalStringDistance( rhs.first, key ); + if( C::debugCompare ) error() << "Left value: " << l << " right value: " << r << std::endl; return l < r; } ); - if( optimalStringDistance( closest.first, key ) < sqrt( key.size() ) ) + if( closest == end( storage ) ) throw std::out_of_range{ IOStreams::String() << "The key `" << key << "` could not be found." }; + else if( optimalStringDistance( closest->first, key ) < sqrt( key.size() ) ) { - throw std::out_of_range{ Utility::String() << "The key `" << key << "` could not be found. Did you mean `" - << closest.first << "`?" }; + throw std::out_of_range{ IOStreams::String() << "The key `" << key << "` could not be found. Did you mean `" + << closest->first << "`?" }; } - throw std::out_of_range{ Utility::String() << "The key `" << key << "` could not be found." }; + throw std::out_of_range{ IOStreams::String() << "The key `" << key << "` could not be found." }; } return storage.at( key ); } public: - Value &at( const std::string_view key ) { return at_impl( *this, key ); } - const Value &at( const std::string_view key ) const { return at_impl( *this, key ); } + Value &at( const std::string & key ) { return at_impl( *this, key ); } + const Value &at( const std::string &key ) const { return at_impl( *this, key ); } - bool contains( const std::string_view key ) const { return storage.contains( key ); } + bool contains( const std::string &key ) const { return storage.contains( key ); } - void insert( const std::string_view key, Value v ) { if( contains( key ) ) storage.insert( { key, std::move( v ) } ); } + void + insert( const std::string &key, Value v ) + { + if( not contains( key ) ) storage.emplace( key, std::move( v ) ); + if( C::debugInsertion ) error() << "Map has " << storage.size() << " entries" << std::endl; + } };