source: trunk/nv/stl/string_table.hh @ 431

Last change on this file since 431 was 431, checked in by epyon, 10 years ago
  • hash storage type
  • string hash storage type
  • several minor fixes
  • more cleanups needed
File size: 2.5 KB
RevLine 
[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>
18#include <nv/stl/unordered_map.hh>
[422]19#include <nv/stl/stream.hh>
[279]20
21namespace 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;
[431]32                typedef unordered_map< 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 );
39                key_type insert( const value_type& str )
[279]40                {
[431]41                        hash_type hash_value = str.get_hash< uint64 >();
[422]42                        insert( hash_value, str );
[431]43                        return key_type( hash_value );
[422]44                }
[279]45
[422]46                bool exists( key_type i ) const
[279]47                {
[431]48                        return m_map.find( i.value() ) != m_map.end();
[279]49                }
50
[422]51                value_type at( key_type i ) const
[279]52                {
[431]53                        const auto& it = m_map.find( i.value() );
[422]54                        NV_ASSERT_ALWAYS( it != m_map.end(), "Key not found in string_table!" );
55                        return extract_raw( it->second );
[279]56                }
57
[422]58                value_type operator[]( key_type i ) const
[279]59                {
[431]60                        const auto& it = m_map.find( i.value() );
[422]61                        return it != m_map.end() ? extract_raw( it->second ) : value_type();
[279]62                }
63
[425]64                uint32 size() const { return m_map.size(); }
65                bool empty() const { return m_map.empty(); }
[422]66                uint32 dump_size() const;
67                void dump( stream& out ) const;
68        protected:
69                void insert( hash_type h, const value_type& str )
[279]70                {
[422]71                        NV_ASSERT_ALWAYS( str.size() < 0xFFF0, "String table can only hold strings up to 64k!" );
72                        auto it = m_map.find( h );
73                        if ( it != m_map.end() )
74                        {
75                                // TODO : perform comparison check if in debug mode!
76                                return;
77                        }
78                        insert_raw( h, str.data(), static_cast<length_type>( str.size() ) );
[279]79                }
[422]80
81
82                size_type insert_raw( hash_type h, const char* str, length_type s );
83
84                value_type extract_raw( size_type index ) const
[279]85                {
[422]86                        uint16 length = *reinterpret_cast<const uint16*>( m_data.data() + index );
87                        return value_type( m_data.data() + index + 2, length );
[279]88                }
89
[422]90                indexer_type m_map;
91                storage_type m_data;
[279]92        };
93
94}
95
[428]96#endif // NV_STL_STRING_TABLE_HH
Note: See TracBrowser for help on using the repository browser.