Class: DataStructuresRMolinari::CDisjointUnion

Inherits:
Object
  • Object
show all
Defined in:
ext/c_disjoint_union/disjoint_union.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

A single parameter is optional. If given it should be a non-negative integer and specifies the initial size, s, of the universe 0, 1, …, s-1.

If no argument is given we act as though a value of 0 were passed.



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'ext/c_disjoint_union/disjoint_union.c', line 327

static VALUE disjoint_union_init(int argc, VALUE *argv, VALUE self) {
  if (argc == 0) {
    return self;
  } else if (argc > 1) {
    rb_raise(rb_eArgError, "wrong number of arguments");
  } else {
    size_t initial_size = checked_nonneg_fixnum(argv[0]);
    disjoint_union_data *disjoint_union = unwrapped(self);

    for (size_t i = 0; i < initial_size; i++) {
      add_new_element(disjoint_union, i);
    }
  }
  return self;
}

Instance Method Details

#find(arg) ⇒ Integer

The canonical representative of the subset containing e. Two elements d and e are in the same subset exactly when find(d) == find(e).

The parameter must be in the universe of elements.

Returns:

  • (Integer)

    one of the universe of elements



381
382
383
# File 'ext/c_disjoint_union/disjoint_union.c', line 381

static VALUE disjoint_union_find(VALUE self, VALUE arg) {
  return LONG2NUM(find(unwrapped(self), checked_nonneg_fixnum(arg)));
}

#make_set(arg) ⇒ Object

Add a new subset to the universe containing the element new_v.

Parameters:

  • the

    new element, starting in its own singleton subset

    • it must be a non-negative integer, not already part of the universe of elements.



360
361
362
363
364
# File 'ext/c_disjoint_union/disjoint_union.c', line 360

static VALUE disjoint_union_make_set(VALUE self, VALUE arg) {
  add_new_element(unwrapped(self), checked_nonneg_fixnum(arg));

  return Qnil;
}

#subset_countObject

Returns the number of subsets into which the universe is currently partitioned.

Returns:

  • the number of subsets into which the universe is currently partitioned.



369
370
371
# File 'ext/c_disjoint_union/disjoint_union.c', line 369

static VALUE disjoint_union_subset_count(VALUE self) {
  return LONG2NUM(unwrapped(self)->subset_count);
}

#unite(arg1, arg2) ⇒ Object

Declare that the arguments are equivalent, i.e., in the same subset. If they are already in the same subset this is a no-op.

Each argument must be in the universe of elements



390
391
392
393
394
# File 'ext/c_disjoint_union/disjoint_union.c', line 390

static VALUE disjoint_union_unite(VALUE self, VALUE arg1, VALUE arg2) {
  unite(unwrapped(self), checked_nonneg_fixnum(arg1), checked_nonneg_fixnum(arg2));

  return Qnil;
}