source: trunk/nv/stl/string_table.hh

Last change on this file was 485, checked in by epyon, 9 years ago
  • massive update (need to start doing atomics again) :/
File size: 2.5 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/**
8 * @file string_table.hh
9 * @author Kornel Kisielewicz
10 */
11
12#ifndef NV_STL_STRING_TABLE_HH
13#define NV_STL_STRING_TABLE_HH
14
15#include <nv/common.hh>
16#include <nv/stl/vector.hh>
17#include <nv/stl/string.hh>
18#include <nv/stl/hash_store.hh>
19#include <nv/stl/stream.hh>
20
21namespace nv
22{
23
24        class string_table : noncopyable
25        {
26        public:
27                typedef string_view value_type;
28                typedef shash64 key_type;
29                typedef uint64 hash_type;
30                typedef uint32 size_type;
31                typedef uint16 length_type;
32                typedef hash_store< hash_type, size_type > indexer_type;
33                typedef vector< char > storage_type;
34                typedef indexer_type::const_iterator const_iterator;
35
36                string_table() {}
37                string_table( stream& in );
38                void insert( string_table* in );
39
40                key_type insert( const value_type& str )
41                {
42                        hash_type hash_value = str.get_hash< uint64 >();
43                        insert( hash_value, str );
44                        return key_type( hash_value );
45                }
46
47                bool exists( key_type i ) const
48                {
49                        return m_map.find( i.value() ) != m_map.end();
50                }
51
52                value_type at( key_type i ) const
53                {
54                        const auto& it = m_map.find( i.value() );
55                        NV_ASSERT_ALWAYS( it != m_map.end(), "Key not found in string_table!" );
56                        return extract_raw( it->second );
57                }
58
59                value_type operator[]( key_type i ) const
60                {
61                        const auto& it = m_map.find( i.value() );
62                        return it != m_map.end() ? extract_raw( it->second ) : value_type();
63                }
64
65                uint32 size() const { return m_map.size(); }
66                bool empty() const { return m_map.empty(); }
67                uint32 dump_size() const;
68                void dump( stream& out ) const;
69        protected:
70                void insert( hash_type h, const value_type& str )
71                {
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() ) );
80                }
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
86                {
87                        uint16 length = *reinterpret_cast<const uint16*>( m_data.data() + index );
88                        return value_type( m_data.data() + index + 2, length );
89                }
90
91                indexer_type m_map;
92                storage_type m_data;
93        };
94
95}
96
97#endif // NV_STL_STRING_TABLE_HH
Note: See TracBrowser for help on using the repository browser.