@@ -47,7 +47,7 @@ struct rb_id_table {
4747
4848#if SIZEOF_VALUE == 8
4949#define ITEM_GET_KEY (tbl , i ) ((tbl)->items[i].key)
50- #define ITEM_KEY_ISSET (tbl , i ) ((tbl)->items[i].key)
50+ #define ITEM_KEY_ISSET (tbl , i ) ((tbl)->items && (tbl)->items [i].key)
5151#define ITEM_COLLIDED (tbl , i ) ((tbl)->items[i].collision)
5252#define ITEM_SET_COLLIDED (tbl , i ) ((tbl)->items[i].collision = 1)
5353static inline void
@@ -331,45 +331,51 @@ rb_id_table_foreach_values_with_replace(struct rb_id_table *tbl, rb_id_table_for
331331 }
332332}
333333
334- static void
335- managed_id_table_free (void * data )
334+ void
335+ rb_managed_id_table_free (void * data )
336336{
337337 struct rb_id_table * tbl = (struct rb_id_table * )data ;
338338 rb_id_table_free_items (tbl );
339339}
340340
341- static size_t
342- managed_id_table_memsize (const void * data )
343- {
344- const struct rb_id_table * tbl = (const struct rb_id_table * )data ;
345- return rb_id_table_memsize (tbl ) - sizeof (struct rb_id_table );
346- }
347-
348- static const rb_data_type_t managed_id_table_type = {
341+ const rb_data_type_t rb_managed_id_table_type = {
349342 .wrap_struct_name = "VM/managed_id_table" ,
350343 .function = {
351344 .dmark = NULL , // Nothing to mark
352- .dfree = (RUBY_DATA_FUNC )managed_id_table_free ,
353- .dsize = managed_id_table_memsize ,
345+ .dfree = (RUBY_DATA_FUNC )rb_managed_id_table_free ,
346+ .dsize = rb_managed_id_table_memsize ,
354347 },
355348 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE ,
356349};
357350
351+ size_t
352+ rb_managed_id_table_memsize (const void * data )
353+ {
354+ const struct rb_id_table * tbl = (const struct rb_id_table * )data ;
355+ return rb_id_table_memsize (tbl ) - sizeof (struct rb_id_table );
356+ }
357+
358358static inline struct rb_id_table *
359359managed_id_table_ptr (VALUE obj )
360360{
361361 return RTYPEDDATA_GET_DATA (obj );
362362}
363363
364364VALUE
365- rb_managed_id_table_new ( size_t capa )
365+ rb_managed_id_table_create ( const rb_data_type_t * type , size_t capa )
366366{
367367 struct rb_id_table * tbl ;
368- VALUE obj = TypedData_Make_Struct (0 , struct rb_id_table , & managed_id_table_type , tbl );
368+ VALUE obj = TypedData_Make_Struct (0 , struct rb_id_table , type , tbl );
369369 rb_id_table_init (tbl , capa );
370370 return obj ;
371371}
372372
373+ VALUE
374+ rb_managed_id_table_new (size_t capa )
375+ {
376+ return rb_managed_id_table_create (& rb_managed_id_table_type , capa );
377+ }
378+
373379static enum rb_id_table_iterator_result
374380managed_id_table_dup_i (ID id , VALUE val , void * data )
375381{
@@ -382,10 +388,10 @@ VALUE
382388rb_managed_id_table_dup (VALUE old_table )
383389{
384390 RUBY_ASSERT (RB_TYPE_P (old_table , T_DATA ));
385- RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (old_table ), & managed_id_table_type ));
391+ RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (old_table ), & rb_managed_id_table_type ));
386392
387393 struct rb_id_table * new_tbl ;
388- VALUE obj = TypedData_Make_Struct (0 , struct rb_id_table , & managed_id_table_type , new_tbl );
394+ VALUE obj = TypedData_Make_Struct (0 , struct rb_id_table , & rb_managed_id_table_type , new_tbl );
389395 struct rb_id_table * old_tbl = RTYPEDDATA_GET_DATA (old_table );
390396 rb_id_table_init (new_tbl , old_tbl -> num + 1 );
391397 rb_id_table_foreach (old_tbl , managed_id_table_dup_i , new_tbl );
396402rb_managed_id_table_lookup (VALUE table , ID id , VALUE * valp )
397403{
398404 RUBY_ASSERT (RB_TYPE_P (table , T_DATA ));
399- RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & managed_id_table_type ));
405+ RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & rb_managed_id_table_type ));
400406
401407 return rb_id_table_lookup (RTYPEDDATA_GET_DATA (table ), id , valp );
402408}
405411rb_managed_id_table_insert (VALUE table , ID id , VALUE val )
406412{
407413 RUBY_ASSERT (RB_TYPE_P (table , T_DATA ));
408- RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & managed_id_table_type ));
414+ RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & rb_managed_id_table_type ));
409415
410416 return rb_id_table_insert (RTYPEDDATA_GET_DATA (table ), id , val );
411417}
@@ -414,7 +420,7 @@ size_t
414420rb_managed_id_table_size (VALUE table )
415421{
416422 RUBY_ASSERT (RB_TYPE_P (table , T_DATA ));
417- RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & managed_id_table_type ));
423+ RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & rb_managed_id_table_type ));
418424
419425 return rb_id_table_size (RTYPEDDATA_GET_DATA (table ));
420426}
@@ -423,7 +429,25 @@ void
423429rb_managed_id_table_foreach (VALUE table , rb_id_table_foreach_func_t * func , void * data )
424430{
425431 RUBY_ASSERT (RB_TYPE_P (table , T_DATA ));
426- RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & managed_id_table_type ));
432+ RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & rb_managed_id_table_type ));
427433
428434 rb_id_table_foreach (RTYPEDDATA_GET_DATA (table ), func , data );
429435}
436+
437+ void
438+ rb_managed_id_table_foreach_values (VALUE table , rb_id_table_foreach_values_func_t * func , void * data )
439+ {
440+ RUBY_ASSERT (RB_TYPE_P (table , T_DATA ));
441+ RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & rb_managed_id_table_type ));
442+
443+ rb_id_table_foreach_values (RTYPEDDATA_GET_DATA (table ), func , data );
444+ }
445+
446+ int
447+ rb_managed_id_table_delete (VALUE table , ID id )
448+ {
449+ RUBY_ASSERT (RB_TYPE_P (table , T_DATA ));
450+ RUBY_ASSERT (rb_typeddata_inherited_p (RTYPEDDATA_TYPE (table ), & rb_managed_id_table_type ));
451+
452+ return rb_id_table_delete (RTYPEDDATA_GET_DATA (table ), id );
453+ }
0 commit comments