source: trunk/nv/core/handle.hh @ 350

Last change on this file since 350 was 350, checked in by epyon, 10 years ago
  • massive untracked updates
File size: 8.9 KB
RevLine 
[270]1// Copyright (C) 2014 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7/**
8 * @file handle.hh
9 * @author Kornel Kisielewicz
10 */
11
[319]12#ifndef NV_CORE_HANDLE_HH
13#define NV_CORE_HANDLE_HH
[270]14
[319]15#include <nv/core/common.hh>
16#include <nv/core/array.hh>
[270]17
18namespace nv
19{
20
21        template <
22                typename T = uint32,
23                unsigned IBITS = 16,
24                unsigned CBITS = 16,
25                typename TAG = void
26        >
27        class handle
28        {
29        public:
30                typedef T value_type;
[333]31                typedef TAG tag_type;
[270]32                static const int INDEX_BITS   = IBITS;
33                static const int COUNTER_BITS = IBITS;
34                static const T MAX_INDEX   = (1 << IBITS) - 1;
35                static const T MAX_COUNTER = (1 << CBITS) - 1;
36
37                handle() : m_index(0), m_counter(0) {}
38
[273]39                inline bool operator==(const handle& rhs) const {return m_index == rhs.m_index && m_counter == rhs.m_counter; }
40                inline bool operator!=(const handle& rhs) const {return !(*this == rhs);}
[270]41
42                bool is_nil() const { return m_index == 0 && m_counter == 0; }
43                bool is_valid() const { return !is_nil(); }
[273]44                T index() const { return m_index; }
[323]45                size_t hash() const { return std::hash<T>()( T( m_counter << IBITS | m_index ) ); }
[270]46        protected:
47                T m_index   : IBITS;
48                T m_counter : CBITS;
49
50                handle( T a_index, T a_counter ) : m_index( a_index ), m_counter( a_counter ) {}
[333]51                template < typename H >
52                friend class handle_operator;
[270]53        };
54
[333]55        template < typename HANDLE >
56        class handle_operator
57        {
58        public:
59                typedef typename HANDLE::value_type value_type;
[273]60
[333]61                static HANDLE create( value_type index, value_type counter )
62                {
63                        return HANDLE( index, counter );
64                }
65                static value_type get_counter( const HANDLE& h ) { return h.m_counter; }
66                static value_type get_index( const HANDLE& h ) { return h.m_index; }
67        };
[273]68
[347]69        template < typename HANDLE >
70        class handle_manager
[270]71        {
[347]72                static const sint32 NONE = -1;
73                static const sint32 USED = -2;
[270]74        public:
[347]75
[270]76                typedef HANDLE handle;
77                typedef typename HANDLE::value_type value_type;
78
[347]79                handle_manager() : m_first_free( NONE ), m_last_free( NONE ) {}
[270]80
[347]81                handle create_handle()
[270]82                {
[347]83                        typedef handle_operator<HANDLE> hop;
84                        value_type i = get_free_entry();
[270]85                        m_entries[i].counter++;
[350]86                        NV_ASSERT( m_entries[i].counter != 0, "Out of handles!" );
[347]87                        m_entries[i].next_free = USED;
[333]88                        return hop::create( i, m_entries[i].counter );
[270]89                }
90
91                void free_handle( handle h )
92                {
[350]93                        value_type index = h.index();
94                        typedef handle_operator<HANDLE> hop;
95                        NV_ASSERT( m_entries[index].next_free == USED, "Unused handle freed!" );
96                        NV_ASSERT( m_entries[index].counter == hop::get_counter( h ), "Handle corruption!" );
97                        m_entries[index].next_free = NONE;
98                        if ( m_last_free == NONE )
[270]99                        {
[350]100                                m_first_free = m_last_free = index;
[270]101                                return;
102                        }
[350]103                        m_entries[(unsigned)m_last_free].next_free = index;
104                        m_last_free = index;
[270]105                }
106
[339]107                bool is_valid( handle h ) const
108                {
[347]109                        typedef handle_operator<HANDLE> hop;
[339]110                        if ( h.is_nil() ) return false;
111                        if ( h.index() >= m_entries.size() ) return false;
[347]112                        const index_entry& entry = m_entries[h.index()];
113                        return entry.next_free == USED && entry.counter == hop::get_counter( h );
[339]114                }
115
[270]116        private:
117                struct index_entry
118                {
119                        value_type counter;
[347]120                        sint32     next_free;
[270]121
[347]122                        index_entry() : counter( 0 ), next_free( NONE ) {}
[270]123                };
124
125                value_type get_free_entry()
126                {
[350]127                        if ( m_first_free != NONE )
[270]128                        {
129                                value_type result = (value_type)m_first_free;
130                                m_first_free = m_entries[result].next_free;
[347]131                                m_entries[result].next_free = USED;
[350]132                                if ( m_first_free == NONE ) m_last_free = NONE;
[270]133                                return result;
134                        }
135                        m_entries.emplace_back();
136                        return value_type( m_entries.size() - 1 );
137                }
138
[347]139                sint32 m_first_free;
140                sint32 m_last_free;
[270]141                std::vector< index_entry > m_entries;
142        };
143
[274]144        template < typename T, typename HANDLE = handle<>, typename TINDEX = sint32 >
145        class packed_indexed_array
146        {
147        public:
148                typedef HANDLE                   handle;
149                typedef TINDEX                   index_type;
150                typedef std::vector< T >         storage;
151                typedef T                        value_type;
152                typedef typename storage::iterator        iterator;
153                typedef typename storage::const_iterator  const_iterator;
154                typedef typename storage::reference       reference;
155                typedef typename storage::const_reference const_reference;
[273]156
[274]157                packed_indexed_array() {}
158                packed_indexed_array( uint32 reserve )
159                {
160                        m_data.reserve( reserve );
161                        m_indexes.reserve( reserve );
162                }
[273]163
[274]164                T* insert( handle h )
165                {
[323]166                        resize_indexes_to( (index_type) h.index() );
167                        m_indexes[ h.index() ] = (index_type) m_data.size();
[295]168                        m_handles.push_back( h );
[274]169                        m_data.emplace_back();
170                        return &(m_data.back());
171                }
172
173                bool exists( handle h )
174                {
175                        if ( h.is_nil() || h.index() >= m_indexes.size() ) return false;
176                        return m_indexes[ h.index() ] >= 0;             
177                }
178
179                T* get( handle h )
180                {
181                        if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr;
182                        index_type i = m_indexes[ h.index() ];
[323]183                        return i >= 0 ? &(m_data[ (unsigned)i ]) : nullptr;
[274]184                }
185
[347]186                const T* get( handle h ) const
187                {
188                        if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr;
189                        index_type i = m_indexes[h.index()];
190                        return i >= 0 ? &( m_data[(unsigned)i] ) : nullptr;
191                }
192
[274]193                void remove( handle h )
194                {
[339]195                        if ( h.is_nil() || h.index() >= m_indexes.size() || m_indexes[h.index()] == -1 )
196                                return;
[274]197                        handle swap_handle    = m_handles.back();
198                        sint32 dead_eindex    = m_indexes[ h.index() ];
199                        if ( dead_eindex != (sint32)m_data.size()-1 )
200                        {
[323]201                                m_data[ (unsigned)dead_eindex ]    = m_data.back();
202                                m_handles[ (unsigned)dead_eindex ] = swap_handle;
203                                m_indexes[ swap_handle.index() ]   = dead_eindex;
[274]204                        }
205                        m_data.pop_back();
206                        m_handles.pop_back();
207                        m_indexes[ h.index() ] = -1;
208                }
209
210                void clear()
211                {
212                        m_data.clear();
213                        m_handles.clear();
214                        m_indexes.clear();
215                }
216
[347]217                handle get_handle( index_type i ) const { return m_handles[(unsigned)i]; }
218
[274]219                const value_type& operator[] ( index_type i ) const { return m_data[i]; }
220                value_type& operator[] ( index_type i ) { return m_data[i]; }
221                size_t size() const { return m_data.size(); }
222
223                iterator        begin()        { return m_data.begin(); }
224                const_iterator  begin()  const { return m_data.cbegin(); }
225                const_iterator  cbegin() const { return m_data.cbegin(); }
226
227                iterator        end()        { return m_data.end(); }
228                const_iterator  end()  const { return m_data.cend(); }
229                const_iterator  cend() const { return m_data.cend(); }
230
231        private:
232                void resize_indexes_to( index_type i )
233                {
234                        index_type size = (index_type)m_indexes.size();
235                        if ( i >= size )
236                        {
237                                if ( size == 0 ) size = 1;
238                                while ( i >= size ) size = size * 2;
[323]239                                m_indexes.resize( (size_t)size, -1 );
[274]240                        }
241                }
242
243                std::vector< T >          m_data;
244                std::vector< handle >     m_handles;
245                std::vector< index_type > m_indexes;
246        };
247
248
[271]249        template < typename T, typename HANDLE = handle<>, typename TINDEX = sint32 >
[347]250        class handle_store
[271]251        {
252        public:
[272]253                typedef HANDLE                   handle;
254                typedef TINDEX                   index_type;
255                typedef std::vector< T >         storage;
256                typedef T                        value_type;
257                typedef typename storage::iterator        iterator;
258                typedef typename storage::const_iterator  const_iterator;
259                typedef typename storage::reference       reference;
260                typedef typename storage::const_reference const_reference;
[270]261
[347]262                handle_store() {}
[273]263
[347]264                explicit handle_store( uint32 reserve )
[273]265                {
266                        m_data.reserve( reserve );
267                }
268
[271]269                handle create()
270                {
[347]271                        handle h = m_indexes.create_handle();
272                        m_data.insert( h );
273                        return h;
[271]274                }
[273]275
[339]276                bool is_valid( handle h )
277                {
[347]278                        return m_indexes.is_valid( h );
[339]279                }
280
[287]281                const value_type* get( handle h ) const
282                {
[347]283                        return m_data.get( h );
[287]284                }
285
[271]286                value_type* get( handle h )
287                {
[347]288                        return m_data.get( h );
[271]289                }
[273]290
[271]291                void destroy( handle e )
292                {
[347]293                        m_data.remove( e );
[271]294                        m_indexes.free_handle( e );
295                }
[272]296
[347]297                handle get_handle( index_type i ) const { return m_data.get_handle(i); }
[323]298                const value_type& operator[] ( index_type i ) const { return m_data[(unsigned)i]; }
299                value_type& operator[] ( index_type i ) { return m_data[(unsigned)i]; }
[274]300                size_t size() const { return m_data.size(); }
301
[347]302                iterator        begin() { return m_data.begin(); }
[272]303                const_iterator  begin()  const { return m_data.cbegin(); }
304                const_iterator  cbegin() const { return m_data.cbegin(); }
305
[347]306                iterator        end() { return m_data.end(); }
[272]307                const_iterator  end()  const { return m_data.cend(); }
308                const_iterator  cend() const { return m_data.cend(); }
309
[271]310        private:
[347]311                packed_indexed_array< T, handle, TINDEX > m_data;
312                handle_manager< handle >                  m_indexes;
[271]313        };
[347]314       
[273]315}
[271]316
[273]317namespace std
318{
319        template <
320                typename T,
321                unsigned IBITS,
322                unsigned CBITS,
323                typename TAG
324        >
325        struct hash<nv::handle<T,IBITS,CBITS,TAG>>
326        {
327                size_t operator()(const nv::handle<T,IBITS,CBITS,TAG>& h) const
328                {
329                        return h.hash();
330                }
331        };
[270]332}
333
[319]334#endif // NV_CORE_HANDLE_HH
Note: See TracBrowser for help on using the repository browser.