Changeset 273


Ignore:
Timestamp:
07/04/14 19:13:27 (11 years ago)
Author:
epyon
Message:
  • entity_store space reservation
  • entity_store indexed access and get_handle
  • hash support for handles
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/handle.hh

    r272 r273  
    3636                handle() : m_index(0), m_counter(0) {}
    3737
    38                 inline bool operator==(const handle& rhs){return m_index == rhs.m_index && m_counter == rhs.m_counter; }
    39                 inline bool operator!=(const handle& rhs){return !(*this == rhs);}
     38                inline bool operator==(const handle& rhs) const {return m_index == rhs.m_index && m_counter == rhs.m_counter; }
     39                inline bool operator!=(const handle& rhs) const {return !(*this == rhs);}
    4040
    4141                bool is_nil() const { return m_index == 0 && m_counter == 0; }
    4242                bool is_valid() const { return !is_nil(); }
    43 
     43                T index() const { return m_index; }
     44                size_t hash() const { return std::hash<T>()( m_counter << IBITS | m_index ); }
    4445        protected:
    4546                T m_index   : IBITS;
     
    5051                friend class index_store;
    5152        };
     53
     54
    5255
    5356        template < typename HANDLE, typename TINDEX = sint32 >
     
    120123        };
    121124
     125
     126
    122127        template < typename T, typename HANDLE = handle<>, typename TINDEX = sint32 >
    123128        class entity_store
     
    134139
    135140                entity_store() {}
     141
     142                explicit entity_store( uint32 reserve )
     143                {
     144                        m_handles.reserve( reserve );
     145                        m_data.reserve( reserve );
     146                }
     147
    136148                handle create()
    137149                {
     
    140152                        return m_handles.back();
    141153                }
     154
    142155                value_type* get( handle h )
    143156                {
     
    146159                        return eindex >= 0 ? &(m_data[ eindex ]) : nullptr;
    147160                }
     161
     162                handle get_handle( index_type i ) const { return m_handles[i]; }
     163                const T& operator[] ( index_type i ) const { return m_data[i]; }
     164                T& operator[] ( index_type i ) { return m_data[i]; }
     165                size_t size() const { return m_data.size(); }
     166               
    148167                void destroy( handle e )
    149168                {
     
    175194        };
    176195
    177 
    178 
    179196}
    180197
     198namespace std
     199{
     200        template <
     201                typename T,
     202                unsigned IBITS,
     203                unsigned CBITS,
     204                typename TAG
     205        >
     206        struct hash<nv::handle<T,IBITS,CBITS,TAG>>
     207        {
     208                size_t operator()(const nv::handle<T,IBITS,CBITS,TAG>& h) const
     209                {
     210                        return h.hash();
     211                }
     212        };
     213}
     214
    181215#endif // NV_HANDLE_HH
Note: See TracChangeset for help on using the changeset viewer.