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?, #object?, #root

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)


1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
# File 'ext/usamin/usamin.cpp', line 1005

static VALUE w_array_operator_indexer(const int argc, const 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], value->root_document));
            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], value->root_document));
            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)], value->root_document);
    }
    return Qnil;
}

#at(nth) ⇒ Object

Parameters:

  • nth (Integer)

Returns:

  • (Object)


1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'ext/usamin/usamin.cpp', line 1046

static VALUE w_array_at(const VALUE self, const 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)], value->root_document);
    return Qnil;
}

#compact(nth) ⇒ ::Array

Returns:

  • (::Array)


1061
1062
1063
1064
1065
1066
1067
1068
1069
# File 'ext/usamin/usamin.cpp', line 1061

static VALUE w_array_compact(const VALUE self, const VALUE nth) {
    UsaminValue *value = get_value(self);
    check_array(value);
    VALUE ret = rb_ary_new2(value->value->Size());
    for (auto &v : value->value->GetArray())
        if (!v.IsNull())
            rb_ary_push(ret, eval(v, value->root_document));
    return ret;
}

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

Yields:

  • (value)

Returns:

  • (Enumerator | self)


1079
1080
1081
1082
1083
1084
1085
1086
# File 'ext/usamin/usamin.cpp', line 1079

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

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

Yields:

Yield Parameters:

  • index (Integer)

Returns:

  • (Enumerator | self)


1093
1094
1095
1096
1097
1098
1099
1100
# File 'ext/usamin/usamin.cpp', line 1093

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)


1102
1103
1104
1105
1106
# File 'ext/usamin/usamin.cpp', line 1102

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)


1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
# File 'ext/usamin/usamin.cpp', line 1124

static VALUE w_array_fetch(const int argc, const 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)], value->root_document);

    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)


1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
# File 'ext/usamin/usamin.cpp', line 1155

static VALUE w_array_find_index(const int argc, const 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_r((*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], value->root_document))))
            return UINT2NUM(i);
    }
    return Qnil;
}

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

Overloads:

  • #firstObject | nil

    Returns:

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

    Returns:

    • (::Array<Object>)


1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
# File 'ext/usamin/usamin.cpp', line 1197

static VALUE w_array_first(const int argc, const 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(), value->root_document);
    } 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, value->root_document));
        return ret;
    }
}

#include?(val) ⇒ Boolean

Returns:

  • (Boolean)


1221
1222
1223
1224
1225
1226
1227
1228
# File 'ext/usamin/usamin.cpp', line 1221

static VALUE w_array_include(const VALUE self, const VALUE val) {
    UsaminValue *value = get_value(self);
    check_array(value);
    for (auto &v : value->value->GetArray())
        if (rb_equal(val, eval_r(v)))
            return Qtrue;
    return Qfalse;
}

#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)


1186
1187
1188
# File 'ext/usamin/usamin.cpp', line 1186

static VALUE w_array_index(const int argc, const 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>)


1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
# File 'ext/usamin/usamin.cpp', line 1237

static VALUE w_array_last(const int argc, const 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), value->root_document) : 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, value->root_document));
        return ret;
    }
}

#lengthInteger

Returns:

  • (Integer)


1259
1260
1261
1262
1263
# File 'ext/usamin/usamin.cpp', line 1259

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

#marshal_dumpString

Dumps data in JSON.

Returns:

  • (String)


586
587
588
589
590
591
592
593
# File 'ext/usamin/usamin.cpp', line 586

static VALUE w_value_marshal_dump(const VALUE self) {
    UsaminValue *value = get_value(self);
    check_value(value);
    rapidjson::StringBuffer buf;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buf);
    write_value(writer, *value->value);
    return rb_str_new(buf.GetString(), buf.GetSize());
}

#marshal_load(source) ⇒ Object

Loads marshal data.



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'ext/usamin/usamin.cpp', line 598

static VALUE w_value_marshal_load(const VALUE self, const VALUE source) {
    Check_Type(source, T_STRING);
    RubynizedDocument *doc = new RubynizedDocument();
    rapidjson::ParseResult result = doc->Parse<rapidjson::kParseFullPrecisionFlag | rapidjson::kParseNanAndInfFlag>(RSTRING_PTR(source), RSTRING_LEN(source));
    if (!result) {
        delete doc;
        rb_raise(rb_eParserError, "%s Offset: %lu", GetParseError_En(result.Code()), result.Offset());
    }
    if (doc->IsObject() || doc->IsArray()) {
        UsaminValue *value = new UsaminValue(doc, true);
        set_value(self, value);
        value->root_document = self;
    } else {
        auto type = doc->GetType();
        delete doc;
        rb_raise(rb_eUsaminError, "Invalid Value Type for marshal_load: %d", type);
    }
    return Qnil;
}

#sizeInteger

Returns:

  • (Integer)


1259
1260
1261
1262
1263
# File 'ext/usamin/usamin.cpp', line 1259

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)


1279
1280
1281
# File 'ext/usamin/usamin.cpp', line 1279

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

#to_ary::Array<Object>

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

Returns:

  • (::Array<Object>)


1288
1289
1290
1291
1292
# File 'ext/usamin/usamin.cpp', line 1288

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