1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <unordered_map>
#include <vector>
#include "string_id.h"
namespace
{
using InternMapType = std::unordered_map<std::string, int>;
using ReverseLookupVecType = std::vector<const std::string *>;
} // namespace
static InternMapType &get_intern_map()
{
static InternMapType map{};
return map;
}
static ReverseLookupVecType &get_reverse_lookup_vec()
{
static ReverseLookupVecType vec{};
return vec;
}
template<typename S>
static int universal_string_id_intern( S &&s )
{
int next_id = get_reverse_lookup_vec().size();
const auto &pair = get_intern_map().emplace( std::forward<S>( s ), next_id );
if( pair.second ) { // inserted
get_reverse_lookup_vec().push_back( &pair.first->first );
}
return pair.first->second;
}
int string_identity_static::string_id_intern( const std::string &s )
{
return universal_string_id_intern( s );
}
int string_identity_static::string_id_intern( std::string &s )
{
return universal_string_id_intern( s );
}
int string_identity_static::string_id_intern( std::string &&s )
{
return universal_string_id_intern( std::move( s ) );
}
const std::string &string_identity_static::get_interned_string( int id )
{
return *get_reverse_lookup_vec()[id];
}
int string_identity_static::empty_interned_string()
{
static int empty_string_id = string_id_intern( "" );
return empty_string_id;
}
|