[395] | 1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
[279] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[279] | 6 |
|
---|
| 7 | /**
|
---|
| 8 | * @file string_table.hh
|
---|
| 9 | * @author Kornel Kisielewicz
|
---|
| 10 | */
|
---|
| 11 |
|
---|
[428] | 12 | #ifndef NV_STL_STRING_TABLE_HH
|
---|
| 13 | #define NV_STL_STRING_TABLE_HH
|
---|
[279] | 14 |
|
---|
[395] | 15 | #include <nv/common.hh>
|
---|
[392] | 16 | #include <nv/stl/vector.hh>
|
---|
| 17 | #include <nv/stl/string.hh>
|
---|
[432] | 18 | #include <nv/stl/hash_store.hh>
|
---|
[422] | 19 | #include <nv/stl/stream.hh>
|
---|
[279] | 20 |
|
---|
| 21 | namespace nv
|
---|
| 22 | {
|
---|
[422] | 23 |
|
---|
[279] | 24 | class string_table : noncopyable
|
---|
| 25 | {
|
---|
| 26 | public:
|
---|
[422] | 27 | typedef string_view value_type;
|
---|
[431] | 28 | typedef shash64 key_type;
|
---|
[422] | 29 | typedef uint64 hash_type;
|
---|
| 30 | typedef uint32 size_type;
|
---|
| 31 | typedef uint16 length_type;
|
---|
[432] | 32 | typedef hash_store< hash_type, size_type > indexer_type;
|
---|
[422] | 33 | typedef vector< char > storage_type;
|
---|
| 34 | typedef indexer_type::const_iterator const_iterator;
|
---|
[280] | 35 |
|
---|
[422] | 36 | string_table() {}
|
---|
| 37 | string_table( stream& in );
|
---|
| 38 | void insert( string_table* in );
|
---|
[485] | 39 |
|
---|
[422] | 40 | key_type insert( const value_type& str )
|
---|
[279] | 41 | {
|
---|
[431] | 42 | hash_type hash_value = str.get_hash< uint64 >();
|
---|
[422] | 43 | insert( hash_value, str );
|
---|
[431] | 44 | return key_type( hash_value );
|
---|
[422] | 45 | }
|
---|
[279] | 46 |
|
---|
[422] | 47 | bool exists( key_type i ) const
|
---|
[279] | 48 | {
|
---|
[431] | 49 | return m_map.find( i.value() ) != m_map.end();
|
---|
[279] | 50 | }
|
---|
| 51 |
|
---|
[422] | 52 | value_type at( key_type i ) const
|
---|
[279] | 53 | {
|
---|
[431] | 54 | const auto& it = m_map.find( i.value() );
|
---|
[422] | 55 | NV_ASSERT_ALWAYS( it != m_map.end(), "Key not found in string_table!" );
|
---|
| 56 | return extract_raw( it->second );
|
---|
[279] | 57 | }
|
---|
| 58 |
|
---|
[422] | 59 | value_type operator[]( key_type i ) const
|
---|
[279] | 60 | {
|
---|
[431] | 61 | const auto& it = m_map.find( i.value() );
|
---|
[422] | 62 | return it != m_map.end() ? extract_raw( it->second ) : value_type();
|
---|
[279] | 63 | }
|
---|
| 64 |
|
---|
[425] | 65 | uint32 size() const { return m_map.size(); }
|
---|
| 66 | bool empty() const { return m_map.empty(); }
|
---|
[422] | 67 | uint32 dump_size() const;
|
---|
| 68 | void dump( stream& out ) const;
|
---|
| 69 | protected:
|
---|
| 70 | void insert( hash_type h, const value_type& str )
|
---|
[279] | 71 | {
|
---|
[422] | 72 | NV_ASSERT_ALWAYS( str.size() < 0xFFF0, "String table can only hold strings up to 64k!" );
|
---|
| 73 | auto it = m_map.find( h );
|
---|
| 74 | if ( it != m_map.end() )
|
---|
| 75 | {
|
---|
| 76 | // TODO : perform comparison check if in debug mode!
|
---|
| 77 | return;
|
---|
| 78 | }
|
---|
| 79 | insert_raw( h, str.data(), static_cast<length_type>( str.size() ) );
|
---|
[279] | 80 | }
|
---|
[422] | 81 |
|
---|
| 82 |
|
---|
| 83 | size_type insert_raw( hash_type h, const char* str, length_type s );
|
---|
| 84 |
|
---|
| 85 | value_type extract_raw( size_type index ) const
|
---|
[279] | 86 | {
|
---|
[422] | 87 | uint16 length = *reinterpret_cast<const uint16*>( m_data.data() + index );
|
---|
| 88 | return value_type( m_data.data() + index + 2, length );
|
---|
[279] | 89 | }
|
---|
| 90 |
|
---|
[422] | 91 | indexer_type m_map;
|
---|
| 92 | storage_type m_data;
|
---|
[279] | 93 | };
|
---|
| 94 |
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[428] | 97 | #endif // NV_STL_STRING_TABLE_HH
|
---|