Changeset 422 for trunk/src/io
- Timestamp:
- 07/16/15 19:22:35 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/io/string_table.cc
r421 r422 7 7 #include "nv/io/string_table.hh" 8 8 9 nv::string_table _creator::string_table_creator()9 nv::string_table::string_table( stream& in ) 10 10 { 11 insert(""); // 0 always is empty string 11 uint32 entry_count = 0; 12 in.read( &entry_count, sizeof( entry_count ), 1 ); 13 m_map.reserve( entry_count ); 14 indexer_type::value_type entry; 15 for ( uint32 i = 0; i < entry_count; ) 16 { 17 in.read( &entry, sizeof( entry ), 1 ); 18 // TODO: this performs a existence check - 19 // using a per-container stream implementation would avoid this! 20 m_map.insert( entry ); 21 } 22 23 uint32 data_size = 0; 24 in.read( &data_size, sizeof( data_size ), 1 ); 25 // TODO: either no-init resize, or per-container implementation? 26 m_data.resize( data_size ); 27 in.read( m_data.data(), data_size, 1 ); 12 28 } 13 29 14 nv::string_table_creator::index nv::string_table_creator::insert( const string_view& s)30 void nv::string_table::insert( string_table* in ) 15 31 { 16 uint64 hash_value = hash_string< uint64 >( s.data() );17 auto i = m_map.find( hash_value);18 if ( i != m_map.end())32 m_data.reserve( m_data.size() + in->m_data.size() ); 33 m_map.reserve( m_map.size() + in->m_map.size() ); 34 for ( const auto& hash_index : in->m_map ) 19 35 { 20 return i->second;36 insert( hash_index.first, in->extract_raw( hash_index.second ) ); 21 37 } 22 const char* cs = s.data();23 uint32 cs_size = s.size() + 1;24 NV_ASSERT( m_offsets.size() < index(-1), "Too many strings!" );25 index result = index( m_offsets.size() );26 size_t dsize = m_data.size();27 m_offsets.push_back( dsize );28 m_data.resize( dsize + cs_size );29 raw_copy( cs, cs + cs_size, m_data.data() + dsize );30 m_map[ hash_value ] = result;31 return result;32 38 } 33 39 34 nv:: string_table* nv::string_table_creator::create_table() const40 nv::uint32 nv::string_table::dump_size() const 35 41 { 36 offset* offsets = new offset[m_offsets.size()]; 37 char* data = new char [m_data.size()]; 38 raw_copy( m_offsets.begin(), m_offsets.end(), offsets ); 39 raw_copy( m_data.begin(), m_data.end(), data ); 40 return new string_table( data, m_data.size(), offsets, index( m_offsets.size() ) ); 42 return sizeof( uint32 ) + sizeof( indexer_type::value_type ) * m_map.size() + 43 sizeof( uint32 ) + m_data.size(); 41 44 } 42 45 43 void nv::string_table _creator::dump( nv::stream*out ) const46 void nv::string_table::dump( nv::stream& out ) const 44 47 { 45 index count = index( m_offsets.size() ); 46 uint32 size = m_data.size(); 47 out->write( &count, sizeof( count ), 1 ); 48 out->write( &size, sizeof( size ), 1 ); 49 out->write( m_offsets.data(), sizeof( offset ), count ); 50 out->write( m_data.data(), sizeof( char ), size ); 48 // TODO these should be generic stream operations 49 uint32 entry_count = m_map.size(); 50 out.write( &entry_count, sizeof( entry_count ), 1 ); 51 for ( const auto& entry : m_map ) 52 { 53 out.write( &entry, sizeof( entry ), 1 ); 54 } 55 56 uint32 data_size = m_data.size(); 57 out.write( &data_size, sizeof( data_size ), 1 ); 58 // TODO: either no-init resize, or per-container implementation? 59 out.write( m_data.data(), data_size, 1 ); 51 60 } 52 61 53 const char* nv::string_table_creator::get( index i ) const 62 nv::string_table::size_type nv::string_table::insert_raw( hash_type h, const char* str, length_type length ) 54 63 { 55 return i < m_offsets.size() ? m_data.data() + m_offsets[i] : nullptr; 64 size_type dsize = static_cast<uint32>( m_data.size() ); 65 // TODO : resize without init! 66 m_data.resize( dsize + 2 + length + 1 ); 67 *reinterpret_cast<uint16*>( m_data.data() + dsize ) = length; 68 raw_copy( str, str + length + 1, m_data.data() + dsize + 2 ); 69 m_map[h] = dsize; 70 return dsize; 56 71 } 57 72 58 nv::string_table_creator::index nv::string_table_creator::get( const string_view& s ) const59 {60 uint64 hash_value = hash_string< uint64 >( s.data() );61 auto i = m_map.find( hash_value );62 if ( i != m_map.end() )63 {64 return i->second;65 }66 return 0;67 }68 69 void nv::string_table_creator::clear()70 {71 m_map.clear();72 m_offsets.clear();73 m_data.clear();74 }75 76 nv::uint32 nv::string_table_creator::dump_size() const77 {78 return sizeof( index ) + sizeof( uint32 ) +79 sizeof( offset ) * m_offsets.size() +80 sizeof( char ) * m_data.size();81 }82
Note: See TracChangeset
for help on using the changeset viewer.