source: trunk/src/stl/string_table.cc @ 481

Last change on this file since 481 was 428, checked in by epyon, 10 years ago
  • renaming mesh_nodes_data members to more compliant
  • moved string_table to stl
File size: 2.3 KB
Line 
1// Copyright (C) 2014-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/stl/string_table.hh"
8
9nv::string_table::string_table( stream& in )
10{
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; i++ )
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 );
28}
29
30void nv::string_table::insert( string_table* in )
31{
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 )
35        {
36                insert( hash_index.first, in->extract_raw( hash_index.second ) );
37        }
38}
39
40nv::uint32 nv::string_table::dump_size() const
41{
42        return sizeof( uint32 ) + sizeof( indexer_type::value_type ) * m_map.size() +
43                sizeof( uint32 ) + m_data.size();
44}
45
46void nv::string_table::dump( nv::stream& out ) const
47{
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 );
60}
61
62nv::string_table::size_type  nv::string_table::insert_raw( hash_type h, const char* str, length_type length )
63{
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        if ( length > 0 )
69                raw_copy( str, str + length, m_data.data() + dsize + 2 );
70        m_data[dsize + 2 + length] = 0;
71        m_map[h] = dsize;
72        return dsize;
73}
74
Note: See TracBrowser for help on using the repository browser.