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 |
|
---|
12 | #ifndef NV_CORE_HANDLE_HH
|
---|
13 | #define NV_CORE_HANDLE_HH
|
---|
14 |
|
---|
15 | #include <nv/core/common.hh>
|
---|
16 | #include <nv/core/array.hh>
|
---|
17 |
|
---|
18 | namespace 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;
|
---|
31 | typedef TAG tag_type;
|
---|
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 |
|
---|
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);}
|
---|
41 |
|
---|
42 | bool is_nil() const { return m_index == 0 && m_counter == 0; }
|
---|
43 | bool is_valid() const { return !is_nil(); }
|
---|
44 | T index() const { return m_index; }
|
---|
45 | size_t hash() const { return std::hash<T>()( T( m_counter << IBITS | m_index ) ); }
|
---|
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 ) {}
|
---|
51 | template < typename H >
|
---|
52 | friend class handle_operator;
|
---|
53 | };
|
---|
54 |
|
---|
55 | template < typename HANDLE >
|
---|
56 | class handle_operator
|
---|
57 | {
|
---|
58 | public:
|
---|
59 | typedef typename HANDLE::value_type value_type;
|
---|
60 |
|
---|
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 | };
|
---|
68 |
|
---|
69 |
|
---|
70 | template < typename HANDLE, typename TINDEX = sint32 >
|
---|
71 | class index_store
|
---|
72 | {
|
---|
73 | public:
|
---|
74 | typedef HANDLE handle;
|
---|
75 | typedef TINDEX index_type;
|
---|
76 | typedef typename HANDLE::value_type value_type;
|
---|
77 |
|
---|
78 | index_store() : m_first_free(-1), m_last_free(-1) {}
|
---|
79 |
|
---|
80 | handle create_handle( index_type index )
|
---|
81 | {
|
---|
82 | typedef handle_operator<HANDLE> hop;
|
---|
83 | value_type i = get_free_entry();
|
---|
84 | m_entries[i].index = index;
|
---|
85 | m_entries[i].counter++;
|
---|
86 | return hop::create( i, m_entries[i].counter );
|
---|
87 | }
|
---|
88 |
|
---|
89 | void free_handle( handle h )
|
---|
90 | {
|
---|
91 | m_entries[ h.index() ].index = -1;
|
---|
92 | m_entries[ h.index() ].next_free = -1;
|
---|
93 | if ( m_last_free == -1 )
|
---|
94 | {
|
---|
95 | m_first_free = m_last_free = h.index();
|
---|
96 | return;
|
---|
97 | }
|
---|
98 | m_entries[ (unsigned)m_last_free ].next_free = h.index();
|
---|
99 | m_last_free = h.index();
|
---|
100 | }
|
---|
101 |
|
---|
102 | void swap_indices( handle h1, handle h2 )
|
---|
103 | {
|
---|
104 | std::swap( m_entries[ h1.index() ].index, m_entries[ h2.index() ].index );
|
---|
105 | }
|
---|
106 |
|
---|
107 | sint32 get_index( handle h ) const
|
---|
108 | {
|
---|
109 | typedef handle_operator<HANDLE> hop;
|
---|
110 | return m_entries[ h.index() ].counter == hop::get_counter(h) ? m_entries[ h.index() ].index : -1;
|
---|
111 | }
|
---|
112 | private:
|
---|
113 | struct index_entry
|
---|
114 | {
|
---|
115 | index_type index;
|
---|
116 | value_type counter;
|
---|
117 | index_type next_free;
|
---|
118 |
|
---|
119 | index_entry() : index(0), counter(0), next_free(-1) {}
|
---|
120 | };
|
---|
121 |
|
---|
122 | value_type get_free_entry()
|
---|
123 | {
|
---|
124 | if ( m_first_free != -1 )
|
---|
125 | {
|
---|
126 | value_type result = (value_type)m_first_free;
|
---|
127 | m_first_free = m_entries[result].next_free;
|
---|
128 | m_entries[result].next_free = -1;
|
---|
129 | if ( m_first_free == -1 ) m_last_free = -1;
|
---|
130 | return result;
|
---|
131 | }
|
---|
132 | m_entries.emplace_back();
|
---|
133 | return value_type( m_entries.size() - 1 );
|
---|
134 | }
|
---|
135 |
|
---|
136 | index_type m_first_free;
|
---|
137 | index_type m_last_free;
|
---|
138 | std::vector< index_entry > m_entries;
|
---|
139 | };
|
---|
140 |
|
---|
141 | template < typename T, typename HANDLE = handle<>, typename TINDEX = sint32 >
|
---|
142 | class packed_indexed_array
|
---|
143 | {
|
---|
144 | public:
|
---|
145 | typedef HANDLE handle;
|
---|
146 | typedef TINDEX index_type;
|
---|
147 | typedef std::vector< T > storage;
|
---|
148 | typedef T value_type;
|
---|
149 | typedef typename storage::iterator iterator;
|
---|
150 | typedef typename storage::const_iterator const_iterator;
|
---|
151 | typedef typename storage::reference reference;
|
---|
152 | typedef typename storage::const_reference const_reference;
|
---|
153 |
|
---|
154 | packed_indexed_array() {}
|
---|
155 | packed_indexed_array( uint32 reserve )
|
---|
156 | {
|
---|
157 | m_data.reserve( reserve );
|
---|
158 | m_indexes.reserve( reserve );
|
---|
159 | }
|
---|
160 |
|
---|
161 | T* insert( handle h )
|
---|
162 | {
|
---|
163 | resize_indexes_to( (index_type) h.index() );
|
---|
164 | m_indexes[ h.index() ] = (index_type) m_data.size();
|
---|
165 | m_handles.push_back( h );
|
---|
166 | m_data.emplace_back();
|
---|
167 | return &(m_data.back());
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool exists( handle h )
|
---|
171 | {
|
---|
172 | if ( h.is_nil() || h.index() >= m_indexes.size() ) return false;
|
---|
173 | return m_indexes[ h.index() ] >= 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | T* get( handle h )
|
---|
177 | {
|
---|
178 | if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr;
|
---|
179 | index_type i = m_indexes[ h.index() ];
|
---|
180 | return i >= 0 ? &(m_data[ (unsigned)i ]) : nullptr;
|
---|
181 | }
|
---|
182 |
|
---|
183 | void remove( handle h )
|
---|
184 | {
|
---|
185 | handle swap_handle = m_handles.back();
|
---|
186 | sint32 dead_eindex = m_indexes[ h.index() ];
|
---|
187 | if ( dead_eindex != (sint32)m_data.size()-1 )
|
---|
188 | {
|
---|
189 | m_data[ (unsigned)dead_eindex ] = m_data.back();
|
---|
190 | m_handles[ (unsigned)dead_eindex ] = swap_handle;
|
---|
191 | m_indexes[ swap_handle.index() ] = dead_eindex;
|
---|
192 | }
|
---|
193 | m_data.pop_back();
|
---|
194 | m_handles.pop_back();
|
---|
195 | m_indexes[ h.index() ] = -1;
|
---|
196 | }
|
---|
197 |
|
---|
198 | void clear()
|
---|
199 | {
|
---|
200 | m_data.clear();
|
---|
201 | m_handles.clear();
|
---|
202 | m_indexes.clear();
|
---|
203 | }
|
---|
204 |
|
---|
205 | const value_type& operator[] ( index_type i ) const { return m_data[i]; }
|
---|
206 | value_type& operator[] ( index_type i ) { return m_data[i]; }
|
---|
207 | size_t size() const { return m_data.size(); }
|
---|
208 |
|
---|
209 | iterator begin() { return m_data.begin(); }
|
---|
210 | const_iterator begin() const { return m_data.cbegin(); }
|
---|
211 | const_iterator cbegin() const { return m_data.cbegin(); }
|
---|
212 |
|
---|
213 | iterator end() { return m_data.end(); }
|
---|
214 | const_iterator end() const { return m_data.cend(); }
|
---|
215 | const_iterator cend() const { return m_data.cend(); }
|
---|
216 |
|
---|
217 | private:
|
---|
218 | void resize_indexes_to( index_type i )
|
---|
219 | {
|
---|
220 | index_type size = (index_type)m_indexes.size();
|
---|
221 | if ( i >= size )
|
---|
222 | {
|
---|
223 | if ( size == 0 ) size = 1;
|
---|
224 | while ( i >= size ) size = size * 2;
|
---|
225 | m_indexes.resize( (size_t)size, -1 );
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | std::vector< T > m_data;
|
---|
230 | std::vector< handle > m_handles;
|
---|
231 | std::vector< index_type > m_indexes;
|
---|
232 | };
|
---|
233 |
|
---|
234 |
|
---|
235 | template < typename T, typename HANDLE = handle<>, typename TINDEX = sint32 >
|
---|
236 | class entity_store
|
---|
237 | {
|
---|
238 | public:
|
---|
239 | typedef HANDLE handle;
|
---|
240 | typedef TINDEX index_type;
|
---|
241 | typedef std::vector< T > storage;
|
---|
242 | typedef T value_type;
|
---|
243 | typedef typename storage::iterator iterator;
|
---|
244 | typedef typename storage::const_iterator const_iterator;
|
---|
245 | typedef typename storage::reference reference;
|
---|
246 | typedef typename storage::const_reference const_reference;
|
---|
247 |
|
---|
248 | entity_store() {}
|
---|
249 |
|
---|
250 | explicit entity_store( uint32 reserve )
|
---|
251 | {
|
---|
252 | m_handles.reserve( reserve );
|
---|
253 | m_data.reserve( reserve );
|
---|
254 | }
|
---|
255 |
|
---|
256 | handle create()
|
---|
257 | {
|
---|
258 | m_data.emplace_back();
|
---|
259 | m_handles.push_back( m_indexes.create_handle( sint32( m_data.size() - 1 ) ) );
|
---|
260 | return m_handles.back();
|
---|
261 | }
|
---|
262 |
|
---|
263 | const value_type* get( handle h ) const
|
---|
264 | {
|
---|
265 | if ( h.is_nil() ) return nullptr;
|
---|
266 | sint32 eindex = m_indexes.get_index( h );
|
---|
267 | return eindex >= 0 ? &(m_data[ (unsigned)eindex ]) : nullptr;
|
---|
268 | }
|
---|
269 |
|
---|
270 | value_type* get( handle h )
|
---|
271 | {
|
---|
272 | if ( h.is_nil() ) return nullptr;
|
---|
273 | sint32 eindex = m_indexes.get_index( h );
|
---|
274 | return eindex >= 0 ? &(m_data[ (unsigned)eindex ]) : nullptr;
|
---|
275 | }
|
---|
276 |
|
---|
277 | void destroy( handle e )
|
---|
278 | {
|
---|
279 | handle swap_handle = m_handles.back();
|
---|
280 | sint32 dead_eindex = m_indexes.get_index( e );
|
---|
281 | if ( dead_eindex != (sint32)m_data.size()-1 )
|
---|
282 | {
|
---|
283 | m_data[ (unsigned)dead_eindex ] = m_data.back();
|
---|
284 | m_handles[ (unsigned)dead_eindex ] = m_handles.back();
|
---|
285 | }
|
---|
286 | m_data.pop_back();
|
---|
287 | m_handles.pop_back();
|
---|
288 | m_indexes.swap_indices( e, swap_handle );
|
---|
289 | m_indexes.free_handle( e );
|
---|
290 | }
|
---|
291 |
|
---|
292 | handle get_handle( index_type i ) const { return m_handles[(unsigned)i]; }
|
---|
293 | const value_type& operator[] ( index_type i ) const { return m_data[(unsigned)i]; }
|
---|
294 | value_type& operator[] ( index_type i ) { return m_data[(unsigned)i]; }
|
---|
295 | size_t size() const { return m_data.size(); }
|
---|
296 |
|
---|
297 | iterator begin() { return m_data.begin(); }
|
---|
298 | const_iterator begin() const { return m_data.cbegin(); }
|
---|
299 | const_iterator cbegin() const { return m_data.cbegin(); }
|
---|
300 |
|
---|
301 | iterator end() { return m_data.end(); }
|
---|
302 | const_iterator end() const { return m_data.cend(); }
|
---|
303 | const_iterator cend() const { return m_data.cend(); }
|
---|
304 |
|
---|
305 | private:
|
---|
306 | std::vector< handle > m_handles;
|
---|
307 | storage m_data;
|
---|
308 | index_store< handle, TINDEX > m_indexes;
|
---|
309 | };
|
---|
310 |
|
---|
311 | }
|
---|
312 |
|
---|
313 | namespace std
|
---|
314 | {
|
---|
315 | template <
|
---|
316 | typename T,
|
---|
317 | unsigned IBITS,
|
---|
318 | unsigned CBITS,
|
---|
319 | typename TAG
|
---|
320 | >
|
---|
321 | struct hash<nv::handle<T,IBITS,CBITS,TAG>>
|
---|
322 | {
|
---|
323 | size_t operator()(const nv::handle<T,IBITS,CBITS,TAG>& h) const
|
---|
324 | {
|
---|
325 | return h.hash();
|
---|
326 | }
|
---|
327 | };
|
---|
328 | }
|
---|
329 |
|
---|
330 | #endif // NV_CORE_HANDLE_HH
|
---|