Class: Usamin::Hash

Inherits:
Value
  • Object
show all
Includes:
Enumerable
Defined in:
ext/usamin/usamin.cpp

Instance Method Summary collapse

Methods inherited from Value

#array?, #eval, #eval_r, #frozen?, #hash?, #marshal_dump, #marshal_load, #object?

Instance Method Details

#[](key) ⇒ Object | nil

Note:

This method has linear time complexity.

Returns:

  • (Object | nil)


551
552
553
554
555
556
557
558
559
# File 'ext/usamin/usamin.cpp', line 551

static VALUE w_hash_operator_indexer(const VALUE self, VALUE key) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE kvalue = get_utf8_str(key);
    for (auto &m : value->value->GetObject())
        if (str_compare(RSTRING_PTR(kvalue), RSTRING_LEN(kvalue), m.name.GetString(), m.name.GetStringLength()))
            return eval(m.value);
    return Qnil;
}

#assoc(key) ⇒ Object | nil

Note:

This method has linear time complexity.

Returns:

  • (Object | nil)


551
552
553
554
555
556
557
558
559
# File 'ext/usamin/usamin.cpp', line 551

static VALUE w_hash_operator_indexer(const VALUE self, VALUE key) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE kvalue = get_utf8_str(key);
    for (auto &m : value->value->GetObject())
        if (str_compare(RSTRING_PTR(kvalue), RSTRING_LEN(kvalue), m.name.GetString(), m.name.GetStringLength()))
            return eval(m.value);
    return Qnil;
}

#each {|key, value| ... } ⇒ Enumerator | self

Yields:

  • (key, value)

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerator | self)


571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'ext/usamin/usamin.cpp', line 571

static VALUE w_hash_each(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
    if (rb_proc_arity(rb_block_proc()) > 1) {
        for (auto &m : value->value->GetObject()) {
            VALUE args[] = { eval_str(m.name), eval(m.value) };
            rb_yield_values2(2, args);
        }
    } else {
        for (auto &m : value->value->GetObject())
            rb_yield(rb_assoc_new(eval_str(m.name), eval(m.value)));
    }
    return self;
}

#each_key {|key| ... } ⇒ Enumerator | self

Yields:

  • (key)

Yield Parameters:

  • key (String)

Returns:

  • (Enumerator | self)


592
593
594
595
596
597
598
599
# File 'ext/usamin/usamin.cpp', line 592

static VALUE w_hash_each_key(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
    for (auto &m : value->value->GetObject())
        rb_yield(eval_str(m.name));
    return self;
}

#each_pair {|key, value| ... } ⇒ Enumerator | self

Yields:

  • (key, value)

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerator | self)


571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'ext/usamin/usamin.cpp', line 571

static VALUE w_hash_each(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
    if (rb_proc_arity(rb_block_proc()) > 1) {
        for (auto &m : value->value->GetObject()) {
            VALUE args[] = { eval_str(m.name), eval(m.value) };
            rb_yield_values2(2, args);
        }
    } else {
        for (auto &m : value->value->GetObject())
            rb_yield(rb_assoc_new(eval_str(m.name), eval(m.value)));
    }
    return self;
}

#each_value {|value| ... } ⇒ Enumerator | self

Yields:

  • (value)

Returns:

  • (Enumerator | self)


605
606
607
608
609
610
611
612
# File 'ext/usamin/usamin.cpp', line 605

static VALUE w_hash_each_value(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
    for (auto &m : value->value->GetObject())
        rb_yield(eval(m.value));
    return self;
}

#empty?Boolean

Returns:

  • (Boolean)


614
615
616
617
618
# File 'ext/usamin/usamin.cpp', line 614

static VALUE w_hash_isempty(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    return value->value->MemberCount() == 0 ? Qtrue : Qfalse;
}

#fetch(key, default = nil) ⇒ Object #fetch(key) {|key| ... } ⇒ Object

Overloads:

  • #fetch(key, default = nil) ⇒ Object

    Parameters:

    • key (String)
    • default (Object) (defaults to: nil)

    Returns:

    • (Object)
  • #fetch(key) {|key| ... } ⇒ Object

    Parameters:

    • key (String)

    Yields:

    • (key)

    Returns:

    • (Object)


631
632
633
634
635
636
637
638
639
640
# File 'ext/usamin/usamin.cpp', line 631

static VALUE w_hash_fetch(const int argc, VALUE* argv, const VALUE self) {
    rb_check_arity(argc, 1, 2);
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE kvalue = get_utf8_str(argv[0]);
    for (auto &m : value->value->GetObject())
        if (str_compare(RSTRING_PTR(kvalue), RSTRING_LEN(kvalue), m.name.GetString(), m.name.GetStringLength()))
            return eval(m.value);
    return argc == 2 ? argv[1] : rb_block_given_p() ? rb_yield(argv[0]) : Qnil;
}

#has_key?(name) ⇒ Boolean

Note:

This method has linear time complexity.

Returns:

  • (Boolean)


645
646
647
648
649
650
651
652
653
# File 'ext/usamin/usamin.cpp', line 645

static VALUE w_hash_haskey(const VALUE self, VALUE name) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE key = get_utf8_str(name);
    for (auto &m : value->value->GetObject())
        if (str_compare(RSTRING_PTR(key), RSTRING_LEN(key), m.name.GetString(), m.name.GetStringLength()))
            return Qtrue;
    return Qfalse;
}

#include?(name) ⇒ Boolean

Note:

This method has linear time complexity.

Returns:

  • (Boolean)


645
646
647
648
649
650
651
652
653
# File 'ext/usamin/usamin.cpp', line 645

static VALUE w_hash_haskey(const VALUE self, VALUE name) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE key = get_utf8_str(name);
    for (auto &m : value->value->GetObject())
        if (str_compare(RSTRING_PTR(key), RSTRING_LEN(key), m.name.GetString(), m.name.GetStringLength()))
            return Qtrue;
    return Qfalse;
}

#key?(name) ⇒ Boolean

Note:

This method has linear time complexity.

Returns:

  • (Boolean)


645
646
647
648
649
650
651
652
653
# File 'ext/usamin/usamin.cpp', line 645

static VALUE w_hash_haskey(const VALUE self, VALUE name) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE key = get_utf8_str(name);
    for (auto &m : value->value->GetObject())
        if (str_compare(RSTRING_PTR(key), RSTRING_LEN(key), m.name.GetString(), m.name.GetStringLength()))
            return Qtrue;
    return Qfalse;
}

#keys::Array<String>

Returns:

  • (::Array<String>)


658
659
660
661
662
663
664
665
# File 'ext/usamin/usamin.cpp', line 658

static VALUE w_hash_keys(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE ret = rb_ary_new2(value->value->MemberCount());
    for (auto &m : value->value->GetObject())
        rb_ary_push(ret, eval_str(m.name));
    return ret;
}

#lengthInteger

Returns:

  • (Integer)


670
671
672
673
674
# File 'ext/usamin/usamin.cpp', line 670

static VALUE w_hash_length(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    return UINT2NUM(value->value->MemberCount());
}

#member?(name) ⇒ Boolean

Note:

This method has linear time complexity.

Returns:

  • (Boolean)


645
646
647
648
649
650
651
652
653
# File 'ext/usamin/usamin.cpp', line 645

static VALUE w_hash_haskey(const VALUE self, VALUE name) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE key = get_utf8_str(name);
    for (auto &m : value->value->GetObject())
        if (str_compare(RSTRING_PTR(key), RSTRING_LEN(key), m.name.GetString(), m.name.GetStringLength()))
            return Qtrue;
    return Qfalse;
}

#select {|key, value| ... } ⇒ Enumerator | ::Hash

Yields:

  • (key, value)

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerator | ::Hash)


682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'ext/usamin/usamin.cpp', line 682

static VALUE w_hash_select(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, hash_enum_size);
    VALUE hash = rb_hash_new();
    if (rb_proc_arity(rb_block_proc()) > 1) {
        for (auto &m : value->value->GetObject()) {
            VALUE args[] = { eval_str(m.name), eval(m.value) };
            if (RTEST(rb_yield_values2(2, args)))
                rb_hash_aset(hash, args[0], args[1]);
        }
    } else {
        for (auto &m : value->value->GetObject()) {
            VALUE key = eval_str(m.name);
            VALUE val = eval(m.value);
            if (RTEST(rb_yield(rb_assoc_new(key, val))))
                rb_hash_aset(hash, key, val);
        }
    }
    return hash;
}

#sizeInteger

Returns:

  • (Integer)


670
671
672
673
674
# File 'ext/usamin/usamin.cpp', line 670

static VALUE w_hash_length(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    return UINT2NUM(value->value->MemberCount());
}

#to_h::Hash

Convert to Ruby Hash. Same as Value#eval.

Returns:

  • (::Hash)


709
710
711
712
713
# File 'ext/usamin/usamin.cpp', line 709

static VALUE w_hash_eval(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    return eval_object(*(value->value));
}

#to_hash::Hash

Convert to Ruby Hash. Same as Value#eval.

Returns:

  • (::Hash)


709
710
711
712
713
# File 'ext/usamin/usamin.cpp', line 709

static VALUE w_hash_eval(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    return eval_object(*(value->value));
}

#values::Array<Object>

Returns:

  • (::Array<Object>)


718
719
720
721
722
723
724
725
# File 'ext/usamin/usamin.cpp', line 718

static VALUE w_hash_values(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE ret = rb_ary_new2(value->value->MemberCount());
    for (auto &m : value->value->GetObject())
        rb_ary_push(ret, eval(m.value));
    return ret;
}