Class: SparseArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sparse_array/version.rb,
ext/sp_ar.c

Constant Summary collapse

VERSION =
"0.0.5"

Instance Method Summary collapse

Instance Method Details

#[](ri) ⇒ Object



416
417
418
419
420
421
422
423
424
425
# File 'ext/sp_ar.c', line 416

static VALUE
sparse_array_get(VALUE self, VALUE ri)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    spar_lookup(table, i, &res);
    return (VALUE)res;
}

#[]=(ri, val) ⇒ Object



453
454
455
456
457
458
459
460
461
# File 'ext/sp_ar.c', line 453

static VALUE
sparse_array_set(VALUE self, VALUE ri, VALUE val)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    GetSparseArray(self, table);
    spar_insert(table, i, val);
    return val;
}

#clearObject



491
492
493
494
495
496
497
498
# File 'ext/sp_ar.c', line 491

static VALUE
sparse_array_clear(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    spar_clear(table);
    return self;
}

#countObject



475
476
477
478
479
480
481
# File 'ext/sp_ar.c', line 475

static VALUE
sparse_array_size(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    return UINT2NUM(table->num_entries);
}

#delete(ri) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
# File 'ext/sp_ar.c', line 463

static VALUE
sparse_array_del(VALUE self, VALUE ri)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    if (spar_delete(table, i, &res))
        return (VALUE)res;
    return Qnil;
}

#eachObject



527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'ext/sp_ar.c', line 527

static VALUE
sparse_array_each(VALUE self)
{
    spar_table *table;
    VALUE y[2] = {Qnil, Qnil};
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    SPAR_FOREACH_START(table);
    y[0] = UINT2NUM(entry->key);
    y[1] = (VALUE)value;
    rb_yield_values2(2, y);
    SPAR_FOREACH_END();
    return self;
}

#each_keyObject



542
543
544
545
546
547
548
549
550
551
552
553
# File 'ext/sp_ar.c', line 542

static VALUE
sparse_array_each_key(VALUE self)
{
    spar_table *table;
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    SPAR_FOREACH_START(table);
    (void)value;
    rb_yield(UINT2NUM(entry->key));
    SPAR_FOREACH_END();
    return self;
}

#each_pairObject



527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'ext/sp_ar.c', line 527

static VALUE
sparse_array_each(VALUE self)
{
    spar_table *table;
    VALUE y[2] = {Qnil, Qnil};
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    SPAR_FOREACH_START(table);
    y[0] = UINT2NUM(entry->key);
    y[1] = (VALUE)value;
    rb_yield_values2(2, y);
    SPAR_FOREACH_END();
    return self;
}

#each_valueObject



555
556
557
558
559
560
561
562
563
564
565
# File 'ext/sp_ar.c', line 555

static VALUE
sparse_array_each_value(VALUE self)
{
    spar_table *table;
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArrayInt(self, table);
    SPAR_FOREACH_START(table);
    rb_yield((VALUE)(value));
    SPAR_FOREACH_END();
    return self;
}

#empty?Boolean

Returns:

  • (Boolean)


483
484
485
486
487
488
489
# File 'ext/sp_ar.c', line 483

static VALUE
sparse_array_empty_p(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    return table->num_entries ? Qfalse : Qtrue;
}

#fetch(ri, def) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
# File 'ext/sp_ar.c', line 440

static VALUE
sparse_array_fetch(VALUE self, VALUE ri, VALUE def)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    if (spar_lookup(table, i, &res))
        return (VALUE)res;
    else
        return def;
}

#has_key?(ri) ⇒ Boolean

Returns:

  • (Boolean)


427
428
429
430
431
432
433
434
435
436
437
438
# File 'ext/sp_ar.c', line 427

static VALUE
sparse_array_include(VALUE self, VALUE ri)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    if (spar_lookup(table, i, &res))
        return Qtrue;
    else
        return Qfalse;
}

#include?(ri) ⇒ Boolean

Returns:

  • (Boolean)


427
428
429
430
431
432
433
434
435
436
437
438
# File 'ext/sp_ar.c', line 427

static VALUE
sparse_array_include(VALUE self, VALUE ri)
{
    spar_table *table;
    spar_index_t i = NUM2UINT(ri);
    st_data_t res = Qnil;
    GetSparseArray(self, table);
    if (spar_lookup(table, i, &res))
        return Qtrue;
    else
        return Qfalse;
}

#initialize_copy(copy) ⇒ Object



567
568
569
570
571
572
573
574
575
576
577
# File 'ext/sp_ar.c', line 567

static VALUE
sparse_array_init_copy(VALUE self, VALUE copy)
{
    spar_table *table;
    spar_table *copied;
    GetSparseArray(self, table);
    GetSparseArray(copy, copied);
    rb_obj_init_copy(self, copy);
    spar_copy_to(table, copied);
    return copy;
}

#inspectObject



600
601
602
603
604
605
606
607
608
# File 'ext/sp_ar.c', line 600

static VALUE
sparse_array_inspect(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    if (table->num_entries == 0)
        return rb_usascii_str_new2("<SparseArray>");
    return rb_exec_recursive(sparse_array_inspect_rec, self, 0);
}

#keysObject



500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'ext/sp_ar.c', line 500

static VALUE
sparse_array_keys(VALUE self)
{
    spar_table *table;
    VALUE res;
    GetSparseArray(self, table);
    res = rb_ary_new2(table->num_entries);
    SPAR_FOREACH_START(table);
    (void)value;
    rb_ary_push(res, UINT2NUM(entry->key));
    SPAR_FOREACH_END();
    return res;
}

#sizeObject



475
476
477
478
479
480
481
# File 'ext/sp_ar.c', line 475

static VALUE
sparse_array_size(VALUE self)
{
    spar_table *table;
    GetSparseArray(self, table);
    return UINT2NUM(table->num_entries);
}

#valuesObject



514
515
516
517
518
519
520
521
522
523
524
525
# File 'ext/sp_ar.c', line 514

static VALUE
sparse_array_values(VALUE self)
{
    spar_table *table;
    VALUE res;
    GetSparseArray(self, table);
    res = rb_ary_new2(table->num_entries);
    SPAR_FOREACH_START(table);
    rb_ary_push(res, (VALUE)value);
    SPAR_FOREACH_END();
    return res;
}