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)


589
590
591
592
593
594
595
596
# File 'ext/usamin/usamin.cpp', line 589

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

#assoc(key) ⇒ ::Array | nil

Note:

This method has linear time complexity.

Returns:

  • (::Array | nil)


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

static VALUE w_hash_assoc(const VALUE self, const VALUE key) {
    UsaminValue *value = get_value(self);
    check_object(value);
    for (auto &m : value->value->GetObject())
        if (str_compare_xx(key, m.name))
            return rb_assoc_new(eval_str(m.name), eval(m.value, self));
    return Qnil;
}

#compact::Hash

Returns:

  • (::Hash)


615
616
617
618
619
620
621
622
623
# File 'ext/usamin/usamin.cpp', line 615

static VALUE w_hash_compact(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE hash = rb_hash_new();
    for (auto &m : value->value->GetObject())
        if (!m.value.IsNull())
            rb_hash_aset(hash, eval(m.name, self), eval(m.value, self));
    return hash;
}

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

Yields:

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerator | self)


635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'ext/usamin/usamin.cpp', line 635

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, self) };
            rb_yield_values2(2, args);
        }
    } else {
        for (auto &m : value->value->GetObject())
            rb_yield(rb_assoc_new(eval_str(m.name), eval(m.value, self)));
    }
    return self;
}

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

Yields:

Yield Parameters:

  • key (String)

Returns:

  • (Enumerator | self)


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

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:

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerator | self)


635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'ext/usamin/usamin.cpp', line 635

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, self) };
            rb_yield_values2(2, args);
        }
    } else {
        for (auto &m : value->value->GetObject())
            rb_yield(rb_assoc_new(eval_str(m.name), eval(m.value, self)));
    }
    return self;
}

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

Yields:

  • (value)

Returns:

  • (Enumerator | self)


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

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, self));
    return self;
}

#empty?Boolean

Returns:

  • (Boolean)


678
679
680
681
682
# File 'ext/usamin/usamin.cpp', line 678

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:

    Returns:

    • (Object)


695
696
697
698
699
700
701
702
703
# File 'ext/usamin/usamin.cpp', line 695

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

#fetch_values(*args) ⇒ ::Array<Object>

Parameters:

  • key (String)

Returns:

  • (::Array<Object>)


709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'ext/usamin/usamin.cpp', line 709

static VALUE w_hash_fetch_values(const int argc, VALUE *argv, const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE ret = rb_ary_new2(argc);
    for (int i = 0; i < argc; i++) {
        bool found = false;
        for (auto &m : value->value->GetObject()) {
            if (str_compare_xx(argv[i], m.name)) {
                rb_ary_push(ret, eval(m.value, self));
                found = true;
                break;
            }
        }
        if (!found) {
            if (rb_block_given_p()) {
                rb_ary_push(ret, rb_yield(argv[i]));
            } else {
                rb_ary_free(ret);
                rb_raise(rb_eKeyError, "key not found: \"%s\"", StringValueCStr(argv[i]));
                return Qnil;
            }
        }
    }
    return ret;
}

#has_key?(key) ⇒ Boolean

Note:

This method has linear time complexity.

Returns:

  • (Boolean)


738
739
740
741
742
743
744
745
# File 'ext/usamin/usamin.cpp', line 738

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

#has_value?(val) ⇒ Boolean

Returns:

  • (Boolean)


747
748
749
750
751
752
753
754
# File 'ext/usamin/usamin.cpp', line 747

static VALUE w_hash_hasvalue(const VALUE self, const VALUE val) {
    UsaminValue *value = get_value(self);
    check_object(value);
    for (auto &m : value->value->GetObject())
        if (rb_equal(val, eval_r(m.value)))
            return Qtrue;
    return Qfalse;
}

#include?(key) ⇒ Boolean

Note:

This method has linear time complexity.

Returns:

  • (Boolean)


738
739
740
741
742
743
744
745
# File 'ext/usamin/usamin.cpp', line 738

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

#index(val) ⇒ String | nil

Returns:

  • (String | nil)


759
760
761
762
763
764
765
766
# File 'ext/usamin/usamin.cpp', line 759

static VALUE w_hash_key(const VALUE self, const VALUE val) {
    UsaminValue *value = get_value(self);
    check_object(value);
    for (auto &m : value->value->GetObject())
        if (rb_equal(val, eval_r(m.value)))
            return eval_str(m.name);
    return Qnil;
}

#key(val) ⇒ String | nil

Returns:

  • (String | nil)


759
760
761
762
763
764
765
766
# File 'ext/usamin/usamin.cpp', line 759

static VALUE w_hash_key(const VALUE self, const VALUE val) {
    UsaminValue *value = get_value(self);
    check_object(value);
    for (auto &m : value->value->GetObject())
        if (rb_equal(val, eval_r(m.value)))
            return eval_str(m.name);
    return Qnil;
}

#key?(key) ⇒ Boolean

Note:

This method has linear time complexity.

Returns:

  • (Boolean)


738
739
740
741
742
743
744
745
# File 'ext/usamin/usamin.cpp', line 738

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

#keys::Array<String>

Returns:

  • (::Array<String>)


771
772
773
774
775
776
777
778
# File 'ext/usamin/usamin.cpp', line 771

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)


783
784
785
786
787
# File 'ext/usamin/usamin.cpp', line 783

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

#member?(key) ⇒ Boolean

Note:

This method has linear time complexity.

Returns:

  • (Boolean)


738
739
740
741
742
743
744
745
# File 'ext/usamin/usamin.cpp', line 738

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

#rassoc(val) ⇒ ::Array | nil

Returns:

  • (::Array | nil)


792
793
794
795
796
797
798
799
# File 'ext/usamin/usamin.cpp', line 792

static VALUE w_hash_rassoc(const VALUE self, const VALUE val) {
    UsaminValue *value = get_value(self);
    check_object(value);
    for (auto &m : value->value->GetObject())
        if (rb_funcall(val, rb_intern("=="), 1, eval_r(m.value)))
            return rb_assoc_new(eval_str(m.name), eval(m.value, self));
    return Qnil;
}

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

Yields:

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerator | ::Hash)


835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'ext/usamin/usamin.cpp', line 835

static VALUE w_hash_reject(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, self) };
            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, self);
            if (!RTEST(rb_yield(rb_assoc_new(key, val))))
                rb_hash_aset(hash, key, val);
        }
    }
    return hash;
}

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

Yields:

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerator | ::Hash)


807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# File 'ext/usamin/usamin.cpp', line 807

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, self) };
            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, self);
            if (RTEST(rb_yield(rb_assoc_new(key, val))))
                rb_hash_aset(hash, key, val);
        }
    }
    return hash;
}

#sizeInteger

Returns:

  • (Integer)


783
784
785
786
787
# File 'ext/usamin/usamin.cpp', line 783

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

#slice(*args) {|key, value| ... } ⇒ Enumerator | ::Hash

Yields:

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerator | ::Hash)


863
864
865
866
867
868
869
870
871
872
# File 'ext/usamin/usamin.cpp', line 863

static VALUE w_hash_slice(const int argc, const VALUE *argv, const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE hash = rb_hash_new();
    for (int i = 0; i < argc; i++)
        for (auto &m : value->value->GetObject())
            if (str_compare_xx(argv[i], m.name))
                rb_hash_aset(hash, eval_str(m.name), eval(m.value, self));
    return hash;
}

#to_a::Array<Object>

Returns:

  • (::Array<Object>)


888
889
890
891
892
893
894
895
# File 'ext/usamin/usamin.cpp', line 888

static VALUE w_hash_to_a(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, rb_assoc_new(eval_str(m.name), eval(m.value, self)));
    return ret;
}

#to_h::Hash

Convert to Ruby Hash. Same as Value#eval.

Returns:

  • (::Hash)


879
880
881
882
883
# File 'ext/usamin/usamin.cpp', line 879

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

#to_hash::Hash

Convert to Ruby Hash. Same as Value#eval.

Returns:

  • (::Hash)


879
880
881
882
883
# File 'ext/usamin/usamin.cpp', line 879

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

#transform_keys {|key| ... } ⇒ Enumerator | ::Hash

Yields:

Yield Parameters:

  • key (String)

Returns:

  • (Enumerator | ::Hash)


914
915
916
917
918
919
920
921
922
# File 'ext/usamin/usamin.cpp', line 914

static VALUE w_hash_transform_keys(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();
    for (auto &m : value->value->GetObject())
        rb_hash_aset(hash, rb_yield(eval_str(m.name)), eval(m.value, self));
    return hash;
}

#transform_values {|value| ... } ⇒ Enumerator | ::Hash

Yields:

  • (value)

Yield Parameters:

  • value (Object)

Returns:

  • (Enumerator | ::Hash)


929
930
931
932
933
934
935
936
937
# File 'ext/usamin/usamin.cpp', line 929

static VALUE w_hash_transform_values(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();
    for (auto &m : value->value->GetObject())
        rb_hash_aset(hash, eval_str(m.name), rb_yield(eval(m.value, self)));
    return hash;
}

#value?(val) ⇒ Boolean

Returns:

  • (Boolean)


747
748
749
750
751
752
753
754
# File 'ext/usamin/usamin.cpp', line 747

static VALUE w_hash_hasvalue(const VALUE self, const VALUE val) {
    UsaminValue *value = get_value(self);
    check_object(value);
    for (auto &m : value->value->GetObject())
        if (rb_equal(val, eval_r(m.value)))
            return Qtrue;
    return Qfalse;
}

#values::Array<Object>

Returns:

  • (::Array<Object>)


900
901
902
903
904
905
906
907
# File 'ext/usamin/usamin.cpp', line 900

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, self));
    return ret;
}

#values_at(*args) ⇒ ::Array<Object>

Parameters:

  • keys (String)

Returns:

  • (::Array<Object>)


943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'ext/usamin/usamin.cpp', line 943

static VALUE w_hash_values_at(const int argc, const VALUE *argv, const VALUE self) {
    UsaminValue *value = get_value(self);
    check_object(value);
    VALUE ret = rb_ary_new2(argc);
    for (int i = 0; i < argc; i++) {
        VALUE data = Qnil;
        for (auto &m : value->value->GetObject()) {
            if (str_compare_xx(argv[i], m.name)) {
                data = eval(m.value, self);
                break;
            }
        }
        rb_ary_push(ret, data);
    }
    return ret;
}