Class: Usamin::Array

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

#[](nth) ⇒ Object | nil #[](start, length) ⇒ ::Array<Object> | nil #[](range) ⇒ ::Array<Object> | nil

Overloads:

  • #[](nth) ⇒ Object | nil

    Parameters:

    • nth (Integer)

    Returns:

    • (Object | nil)
  • #[](start, length) ⇒ ::Array<Object> | nil

    Parameters:

    • start (Integer)
    • length (Integer)

    Returns:

    • (::Array<Object> | nil)
  • #[](range) ⇒ ::Array<Object> | nil

    Parameters:

    • range (Range)

    Returns:

    • (::Array<Object> | nil)


742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
# File 'ext/usamin/usamin.cpp', line 742

static VALUE w_array_operator_indexer(const int argc, VALUE* argv, const VALUE self) {
    rb_check_arity(argc, 1, 2);
    UsaminValue *value = get_value(self);
    check_array(value);
    rapidjson::SizeType sz = value->value->Size();
    if (argc == 2) {
        long beg = FIX2LONG(argv[0]);
        long len = FIX2LONG(argv[1]);
        if (beg < 0)
            beg += sz;
        if (beg >= 0 && len >= 0) {
            rapidjson::SizeType end = static_cast<rapidjson::SizeType>(beg + len);
            if (end > sz)
                end = sz;
            VALUE ret = rb_ary_new2(end - beg);
            for (rapidjson::SizeType i = static_cast<rapidjson::SizeType>(beg); i < end; i++)
                rb_ary_push(ret, eval((*value->value)[i]));
            return ret;
        }
    } else if (rb_obj_is_kind_of(argv[0], rb_cRange)) {
        long beg, len;
        if (rb_range_beg_len(argv[0], &beg, &len, sz, 0) == Qtrue) {
            VALUE ret = rb_ary_new2(len);
            for (rapidjson::SizeType i = static_cast<rapidjson::SizeType>(beg); i < beg + len; i++)
                rb_ary_push(ret, eval((*value->value)[i]));
            return ret;
        }
    } else {
        long l = FIX2LONG(argv[0]);
        if (l < 0)
            l += sz;
        if (0 <= l && l < sz)
            return eval((*value->value)[static_cast<rapidjson::SizeType>(l)]);
    }
    return Qnil;
}

#at(nth) ⇒ Object

Parameters:

  • nth (Integer)

Returns:

  • (Object)


783
784
785
786
787
788
789
790
791
792
793
# File 'ext/usamin/usamin.cpp', line 783

static VALUE w_array_at(const VALUE self, VALUE nth) {
    UsaminValue *value = get_value(self);
    check_array(value);
    long l = FIX2LONG(nth);
    rapidjson::SizeType sz = value->value->Size();
    if (l < 0)
        l += sz;
    if (0 <= l && l < sz)
        return eval((*value->value)[static_cast<rapidjson::SizeType>(l)]);
    return Qnil;
}

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

Yields:

  • (value)

Returns:

  • (Enumerator | self)


803
804
805
806
807
808
809
810
# File 'ext/usamin/usamin.cpp', line 803

static VALUE w_array_each(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, array_enum_size);
    for (auto &v : value->value->GetArray())
        rb_yield(eval(v));
    return self;
}

#each_index {|index| ... } ⇒ Enumerator | self

Yields:

Yield Parameters:

  • index (Integer)

Returns:

  • (Enumerator | self)


817
818
819
820
821
822
823
824
# File 'ext/usamin/usamin.cpp', line 817

static VALUE w_array_each_index(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, array_enum_size);
    for (rapidjson::SizeType i = 0; i < value->value->Size(); i++)
        rb_yield(UINT2NUM(i));
    return self;
}

#empty?Boolean

Returns:

  • (Boolean)


826
827
828
829
830
# File 'ext/usamin/usamin.cpp', line 826

static VALUE w_array_isempty(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return value->value->Size() == 0 ? Qtrue : Qfalse;
}

#fetch(nth) ⇒ Object #fetch(nth, ifnone) ⇒ Object #fetch(nth) {|nth| ... } ⇒ Object

Overloads:

  • #fetch(nth) ⇒ Object

    Parameters:

    • nth (Integer)

    Returns:

    • (Object)

    Raises:

    • (IndexError)

      if nth is out of array bounds

  • #fetch(nth, ifnone) ⇒ Object

    Parameters:

    • nth (Integer)
    • ifnone (Object)

    Returns:

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

    Parameters:

    • nth (Integer)

    Yields:

    • (nth)

    Returns:

    • (Object)


848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'ext/usamin/usamin.cpp', line 848

static VALUE w_array_fetch(const int argc, VALUE* argv, const VALUE self) {
    rb_check_arity(argc, 1, 2);
    UsaminValue *value = get_value(self);
    check_array(value);
    rapidjson::SizeType sz = value->value->Size();

    long l = FIX2LONG(argv[0]);
    if (l < 0)
        l += sz;
    if (0 <= l && l < sz)
        return eval((*value->value)[static_cast<rapidjson::SizeType>(l)]);

    if (argc == 2)
        return argv[1];
    else if (rb_block_given_p())
        return rb_yield(argv[0]);
    else
        rb_raise(rb_eIndexError, "index %ld outside of array bounds: %ld...%u", FIX2LONG(argv[0]), -static_cast<long>(sz), sz);
    return Qnil;
}

#find_index(val) ⇒ Integer | nil #find_index {|item| ... } ⇒ Integer | nil

Overloads:

  • #find_index(val) ⇒ Integer | nil

    Parameters:

    • val (Object)

    Returns:

    • (Integer | nil)
  • #find_index {|item| ... } ⇒ Integer | nil

    Yields:

    • (item)

    Yield Parameters:

    • item (Object)

    Returns:

    • (Integer | nil)


879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'ext/usamin/usamin.cpp', line 879

static VALUE w_array_find_index(int argc, VALUE* argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);

    if (argc == 1) {
        for (rapidjson::SizeType i = 0; i < value->value->Size(); i++) {
            if (rb_equal(argv[0], eval((*value->value)[i])) == Qtrue)
                return UINT2NUM(i);
        }
        return Qnil;
    }

    RETURN_SIZED_ENUMERATOR(self, 0, nullptr, array_enum_size);
    for (rapidjson::SizeType i = 0; i < value->value->Size(); i++) {
        if (RTEST(rb_yield(eval((*value->value)[i]))))
            return UINT2NUM(i);
    }
    return Qnil;
}

#firstObject | nil #first(n) ⇒ ::Array<Object>

Overloads:

  • #firstObject | nil

    Returns:

    • (Object | nil)
  • #first(n) ⇒ ::Array<Object>

    Returns:

    • (::Array<Object>)


921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'ext/usamin/usamin.cpp', line 921

static VALUE w_array_first(const int argc, VALUE* argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);
    rapidjson::SizeType sz = value->value->Size();

    if (argc == 0) {
        if (sz == 0)
            return Qnil;
        return eval(*value->value->Begin());
    } else {
        long l = FIX2LONG(argv[0]);
        if (l > sz)
            l = sz;
        VALUE ret = rb_ary_new2(l);
        for (auto v = value->value->Begin(); v < value->value->Begin() + l; v++)
            rb_ary_push(ret, eval(*v));
        return ret;
    }
}

#index(val) ⇒ Integer | nil #index {|item| ... } ⇒ Integer | nil

Overloads:

  • #index(val) ⇒ Integer | nil

    Parameters:

    • val (Object)

    Returns:

    • (Integer | nil)
  • #index {|item| ... } ⇒ Integer | nil

    Yields:

    • (item)

    Yield Parameters:

    • item (Object)

    Returns:

    • (Integer | nil)


910
911
912
# File 'ext/usamin/usamin.cpp', line 910

static VALUE w_array_index(int argc, VALUE* argv, const VALUE self) {
    return w_array_find_index(argc, argv, self);
}

#lastObject | nil #last(n) ⇒ ::Array<Object>

Overloads:

  • #lastObject | nil

    Returns:

    • (Object | nil)
  • #last(n) ⇒ ::Array<Object>

    Returns:

    • (::Array<Object>)


949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
# File 'ext/usamin/usamin.cpp', line 949

static VALUE w_array_last(const int argc, VALUE* argv, const VALUE self) {
    rb_check_arity(argc, 0, 1);
    UsaminValue *value = get_value(self);
    check_array(value);
    rapidjson::SizeType sz = value->value->Size();

    if (argc == 0) {
        return sz > 0 ? eval(*(value->value->End() - 1)) : Qnil;
    } else {
        long l = FIX2LONG(argv[0]);
        if (l > sz)
            l = sz;
        VALUE ret = rb_ary_new2(l);
        for (auto v = value->value->End() - l; v < value->value->End(); v++)
            rb_ary_push(ret, eval(*v));
        return ret;
    }
}

#lengthInteger

Returns:

  • (Integer)


971
972
973
974
975
# File 'ext/usamin/usamin.cpp', line 971

static VALUE w_array_length(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return UINT2NUM(value->value->Size());
}

#sizeInteger

Returns:

  • (Integer)


971
972
973
974
975
# File 'ext/usamin/usamin.cpp', line 971

static VALUE w_array_length(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return UINT2NUM(value->value->Size());
}

#slice(nth) ⇒ Object | nil #slice(start, length) ⇒ ::Array<Object> | nil #slice(range) ⇒ ::Array<Object> | nil

Overloads:

  • #slice(nth) ⇒ Object | nil

    Parameters:

    • nth (Integer)

    Returns:

    • (Object | nil)
  • #slice(start, length) ⇒ ::Array<Object> | nil

    Parameters:

    • start (Integer)
    • length (Integer)

    Returns:

    • (::Array<Object> | nil)
  • #slice(range) ⇒ ::Array<Object> | nil

    Parameters:

    • range (Range)

    Returns:

    • (::Array<Object> | nil)


991
992
993
# File 'ext/usamin/usamin.cpp', line 991

static VALUE w_array_slice(const int argc, VALUE* argv, const VALUE self) {
    return w_array_operator_indexer(argc, argv, self);
}

#to_a::Array<Object>

Convert to Ruby data structures. Same as Value#eval.

Returns:

  • (::Array<Object>)


1000
1001
1002
1003
1004
# File 'ext/usamin/usamin.cpp', line 1000

static VALUE w_array_eval(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return eval_array(*(value->value));
}

#to_ary::Array<Object>

Convert to Ruby data structures. Same as Value#eval.

Returns:

  • (::Array<Object>)


1000
1001
1002
1003
1004
# File 'ext/usamin/usamin.cpp', line 1000

static VALUE w_array_eval(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_array(value);
    return eval_array(*(value->value));
}