X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=libs%2Fstring%2Fpooledstring.h;h=a388acffa3a4682e9add5d37b2bb1d357130126a;hb=e4287c28bb2dafedc81c66e63951d947cfbeb225;hp=4ff2d61f68020b830cd77a6058e88b6b13a4d775;hpb=203343b01a7ad87cb3d136689c9936ff5bc23c01;p=xonotic%2Fnetradiant.git diff --git a/libs/string/pooledstring.h b/libs/string/pooledstring.h index 4ff2d61f..a388acff 100644 --- a/libs/string/pooledstring.h +++ b/libs/string/pooledstring.h @@ -1,5 +1,5 @@ -#if !defined(INCLUDED_POOLEDSTRING_H) +#if !defined( INCLUDED_POOLEDSTRING_H ) #define INCLUDED_POOLEDSTRING_H #include @@ -13,24 +13,23 @@ class StringPool : public HashTable Ordered; - Ordered ordered; - std::size_t total = 0; - 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; - pooled += size + 20; - ordered.insert(Ordered::value_type((*i).value, (*i).key)); - } - globalOutputStream() << "total: " << Unsigned(total) << " pooled:" << Unsigned(pooled) << "\n"; - for(Ordered::iterator i = ordered.begin(); i != ordered.end(); ++i) - { - globalOutputStream() << (*i).second << " " << Unsigned((*i).first) << "\n"; - } +inline void StringPool_analyse( StringPool& pool ){ + typedef std::multimap Ordered; + Ordered ordered; + std::size_t total = 0; + 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; + pooled += size + 20; + ordered.insert( Ordered::value_type( ( *i ).value, ( *i ).key ) ); + } + globalOutputStream() << "total: " << Unsigned( total ) << " pooled:" << Unsigned( pooled ) << "\n"; + for ( Ordered::iterator i = ordered.begin(); i != ordered.end(); ++i ) + { + globalOutputStream() << ( *i ).second << " " << Unsigned( ( *i ).first ) << "\n"; + } } @@ -40,68 +39,54 @@ inline void StringPool_analyse(StringPool& pool) template class PooledString { - StringPool::iterator m_i; - static StringPool::iterator increment(StringPool::iterator i) - { - ++(*i).value; - 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 increment(i); - } - static void erase(StringPool::iterator i) - { - if(--(*i).value == 0) - { - char* string = (*i).key; - PoolContext::instance().erase(i); - string_release(string, string_length(string)); - } - } +StringPool::iterator m_i; +static StringPool::iterator increment( StringPool::iterator i ){ + ++( *i ).value; + 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 increment( i ); +} +static void erase( StringPool::iterator i ){ + if ( --( *i ).value == 0 ) { + char* string = ( *i ).key; + PoolContext::instance().erase( i ); + string_release( string, string_length( string ) ); + } +} public: - PooledString() : m_i(insert("")) - { - } - PooledString(const PooledString& other) : m_i(increment(other.m_i)) - { - } - PooledString(const char* string) : m_i(insert(string)) - { - } - ~PooledString() - { - erase(m_i); - } - PooledString& operator=(const PooledString& other) - { - PooledString tmp(other); - tmp.swap(*this); - return *this; - } - PooledString& operator=(const char* string) - { - PooledString tmp(string); - tmp.swap(*this); - return *this; - } - void swap(PooledString& other) - { - std::swap(m_i, other.m_i); - } - bool operator==(const PooledString& other) const - { - return m_i == other.m_i; - } - const char* c_str() const - { - return (*m_i).key; - } +PooledString() : m_i( insert( "" ) ){ +} +PooledString( const PooledString& other ) : m_i( increment( other.m_i ) ){ +} +PooledString( const char* string ) : m_i( insert( string ) ){ +} +~PooledString(){ + erase( m_i ); +} +PooledString& operator=( const PooledString& other ){ + PooledString tmp( other ); + tmp.swap( *this ); + return *this; +} +PooledString& operator=( const char* string ){ + PooledString tmp( string ); + tmp.swap( *this ); + return *this; +} +void swap( PooledString& other ){ + std::swap( m_i, other.m_i ); +} +bool operator==( const PooledString& other ) const { + return m_i == other.m_i; +} +const char* c_str() const { + return ( *m_i ).key; +} };