From: Mattia Basaglia Date: Mon, 20 Jul 2015 20:48:30 +0000 (+0200) Subject: Replace custom HashTable with std::unordered_map - friendly version X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=002a76cedc06e8f8b9a25ee19067caca954e7c90;p=xonotic%2Fnetradiant.git Replace custom HashTable with std::unordered_map - friendly version --- diff --git a/libs/container/cache.h b/libs/container/cache.h index 880a7a7a..3788fb14 100644 --- a/libs/container/cache.h +++ b/libs/container/cache.h @@ -127,11 +127,11 @@ iterator find( const Key& key ){ } void capture( iterator i ){ - ( *i ).value.increment(); + ( *i ).second.increment(); } void release( iterator i ){ - if ( ( *i ).value.decrement() == 0 ) { - CreationPolicy::destroy( ( *i ).value.get() ); + if ( ( *i ).second.decrement() == 0 ) { + CreationPolicy::destroy( ( *i ).second.get() ); m_map.erase( i ); } } @@ -145,11 +145,11 @@ Element& capture( const Key& key ){ } return elem; #else - iterator i = m_map.insert( key, Element() ); - if ( ( *i ).value.increment() == 1 ) { - ( *i ).value.set( CreationPolicy::construct( ( *i ).key ) ); + iterator i = m_map.emplace( key, Element() ).first; + if ( ( *i ).second.increment() == 1 ) { + ( *i ).second.set( CreationPolicy::construct( ( *i ).first ) ); } - return ( *i ).value; + return ( *i ).second; #endif } #else @@ -157,9 +157,9 @@ value_type& capture( const Key& key ){ iterator i = m_map.find( key ); if ( i == m_map.end() ) { i = m_map.insert( key, Element() ); - ( *i ).value.set( CreationPolicy::construct( ( *i ).key ) ); + ( *i ).second.set( CreationPolicy::construct( ( *i ).first ) ); } - ( *i ).value.increment(); + ( *i ).second.increment(); return ( *i ); } #endif diff --git a/libs/container/hashtable.cpp b/libs/container/hashtable.cpp index 5e49e0e8..046267ac 100644 --- a/libs/container/hashtable.cpp +++ b/libs/container/hashtable.cpp @@ -44,7 +44,7 @@ void testStuff(){ for ( MyHashTable::iterator i = hashtable.begin(); i != hashtable.end(); ++i ) { - if ( ( *i ).key != "bleh" ) { + if ( ( *i ).first != "bleh" ) { ++hashtable["count"]; // insertion does not invalidate iterators } } diff --git a/libs/container/hashtable.h b/libs/container/hashtable.h index 2c5677ce..1deb231e 100644 --- a/libs/container/hashtable.h +++ b/libs/container/hashtable.h @@ -22,12 +22,17 @@ #if !defined( INCLUDED_CONTAINER_HASHTABLE_H ) #define INCLUDED_CONTAINER_HASHTABLE_H +#include + +template > + using HashTable = std::unordered_map; + +#if 0 #include #include #include #include "debugging/debugging.h" - namespace HashTableDetail { inline std::size_t next_power_of_two( std::size_t size ){ @@ -239,7 +244,7 @@ BucketNode* bucket_find( Bucket bucket, hash_type hash, const Key& key ){ return 0; } - if ( nodeHash == hash && KeyEqual::operator()( ( *i ).key, key ) ) { + if ( nodeHash == hash && KeyEqual::operator()( ( *i ).first, key ) ) { return i.node(); } } @@ -408,3 +413,5 @@ void clear(){ }; #endif + +#endif diff --git a/libs/string/pooledstring.h b/libs/string/pooledstring.h index a388acff..aa42e45f 100644 --- a/libs/string/pooledstring.h +++ b/libs/string/pooledstring.h @@ -20,10 +20,10 @@ inline void StringPool_analyse( StringPool& pool ){ std::size_t pooled = 0; for ( StringPool::iterator i = pool.begin(); i != pool.end(); ++i ) { - std::size_t size = string_length( ( *i ).key ) + 1; - total += size * ( *i ).value; + std::size_t size = string_length( ( *i ).first ) + 1; + total += size * ( *i ).second; pooled += size + 20; - ordered.insert( Ordered::value_type( ( *i ).value, ( *i ).key ) ); + ordered.insert( Ordered::value_type( ( *i ).second, ( *i ).first ) ); } globalOutputStream() << "total: " << Unsigned( total ) << " pooled:" << Unsigned( pooled ) << "\n"; for ( Ordered::iterator i = ordered.begin(); i != ordered.end(); ++i ) @@ -41,19 +41,19 @@ class PooledString { StringPool::iterator m_i; static StringPool::iterator increment( StringPool::iterator i ){ - ++( *i ).value; + ++( *i ).second; return i; } static StringPool::iterator insert( const char* string ){ StringPool::iterator i = PoolContext::instance().find( const_cast( string ) ); if ( i == PoolContext::instance().end() ) { - return PoolContext::instance().insert( string_clone( string ), 1 ); + return PoolContext::instance().emplace( string_clone( string ), 1 ).first; } return increment( i ); } static void erase( StringPool::iterator i ){ - if ( --( *i ).value == 0 ) { - char* string = ( *i ).key; + if ( --( *i ).second == 0 ) { + char* string = ( *i ).first; PoolContext::instance().erase( i ); string_release( string, string_length( string ) ); } @@ -85,7 +85,7 @@ bool operator==( const PooledString& other ) const { return m_i == other.m_i; } const char* c_str() const { - return ( *m_i ).key; + return ( *m_i ).first; } }; diff --git a/plugins/entity/skincache.cpp b/plugins/entity/skincache.cpp index c65355d9..d4f9b972 100644 --- a/plugins/entity/skincache.cpp +++ b/plugins/entity/skincache.cpp @@ -276,14 +276,14 @@ void realise(){ m_realised = true; for ( Cache::iterator i = m_cache.begin(); i != m_cache.end(); ++i ) { - ( *i ).value->realise( ( *i ).key.c_str() ); + ( *i ).second->realise( ( *i ).first.c_str() ); } } void unrealise(){ m_realised = false; for ( Cache::iterator i = m_cache.begin(); i != m_cache.end(); ++i ) { - ( *i ).value->unrealise(); + ( *i ).second->unrealise(); } g_skins.unrealise(); } diff --git a/radiant/referencecache.cpp b/radiant/referencecache.cpp index e4e03c40..5773fa32 100644 --- a/radiant/referencecache.cpp +++ b/radiant/referencecache.cpp @@ -258,9 +258,9 @@ ModelCache::iterator ModelCache_find( const char* path, const char* name ){ ModelCache::iterator ModelCache_insert( const char* path, const char* name, scene::Node& node ){ if ( g_modelCache_enabled ) { - return g_modelCache.insert( ModelKey( path, name ), NodeSmartReference( node ) ); + return g_modelCache.emplace( ModelKey( path, name ), NodeSmartReference( node ) ).first; } - return g_modelCache.insert( ModelKey( "", "" ), g_nullModel ); + return g_modelCache.emplace( ModelKey( "", "" ), g_nullModel ).first; } void ModelCache_flush( const char* path, const char* name ){ @@ -374,7 +374,7 @@ struct ModelResource : public Resource ); } - setModel( ( *i ).value ); + setModel( ( *i ).second ); } else { @@ -424,7 +424,7 @@ struct ModelResource : public Resource void setNode( scene::Node* node ){ ModelCache::iterator i = ModelCache_find( m_path.c_str(), m_name.c_str() ); if ( i != g_modelCache.end() ) { - ( *i ).value = NodeSmartReference( *node ); + ( *i ).second = NodeSmartReference( *node ); } setModel( NodeSmartReference( *node ) ); @@ -584,8 +584,8 @@ void realise(){ for ( ModelReferencesSnapshot::iterator i = snapshot.begin(); i != snapshot.end(); ++i ) { ModelReferences::value_type& value = *( *i ); - if ( value.value.count() != 1 ) { - value.value.get()->realise(); + if ( value.second.count() != 1 ) { + value.second.get()->realise(); } } } @@ -600,8 +600,8 @@ void unrealise(){ for ( ModelReferencesSnapshot::iterator i = snapshot.begin(); i != snapshot.end(); ++i ) { ModelReferences::value_type& value = *( *i ); - if ( value.value.count() != 1 ) { - value.value.get()->unrealise(); + if ( value.second.count() != 1 ) { + value.second.get()->unrealise(); } } } @@ -613,7 +613,7 @@ void refresh(){ ModelReferencesSnapshot snapshot( m_references ); for ( ModelReferencesSnapshot::iterator i = snapshot.begin(); i != snapshot.end(); ++i ) { - ModelResource* resource = ( *( *i ) ).value.get(); + ModelResource* resource = ( *( *i ) ).second.get(); if ( !resource->isMap() ) { resource->refresh(); } @@ -638,7 +638,7 @@ void SaveReferences(){ ScopeDisableScreenUpdates disableScreenUpdates( "Processing...", "Saving Map" ); for ( HashtableReferenceCache::iterator i = g_referenceCache.begin(); i != g_referenceCache.end(); ++i ) { - ( *i ).value->save(); + ( *i ).second->save(); } MapChanged(); } @@ -646,7 +646,7 @@ void SaveReferences(){ bool References_Saved(){ for ( HashtableReferenceCache::iterator i = g_referenceCache.begin(); i != g_referenceCache.end(); ++i ) { - scene::Node* node = ( *i ).value->getNode(); + scene::Node* node = ( *i ).second->getNode(); if ( node != 0 ) { MapFile* map = Node_getMapFile( *node ); if ( map != 0 && !map->saved() ) { diff --git a/radiant/renderstate.cpp b/radiant/renderstate.cpp index 1078eff5..11c7b64b 100644 --- a/radiant/renderstate.cpp +++ b/radiant/renderstate.cpp @@ -1183,7 +1183,7 @@ OpenGLShaderCache() ~OpenGLShaderCache(){ for ( Shaders::iterator i = m_shaders.begin(); i != m_shaders.end(); ++i ) { - globalOutputStream() << "leaked shader: " << makeQuoted( ( *i ).key.c_str() ) << "\n"; + globalOutputStream() << "leaked shader: " << makeQuoted( ( *i ).first.c_str() ) << "\n"; } } Shader* capture( const char* name ){ @@ -1327,8 +1327,8 @@ void realise(){ for ( Shaders::iterator i = m_shaders.begin(); i != m_shaders.end(); ++i ) { - if ( !( *i ).value.empty() ) { - ( *i ).value->realise( i->key ); + if ( !( *i ).second.empty() ) { + ( *i ).second->realise( i->first ); } } } @@ -1337,8 +1337,8 @@ void unrealise(){ if ( ++m_unrealised == 1 ) { for ( Shaders::iterator i = m_shaders.begin(); i != m_shaders.end(); ++i ) { - if ( !( *i ).value.empty() ) { - ( *i ).value->unrealise(); + if ( !( *i ).second.empty() ) { + ( *i ).second->unrealise(); } } if ( GlobalOpenGL().contextValid && lightingSupported() && lightingEnabled() ) { diff --git a/radiant/textures.cpp b/radiant/textures.cpp index 47200afd..a0aa029b 100644 --- a/radiant/textures.cpp +++ b/radiant/textures.cpp @@ -505,8 +505,8 @@ void realise(){ for ( qtextures_t::iterator i = m_qtextures.begin(); i != m_qtextures.end(); ++i ) { - if ( !( *i ).value.empty() ) { - qtexture_realise( *( *i ).value, ( *i ).key ); + if ( !( *i ).second.empty() ) { + qtexture_realise( *( *i ).second, ( *i ).first ); } } if ( m_observer != 0 ) { @@ -521,8 +521,8 @@ void unrealise(){ } for ( qtextures_t::iterator i = m_qtextures.begin(); i != m_qtextures.end(); ++i ) { - if ( !( *i ).value.empty() ) { - qtexture_unrealise( *( *i ).value ); + if ( !( *i ).second.empty() ) { + qtexture_unrealise( *( *i ).second ); } } } @@ -560,7 +560,7 @@ void Textures_ModeChanged(){ for ( TexturesMap::iterator i = g_texturesmap->begin(); i != g_texturesmap->end(); ++i ) { - glBindTexture( GL_TEXTURE_2D, ( *i ).value->texture_number ); + glBindTexture( GL_TEXTURE_2D, ( *i ).second->texture_number ); SetTexParameters( g_texture_mode ); }