Changeset 538


Ignore:
Timestamp:
01/23/17 21:04:56 (8 years ago)
Author:
epyon
Message:
  • ECS rollback of usage of index_table
  • ECS prototype hashed_index_table
  • lua iterator interface
  • lua stack_proxy interface
  • table_guard iteration
Location:
trunk
Files:
4 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/ecs/component.hh

    r537 r538  
    2929        {
    3030
    31                 template < typename Ecs, typename Component >
     31                template <
     32                        typename Ecs,
     33                        typename Component,
     34                        template < typename, typename > class IndexTable = index_table
     35                >
    3236                class component : public Ecs::component_interface
    3337                {
    3438                public:
    35                         typedef Ecs                                      ecs_type;
    36                         typedef typename ecs_type::message               message_type;
    37                         typedef typename ecs_type::handle_type           handle_type;
    38                         typedef typename ecs_type::time_type             time_type;
    39                         typedef Component                                value_type;
    40                         typedef Component                                component_type;
    41                         typedef index_storage< handle_type, value_type > storage_type;
    42                         typedef typename storage_type::index_type        index_type;
     39                        typedef Ecs                                    ecs_type;
     40                        typedef typename ecs_type::message             message_type;
     41                        typedef typename ecs_type::handle_type         handle_type;
     42                        typedef typename ecs_type::time_type           time_type;
     43                        typedef IndexTable< handle_type, sint32 >      index_table_type;
     44                        typedef Component                              value_type;
     45                        typedef Component                              component_type;
     46                        typedef vector< value_type >                   storage_type;
     47                        typedef typename index_table_type::index_type  index_type;
    4348
    44                         typedef typename storage_type::iterator          iterator;
    45                         typedef typename storage_type::const_iterator    const_iterator;
    46                         typedef typename storage_type::reference         reference;
    47                         typedef typename storage_type::const_reference   const_reference;
     49                        typedef typename storage_type::iterator        iterator;
     50                        typedef typename storage_type::const_iterator  const_iterator;
     51                        typedef typename storage_type::reference       reference;
     52                        typedef typename storage_type::const_reference const_reference;
    4853
    4954                        component( ecs_type& a_ecs, string_view a_name, uint32 reserve = 0 )
    50                                 : m_ecs( a_ecs ), m_data( reserve )
     55                                : m_ecs( a_ecs )
    5156                        {
    5257                                m_ecs.register_component<component_type>( a_name, this );
     58                                if ( reserve != 0 )
     59                                {
     60                                        m_data.reserve( reserve );
     61                                }
    5362                        }
    5463
    55                         inline value_type& insert( handle_type h ) { return m_data.insert( h ); }
     64                        inline value_type& insert( handle_type h )
     65                        {
     66                                index_type i = m_index.insert( h );
     67                                NV_ASSERT( i == index_type( m_data.size() ), "Fail!" );
     68                                NV_UNUSED( i );
     69                                m_data.emplace_back();
     70                                return m_data.back();
     71                        }
    5672
    5773                        template < typename ...Args >
    5874                        value_type& insert( handle_type h, Args&&... args )
    5975                        {
    60                                 return m_data.insert( h, nv::forward<Args>( args )... );
     76                                index_type i = m_index.insert( h );
     77                                NV_ASSERT( i == index_type( m_data.size() ), "Fail!" );
     78                                NV_UNUSED( i );
     79                                m_data.emplace_back( nv::forward<Args>( args )... );
     80                                return m_data.back();
    6181                        }
    6282
    6383                        bool exists( handle_type h )
    6484                        {
    65                                 return m_data.exists( h );
     85                                return m_index.exists( h );
    6686                        }
    6787
     
    7393                        virtual void clear()
    7494                        {
    75                                 for ( uint32 i = 0; i < size(); ++i )
     95                                for ( uint32 i = 0; i < m_data.size(); ++i )
    7696                                        destroy( &m_data[i] );
     97                                m_index.clear();
    7798                                m_data.clear();
    7899                        }
    79100
    80                         value_type* get( handle_type h ) { return m_data.get(h); }
    81                         const value_type* get( handle_type h ) const { return m_data.get( h ); }
     101                        value_type* get( handle_type h )
     102                        {
     103                                index_type i = m_index.get( h );
     104                                return i >= 0 ? &( m_data[unsigned( i )] ) : nullptr;
     105                        }
     106                        const value_type* get( handle_type h ) const
     107                        {
     108                                index_type i = m_index.get( h );
     109                                return i >= 0 ? &( m_data[unsigned( i )] ) : nullptr;
     110                        }
    82111
    83112                        void* get_raw( handle_type h ) { return get( h ); }
     
    86115                        virtual void remove( handle_type h )
    87116                        {
    88                                 value_type* v = m_data.get( h );
     117                                value_type* v = get( h );
    89118                                if ( v == nullptr ) return;
    90119                                destroy( v );
    91                                 m_data.remove( h );
     120                                index_type dead_eindex = m_index.remove_swap( h );
     121                                if ( dead_eindex == -1 ) return;
     122                                if ( dead_eindex != static_cast<index_type>( m_data.size() - 1 ) )
     123                                {
     124                                        m_data[unsigned( dead_eindex )] = move( m_data.back() );
     125                                }
     126                                m_data.pop_back();
    92127                        }
    93128
     
    106141                                clear();
    107142                        }
    108 
    109 
    110                         inline handle_type get_handle( index_type i ) const { return m_data.get_handle( i ); }
     143                       
     144                        inline handle_type get_handle( index_type i ) const { return m_index.get_handle( i ); }
    111145
    112146                        inline const value_type& operator[] ( index_type i ) const { return m_data[i]; }
     
    124158                protected:
    125159                        ecs_type&        m_ecs;
     160                        index_table_type m_index;
    126161                        storage_type     m_data;
    127162                };
  • trunk/nv/ecs/component_storage.hh

    r537 r538  
    1818#include <nv/stl/index_table.hh>
    1919#include <nv/stl/handle_manager.hh>
     20#include <nv/stl/hash_map.hh>
    2021
    2122namespace nv
     
    3233                        typedef Component                              value_type;
    3334                        typedef vector< value_type >                   storage_type;
    34                         typedef index_table< handle_type >             index_table_type;
    35                         typedef typename index_table_type::index_type  index_type;
     35                        typedef sint32                                 index_type;
    3636                        typedef typename storage_type::iterator        iterator;
    3737                        typedef typename storage_type::const_iterator  const_iterator;
     
    297297
    298298                class direct_storage;
    299                 class hash_storage;
     299                /*
     300                template < typename Handle, typename Component >
     301                class hash_storage
     302                {
     303                public:
     304                        typedef Handle                                 handle_type;
     305                        typedef Component                              value_type;
     306                        typedef vector< value_type >                   storage_type;
     307                        typedef typename storage_type::iterator        iterator;
     308                        typedef typename storage_type::const_iterator  const_iterator;
     309                        typedef typename storage_type::reference       reference;
     310                        typedef typename storage_type::const_reference const_reference;
     311               
     312                        explicit hash_storage( uint32 reserve = 0 )
     313                        {
     314                                if ( reserve != 0 )
     315                                {
     316                                        m_data.reserve( reserve );
     317                                }
     318                        }
     319
     320                        inline value_type& insert( handle_type h )
     321                        {
     322                                auto result = m_data.insert_key( h );
     323                                NV_ASSERT( result.second, "Reinsert of handle!" );
     324                                m_data.emplace_back();
     325                                return m_data.back();
     326                        }
     327
     328                        template < typename ...Args >
     329                        inline value_type& insert( handle_type h, Args&&... args )
     330                        {
     331                                insert_index( h );
     332                                m_data.emplace_back( nv::forward<Args>( args )... );
     333                                return m_data.back();
     334                        }
     335
     336                        inline bool exists( handle_type h )
     337                        {
     338                                if ( h.is_nil() || h.index() >= m_indexes.size() ) return false;
     339                                return m_indexes[h.index()] >= 0;
     340                        }
     341
     342                        value_type* get( handle_type h )
     343                        {
     344                                if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr;
     345                                index_type i = m_indexes[h.index()];
     346                                return i >= 0 ? &( m_data[unsigned( i )] ) : nullptr;
     347                        }
     348
     349                        const value_type* get( handle_type h ) const
     350                        {
     351                                if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr;
     352                                index_type i = m_indexes[h.index()];
     353                                return i >= 0 ? &( m_data[unsigned( i )] ) : nullptr;
     354                        }
     355
     356                        void clear()
     357                        {
     358                                m_data.clear();
     359                        }
     360
     361                        void remove( handle_type h )
     362                        {
     363
     364
     365                        }
     366
     367                        inline handle_type get_handle( sint32 i ) const { return m_handles[unsigned( i )]; }
     368
     369                        inline const value_type& operator[] ( index_type i ) const { return m_data[i]; }
     370                        inline value_type& operator[] ( index_type i ) { return m_data[i]; }
     371
     372                        inline size_t size() const { return m_data.size(); }
     373
     374                        inline iterator        begin() { return m_data.begin(); }
     375                        inline const_iterator  begin()  const { return m_data.cbegin(); }
     376
     377                        inline iterator        end() { return m_data.end(); }
     378                        inline const_iterator  end()  const { return m_data.cend(); }
     379
     380                protected:
     381                        vector< value_type > m_data;
     382                };
     383
     384
    300385                class hash_multi_storage;
    301 
     386                */
    302387        }
    303388
  • trunk/nv/lua/lua_raw.hh

    r533 r538  
    1 // Copyright (C) 2012-2015 ChaosForge Ltd
     1// Copyright (C) 2012-2017 ChaosForge Ltd
    22// http://chaosforge.org/
    33//
  • trunk/nv/lua/lua_state.hh

    r534 r538  
    1919#include <nv/stl/type_traits/function.hh>
    2020
     21#include <nv/lua/lua_proxy.hh>
     22#include <nv/lua/lua_iterator.hh>
    2123#include <nv/lua/lua_handle.hh>
    2224#include <nv/lua/lua_path.hh>
     
    3739
    3840                class state;
     41                class lua_iterator_provider;
    3942
    4043                using lua_rtti_read_function  = bool(*)( state*, const type_entry*, void*, int index );
     
    201204
    202205                        const type_data* get_type_data() const { return m_lua_types; }
     206
     207
     208
    203209
    204210                protected:
     
    256262                        int get_stack_size();
    257263                        void log_stack();
     264
    258265                        lua_State* get_raw();
    259266                        ref register_object( void* o, string_view lua_name );
     
    363370                };
    364371
     372
     373
     374
    365375                class table_guard : public state_wrapper
    366376                {
     
    370380                        virtual ~table_guard();
    371381                        uint32 get_size();
    372                        
     382                        iterator_provider< kv_iterator > pairs()
     383                        {
     384                                return iterator_provider< kv_iterator >( m_parent, -1 );
     385                        }
     386                        iterator_provider< key_iterator > keys()
     387                        {
     388                                return iterator_provider< key_iterator >( m_parent, -1 );
     389                        }
     390                        iterator_provider< value_iterator > values()
     391                        {
     392                                return iterator_provider< value_iterator >( m_parent, -1 );
     393                        }
     394
     395// TO FUCKING DO:
     396//                      const stack_proxy operator[]( const string_view& key ) const;
     397//                      const stack_proxy operator[]( sint32 key ) const;
     398//                      stack_proxy operator[]( const string_view& key );
     399//                      stack_proxy operator[]( sint32 key );
     400
     401
    373402                        bool has_field( string_view element );
    374403                       
  • trunk/nv/stl/handle.hh

    r533 r538  
    1616#include <nv/stl/vector.hh>
    1717#include <nv/stl/index_table.hh>
     18#include <nv/stl/functional/hash.hh>
    1819
    1920namespace nv
     
    4647
    4748                constexpr T index() const { return m_index; }
    48                 //uint32 hash() const { return hash<T>()( T( m_counter << IBITS | m_index ) ); }
    49                 constexpr uint32 hash() const { NV_ASSERT( false, "UNIMPLEMENTED!" ); return 0; }
     49                template < typename H = uint64 >
     50                constexpr H hash() const { return nv::hash<T,H>()( T( m_counter << IBITS | m_index ) ); }
    5051        protected:
    5152                T m_index   : IBITS;
     
    5758        };
    5859
     60        template <
     61                typename H,
     62                typename T,
     63                unsigned IBITS,
     64                unsigned CBITS,
     65                typename TAG
     66        >
     67        struct hash< handle< T, IBITS, CBITS, TAG >, H >
     68                : detail::hash_base< handle< T, IBITS, CBITS, TAG >, H >
     69        {
     70                static constexpr H get( const handle< T, IBITS, CBITS, TAG >& value )
     71                {
     72                        return value.hash<H>();
     73                }
     74                inline H operator()( const handle< T, IBITS, CBITS, TAG >& value ) const { return get( value ); }
     75        };
    5976
    6077}
  • trunk/nv/stl/index_table.hh

    r537 r538  
    1616#include <nv/common.hh>
    1717#include <nv/stl/vector.hh>
     18#include <nv/stl/hash_map.hh>
    1819
    1920namespace nv
     
    116117        };
    117118
     119        template <
     120                typename Handle,
     121                typename Index = sint32
     122        >
     123                class hashed_index_table
     124        {
     125        public:
     126                typedef Handle              handle_type;
     127                typedef Index               index_type;
     128
     129                hashed_index_table() {}
     130                hashed_index_table( uint32 reserve )
     131                {
     132                        m_indexes.reserve( reserve );
     133                }
     134
     135                index_type insert( handle_type h )
     136                {
     137                        NV_ASSERT( m_indexes.find( h ) == m_indexes.end(), "Reinserting handle!" );
     138                        index_type lindex = m_handles.size();
     139                        m_indexes[h] = index_type( lindex );
     140                        m_handles.push_back( h );
     141                        return lindex;
     142                }
     143
     144                bool exists( handle_type h ) const
     145                {
     146                        auto ih = m_indexes.find( h );
     147                        return ( ih != m_indexes.end() );
     148                }
     149
     150                index_type get( handle_type h ) const
     151                {
     152                        auto ih = m_indexes.find( h );
     153                        if ( ih == m_indexes.end() ) return -1;
     154                        return ih->second;
     155                }
     156
     157                index_type remove_swap( handle_type h )
     158                {
     159                        if ( h.is_nil() ) return -1;
     160                        auto ih = m_indexes.find( h );
     161                        if ( ih == m_indexes.end() ) return -1;
     162                        handle_type swap_handle = m_handles.back();
     163                        index_type  dead_eindex = ih->second;
     164                        if ( dead_eindex != static_cast<index_type>( m_handles.size() - 1 ) )
     165                        {
     166                                m_handles[unsigned( dead_eindex )] = swap_handle;
     167                                m_indexes[swap_handle] = dead_eindex;
     168                        }
     169                        m_handles.pop_back();
     170                        m_indexes.erase( h );
     171                        return dead_eindex;
     172                }
     173
     174//              index_type remove_swap( index_type dead_eindex )
     175//              {
     176//                      if ( uint32( dead_eindex ) >= m_handles.size() ) return -1;
     177//                      handle_type h = m_handles[dead_eindex];
     178//                      handle_type swap_handle = m_handles.back();
     179//                      if ( dead_eindex != static_cast<index_type>( m_handles.size() - 1 ) )
     180//                      {
     181//                              m_handles[unsigned( dead_eindex )] = swap_handle;
     182//                              m_indexes[swap_handle] = dead_eindex;
     183//                      }
     184//                      m_handles.pop_back();
     185//                      m_indexes.erase( h );
     186//                      return dead_eindex;
     187//              }
     188
     189
     190                void clear()
     191                {
     192                        m_handles.clear();
     193                        m_indexes.clear();
     194                }
     195
     196                handle_type get_handle( index_type i ) const { return m_handles[unsigned( i )]; }
     197
     198                uint32 size() const { return m_handles.size(); }
     199
     200        private:
     201
     202                vector< handle_type > m_handles;
     203                hash_map< handle_type, index_type > m_indexes;
     204        };
    118205
    119206}
  • trunk/src/lua/lua_state.cc

    r534 r538  
    938938//      insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> );
    939939}
     940
Note: See TracChangeset for help on using the changeset viewer.