Add Set C-API

This should be a minimal C-API needed to deal with Set objects. It
supports creating the sets, checking whether an element is the set,
adding and removing elements, iterating over the elements, clearing
a set, and returning the size of the set.

Co-authored-by: Nobuyoshi Nakada <nobu.nakada@gmail.com>
This commit is contained in:
Jeremy Evans 2025-06-28 17:09:24 -07:00
parent 08d4f7893e
commit 2ab38691a2
8 changed files with 322 additions and 3 deletions

50
set.c
View file

@ -1913,6 +1913,56 @@ compat_loader(VALUE self, VALUE a)
return set_i_from_hash(self, rb_ivar_get(a, id_i_hash));
}
/* C-API functions */
void
rb_set_foreach(VALUE set, int (*func)(VALUE element, VALUE arg), VALUE arg)
{
set_iter(set, func, arg);
}
VALUE
rb_set_new(void)
{
return set_alloc_with_size(rb_cSet, 0);
}
VALUE
rb_set_new_capa(size_t capa)
{
return set_alloc_with_size(rb_cSet, (st_index_t)capa);
}
bool
rb_set_lookup(VALUE set, VALUE element)
{
return RSET_IS_MEMBER(set, element);
}
bool
rb_set_add(VALUE set, VALUE element)
{
return set_i_add_p(set, element) != Qnil;
}
VALUE
rb_set_clear(VALUE set)
{
return set_i_clear(set);
}
bool
rb_set_delete(VALUE set, VALUE element)
{
return set_i_delete_p(set, element) != Qnil;
}
size_t
rb_set_size(VALUE set)
{
return RSET_SIZE(set);
}
/*
* Document-class: Set
*