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.3"

Instance Method Summary collapse

Instance Method Details

#[](ri) ⇒ Object



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

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



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

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



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

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

#countObject



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

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

#delete(ri) ⇒ Object



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

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



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'ext/sp_ar.c', line 526

static VALUE
sparse_array_each(VALUE self)
{
    spar_table *table;
    VALUE keys;
    long i, size;
    VALUE *p;
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    keys = sparse_array_keys(self);
    size = RARRAY_LEN(keys);
    p = RARRAY_PTR(keys);
    for(i=0; i<size; i++) {
        spar_index_t k = NUM2UINT(p[i]);
        st_data_t v = Qnil;
        if (spar_lookup(table, k, &v))
            rb_yield(rb_assoc_new(p[i], (VALUE)v));
    }
    RB_GC_GUARD(keys);
    return self;
}

#each_keyObject



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'ext/sp_ar.c', line 548

static VALUE
sparse_array_each_key(VALUE self)
{
    spar_table *table;
    VALUE keys;
    long i, size;
    VALUE *p;
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    keys = sparse_array_keys(self);
    size = RARRAY_LEN(keys);
    p = RARRAY_PTR(keys);
    for(i=0; i<size; i++) {
        spar_index_t k = NUM2UINT(p[i]);
        st_data_t v = Qnil;
        if (spar_lookup(table, k, &v))
            rb_yield(p[i]);
    }
    RB_GC_GUARD(keys);
    return self;
}

#each_pairObject



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'ext/sp_ar.c', line 526

static VALUE
sparse_array_each(VALUE self)
{
    spar_table *table;
    VALUE keys;
    long i, size;
    VALUE *p;
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    keys = sparse_array_keys(self);
    size = RARRAY_LEN(keys);
    p = RARRAY_PTR(keys);
    for(i=0; i<size; i++) {
        spar_index_t k = NUM2UINT(p[i]);
        st_data_t v = Qnil;
        if (spar_lookup(table, k, &v))
            rb_yield(rb_assoc_new(p[i], (VALUE)v));
    }
    RB_GC_GUARD(keys);
    return self;
}

#each_valueObject



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'ext/sp_ar.c', line 570

static VALUE
sparse_array_each_value(VALUE self)
{
    spar_table *table;
    VALUE keys;
    long i, size;
    VALUE *p;
    RETURN_ENUMERATOR(self, 0, 0);
    GetSparseArray(self, table);
    keys = sparse_array_keys(self);
    size = RARRAY_LEN(keys);
    p = RARRAY_PTR(keys);
    for(i=0; i<size; i++) {
        spar_index_t k = NUM2UINT(p[i]);
        st_data_t v = Qnil;
        if (spar_lookup(table, k, &v))
            rb_yield(v);
    }
    RB_GC_GUARD(keys);
    return self;
}

#empty?Boolean

Returns:

  • (Boolean)


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

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

#fetch(ri, def) ⇒ Object



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

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)


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

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)


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

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



592
593
594
595
596
597
598
599
600
601
602
# File 'ext/sp_ar.c', line 592

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



625
626
627
628
629
630
631
632
633
# File 'ext/sp_ar.c', line 625

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



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

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



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

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

#valuesObject



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

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