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.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'ext/c_disjoint_union/disjoint_union.c', line 269

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);

    pair_vector *pair_vec = disjoint_union->pairs;
    resize(pair_vec, initial_size);

    for (size_t i = 0; i < initial_size; i++) {
      lval(pair_vec, i) = make_data_pair(i, 0);
    }
    disjoint_union->subset_count = initial_size;
  }
  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



327
328
329
# File 'ext/c_disjoint_union/disjoint_union.c', line 327

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:

  • arg

    the new element, starting in its own singleton subset

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



306
307
308
309
310
# File 'ext/c_disjoint_union/disjoint_union.c', line 306

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.



315
316
317
# File 'ext/c_disjoint_union/disjoint_union.c', line 315

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



336
337
338
339
340
# File 'ext/c_disjoint_union/disjoint_union.c', line 336

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

  return Qnil;
}