source: trunk/src/io/string_table.cc @ 421

Last change on this file since 421 was 421, checked in by epyon, 10 years ago
  • move ops for data_channel_set
  • string_table works on string_views, stores hashes
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/io/string_table.hh"
8
9nv::string_table_creator::string_table_creator()
10{
11        insert(""); // 0 always is empty string
12}
13
14nv::string_table_creator::index nv::string_table_creator::insert( const string_view& s )
15{
16        uint64 hash_value = hash_string< uint64 >( s.data() );
17        auto i = m_map.find( hash_value );
18        if ( i != m_map.end() )
19        {
20                return i->second;
21        }
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}
33
34nv::string_table* nv::string_table_creator::create_table() const
35{
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() ) );
41}
42
43void nv::string_table_creator::dump( nv::stream* out ) const
44{
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 );
51}
52
53const char* nv::string_table_creator::get( index i ) const
54{
55        return i < m_offsets.size() ? m_data.data() + m_offsets[i] : nullptr;
56}
57
58nv::string_table_creator::index nv::string_table_creator::get( const string_view& s ) const
59{
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
69void nv::string_table_creator::clear()
70{
71        m_map.clear();
72        m_offsets.clear();
73        m_data.clear();
74}
75
76nv::uint32 nv::string_table_creator::dump_size() const
77{
78        return sizeof( index ) + sizeof( uint32 ) +
79                sizeof( offset ) * m_offsets.size() +
80                sizeof( char ) * m_data.size();
81}
82
Note: See TracBrowser for help on using the repository browser.